package com.yihu.jw.base.service.dict;

import com.yihu.jw.base.service.dict.importManage.BasDrugUseExcelDO;
import com.yihu.jw.base.service.dict.importManage.BasFrequencyExcelDO;
import com.yihu.jw.dict.dao.BaseDrugUseDictDao;
import com.yihu.jw.dict.dao.BaseFrequencyDictDao;
import com.yihu.jw.dict.dao.BaseTemplateDictDao;
import com.yihu.jw.entity.base.dict.BaseDrugUseDictDO;
import com.yihu.jw.entity.base.dict.BaseFrequencyDictDO;
import com.yihu.jw.entity.base.dict.BaseTemplateDictDO;
import com.yihu.jw.exception.business.ManageException;
import com.yihu.jw.restmodel.web.MixEnvelop;
import com.yihu.jw.utils.hibernate.HibenateUtils;
import com.yihu.mysql.query.BaseJpaService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.yihu.jw.util.common.BeanUtils.getNullPropertyNames;

@Service
public class BaseFrequencyService extends BaseJpaService<BaseFrequencyDictDO, BaseFrequencyDictDao> {
    @Autowired
    private HibenateUtils hibenateUtils;
    @Autowired
    private BaseFrequencyDictDao baseFrequencyDictDao;
    @Autowired
    private BaseTemplateDictDao baseTemplateDictDao;

    public void saveFrequency(BaseFrequencyDictDO baseFrequencyDictDO) throws Exception{
        BaseFrequencyDictDO baseFrequencyDictDO1=baseFrequencyDictDao.findByCode(baseFrequencyDictDO.getFreq_code());
        if (StringUtils.isNotBlank(baseFrequencyDictDO.getId())){
            if (baseFrequencyDictDO1!=null){
                throw  new Exception("该频次code已存在");
            }
            BaseFrequencyDictDO oldFrequency= baseFrequencyDictDao.findOne(baseFrequencyDictDO.getId());
            BeanUtils.copyProperties(baseFrequencyDictDO, oldFrequency, getNullPropertyNames(baseFrequencyDictDO));
            baseFrequencyDictDao.save(oldFrequency);
        }else {
            if (baseFrequencyDictDO1!=null){
                if (!baseFrequencyDictDO1.getId().equalsIgnoreCase(baseFrequencyDictDO.getId())){
                    throw  new Exception("该频次code已被使用");
                }
            }
            baseFrequencyDictDO.setCreateTime(new Date());
            baseFrequencyDictDO.setIs_del("1");
            baseFrequencyDictDao.save(baseFrequencyDictDO);
        }
    }
    public MixEnvelop findFrequencyList(String name, String status, Integer page , Integer pageSize){
        String sql="select t.id as \"id\"," +
                " t.freq_code as \"freq_code\"," +
                " t.freq_name as \"freq_name\"," +
                " t.freq_sp as \"freq_sp\"," +
                " t.daily_times as \"daily_times\"," +
                " t.weekly_times as \"weekly_times\"," +
                " t.hosp_code as \"hosp_code\"," +
                " t.is_del as \"is_del\"," +
                " t.is_op_ip as \"is_op_ip\"," +
                " t.status as \"status\"," +
                " t.create_time as \"create_time\"" +
                "from base_frequency_dict t " +
                " where t.is_del='1'";
        String sqlCount =" select count(1) as \"count\" from base_frequency_dict t where t.is_del='1'";
        if (StringUtils.isNoneBlank(name)){
            sql+=" and (t.freq_code like '%"+name+"%' or t.freq_name like '%"+name+"%')";
            sqlCount+=" and (t.freq_code like '%"+name+"%' or t.freq_name like '%"+name+"%')";
        }
        if (StringUtils.isNoneBlank(status)){
            sql+=" and t.status ='"+status+"'";
            sqlCount+=" and t.status ='"+status+"'";
        }
        List<Map<String,Object>> list=hibenateUtils.createSQLQuery(sql,page,pageSize);
        for (Map<String,Object> map:list){
            if (map.get("status")!=null&&"1".equalsIgnoreCase(map.get("status").toString())){
                map.put("statusName","生效中");
            }else {
                map.put("statusName","已失效");
            }
        }
        List<Map<String,Object>> countList=hibenateUtils.createSQLQuery(sqlCount);
        Integer count=0;
        if (countList!=null&&countList.size()>0) {
            count = Integer.valueOf(countList.get(0).get("count").toString());
        }
        MixEnvelop mixEnvelop = new MixEnvelop();
        mixEnvelop.setPageSize(pageSize);
        mixEnvelop.setCurrPage(page);
        mixEnvelop.setDetailModelList(list);
        mixEnvelop.setTotalCount(count);
        return mixEnvelop;
    }
    public BaseFrequencyDictDO findOneFrequency(String id){
        return baseFrequencyDictDao.findOne(id);
    }
    public Map<String, Object> batchInsertDrugs(List<BasFrequencyExcelDO> drugs, String templateId) throws ManageException {
        Map<String, Object> result = new HashMap<>();
        //批量存储的集合
//        int correctCount = 0;
//        List<BasDrugExcelDO> corrects = new ArrayList<>();
        BaseFrequencyDictDO baseFrequencyDictDO;
        //批量存储
        for(BasFrequencyExcelDO one:drugs) {
            baseFrequencyDictDO = new BaseFrequencyDictDO();
            baseFrequencyDictDO.setCreateTime(new Date());
            baseFrequencyDictDO.setIs_del("1");
            baseFrequencyDictDO.setDaily_times(one.getDaily_times());
            baseFrequencyDictDO.setFreq_code(one.getFreq_code());
            baseFrequencyDictDO.setFreq_name(one.getFreq_name());
            baseFrequencyDictDO.setFreq_sp(one.getFreq_sp());
            baseFrequencyDictDO.setHosp_code(one.getHosp_code());
            baseFrequencyDictDO.setIs_op_ip(one.getIs_op_ip());
            baseFrequencyDictDO.setStatus(one.getStatus());
            baseFrequencyDictDO.setWeekly_times(one.getWeekly_times());
            baseFrequencyDictDO=baseFrequencyDictDao.save(baseFrequencyDictDO);
            if (StringUtils.isNoneBlank(templateId)){
                BaseTemplateDictDO baseTemplateDictDO = new BaseTemplateDictDO();
                baseTemplateDictDO.setTemplateId(templateId);
                baseTemplateDictDO.setIsDel("1");
                baseTemplateDictDO.setDictId(baseFrequencyDictDO.getId());
                baseTemplateDictDO.setCreateTime(new Date());
                baseTemplateDictDao.save(baseTemplateDictDO);
            }
        }
        result.put("correctCount", drugs.size());
        return result;
    }
    public void updateFrequencyStatus(String id,String status,String del) throws  Exception{
        BaseFrequencyDictDO baseFrequencyDictDO = baseFrequencyDictDao.findOne(id);
        if (baseFrequencyDictDO!=null){
            if (StringUtils.isNotBlank(status)){
                baseFrequencyDictDO.setStatus(status);
            }
            if (StringUtils.isNotBlank(del)){
                baseFrequencyDictDO.setIs_del(del);
                List<BaseTemplateDictDO> baseTemplateDictDO = baseTemplateDictDao.findByDictId(id);
                if (baseTemplateDictDO!=null&&baseTemplateDictDO.size()>0){
                    throw  new Exception("该使频次已存在现有模板之中不允许删除");
                }
            }
            baseFrequencyDictDao.save(baseFrequencyDictDO);
        }
    }
}
