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

import com.yihu.jw.base.service.dict.importManage.BasDrugExcelDO;
import com.yihu.jw.base.service.doctor.excelImport.BaseDoctorExcelDO;
import com.yihu.jw.dict.dao.BaseDrugDictDao;
import com.yihu.jw.dict.dao.BaseTemplateDictDao;
import com.yihu.jw.entity.base.dict.*;
import com.yihu.jw.entity.base.doctor.BaseDoctorDO;
import com.yihu.jw.entity.base.doctor.BaseDoctorHospitalDO;
import com.yihu.jw.entity.base.doctor.BaseDoctorRoleDO;
import com.yihu.jw.entity.base.org.BaseOrgDO;
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 com.yihu.utils.security.MD5;
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.*;

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

@Service
public class BaseDrugDictService extends BaseJpaService<BaseDrugDictDO, BaseDrugDictDao> {
    @Autowired
    private HibenateUtils hibenateUtils;
    @Autowired
    private BaseDrugDictDao baseDrugDictDao;
    @Autowired
    private BaseTemplateDictDao baseTemplateDictDao;

    public BaseDrugDictDO saveNationalDrug(BaseDrugDictDO nationalDrugDictDO)  throws  Exception{
        BaseDrugDictDO baseDrugDictDO=baseDrugDictDao.findByCode(nationalDrugDictDO.getCharge_code());
        //判断新增或编辑
        if (!StringUtils.isNoneBlank(nationalDrugDictDO.getId())) {
            if (baseDrugDictDO!=null){
                throw  new Exception("该药品编码已存在");
            }
            nationalDrugDictDO.setCreate_time(new Date());
            nationalDrugDictDO.setUpdate_time(new Date());
            nationalDrugDictDO.setIs_del("1");
            return baseDrugDictDao.save(nationalDrugDictDO);
        }else {
            if (baseDrugDictDO!=null){
                if (!nationalDrugDictDO.getId().equalsIgnoreCase(baseDrugDictDO.getId())){
                    throw  new Exception("该药品编码已被使用");
                }
            }
            System.out.println("===="+nationalDrugDictDO.getId());
            BaseDrugDictDO oldDurgDictDO =  baseDrugDictDao.findOne(nationalDrugDictDO.getId());
            BeanUtils.copyProperties(nationalDrugDictDO, oldDurgDictDO, getNullPropertyNames(nationalDrugDictDO));
            oldDurgDictDO.setUpdate_time(new Date());
            return baseDrugDictDao.save(oldDurgDictDO);
        }
    }

    public MixEnvelop  findDrugDict(String name,String status,Integer page,Integer pageSize){
        String sql = "select t.id as \"id\"," +
                " t.charge_code as \"charge_code\"," +
                " t.drugname as \"drugname\"," +
                " t.specification as \"specification\"," +
                " t.py_code as \"py_code\"," +
                " t.supply_code as \"supply_code\"," +
                " t.supply_name as \"supply_name\"," +
                " t.retprice as \"retprice\"," +
                " t.manu_name as \"manu_name\"," +
                " t.weigh_unit as \"weigh_unit\"," +
                " t.mini_unit_name as \"mini_unit_name\"," +
                " t.pack_unit_name as \"pack_unit_name\"," +
                " t.weigh_unit_name as \"weigh_unit_name\"," +
                " t.vol_unit_name as \"vol_unit_name\"," +
                " t.win_no as \"win_no\"," +
                " t.pack_retprice as \"pack_retprice\"," +
                " t.mini_unit as \"mini_unit\"," +
                " t.pack_size as \"pack_size\"," +
                " t.action as \"action\"," +
                " t.serial as \"serial\"," +
                " t.group_no as \"group_no\"," +
                " t.stock_amount as \"stock_amount\"," +
                " t.visible_flag as \"visible_flag\"," +
                " t.drug_flag as \"drug_flag\"," +
                " t.create_time as \"create_time\"," +
                " t.update_time as \"update_time\"," +
                " t.status as \"status\"," +
                " t.mini_stock_amount as \"mini_stock_amount\"," +
                " t.dosage as \"dosage\"," +
                " t.max_stock_amount as \"max_stock_amount\"," +
                " t.pack_unit as \"pack_unit\"," +
                " t.manufactory as \"manufactory\"," +
                " t.vol_unit as \"vol_unit\"," +
                " t.is_del as \"is_del\"" +
                " from base_drug_dict t " +
                " where t.is_del='1' ";
        String sqlCount = "select count(1) as \"count\" from base_drug_dict t where t.is_del='1' ";
        if (StringUtils.isNoneBlank(name)){
            sql+=" and t.drugname like '%"+name+"%'";
            sqlCount+=" and t.drugname like '%"+name+"%'";
        }
        if (StringUtils.isNoneBlank(status)){
            sql+=" and t.status = '"+status+"'";
            sqlCount+=" and t.status = '"+status+"'";
        }
        List<Map<String,Object>> coutList=hibenateUtils.createSQLQuery(sqlCount);
        Integer count=0 ;
        if (coutList!=null&&coutList.size()>0){
            count=Integer.valueOf(coutList.get(0).get("count").toString());
        }
        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","已失效");
            }
        }
        MixEnvelop mixEnvelop = new MixEnvelop();
        mixEnvelop.setDetailModelList(list);
        mixEnvelop.setCurrPage(page);
        mixEnvelop.setPageSize(pageSize);
        mixEnvelop.setTotalCount(count);
        return mixEnvelop;
    }
    public BaseDrugDictDO findOneDrug(String id){
        return baseDrugDictDao.findOne(id);
    }

    public Map<String, Object> batchInsertDrugs(List<BasDrugExcelDO> drugs,String templateId) throws ManageException {
        Map<String, Object> result = new HashMap<>();
        //批量存储的集合
//        int correctCount = 0;
//        List<BasDrugExcelDO> corrects = new ArrayList<>();
        BaseDrugDictDO baseDrugDictDO;
        //批量存储
        for(BasDrugExcelDO one:drugs) {
            baseDrugDictDO = new BaseDrugDictDO();
            baseDrugDictDO.setCreate_time(new Date());
            baseDrugDictDO.setIs_del("1");
            baseDrugDictDO.setCharge_code(one.getCharge_code());
            baseDrugDictDO.setDosage(one.getDosage());
            baseDrugDictDO.setDrug_flag(one.getDrug_flag());
            baseDrugDictDO.setDrugname(one.getDrugname());
            baseDrugDictDO.setPack_retprice(one.getPack_retprice());
            baseDrugDictDO.setPack_size(one.getPack_size());
            baseDrugDictDO.setPack_unit_name(one.getPack_unit_name());
            baseDrugDictDO.setPack_unit(one.getPack_unit());
            baseDrugDictDO.setWin_no(one.getWin_no());
            baseDrugDictDO.setRetprice(one.getRetprice());
            baseDrugDictDO.setGroup_no(one.getGroup_no());
            baseDrugDictDO.setSerial(one.getSerial());
            baseDrugDictDO.setManu_name(one.getManu_name());
            baseDrugDictDO.setMax_stock_amount(one.getMax_stock_amount());
            baseDrugDictDO.setMini_stock_amount(one.getMini_stock_amount());
            baseDrugDictDO.setMini_unit(one.getMini_unit());
            baseDrugDictDO.setMini_unit_name(one.getMini_unit_name());
            baseDrugDictDO.setPy_code(one.getPy_code());
            baseDrugDictDO.setStock_amount(one.getStock_amount());
            baseDrugDictDO.setVisible_flag(one.getVisible_flag());
            baseDrugDictDO.setSpecification(one.getSpecification());
            baseDrugDictDO.setVol_unit_name(one.getVol_unit_name());
            baseDrugDictDO.setWeigh_unit(one.getWeigh_unit());
            baseDrugDictDO.setWeigh_unit_name(one.getWeigh_unit_name());
            baseDrugDictDO.setManufactory(one.getWeigh_unit_name());
            baseDrugDictDO.setStatus(one.getStatus());
            baseDrugDictDO=baseDrugDictDao.save(baseDrugDictDO);
            if (StringUtils.isNoneBlank(templateId)){
                BaseTemplateDictDO baseTemplateDictDO = new BaseTemplateDictDO();
                baseTemplateDictDO.setTemplateId(templateId);
                baseTemplateDictDO.setIsDel("1");
                baseTemplateDictDO.setDictId(baseDrugDictDO.getId());
                baseTemplateDictDO.setCreateTime(new Date());
                baseTemplateDictDao.save(baseTemplateDictDO);
            }

        }
        result.put("correctCount", drugs.size());
        return result;
    }
    public void updateDrugStatus(String id,String status,String del) throws  Exception{
        BaseDrugDictDO baseDrugDictDO = baseDrugDictDao.findOne(id);
        if (baseDrugDictDO!=null){
            if (StringUtils.isNoneBlank(status)){
                baseDrugDictDO.setStatus(status);
            }
            if (StringUtils.isNoneBlank(del)){
                List<BaseTemplateDictDO> baseTemplateDictDO = baseTemplateDictDao.findByDictId(id);
                if (baseTemplateDictDO!=null&&baseTemplateDictDO.size()>0){
                    throw  new Exception("该药品已存在现有模板之中不允许删除");
                }
                baseDrugDictDO.setIs_del(del);
            }
            baseDrugDictDao.save(baseDrugDictDO);
        }
    }

}
