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

import cn.hnthyy.thmz.entity.MzException;
import cn.hnthyy.thmz.entity.his.MzPatientMi;
import cn.hnthyy.thmz.mapper.his.MzPatientMiMapper;
import cn.hnthyy.thmz.service.his.MzPatientMiService;
import cn.hnthyy.thmz.service.his.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.stereotype.Service;

import java.util.Date;
import java.util.List;
@Slf4j
@Service
public class MzPatientMiServiceImpl implements MzPatientMiService {
    @SuppressWarnings("all")
    @Autowired
    private MzPatientMiMapper mzPatientMiMapper;
    @Autowired
    private MzSerialNoService mzSerialNoService;
    @Autowired
    private HaiCiAdapterService haiCiAdapterService;
    @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
    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 && 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);
    }


}
