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.SysDict;
import com.mosty.common.config.entity.SysVersionManage;
import com.mosty.common.config.entity.vo.AddVersionManageVO;
import com.mosty.common.config.entity.vo.EditVersionManageVO;
import com.mosty.common.config.entity.vo.VersionManagePage;
import com.mosty.common.config.service.SysVersionManageService;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * <p>
 * 版本管理表 前端控制器
 * </p>
 *
 * @author kevin
 * @since 2022-02-21
 */
@Slf4j
@AllArgsConstructor
@RestController
@SuppressWarnings("unused")
@RequestMapping("/sys-version-manage")
@Api(tags = "系统版本管理接口")
public class SysVersionManageController {

    private final SysVersionManageService sysVersionManageService;

    /**
     * 添加系统版本管理
     * @param addVersionManageVo 添加系统版本管理请求
     * @return 添加结果
     */
    @JwtSysUser
    @PostMapping("addVersionManage")
    @Log(title = "系统版本管理功能", businessType = BusinessType.INSERT)
    @ApiOperation(value = "添加系统版本管理", httpMethod = "POST", response = Boolean.class)
    public ResponseResult<Boolean> addVersionManage(@RequestBody @Valid AddVersionManageVO addVersionManageVo) {

        SysVersionManage sysVersionManage = new SysVersionManage();

        sysVersionManage.setSysVersion(addVersionManageVo.getSysVersion());
        sysVersionManage.setPublishTime(addVersionManageVo.getPublishTime());
        sysVersionManage.setAddFunction(addVersionManageVo.getAddFunction());
        sysVersionManage.setFixBug(addVersionManageVo.getFixBug());
        sysVersionManage.setBz(addVersionManageVo.getBz());

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

        sysVersionManage.setXtCjsj(LocalDateTime.now());
        sysVersionManage.setXtZhxgsj(LocalDateTime.now());
        sysVersionManage.setXtZhxgrid(userInfo.getUserId().toString());
        sysVersionManage.setXtZhxgrxm(userInfo.getUserName());
        sysVersionManage.setXtZhxgrbmid(userInfo.getDeptId().toString());
        sysVersionManage.setXtZhxgrbm(userInfo.getDeptName());

        sysVersionManage.setXtLrsj(LocalDateTime.now());
        sysVersionManage.setXtLrrid(userInfo.getUserId().toString());
        sysVersionManage.setXtLrrxm(userInfo.getUserName());
        sysVersionManage.setXtLrrbmid(userInfo.getDeptId().toString());
        sysVersionManage.setXtLrrbm(userInfo.getDeptName());

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

        sysVersionManageService.getBaseMapper().insert(sysVersionManage);
        return ResponseResult.success(Boolean.TRUE);
    }

    /**
     * 修改系统版本管理
     * @param editVersionManageVo 修改系统版本管理请求
     * @return 修改结果
     */
    @JwtSysUser
    @PostMapping("updateVersionManage")
    @Log(title = "系统版本管理功能", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改系统版本管理", httpMethod = "POST", response = Boolean.class)
    public ResponseResult<Boolean> updateVersionManage(@RequestBody @Valid EditVersionManageVO editVersionManageVo) {
        Asserts.check(editVersionManageVo.getId() == null, "主键id不能为空！");
        SysVersionManage sysVersionManage = sysVersionManageService.getBaseMapper().selectById(editVersionManageVo.getId());
        Asserts.check(sysVersionManage == null, "主键为 %s 的系统版本管理信息不存在", editVersionManageVo.getId());

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

        sysVersionManage.setSysVersion(editVersionManageVo.getSysVersion());
        sysVersionManage.setPublishTime(editVersionManageVo.getPublishTime());
        sysVersionManage.setAddFunction(editVersionManageVo.getAddFunction());
        sysVersionManage.setFixBug(editVersionManageVo.getFixBug());
        sysVersionManage.setBz(editVersionManageVo.getBz());

        UserInfo userInfo = UserInfoManager.get();

        sysVersionManage.setXtZhxgsj(LocalDateTime.now());
        sysVersionManage.setXtZhxgrid(userInfo.getUserId().toString());
        sysVersionManage.setXtZhxgrxm(userInfo.getUserName());
        sysVersionManage.setXtZhxgrbmid(userInfo.getDeptId().toString());
        sysVersionManage.setXtZhxgrbm(userInfo.getDeptName());

        sysVersionManageService.getBaseMapper().updateById(sysVersionManage);
        return ResponseResult.success(Boolean.TRUE);
    }

    /**
     * 删除系统版本管理信息
     * @param id 系统版本管理信息id
     * @return 删除结果
     */
    @JwtSysUser
    @GetMapping("deleteVersionManage")
    @Log(title = "系统版本管理功能", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除系统版本管理信息", httpMethod = "POST", response = Boolean.class)
    public ResponseResult<Boolean> deleteVersionManage(@RequestParam("id") Long id) {
        SysVersionManage sysVersionManage = sysVersionManageService.getBaseMapper().selectById(id);
        Asserts.check(sysVersionManage == null, "主键为 %s 的系统版本管理信息不存在", id);

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

        UserInfo userInfo = UserInfoManager.get();

        sysVersionManage.setXtZhxgsj(LocalDateTime.now());
        sysVersionManage.setXtZhxgrid(userInfo.getUserId().toString());
        sysVersionManage.setXtZhxgrxm(userInfo.getUserName());
        sysVersionManage.setXtZhxgrbmid(userInfo.getDeptId().toString());
        sysVersionManage.setXtZhxgrbm(userInfo.getDeptName());

        sysVersionManage.setXtZxbz(DeletedEnum.DELETED.code);
        sysVersionManage.setXtZxyy("");

        sysVersionManageService.getBaseMapper().updateById(sysVersionManage);
        return ResponseResult.success(Boolean.TRUE);
    }

    @GetMapping("selectPage")
    @ApiOperation(value = "分页查询字典信息", httpMethod = "GET", response = SysVersionManage.class)
    public ResponseResult<IPage<SysVersionManage>> selectPage(VersionManagePage versionManagePage) {
        IPage<SysVersionManage> versionManageIPage = sysVersionManageService.selectByPage(versionManagePage);
        return ResponseResult.success(versionManageIPage);
    }

}

