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.SysDept;
import com.mosty.common.core.business.entity.SysUserAppHomeconfig;
import com.mosty.common.core.business.entity.request.SysAppHomeconfigPage;
import com.mosty.common.core.business.entity.request.SysUserAppHomeconfigPage;
import com.mosty.common.core.business.entity.request.SysUserAppHomeconfigQuery;
import com.mosty.common.core.business.entity.vo.SysAppHomeconfigRsVO;
import com.mosty.common.core.business.entity.vo.SysUserAppHomeconfigVo;
import com.mosty.common.core.business.entity.vo.SysUserHomeconfigVo;
import com.mosty.common.core.business.service.SysUserAppHomeconfigService;
import com.mosty.common.token.JwtSysUser;
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("/sysUserAppHomeconfig")
public class SysUserAppHomeconfigController {

    private SysUserAppHomeconfigService sysUserAppHomeconfigService;

    /**
     * 添加用户首页应用
     * @param config
     * @return 添加结果
     */
    @PostMapping("add")
    @JwtSysUser
    @ApiOperation(value = "添加首页应用", httpMethod = "POST", response = Boolean.class)
    @Log(title = "添加首页应用板块",businessType = BusinessType.INSERT)
    public ResponseResult<Boolean> add(@RequestBody @Valid SysUserAppHomeconfig config,
                                            HttpServletRequest servletRequest) {
        String ipAddress = IpUtil.getIpAddress(servletRequest);
        sysUserAppHomeconfigService.saveInfo(config, ipAddress);
        return ResponseResult.success(Boolean.TRUE);
    }

    /**
     * 添加用户首页应用
     * @param plates 部门
     * @return 添加结果
     */
    @PostMapping("addBatch")
    @JwtSysUser
    @ApiOperation(value = "批量添加首页应用板块", httpMethod = "POST", response = Boolean.class)
    @Log(title = "批量添加首页应用板块",businessType = BusinessType.INSERT)
    public ResponseResult<Boolean> addBatch(@RequestBody List<SysUserAppHomeconfig> plates,
                                                 HttpServletRequest servletRequest) {
        String ipAddress = IpUtil.getIpAddress(servletRequest);
        sysUserAppHomeconfigService.saveInfoBatch(plates, ipAddress);
        return ResponseResult.success(Boolean.TRUE);
    }

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

    /**
     * 批量修改用户首页应用
     * @param plates 首页板块信息
     * @return 修改结果
     */
    @PostMapping("updateBatch")
    @JwtSysUser
    @Log(title = "批量修改用户首页应用",businessType = BusinessType.UPDATE)
    @ApiOperation(value = "批量修改用户首页应用", httpMethod = "POST", response = Boolean.class)
    public ResponseResult<Boolean> updateBatch(@RequestBody List<SysUserAppHomeconfig> plates, HttpServletRequest servletRequest) {
        String ipAddress = IpUtil.getIpAddress(servletRequest);
        sysUserAppHomeconfigService.updateInfoBatch(plates, ipAddress);
        return ResponseResult.success(Boolean.TRUE);
    }

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

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

    @GetMapping("queryUserConfig")
    @ApiOperation(value = "查询用户配置", httpMethod = "GET", response = SysDept.class)
    public ResponseResult<List<SysUserAppHomeconfigVo>> queryUserConfig(SysUserAppHomeconfigQuery cofigQuery) {
        List<SysUserAppHomeconfigVo> vos = sysUserAppHomeconfigService.queryUserConfig(cofigQuery);
        return ResponseResult.success(vos);
    }


    /**
     * 添加用户默认配置
     * @param config
     * @return 添加结果
     */
    @PostMapping("addDefaultConfig")
    @JwtSysUser
    @ApiOperation(value = "添加用户默认配置", httpMethod = "POST", response = Boolean.class)
    @Log(title = "添加用户默认配置",businessType = BusinessType.INSERT)
    public ResponseResult<Boolean> addDefaultConfig(@RequestBody @Valid SysUserHomeconfigVo config,
                                       HttpServletRequest servletRequest) {
        String ipAddress = IpUtil.getIpAddress(servletRequest);
        sysUserAppHomeconfigService.saveDefaultConfig(config, ipAddress);
        return ResponseResult.success(Boolean.TRUE);
    }


}

