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

import cn.hnthyy.thmz.common.Constants;
import cn.hnthyy.thmz.entity.his.zd.MzZdYpYsh;
import cn.hnthyy.thmz.enums.UnitTypeEnum;
import cn.hnthyy.thmz.mapper.his.zd.MzZdYpYshMapper;
import cn.hnthyy.thmz.mapper.his.zd.ZdChargeItemMapper;
import cn.hnthyy.thmz.service.his.zd.MzZdYpYshService;
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.List;

@Service
@Slf4j
public class MzZdYpYshServiceImpl implements MzZdYpYshService {
    @SuppressWarnings("all")
    @Autowired
    private MzZdYpYshMapper mzZdYpYshMapper;


    @Override
    public List<MzZdYpYsh> queryMzZdYpYshByCommonParamsAndGroupNo(String commonParams, String groupNo) {
        String letterLowerParamsCase = null;
        String letterParamsUpperCase = null;
        if (StringUtils.isBlank(commonParams)) {
            commonParams = null;
        } else {
            String letterParams = commonParams.replaceAll("\\s*", "").replaceAll("[^(A-Za-z)]", "");
            if (StringUtils.isNotBlank(letterParams)) {
                letterLowerParamsCase = "%" + letterParams.toLowerCase() + "%";
                letterParamsUpperCase = "%" + letterParams.toUpperCase() + "%";
            }
            commonParams = "%" + commonParams + "%";
        }
        List<MzZdYpYsh> mzZdYpYshes = mzZdYpYshMapper.selectMzZdYpYshByCommonParamsAndGroupNo(commonParams, letterLowerParamsCase, letterParamsUpperCase, groupNo);
        //如果查询的是西成药，需要把医材 和诊疗项目，麻、精一，精二也查询过来
        if (Constants.XY_ZCY_GROUP_NO.equals(groupNo)) {
            Integer pageSize =10;
            List<MzZdYpYsh> ycList = mzZdYpYshMapper.selectYcByCommonParams(commonParams, letterLowerParamsCase, letterParamsUpperCase,pageSize);
            mzZdYpYshes.addAll(ycList);
            List<MzZdYpYsh> mjList = mzZdYpYshMapper.selectMjyByCommonParams(commonParams, letterLowerParamsCase, letterParamsUpperCase,pageSize);
            mzZdYpYshes.addAll(mjList);
            List<MzZdYpYsh> jeList = mzZdYpYshMapper.selectJeByCommonParams(commonParams, letterLowerParamsCase, letterParamsUpperCase,pageSize);
            mzZdYpYshes.addAll(jeList);
        }
        if (mzZdYpYshes != null && mzZdYpYshes.size() > 0) {
            mzZdYpYshes.stream().forEach(m -> {
                m.setMiniUnitName(UnitTypeEnum.getUnitTypeByCode(m.getMiniUnit()).name);
                try {
                    if (m.getSpecification().indexOf("x") > 0) {
                        m.setDrugWinDesc(m.getSpecification().substring(0, m.getSpecification().indexOf("x")));
                    } else if (m.getSpecification().indexOf("X") > 0) {
                        m.setDrugWinDesc(m.getSpecification().substring(0, m.getSpecification().indexOf("X")));
                    } else {
                        m.setDrugWinDesc(m.getSpecification().substring(0, m.getSpecification().indexOf("/")));
                    }
                } catch (Exception e) {
                    m.setDrugWinDesc("1");
                    log.info("设置药品记录描述出错，药品规格是{}", m.getSpecification());
                }
            });
        }
        return mzZdYpYshes;
    }

    @Override
    public List<MzZdYpYsh> queryAllYcList() {
        return mzZdYpYshMapper.selectYcByCommonParams(null, null, null,null);
    }

    @Override
    public List<MzZdYpYsh> queryAllMjyList() {
        return mzZdYpYshMapper.selectMjyByCommonParams(null,null,null,null);
    }

    @Override
    public List<MzZdYpYsh> queryAllJeList() {
        return mzZdYpYshMapper.selectJeByCommonParams(null,null,null,null);
    }
}
