package com.hzs.system.base.controller.manage;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzs.common.core.enums.ECheckType;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.domain.system.config.BdCheckConfig;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.base.service.IBdCheckConfigService;
import com.hzs.system.base.vo.BdCheckConfigVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Author: yuhui
 * @Time: 2022/10/18 16:22
 * @Classname: BdCheckConfigController
 * @PackageName: com.hzs.system.base.controller.manage
 */
@RestController
@RequestMapping("/manage/checkConfig")
public class BdCheckConfigController extends BaseController {

    @Autowired
    private IBdCheckConfigService  checkConfigService;

    /**
     * 校验配置列表
     * @return
     */
    @GetMapping("list")
    public AjaxResult list(){
        ECheckType[] values = ECheckType.values();
        List<BdCheckConfigVo> result= new ArrayList<>();
        List<BdCheckConfig> list = checkConfigService.list();
        for (ECheckType value : values) {
            BdCheckConfigVo checkConfigVo = new BdCheckConfigVo();
            int isForceCheck = 0;
            for (BdCheckConfig bdCheckConfig : list) {
                if (value.getValue() == bdCheckConfig.getCheckType().intValue()){
                    isForceCheck = bdCheckConfig.getIsForceCheck().intValue();
                }
            }
            checkConfigVo.setCheckType(value.getValue());
            checkConfigVo.setIsForceCheck(isForceCheck);
            checkConfigVo.setCheckTypeName(value.getLable());
            result.add(checkConfigVo);
        }

        return AjaxResult.success(result);
    }


    /**
     * 校验配置
     * @return
     */
    @PostMapping("update-check-config")
    public AjaxResult save(@RequestBody BdCheckConfig config){
        QueryWrapper<BdCheckConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("CHECK_TYPE",config.getCheckType());
        BdCheckConfig checkConfig = checkConfigService.getOne(queryWrapper);
        if (checkConfig != null){
            checkConfig.setModifiedTime(new Date());
            checkConfig.setPkModified(SecurityUtils.getUserId());
            checkConfig.setIsForceCheck(config.getIsForceCheck());
            checkConfig.setCheckType(config.getCheckType());
            checkConfigService.updateById(checkConfig);
        }else {
            config.setCreationTime(new Date());
            config.setPkCreator(SecurityUtils.getUserId());
            config.setIsForceCheck(config.getIsForceCheck());
            config.setCheckType(config.getCheckType());
            checkConfigService.save(config);
        }
        return AjaxResult.success();
    }
}
