package com.css.simulation.resource.server.adapter.controller.scene_library.score_rule;

import api.common.pojo.common.ResponseBodyVO;
import api.common.pojo.constants.LogConstants;
import api.common.pojo.param.scene.ScoringRulesParam;
import api.common.pojo.po.scene.ScoringRulesPO;
import api.common.pojo.vo.scene.ScoringRulesVO;
import api.common.util.ObjectUtil;
import api.common.util.PythonUtil;
import api.common.util.StringUtil;
import com.css.simulation.resource.server.infra.util.AuthUtil;
import com.css.simulation.resource.server.infra.util.PageUtil;
import com.css.simulation.resource.server.app.service.ScoringRulesService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

//打分规则
@RestController
@RequestMapping(value = "/ScoringRules", name = LogConstants.MODULE_SCORING)
public class ScoreRuleController {

    @Resource
    private ScoringRulesService scoringRulesService;

    //查询列表
    @PostMapping("/queryScoringRulesList")
    public ResponseBodyVO<PageInfo<ScoringRulesVO>> queryScoringRulesList(@RequestBody ScoringRulesParam parms) {
        PageUtil.setPageInfo(parms);
        List<ScoringRulesPO> list = scoringRulesService.queryScoringRulesList(parms);
        PageInfo<ScoringRulesPO> scoringRulesPOPageInfo = new PageInfo<>(list);
        List<ScoringRulesVO> list1 = new ArrayList<>();
        //po转vo
        for (ScoringRulesPO po : list) {
            ScoringRulesVO vo = new ScoringRulesVO();
            BeanUtils.copyProperties(po, vo);
            list1.add(vo);
        }
        PageInfo<ScoringRulesVO> pageInfo = new PageInfo<>(list1);
        pageInfo.setTotal(scoringRulesPOPageInfo.getTotal());

        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, pageInfo);
    }

    //查询详情
    @PostMapping("/queryScoringRules")
    public ResponseBodyVO<ScoringRulesPO> queryScoringRules(@RequestBody ScoringRulesParam parms) {
        if (ObjectUtil.isNull(parms.getRulesId())) {
            return new ResponseBodyVO<>(ResponseBodyVO.Response.CLIENT_FAILURE, "参数RulesId不能为空，请检查");
        }
        ScoringRulesPO po = scoringRulesService.queryScoringRules(parms);
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, po);

    }

    //查唯一
    @PostMapping("/queryScoringRulesCount")
    public ResponseBodyVO<ScoringRulesPO> queryScoringRulesCount(@RequestBody ScoringRulesParam parms) {
        if (ObjectUtil.isNull(parms.getRuleName())) {
            return new ResponseBodyVO<>(ResponseBodyVO.Response.CLIENT_FAILURE, "参数RulesName不能为空，请检查");
        }
        ScoringRulesPO po = scoringRulesService.queryScoringRulesByRuleName(parms);
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, po);
    }


    /**
     * 新增打分规则
     */
    @PostMapping("/saveScoringRules")
    public ResponseBodyVO<String> saveScoringRules(@RequestBody ScoringRulesPO scoringRulesPO) {
        //1 校验 python 语法错误
        String pylint = PythonUtil.pylint(scoringRulesPO.getRuleDetails(),
                PythonUtil.C,
                PythonUtil.R,
                PythonUtil.W,
                PythonUtil.E0401,
                PythonUtil.E0601
        );
        if (!pylint.contains(PythonUtil.PASS)) {
            return new ResponseBodyVO<>(ResponseBodyVO.Response.CLIENT_FAILURE, "代码格式错误，请检查。");
        }
        //2 判断 ruleId 是否为空
        if (StringUtil.isEmpty(scoringRulesPO.getRulesId())) {
            /*
                1 共有另存为私有
                2 私有另存为私有
                3 新增
             */
            // 检查私有是否存在同名平分规则
            ScoringRulesPO tmPo = scoringRulesService.querySelfOnly(scoringRulesPO.getRuleName(), AuthUtil.getCurrentUserId());
            if (ObjectUtil.isNotNull(tmPo)) {
                return new ResponseBodyVO<>(ResponseBodyVO.Response.CLIENT_FAILURE, "私有规则 " + tmPo.getRuleName() + " 已存在，请重新命名。");
            }
            scoringRulesService.saveScoringRules(scoringRulesPO);
        } else {
            /*
                修改保存
             */
            scoringRulesService.updateScoringRules(scoringRulesPO);
        }
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
    }

    //分享
    @PostMapping("/fxScoringRules")
    @PreAuthorize("@AuthorityCheck.admin()")
    public ResponseBodyVO<String> fxScoringRules(@RequestBody ScoringRulesParam params) {
        if (ObjectUtil.isNull(params.getRulesId())) {
            return new ResponseBodyVO<>(ResponseBodyVO.Response.CLIENT_FAILURE, "参数 RulesId 不能为空，请检查", "");
        }
        ScoringRulesPO po = scoringRulesService.queryScoringRules(params);
        params.setShare("1");
        params.setRuleName(po.getRuleName());
        ScoringRulesPO tmPo = scoringRulesService.queryScoringRulesByRuleName(params);
        if (ObjectUtil.isNotNull(tmPo)) {
            po.setShare("1");
            po.setRulesId(tmPo.getRulesId());
            scoringRulesService.updateScoringRules(po);
            return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
        }
        po.setShare("1");
        scoringRulesService.saveScoringRules(po);
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
    }


    /**
     * 删除打分规则
     *
     * @param params 参数
     * @return 结果
     */
    @PostMapping("/deleteScoringRules")
    public ResponseBodyVO<String> deleteScoringRules(@RequestBody ScoringRulesPO params) {
        scoringRulesService.deleteScoringRules(params);
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
    }

    //测试包里面对应查询打分规则列表
    @PostMapping("/queryCsb")
    public ResponseBodyVO<List<ScoringRulesPO>> queryCsb() {
        ScoringRulesParam params = new ScoringRulesParam();
        params.setShare("0");
        List<ScoringRulesPO> list = scoringRulesService.queryScoringRulesListCSB(params);
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, list);
    }

    //测试包里面对应查询打分规则列表
    @PostMapping("/queryCsbNew")
    public ResponseBodyVO<List<Map<String, Object>>> queryCsbNew() {
        ScoringRulesParam params = new ScoringRulesParam();
        params.setShare("0");
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, scoringRulesService.queryScoringRulesListCSBNew(params));
    }

    //该打分规则是否被引用
    @PostMapping("/queryCsbById")
    public ResponseBodyVO<Integer> queryCsbById(@RequestBody ScoringRulesParam params) {
        if (ObjectUtil.isNull(params.getRulesId())) {
            return new ResponseBodyVO<>(ResponseBodyVO.Response.CLIENT_FAILURE, "rulesId 为空，请检查");
        }
        Integer num = scoringRulesService.queryCsbById(params);
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, num);
    }


}
