package cn.hnthyy.thmz.service.impl.his.mz;

import cn.hnthyy.thmz.common.Constants;
import cn.hnthyy.thmz.entity.MzException;
import cn.hnthyy.thmz.entity.his.zy.APatientMi;
import cn.hnthyy.thmz.entity.his.mz.MzPatientMi;
import cn.hnthyy.thmz.entity.his.YjJcPatient;
import cn.hnthyy.thmz.entity.lis.MOrderInfo;
import cn.hnthyy.thmz.entity.lis.MPtntInfo;
import cn.hnthyy.thmz.mapper.his.*;
import cn.hnthyy.thmz.mapper.his.mz.MzChargeDetailMapper;
import cn.hnthyy.thmz.mapper.his.mz.MzPatientMiMapper;
import cn.hnthyy.thmz.mapper.his.mz.MzYjReqMapper;
import cn.hnthyy.thmz.mapper.his.mz.MzyReqrecMapper;
import cn.hnthyy.thmz.mapper.his.zy.APatientMiMapper;
import cn.hnthyy.thmz.mapper.his.zy.ZyActpatientMapper;
import cn.hnthyy.thmz.mapper.lis.MOrderInfoMapper;
import cn.hnthyy.thmz.mapper.lis.MPtntInfoMapper;
import cn.hnthyy.thmz.service.his.mz.MzPatientMiService;
import cn.hnthyy.thmz.service.his.mz.MzSerialNoService;
import cn.hnthyy.thmz.service.thmz.HaiCiAdapterService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class MzPatientMiServiceImpl implements MzPatientMiService {
    @SuppressWarnings("all")
    @Autowired
    private MzPatientMiMapper mzPatientMiMapper;
    @SuppressWarnings("all")
    @Autowired
    private MzChargeDetailMapper mzChargeDetailMapper;
    @SuppressWarnings("all")
    @Autowired
    private MzYjReqMapper mzYjReqMapper;
    @SuppressWarnings("all")
    @Autowired
    private MzyReqrecMapper mzyReqrecMapper;
    @SuppressWarnings("all")
    @Autowired
    private YjJcPatientMapper yjJcPatientMapper;
    @SuppressWarnings("all")
    @Autowired
    private MOrderInfoMapper mOrderInfoMapper;
    @SuppressWarnings("all")
    @Autowired
    private MPtntInfoMapper mPtntInfoMapper;
    @SuppressWarnings("all")
    @Autowired
    private APatientMiMapper aPatientMiMapper;
    @SuppressWarnings("all")
    @Autowired
    private ZyActpatientMapper zyActpatientMapper;
    @Autowired
    private MzSerialNoService mzSerialNoService;
    @Autowired
    private HaiCiAdapterService haiCiAdapterService;

    //是否修改LIS数据开关
    @Value("${updateLisData}")
    private boolean updateLisData;

    @Override
    public MzPatientMi queryByIcCardNo(String icCardNo) {
        return mzPatientMiMapper.selectByIcCardNo(icCardNo);
    }

    @Override
    public List<MzPatientMi> queryByNameFuzzy(String name, Integer pageIndex, Integer pageSize) {
        if (StringUtils.isNotBlank(name)) {
            name = "%" + name + "%";
        }
        return mzPatientMiMapper.selectByNameFuzzy(name, pageIndex, pageSize);
    }

    @Override
    public MzPatientMi queryByPatientId(String patientId) {
        return mzPatientMiMapper.selectByPatientId(patientId);
    }

    @Override
    public int saveMzPatientMi(MzPatientMi mzPatientMi) throws MzException {
        mzPatientMi.setLvDate(new Date());
        String patientNo = mzSerialNoService.getMzPatientNo();
        mzPatientMi.setPatientId(patientNo);
        mzPatientMi.setTimes(1);
        if (StringUtils.isBlank(mzPatientMi.getIcCardNo())) {
            mzPatientMi.setIcCardNo(patientNo);
        }
        return mzPatientMiMapper.insertMzPatientMi(mzPatientMi);
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public int modifyMzPatientMi(MzPatientMi mzPatientMi) {
        if (StringUtils.isBlank(mzPatientMi.getPatientId())) {
            return 0;
        }
        MzPatientMi mzPatientMiDb = mzPatientMiMapper.selectByPatientId(mzPatientMi.getPatientId());
        int num = mzPatientMiMapper.updateMzPatientMi(mzPatientMi);
        if (num > 0) {
            //非120的卡号，修改名称，同步更新收费表的名称和挂号表的名称
            if (!Constants.JZ_IC_CARD.equals(mzPatientMi.getIcCardNo())) {
                mzChargeDetailMapper.updateNameByPatientIdAndTimes(mzPatientMi.getName(), mzPatientMi.getPatientId(), null);
                mzYjReqMapper.updateNameByPatientIdAndTimes(mzPatientMi.getName(), mzPatientMi.getPatientId(), null);
                mzyReqrecMapper.updateNameByPatientIdAndTimes(mzPatientMi.getName(), mzPatientMi.getPatientId(), null);
                //医技检查
                yjJcPatientMapper.updateYjJcPatient(new YjJcPatient(mzPatientMi.getPatientId(),mzPatientMi.getName()));
                if(updateLisData){
                    //lis订单信息
                    mOrderInfoMapper.updateMOrderInfo(new MOrderInfo(mzPatientMi.getPatientId(),mzPatientMi.getName()));
                    //lis病人信息
                    mPtntInfoMapper.updateMPtntInfo(new MPtntInfo(mzPatientMi.getPatientId(),mzPatientMi.getName()));
                }
                //住院病人姓名
                aPatientMiMapper.updateMzPatientMiByMzno(new APatientMi(mzPatientMi.getPatientId(),mzPatientMi.getName()));
                //修改住院病人
                zyActpatientMapper.updateNameByMzNo(mzPatientMi.getName(),mzPatientMi.getPatientId(),"zy_actpatient");
                //修改出院病人
                zyActpatientMapper.updateNameByMzNo(mzPatientMi.getName(),mzPatientMi.getPatientId(),"zy_inactpatient");
            }
            if (mzPatientMiDb != null && !mzPatientMiDb.getIcCardNo().equals(mzPatientMi.getIcCardNo())) {
                haiCiAdapterService.cardChangeNotice(mzPatientMiDb.getIcCardNo(), mzPatientMi.getIcCardNo(), new Date());
            }
        }

        return num;
    }

    @Override
    public List<MzPatientMi> queryBySocialNo(String socialNo, String patientId) {
        try {
            if ("-".equals(socialNo)) {
                return null;
            }
            return mzPatientMiMapper.selectBySocialNo(socialNo, patientId);
        } catch (Exception e) {
            log.error("数据库异常，socialNo=【" + socialNo + "】，patientId=【" + patientId + "】");
            return null;
        }

    }

    @Override
    public List<MzPatientMi> queryByPatientIds(List<String> patientIds) {
        return mzPatientMiMapper.selectByPatientIds(patientIds);
    }

    @Override
    public List<MzPatientMi> queryByPhoneNo(String phoneNo) {
        return mzPatientMiMapper.selectByPhoneNo(phoneNo);
    }

    @Override
    public int clearIcCardNo(String patientId) {
        return mzPatientMiMapper.clearIcCardNo(patientId);
    }

    @Override
    public List<MzPatientMi> queryByName(String name) {
        if (StringUtils.isNotBlank(name)) {
            name = "%" + name + "%";
        }
        return mzPatientMiMapper.selectByNameFuzzy(name, 0, 1000);
    }

    @Override
    public List<MzPatientMi> queryByCommonParams(String commonParams) {
        if (StringUtils.isBlank(commonParams)) {
            return null;
        }
        commonParams = "%" + commonParams + "%";
        return mzPatientMiMapper.selectByCommonParams(commonParams);
    }


}
