package com.lantone.daqe.facade;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lantone.common.enums.IsDeleteEnum;
import com.lantone.common.exception.Asserts;
import com.lantone.common.util.BeanUtil;
import com.lantone.common.util.DateUtil;
import com.lantone.common.util.SysUserUtils;
import com.lantone.daqe.dto.GetRegularPageDTO;
import com.lantone.daqe.entity.ColumnVerify;
import com.lantone.daqe.entity.RegularInfo;
import com.lantone.daqe.entity.RegularMapping;
import com.lantone.daqe.entity.RegularResult;
import com.lantone.daqe.facade.base.ColumnVerifyFacade;
import com.lantone.daqe.facade.base.RegularInfoFacade;
import com.lantone.daqe.facade.base.RegularMappingFacade;
import com.lantone.daqe.facade.base.RegularResultFacade;
import com.lantone.daqe.vo.AddRegularVO;
import com.lantone.daqe.vo.DelRegularByIdVO;
import com.lantone.daqe.vo.GetRegularPageVO;
import com.lantone.daqe.vo.UpRegularByIdVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description: 正则式维护-业务处理类
 * @author: songxl
 * @time: 2022/3/4 10:39
 */
@Component
public class RegularManagementFacade {

    @Autowired
    private RegularInfoFacade regularInfoFacade;

    @Autowired
    private RegularResultFacade regularResultFacade;

    @Autowired
    private RegularMappingFacade regularMappingFacade;

    @Autowired
    private ColumnVerifyFacade columnVerifyFacade;

    /**
     * 获取正则式维护分页列表
     *
     * @param getRegularPageVO
     * @Return com.baomidou.mybatisplus.core.metadata.IPage<com.lantone.daqe.dto.GetRegularPageDTO>
     */
    public IPage<GetRegularPageDTO> getRegularPage(GetRegularPageVO getRegularPageVO) {
        QueryWrapper<RegularInfo> wrapper = new QueryWrapper();
        wrapper.like("name",getRegularPageVO.getName());
        regularInfoFacade.list(wrapper);
        return regularInfoFacade.getBaseMapper().getRegularPage(getRegularPageVO);
    }

    /**
     * 新增正则式 业务处理
     *
     * @param addRegularVO
     * @return 是否新增成功
     */
    public Boolean addRegular(AddRegularVO addRegularVO) {
        RegularInfo regularInfo = new RegularInfo();
        BeanUtils.copyProperties(addRegularVO, regularInfo);
        if (regularInfoFacade.isExist(regularInfo)) {
            Asserts.fail("该正则式已存在！");
        }
        regularInfo.setCreator(SysUserUtils.getCurrentPrinciple() == null ? "0" : SysUserUtils.getCurrentPrinciple());
        regularInfo.setGmtCreate(DateUtil.now());
        return regularInfoFacade.save(regularInfo);
    }

    /**
     * 通过id修改正则式
     *
     * @param upRegularByIdVO
     * @return 是否修改成功
     */
    public Boolean upRegularById(UpRegularByIdVO upRegularByIdVO) {
        if (regularInfoFacade.getById(upRegularByIdVO.getId()) == null) {
            Asserts.fail("该正则式不存在！");
        }
        //修改正则式
        RegularInfo regularInfo = new RegularInfo();
        BeanUtil.copyProperties(upRegularByIdVO, regularInfo);
        regularInfo.setModifier(SysUserUtils.getCurrentPrinciple() == null ? "0" : SysUserUtils.getCurrentPrinciple());
        regularInfo.setGmtModified(DateUtil.now());
        //修改表字段的正则式校验结果表中的正则式
        if (regularInfoFacade.updateById(regularInfo)) {
            if(regularResultFacade.update(new UpdateWrapper<RegularResult>()
                    .eq(upRegularByIdVO.getHospitalId() != null, "hospital_id", upRegularByIdVO.getHospitalId())
                    .eq("regular_id", upRegularByIdVO.getId())
                    .eq("is_deleted", IsDeleteEnum.N.getKey())
                    .set("regular_name", upRegularByIdVO.getName())
                    .set("regular_val", upRegularByIdVO.getVal())
                    .set("modifier", SysUserUtils.getCurrentPrinciple() == null ? "0" : SysUserUtils.getCurrentPrinciple())
                    .set("gmt_modified", DateUtil.now())
                    .set("regular_des", upRegularByIdVO.getDescription()))){
                return columnVerifyFacade.update(new UpdateWrapper<ColumnVerify>()
                        .eq("verify_id", upRegularByIdVO.getId())
                        .eq("is_deleted", IsDeleteEnum.N.getKey())
                        .set("verify_val", upRegularByIdVO.getName())
                        .set("modifier", SysUserUtils.getCurrentPrinciple() == null ? "0" : SysUserUtils.getCurrentPrinciple())
                        .set("gmt_modified", DateUtil.now()));
            }
            }
        return false;
    }

    /**
     * 通过id删除正则式
     *
     * @param delRegularByIdVO
     * @return 是否删除成功
     */
    public Boolean delRegularById(DelRegularByIdVO delRegularByIdVO) {
        Boolean flag = false;
        for (Long id : delRegularByIdVO.getIds()) {
            flag = delRegularById(id, delRegularByIdVO.getHospitalId());
        }
        return flag;
    }

    /**
     * 通过id删除正则式
     *
     * @param id
     * @param hospitalId
     * @Return java.lang.Boolean
     */
    private Boolean delRegularById(Long id, Long hospitalId) {

        if (regularInfoFacade.getById(id) == null) {
            Asserts.fail("该正则式不存在！");
        }
        RegularInfo regularInfo = new RegularInfo();
        regularInfo.setId(id);
        regularInfo.setModifier(SysUserUtils.getCurrentPrinciple() == null ? "0" : SysUserUtils.getCurrentPrinciple());
        regularInfo.setGmtModified(DateUtil.now());
        regularInfo.setIsDeleted(IsDeleteEnum.Y.getKey());
        if (regularInfoFacade.updateById(regularInfo)) {
            //删除表字段的实际值与正则式关联关系
            if(regularMappingFacade.remove(new UpdateWrapper<RegularMapping>()
                    .eq("regular_id", id)
                    .eq("is_deleted", IsDeleteEnum.N.getKey()))){
                return columnVerifyFacade.remove(new UpdateWrapper<ColumnVerify>()
                        .eq("verify_id", id)
                        .eq("is_deleted", IsDeleteEnum.N.getKey()));
            }
            //删除表字段的正则式校验结果表中的正则式
//            return regularResultFacade.update(new UpdateWrapper<RegularResult>()
//                    .eq(hospitalId != null, "hospital_id", hospitalId)
//                    .eq("regular_id", id)
//                    .eq("is_deleted", IsDeleteEnum.N.getKey())
//                    .set("modifier", SysUserUtils.getCurrentPrinciple() == null ? "0" : SysUserUtils.getCurrentPrinciple())
//                    .set("gmt_modified", DateUtil.now())
//                    .set("is_deleted", IsDeleteEnum.Y.getKey()));

        }
        return false;
    }


}