package com.qmth.distributed.print.business.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.distributed.print.business.entity.BasicExamRule;
import com.qmth.distributed.print.business.entity.ExamPrintPlan;
import com.qmth.distributed.print.business.enums.PrintPlanStatusEnum;
import com.qmth.distributed.print.business.mapper.BasicExamRuleMapper;
import com.qmth.distributed.print.business.service.BasicExamRuleService;
import com.qmth.distributed.print.business.service.ExamPrintPlanService;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.SysUser;
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
import com.qmth.teachcloud.common.util.ServletUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 考务规则 服务实现类
 * </p>
 *
 * @author xf
 * @since 2021-03-23
 */
@Service
public class BasicExamRuleServiceImpl extends ServiceImpl<BasicExamRuleMapper, BasicExamRule> implements BasicExamRuleService {

    @Autowired
    private ExamPrintPlanService examPrintPlanService;

    @Override
    public BasicExamRule getBySchoolId() {
        Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
        QueryWrapper<BasicExamRule> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BasicExamRule::getSchoolId, schoolId);
        return this.getOne(queryWrapper);
    }

    @Override
    public BasicExamRule getBySchoolId(Long schoolId) {
        QueryWrapper<BasicExamRule> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BasicExamRule::getSchoolId, schoolId);
        return this.getOne(queryWrapper);
    }

    @Override
    public Long saveExamRule(BasicExamRule examRule) {
        Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
        examRule.setSchoolId(schoolId);
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();

        // 新增
        if (examRule.getId() == null) {
            examRule.insertInfo(sysUser.getId());
            examRule.setOrgId(sysUser.getOrgId());
            this.save(examRule);
        }
        // 修改
        else {
            // 查询未结束的印刷计划
            QueryWrapper<ExamPrintPlan> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(ExamPrintPlan::getSchoolId, schoolId).ne(ExamPrintPlan::getStatus, PrintPlanStatusEnum.END);
            List<ExamPrintPlan> examPrintPlanList = examPrintPlanService.list(queryWrapper);
            if (examPrintPlanList != null && examPrintPlanList.size() > 0) {
                // -- 可直接抛出异常下面的判断均可不用执行
                BasicExamRule basicExamRule = this.getById(examRule.getId());
                // 匹配扩展字段是否有变动
                String oldExtends = basicExamRule.getExtendFields();
                String newExtends = examRule.getExtendFields();
                List<Map> oldMapList = JSONObject.parseArray(oldExtends, Map.class);
                List<Map> newMapList = JSONObject.parseArray(newExtends, Map.class);
                if (oldMapList.size() != newMapList.size()) {
                    throw ExceptionResultEnum.ERROR.exception("有未结束的印刷计划，不能修改扩展字段");
                }
                for (Map map : oldMapList) {
                    String code = String.valueOf(map.get("code"));
                    String name = String.valueOf(map.get("name"));
                    String enable = String.valueOf(map.get("enable"));

                    boolean flag = true;
                    for (Map map1 : newMapList) {
                        String code1 = String.valueOf(map1.get("code"));
                        String name1 = String.valueOf(map1.get("name"));
                        String enable1 = String.valueOf(map1.get("enable"));
                        if (code.equals(code1) && name.equals(name1) && enable.equals(enable1)) {
                            flag = false;
                            break;
                        }
                    }
                    if (flag) {
                        throw ExceptionResultEnum.ERROR.exception("有未结束的印刷计划，不能修改扩展字段");
                    }
                }
                if (!basicExamRule.getReview().equals(examRule.getReview())) {
                    throw ExceptionResultEnum.ERROR.exception("有未结束的印刷计划，不能修改入库审核控制");
                }
                if (!basicExamRule.getIncludePaper().equals(examRule.getIncludePaper())) {
                    throw ExceptionResultEnum.ERROR.exception("有未结束的印刷计划，不能修改包含试卷控制");
                }
                if (!basicExamRule.getCustomCard().equals(examRule.getCustomCard())) {
                    throw ExceptionResultEnum.ERROR.exception("有未结束的印刷计划，不能修改客服制卡控制");
                }
                if (!basicExamRule.getPrintMethod().equals(examRule.getPrintMethod())) {
                    throw ExceptionResultEnum.ERROR.exception("有未结束的印刷计划，不能修改印刷方式控制");
                }
            }
            examRule.updateInfo(sysUser.getId());
            examRule.setOrgId(sysUser.getOrgId());
            this.updateById(examRule);
        }
        return examRule.getId();
    }
}
