package com.mosty.common.config.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mosty.common.base.constant.enums.DeletedEnum;
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.exception.Asserts;
import com.mosty.common.base.util.IpUtil;
import com.mosty.common.config.entity.SysConfig;
import com.mosty.common.config.entity.SysDict;
import com.mosty.common.config.entity.vo.AddSysConfigVO;
import com.mosty.common.config.entity.vo.ConfigPage;
import com.mosty.common.config.entity.vo.DeleteVO;
import com.mosty.common.config.entity.vo.EditSysConfigVO;
import com.mosty.common.config.service.SysConfigService;
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.validation.Valid;
import java.util.Date;

/**
 * <p>
 * 参数配置表 前端控制器
 * </p>
 *
 * @author kevin
 * @since 2022-02-10
 */
@RestController
@AllArgsConstructor
@SuppressWarnings("unused")
@RequestMapping("/sys-config")
@Api(tags = "系统配置接口")
public class SysConfigController {

    private final SysConfigService sysConfigService;

    /**
     * 添加系统配置
     * @param addSysConfigVO 添加系统配置请求
     * @return 添加结果
     */
    @JwtSysUser
    @PostMapping("addSysConfig")
    @Log(title = "系统配置功能", businessType = BusinessType.INSERT)
    @ApiOperation(value = "添加系统配置", httpMethod = "POST", response = Boolean.class)
    public ResponseResult<Boolean> addSysConfig(@RequestBody @Valid AddSysConfigVO addSysConfigVO) {
        SysConfig sysConfig = new SysConfig();

        sysConfig.setPzj(addSysConfigVO.getPzj());
        sysConfig.setPzz(addSysConfigVO.getPzz());
        sysConfig.setPzmc(addSysConfigVO.getPzmc());
        sysConfig.setBz(addSysConfigVO.getBz());

        UserInfo userInfo = UserInfoManager.get();
        sysConfig.setXtZxbz(DeletedEnum.NATURE.code);

        sysConfig.setXtCjsj(new Date());
        sysConfig.setXtZhxgsj(new Date());
        sysConfig.setXtZhxgrid(userInfo.getUserId().toString());
        sysConfig.setXtZhxgrxm(userInfo.getUserName());
        sysConfig.setXtZhxgrbmid(userInfo.getDeptId().toString());
        sysConfig.setXtZhxgrbm(userInfo.getDeptName());

        sysConfig.setXtLrsj(new Date());
        sysConfig.setXtLrrid(userInfo.getUserId().toString());
        sysConfig.setXtLrrxm(userInfo.getUserName());
        sysConfig.setXtLrrbmid(userInfo.getDeptId().toString());
        sysConfig.setXtLrrbm(userInfo.getDeptName());

        // 获取ip地址
        String ipAddress = IpUtil.getIpAddress();
        sysConfig.setXtLrip(ipAddress);
        sysConfig.setXtZhxgip(ipAddress);

        sysConfigService.getBaseMapper().insert(sysConfig);
        return ResponseResult.success(Boolean.TRUE);
    }

    /**
     * 修改系统配置
     * @param editSysConfigVo 系统配置修改请求
     * @return 修改结果
     */
    @JwtSysUser
    @PostMapping("updateSysConfig")
    @Log(title = "系统配置功能", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改系统配置信息", httpMethod = "POST", response = Boolean.class)
    public ResponseResult<Boolean> updateSysConfig(@RequestBody @Valid EditSysConfigVO editSysConfigVo) {
        Asserts.check(editSysConfigVo.getId() == null, "主键id不能为空！");
        SysConfig oldSysConfig = sysConfigService.getBaseMapper().selectById(editSysConfigVo.getId());
        Asserts.check(oldSysConfig == null, "主键为 %s 的系统配置不存在", editSysConfigVo.getId());

        // 获取ip地址
        String ipAddress = IpUtil.getIpAddress();
        oldSysConfig.setXtZhxgip(ipAddress);

        oldSysConfig.setPzj(editSysConfigVo.getPzj());
        oldSysConfig.setPzz(editSysConfigVo.getPzz());
        oldSysConfig.setPzmc(editSysConfigVo.getPzmc());
        oldSysConfig.setBz(editSysConfigVo.getBz());

        UserInfo userInfo = UserInfoManager.get();

        oldSysConfig.setXtZhxgsj(new Date());
        oldSysConfig.setXtZhxgrid(userInfo.getUserId().toString());
        oldSysConfig.setXtZhxgrxm(userInfo.getUserName());
        oldSysConfig.setXtZhxgrbmid(userInfo.getDeptId().toString());
        oldSysConfig.setXtZhxgrbm(userInfo.getDeptName());

        sysConfigService.getBaseMapper().updateById(oldSysConfig);
        return ResponseResult.success(Boolean.TRUE);
    }

    /**
     * 删除系统配置信息
     * @param deleteVO 系统配置信息
     * @return 删除结果
     */
    @JwtSysUser
    @PostMapping("deleteSysConfig")
    @Log(title = "系统配置功能", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除系统配置信息", httpMethod = "POST", response = Boolean.class)
    public ResponseResult<Boolean> deleteSysConfig(@RequestBody DeleteVO deleteVO) {
        SysConfig oldSysConfig = sysConfigService.getBaseMapper().selectById(deleteVO.getId());
        Asserts.check(oldSysConfig == null, "主键为 %s 的系统配置信息不存在", deleteVO.getId());

        // 获取ip地址
        String ipAddress = IpUtil.getIpAddress();
        oldSysConfig.setXtZhxgip(ipAddress);

        UserInfo userInfo = UserInfoManager.get();

        oldSysConfig.setXtZhxgsj(new Date());
        oldSysConfig.setXtZhxgrid(userInfo.getUserId().toString());
        oldSysConfig.setXtZhxgrxm(userInfo.getUserName());
        oldSysConfig.setXtZhxgrbmid(userInfo.getDeptId().toString());
        oldSysConfig.setXtZhxgrbm(userInfo.getDeptName());

        oldSysConfig.setXtZxbz(DeletedEnum.DELETED.code);
        oldSysConfig.setXtZxyy(deleteVO.getZxyy());

        sysConfigService.getBaseMapper().updateById(oldSysConfig);
        return ResponseResult.success(Boolean.TRUE);
    }

    @GetMapping("selectPage")
    @ApiOperation(value = "分页查询系统配置信息", httpMethod = "GET", response = SysConfig.class)
    public ResponseResult<IPage<SysConfig>> selectPage(ConfigPage configPage) {
        IPage<SysConfig> userIPage = sysConfigService.selectByPage(configPage);
        return ResponseResult.success(userIPage);
    }

}

