package com.mosty.common.core.business.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mosty.common.base.domain.ResponseResult;
import com.mosty.common.base.entity.log.BusinessType;
import com.mosty.common.base.entity.log.Log;
import com.mosty.common.base.util.IpUtil;
import com.mosty.common.core.business.entity.SysAppHomeconfig;
import com.mosty.common.core.business.entity.SysDept;
import com.mosty.common.core.business.entity.request.SysAppHomeconfigPage;
import com.mosty.common.core.business.entity.request.SysAppHomeconfigQuery;
import com.mosty.common.core.business.entity.vo.*;
import com.mosty.common.core.business.service.SysAppHomeconfigService;
import com.mosty.common.token.JwtSysUser;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * app首页表 前端控制器
 * </p>
 *
 * @author zengbo
 * @since 2022-08-18
 */
@Api(tags = "app首页接口")
@RestController
@AllArgsConstructor
@RequestMapping("/sysAppHomeconfig")
public class SysAppHomeconfigController {

    private SysAppHomeconfigService sysAppHomeconfigService;

    /**
     * 添加首页应用
     * @param configVO 部门
     * @return 添加结果
     */
    @PostMapping("addAppHome")
    @JwtSysUser
    @ApiOperation(value = "添加首页应用", httpMethod = "POST", response = Boolean.class)
    @Log(title = "添加首页应用",businessType = BusinessType.INSERT)
    public ResponseResult<Boolean> addApp(@RequestBody @Valid SysAppHomeconfigVO configVO,
                                              HttpServletRequest servletRequest) {
        String ipAddress = IpUtil.getIpAddress(servletRequest);
        sysAppHomeconfigService.saveInfo(configVO, ipAddress);
        return ResponseResult.success(Boolean.TRUE);
    }

    /**
     * 修改首页
     * @param configVO 首页信息
     * @return 修改结果
     */
    @PostMapping("updateAppHome")
    @JwtSysUser
    @Log(title = "首页应用",businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改首页", httpMethod = "POST", response = Boolean.class)
    public ResponseResult<Boolean> updateAppHome(@RequestBody @Valid SysAppHomeconfigVO configVO, HttpServletRequest servletRequest) {
        String ipAddress = IpUtil.getIpAddress(servletRequest);
        sysAppHomeconfigService.updateInfo(configVO, ipAddress);
        return ResponseResult.success(Boolean.TRUE);
    }

    /**
     * 删除应用信息
     * @param list 应用id
     * @return 删除结果
     */
    @Log(title = "应用功能",businessType = BusinessType.DELETE)
    @PostMapping("deleteApp")
    @JwtSysUser
    @ApiOperation(value = "删除应用信息", httpMethod = "POST", response = Boolean.class)
    public ResponseResult<Boolean> deleteApp(@RequestBody List<Long> list,
                                                 HttpServletRequest servletRequest) {
        String ipAddress = IpUtil.getIpAddress(servletRequest);
//        UserInfo userInfo = UserInfoManager.get();
        sysAppHomeconfigService.delete(list, ipAddress, null);
        return ResponseResult.success(Boolean.TRUE);
    }

    @GetMapping("selectPage")
    @ApiOperation(value = "分页查询应用信息", httpMethod = "GET", response = SysDept.class)
    public ResponseResult<IPage<SysAppHomeconfig>> selectPage(SysAppHomeconfigPage deptPage) {
        IPage<SysAppHomeconfig> deptIPage = sysAppHomeconfigService.selectByPage(deptPage);
        return ResponseResult.success(deptIPage);
    }

    @GetMapping("queryConfig")
    @ApiOperation(value = "查询应用配置", httpMethod = "GET", response = SysDept.class)
    public ResponseResult<SysAppHomeconfigRsVO> queryConfig(SysAppHomeconfigQuery configQuery) {
        SysAppHomeconfigRsVO vo = sysAppHomeconfigService.queryConfig(configQuery);
        return ResponseResult.success(vo);
    }


}

