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

import com.yihu.jw.base.service.dict.importManage.BasDrugExcelDO;
import com.yihu.jw.base.service.dict.importManage.BasDrugUseExcelDO;
import com.yihu.jw.dict.dao.BaseDrugDictDao;
import com.yihu.jw.dict.dao.BaseDrugUseDictDao;
import com.yihu.jw.dict.dao.BaseTemplateDictDao;
import com.yihu.jw.entity.base.dict.BaseDrugDictDO;
import com.yihu.jw.entity.base.dict.BaseDrugUseDictDO;
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 BaseDrugUseService extends BaseJpaService<BaseDrugUseDictDO, BaseDrugUseDictDao> {
    @Autowired
    private HibenateUtils hibenateUtils;
    @Autowired
    private BaseDrugUseDictDao baseDrugUseDictDao;
    @Autowired
    private BaseTemplateDictDao baseTemplateDictDao;

    public void saveBaseDrugUse(BaseDrugUseDictDO baseDrugUseDictDO) throws  Exception{
        BaseDrugUseDictDO baseDrugUseDictDO1=baseDrugUseDictDao.findByCode(baseDrugUseDictDO.getSupply_code());
        if (StringUtils.isNotBlank(baseDrugUseDictDO.getId())){
            if (baseDrugUseDictDO1!=null){
                throw  new Exception("该使用方式code已存在");
            }
            BaseDrugUseDictDO oldDurgDictDO= baseDrugUseDictDao.findOne(baseDrugUseDictDO.getId());
            BeanUtils.copyProperties(baseDrugUseDictDO, oldDurgDictDO, getNullPropertyNames(baseDrugUseDictDO));
            baseDrugUseDictDao.save(oldDurgDictDO);
        }else {
            if (baseDrugUseDictDO1!=null){
                if (!baseDrugUseDictDO1.getId().equalsIgnoreCase(baseDrugUseDictDO.getId())){
                    throw  new Exception("该使用方式code已被使用");
                }
            }
            baseDrugUseDictDO.setCreateTime(new Date());
            baseDrugUseDictDO.setDel_flag("1");
            baseDrugUseDictDao.save(baseDrugUseDictDO);
        }
    }

    public MixEnvelop findDrugUseList(String name,String status,Integer page ,Integer pageSize){
        String sql="select t.id as \"id\"," +
                " t.supply_code as \"supply_code\"," +
                " t.supply_name as \"supply_name\"," +
                " t.supply_occ as \"supply_occ\"," +
                " t.del_flag as \"del_flag\"," +
                " t.sort_code as \"sort_code\"," +
                " t.print_name as \"print_name\"," +
                " t.py_code as \"py_code\"," +
                " t.d_code as \"d_code\"," +
                " t.yb_name as \"yb_name\"," +
                " t.yb_code as \"yb_code\"," +
                " t.status as \"status\"," +
                " t.create_time as \"create_time\" " +
                "from base_drug_use_dict t " +
                " where t.del_flag='1'";
        String sqlCount =" select count(1) as \"count\" from base_drug_use_dict t where t.del_flag='1'";
        if (StringUtils.isNoneBlank(name)){
            sql+=" and t.supply_name like '%"+name+"%'";
            sqlCount+=" and t.supply_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 BaseDrugUseDictDO findOneUseDict(String dictId){
        return baseDrugUseDictDao.findOne(dictId);
    }


    public Map<String, Object> batchInsertDrugs(List<BasDrugUseExcelDO> drugs, String templateId) throws ManageException {
        Map<String, Object> result = new HashMap<>();
        //批量存储的集合
//        int correctCount = 0;
//        List<BasDrugExcelDO> corrects = new ArrayList<>();
        BaseDrugUseDictDO baseDrugUseDictDO;
        //批量存储
        for(BasDrugUseExcelDO one:drugs) {
            baseDrugUseDictDO = new BaseDrugUseDictDO();
            baseDrugUseDictDO.setCreateTime(new Date());
            baseDrugUseDictDO.setStatus(one.getStatus());
            baseDrugUseDictDO.setDel_flag("1");
            baseDrugUseDictDO.setD_code(one.getD_code());
            baseDrugUseDictDO.setPrint_name(one.getPrint_name());
            baseDrugUseDictDO.setPy_code(one.getPy_code());
            baseDrugUseDictDO.setSort_code(one.getSort_code());
            baseDrugUseDictDO.setSupply_code(one.getSupply_code());
            baseDrugUseDictDO.setSupply_name(one.getSupply_name());
            baseDrugUseDictDO.setSupply_occ(one.getSupply_occ());
            baseDrugUseDictDO.setYb_code(one.getYb_code());
            baseDrugUseDictDO.setYb_name(one.getYb_name());
            baseDrugUseDictDO=baseDrugUseDictDao.save(baseDrugUseDictDO);
            if (StringUtils.isNoneBlank(templateId)){
                BaseTemplateDictDO baseTemplateDictDO = new BaseTemplateDictDO();
                baseTemplateDictDO.setTemplateId(templateId);
                baseTemplateDictDO.setIsDel("1");
                baseTemplateDictDO.setDictId(baseDrugUseDictDO.getId());
                baseTemplateDictDO.setCreateTime(new Date());
                baseTemplateDictDao.save(baseTemplateDictDO);
            }

        }
        result.put("correctCount", drugs.size());
        return result;
    }
    public void updateDrugUseStatus(String id,String status,String del) throws  Exception{
        BaseDrugUseDictDO baseDrugUseDictDO = baseDrugUseDictDao.findOne(id);
        if (baseDrugUseDictDO!=null){
            if (StringUtils.isNoneBlank(status)){
                baseDrugUseDictDO.setStatus(status);
            }
            if (StringUtils.isNoneBlank(del)){
                baseDrugUseDictDO.setDel_flag(del);
                List<BaseTemplateDictDO> baseTemplateDictDO = baseTemplateDictDao.findByDictId(id);
                if (baseTemplateDictDO!=null&&baseTemplateDictDO.size()>0){
                    throw  new Exception("该使用方式已存在现有模板之中不允许删除");
                }
            }
            baseDrugUseDictDao.save(baseDrugUseDictDO);
        }
    }
}
