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


import com.yihu.jw.base.dao.dict.DictJobTitleDao;
import com.yihu.jw.base.dao.doctor.BaseDoctorRoleDictDao;
import com.yihu.jw.base.dao.doctor.BaseDoctorRoleInfoDao;
import com.yihu.jw.base.endpoint.common.excel.AExcelReader;
import com.yihu.jw.dict.dao.BaseDrugDictDao;
import com.yihu.jw.dict.dao.BaseTemplateDictDao;
import com.yihu.jw.dict.dao.DictDoctorDutyDao;
import com.yihu.jw.dict.dao.DictHospitalDeptDao;
import com.yihu.jw.doctor.dao.BaseDoctorDao;
import com.yihu.jw.entity.base.dict.BaseDrugDictDO;
import com.yihu.jw.entity.base.dict.BaseTemplateDictDO;
import com.yihu.jw.entity.base.doctor.BaseDoctorDO;
import com.yihu.jw.org.dao.BaseOrgDao;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;


@Component
public class BaseDrugExcelDOReader extends AExcelReader {
    @Autowired
    private BaseDrugDictDao baseDrugDictDao;
    @Autowired
    private BaseTemplateDictDao baseTemplateDictDao;

    @Override
    public void read(Workbook rwb) throws Exception {
        try{
            Iterator<Sheet> sheets = rwb.sheetIterator();
            int j = 0, rows;
            BasDrugExcelDO basDrugExcelDO;
            getRepeat().put("charge_code", new HashSet<>());
            getRepeat().put("serial", new HashSet<>());
            getRepeat().put("group_no", new HashSet<>());
            getRepeat().put("drugname", new HashSet<>());
            getRepeat().put("dosage", new HashSet<>());
            getRepeat().put("mini_unit", new HashSet<>());
            getRepeat().put("pack_size", new HashSet<>());
            getRepeat().put("specification", new HashSet<>());
            getRepeat().put("retprice", new HashSet<>());
            getRepeat().put("pack_retprice", new HashSet<>());
            getRepeat().put("stock_amount", new HashSet<>());
            getRepeat().put("visible_flag", new HashSet<>());
            getRepeat().put("drug_flag", new HashSet<>());
            getRepeat().put("py_code", new HashSet<>());
            getRepeat().put("pack_unit", new HashSet<>());
            getRepeat().put("manu_name", new HashSet<>());
            getRepeat().put("weigh_unit", new HashSet<>());
            getRepeat().put("vol_unit", new HashSet<>());
            getRepeat().put("mini_unit_name", new HashSet<>());
            getRepeat().put("pack_unit_name", new HashSet<>());
            getRepeat().put("weigh_unit_name", new HashSet<>());
            getRepeat().put("vol_unit_name", new HashSet<>());
            getRepeat().put("win_no", new HashSet<>());
            getRepeat().put("max_stock_amount", new HashSet<>());
            getRepeat().put("mini_stock_amount", new HashSet<>());
            getRepeat().put("manufactory", new HashSet<>());
            getRepeat().put("status", new HashSet<>());
            errorLs.clear();
            correctLs.clear();
            while (sheets.hasNext()){
                Sheet sheet = sheets.next();
                if ((rows = sheet.getLastRowNum()) == 0) {
                    continue;
                }
                for (int i = 1; i <= rows; i++) {
                    basDrugExcelDO = new BasDrugExcelDO();
                    basDrugExcelDO.setCharge_code(replaceBlank(getCellCont(sheet, i, 0)));
                    basDrugExcelDO.setSerial(replaceBlank(getCellCont(sheet, i, 1)));
                    basDrugExcelDO.setGroup_no(getCellCont(sheet, i, 2));
                    basDrugExcelDO.setDrugname(replaceBlank(getCellCont(sheet, i, 3)));
                    basDrugExcelDO.setDosage(replaceBlank(getCellCont(sheet, i, 4)));
                    basDrugExcelDO.setMini_unit(replaceBlank(getCellCont(sheet, i, 5)));
                    basDrugExcelDO.setPack_size(replaceBlank(getCellCont(sheet, i, 6)));
                    basDrugExcelDO.setSpecification(replaceBlank(getCellCont(sheet, i, 7)));
                    basDrugExcelDO.setRetprice(getCellCont(sheet, i, 8));
                    basDrugExcelDO.setPack_retprice(replaceBlank(getCellCont(sheet, i, 9)));
                    basDrugExcelDO.setStock_amount(replaceBlank(getCellCont(sheet, i, 10)));
                    basDrugExcelDO.setVisible_flag(replaceBlank(getCellCont(sheet, i, 11)));
                    basDrugExcelDO.setDrug_flag(replaceBlank(getCellCont(sheet, i, 12)));
                    basDrugExcelDO.setPy_code(replaceBlank(getCellCont(sheet, i, 13)));
                    basDrugExcelDO.setPack_unit(replaceBlank(getCellCont(sheet, i, 14)));
                    basDrugExcelDO.setManu_name(replaceBlank(getCellCont(sheet, i, 15)));
                    basDrugExcelDO.setWeigh_unit(replaceBlank(getCellCont(sheet, i, 16)));
                    basDrugExcelDO.setVol_unit(replaceBlank(getCellCont(sheet, i, 17)));
                    basDrugExcelDO.setMini_unit_name(replaceBlank(getCellCont(sheet, i, 18)));
                    basDrugExcelDO.setPack_unit_name(replaceBlank(getCellCont(sheet, i, 19)));
                    basDrugExcelDO.setWeigh_unit_name(replaceBlank(getCellCont(sheet, i, 20)));
                    basDrugExcelDO.setVol_unit_name(replaceBlank(getCellCont(sheet, i, 21)));
                    basDrugExcelDO.setWin_no(replaceBlank(getCellCont(sheet, i, 22)));
                    basDrugExcelDO.setMax_stock_amount(replaceBlank(getCellCont(sheet, i, 23)));
                    basDrugExcelDO.setMini_stock_amount(replaceBlank(getCellCont(sheet, i, 24)));
                    basDrugExcelDO.setManufactory(replaceBlank(getCellCont(sheet, i, 25)));
                    basDrugExcelDO.setStatus(replaceBlank(getCellCont(sheet, i, 26)));
                    basDrugExcelDO.setExcelSeq(i);
                    int rs = basDrugExcelDO.validate(repeat);
                    if (validate(basDrugExcelDO)== 0||rs == 0) {
                        errorLs.add(basDrugExcelDO);
                    } else if (rs == 1) {
                        correctLs.add(basDrugExcelDO);
                    }

                }
                j++;
            }
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }finally {
            if (rwb != null) {
                rwb.close();
            }
        }
    }
    public int validate(BasDrugExcelDO basDrugExcelDO) {
        int rs = 1;
        if (StringUtils.isNoneBlank(basDrugExcelDO.getCharge_code())){
            List<BaseDrugDictDO> list  = baseDrugDictDao.findByChargeCode(basDrugExcelDO.getCharge_code());
            System.out.println("当前导入数据："+ basDrugExcelDO.getCharge_code());
            if (list!=null&&list.size()>0){
                String templateId = this.getTemplateId();
                if (StringUtils.isNoneBlank(templateId)){
                    BaseTemplateDictDO baseTemplateDictDO = new BaseTemplateDictDO();
                    baseTemplateDictDO.setTemplateId(templateId);
                    baseTemplateDictDO.setIsDel("1");
                    baseTemplateDictDO.setDictId(list.get(0).getId());
                    baseTemplateDictDO.setCreateTime(new Date());
                    baseTemplateDictDao.save(baseTemplateDictDO);
                }
                System.out.println("进验证有重复数据"+list.get(0).getCharge_code());
                return 0;
            }
        }

        return rs;
    }
}

