package digital.laboratory.platform.entrustment.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import digital.laboratory.platform.common.core.exception.ValidateCodeException;
import digital.laboratory.platform.entrustment.entity.Entrustment;
import digital.laboratory.platform.entrustment.entity.EntrustmentIdentificationMaterial;
import digital.laboratory.platform.entrustment.service.AcceptService;
import digital.laboratory.platform.entrustment.service.EntrustmentIdentificationMaterialService;
import digital.laboratory.platform.entrustment.service.EntrustmentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 受理服务层接口 -- 实现类
 */
@Service
public class AcceptServiceImpl implements AcceptService {

    @Resource
    private EntrustmentService entrustmentService;

    @Resource
    private EntrustmentIdentificationMaterialService entrustmentIdentificationMaterialService;

    /**
     * 修改受理后的检材受理编号
     * @param material
     * @return
     */
    @Override
    public Boolean alertMaterialAcceptNo(EntrustmentIdentificationMaterial material) {
        EntrustmentIdentificationMaterial identificationMaterial = entrustmentIdentificationMaterialService.getById(material.getId());
        if (identificationMaterial == null) {
            throw new ValidateCodeException("检材信息不存在！");
        }
        // 获取要修改的检材受理编号
        String materialAcceptNo = material.getAcceptNo();
        if (StrUtil.isBlank(materialAcceptNo)) {
            throw new ValidateCodeException("检材受理编号不能修改为空！");
        }
        /** 判断是否符合编号规则，
         * \\d+：表示一个或多个数字-：表示连字符
         * -：表示连字符
         * ^ 和 $：表示字符串的开始和结束
         */
        String regex = "^\\d+-\\d+-\\d+$";
        if (Pattern.matches(regex, materialAcceptNo)) {
            throw new ValidateCodeException(String.format("%s 不符合检材受理编号的规则！", materialAcceptNo));
        }
        // 校验当前修改的检材受理编号对应的委托的受理编号存不存在, 委托的受理编号是检材受理编号截取最后一个横杠的内容
        String entrustAcceptNo = materialAcceptNo.substring(0, materialAcceptNo.lastIndexOf("-"));
        if (entrustmentService.count(Wrappers.<Entrustment>lambdaQuery().eq(Entrustment::getAcceptNo, entrustAcceptNo)) > 0) {
            throw new ValidateCodeException(String.format("%s 的委托受理信息已经存在！", entrustAcceptNo));
        }
        if (entrustmentIdentificationMaterialService.count(Wrappers.<EntrustmentIdentificationMaterial>lambdaQuery().eq(EntrustmentIdentificationMaterial::getAcceptNo, materialAcceptNo)) > 0) {
            throw new ValidateCodeException(String.format("%s 的委托检材受理信息已经存在！", materialAcceptNo));
        }
        if (entrustmentIdentificationMaterialService.update(Wrappers.<EntrustmentIdentificationMaterial>lambdaUpdate()
                .eq(EntrustmentIdentificationMaterial::getId, material.getId()).set(EntrustmentIdentificationMaterial::getAcceptNo, materialAcceptNo))) {
            // 查询该委托下面的其他已经受理的检材
            List<EntrustmentIdentificationMaterial> materialList = entrustmentIdentificationMaterialService.list(Wrappers.<EntrustmentIdentificationMaterial>lambdaQuery()
                    .eq(EntrustmentIdentificationMaterial::getEntrustmentId, identificationMaterial.getEntrustmentId())
                    .ne(EntrustmentIdentificationMaterial::getAcceptNo, materialAcceptNo)
                    .isNotNull(EntrustmentIdentificationMaterial::getAcceptNo));
            if (CollUtil.isNotEmpty(materialList)) {
                int count = 0;
                for (EntrustmentIdentificationMaterial entrustmentIdentificationMaterial : materialList) {
                    String entrustmentIdentificationMaterialAcceptNo = entrustmentIdentificationMaterial.getAcceptNo();
                    if (entrustmentIdentificationMaterialAcceptNo.contains(entrustAcceptNo)) {
                        continue;
                    }
                    count++;
                    entrustmentIdentificationMaterial.setAcceptNo(entrustAcceptNo
                            + "-"
                            + entrustmentIdentificationMaterialAcceptNo.substring(entrustmentIdentificationMaterialAcceptNo.lastIndexOf("-")));
                }
                return count == 0 ? true : entrustmentIdentificationMaterialService.updateBatchById(materialList);
            }
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }
}
