package thyyxxk.webserver.controller.dictionary;

import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.dictionary.JcPartMaintainDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.zhuyuanyisheng.jianyanjiancha.JcZdClass;
import thyyxxk.webserver.service.PublicServer;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.StringUtil;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/jcPartMaintain")
public class JcPartMaintainController {
    private final JcPartMaintainDao dao;
    private final PublicServer publicServer;

    @Autowired
    public JcPartMaintainController(JcPartMaintainDao dao, PublicServer publicServer) {
        this.dao = dao;
        this.publicServer = publicServer;
    }

    @Data
    public static class JcPart {
        String code;
        String name;
        String pyCode;
        String delFlag;
        String parentCode;
        String parentName;

        public String getPyCode() {
            return null == pyCode ? "" : pyCode;
        }

        public String getDelFlag() {
            return StringUtil.isBlank(delFlag) ? "0" : delFlag;
        }
    }

    @Data
    public static class JcPartAndJcClass {
        List<JcPart> jcPartList;
        List<JcZdClass> jcClassList;
    }

    @GetMapping("/getAllJcPart")
    public ResultVo<JcPartAndJcClass> getAllJcPart() {
        JcPartAndJcClass jcPartAndJcClass = new JcPartAndJcClass();
        jcPartAndJcClass.setJcPartList(dao.getAllJcPart());
        jcPartAndJcClass.setJcClassList(dao.getAllJcZdClass());
        return ResultVoUtil.success(jcPartAndJcClass);
    }

    @PostMapping("/insertNewPart")
    public ResultVo<List<JcPart>> insertNewPart(@RequestBody JcPart jcPart) {
        int sameCount = StringUtil.isBlank(jcPart.getParentCode()) ?
                dao.getSameName(jcPart) : dao.getSameNameAndParent(jcPart);
        if (sameCount > 0) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR,
                    "已有名称和检查类别相同的部位存在，请勿重复添加。");
        }
        jcPart.setCode(generateJcPartCode(jcPart.getParentCode()));
        dao.insertNewPart(jcPart);
        return ResultVoUtil.success(dao.getAllJcPart());
    }

    @PostMapping("/updatePart")
    public ResultVo<List<JcPart>> updatePart(@RequestBody JcPart jcPart) {
        dao.updatePart(jcPart);
        return ResultVoUtil.success(dao.getAllJcPart());
    }

    private String generateJcPartCode(String parentCode) {
        String prefix = getPrefix(parentCode);
        if (null == prefix) {
            String maxCode = dao.getMaxNumericCode();
            if (StringUtil.isBlank(maxCode)) {
                return "0001";
            }
            BigDecimal code = new BigDecimal(maxCode).add(BigDecimal.ONE);
            return publicServer.fmtNoWithLead0(code, 4);
        }
        String maxCode = dao.getMaxLetterCode(prefix);
        if (StringUtil.isBlank(maxCode)) {
            return prefix + "01";
        }
        maxCode = maxCode.substring(2);
        BigDecimal code = new BigDecimal(maxCode).add(BigDecimal.ONE);
        return prefix + publicServer.fmtNoWithLead0(code, 2);
    }

    private String getPrefix(String code) {
        if (StringUtil.isBlank(code)) {
            return null;
        }
        switch (code) {
            case "01":
                return "BC";
            case "07":
                return "DR";
            case "08":
                return "CT";
            case "26":
                return "MR";
            case "27":
                return "EC";
            case "41":
                return "TC";
            case "51":
                return "US";
            default:
                return null;
        }
    }
}
