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


import com.yihu.jw.base.endpoint.common.excel.AExcelReader;
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 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 BaseDrugUseExcelDOReader extends AExcelReader {
    @Autowired
    private BaseDrugUseDictDao baseDrugUseDictDao;
    @Autowired
    private BaseTemplateDictDao baseTemplateDictDao;

    @Override
    public void read(Workbook rwb) throws Exception {
        try{
            Iterator<Sheet> sheets = rwb.sheetIterator();
            int j = 0, rows;
            BasDrugUseExcelDO basDrugUseExcelDO;
            getRepeat().put("supply_code", new HashSet<>());
            getRepeat().put("supply_name", new HashSet<>());
            getRepeat().put("supply_occ", new HashSet<>());
            getRepeat().put("sort_code", new HashSet<>());
            getRepeat().put("print_name", new HashSet<>());
            getRepeat().put("py_code", new HashSet<>());
            getRepeat().put("d_code", new HashSet<>());
            getRepeat().put("yb_name", new HashSet<>());
            getRepeat().put("yb_code", 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++) {
                    basDrugUseExcelDO = new BasDrugUseExcelDO();
                    basDrugUseExcelDO.setSupply_code(replaceBlank(getCellCont(sheet, i, 0)));
                    basDrugUseExcelDO.setSupply_name(replaceBlank(getCellCont(sheet, i, 1)));
                    basDrugUseExcelDO.setSupply_occ(getCellCont(sheet, i, 2));
                    basDrugUseExcelDO.setSort_code(replaceBlank(getCellCont(sheet, i, 3)));
                    basDrugUseExcelDO.setPrint_name(replaceBlank(getCellCont(sheet, i, 4)));
                    basDrugUseExcelDO.setPy_code(replaceBlank(getCellCont(sheet, i, 5)));
                    basDrugUseExcelDO.setD_code(replaceBlank(getCellCont(sheet, i, 6)));
                    basDrugUseExcelDO.setYb_name(getCellCont(sheet, i, 7));
                    basDrugUseExcelDO.setYb_code(replaceBlank(getCellCont(sheet, i, 8)));
                    basDrugUseExcelDO.setStatus(getCellCont(sheet, i, 9));
                    basDrugUseExcelDO.setExcelSeq(i);
                    int rs = basDrugUseExcelDO.validate(repeat);
                    if (validate(basDrugUseExcelDO)== 0||rs == 0) {
                        errorLs.add(basDrugUseExcelDO);
                    } else if (rs == 1) {
                        correctLs.add(basDrugUseExcelDO);
                    }

                }
                j++;
            }
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }finally {
            if (rwb != null) {
                rwb.close();
            }
        }
    }
    public int validate(BasDrugUseExcelDO basDrugUseExcelDO) {
        int rs = 1;
        if (StringUtils.isNoneBlank(basDrugUseExcelDO.getSupply_code())){
            List<BaseDrugUseDictDO> list  = baseDrugUseDictDao.findBySupCode(basDrugUseExcelDO.getSupply_code());
            System.out.println("当前导入数据："+ basDrugUseExcelDO.getSupply_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).getSupply_code());
                return 0;
            }
        }

        return rs;
    }
}

