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


import com.yihu.jw.base.dao.dict.DictIcd10Dao;
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.entity.base.dict.BaseDrugDictDO;
import com.yihu.jw.entity.base.dict.BaseTemplateDictDO;
import com.yihu.jw.entity.base.dict.DictIcd10DO;
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 BaseIcd10ExcelDOReader extends AExcelReader {
    @Autowired
    private DictIcd10Dao dictIcd10Dao;
    @Autowired
    private BaseTemplateDictDao baseTemplateDictDao;

    @Override
    public void read(Workbook rwb) throws Exception {
        try{
            Iterator<Sheet> sheets = rwb.sheetIterator();
            int j = 0, rows;
            BasIcd10ExcelDO basIcd10ExcelDO;
            getRepeat().put("code", new HashSet<>());
            getRepeat().put("name", new HashSet<>());
            getRepeat().put("description", new HashSet<>());
            getRepeat().put("disea_reason", new HashSet<>());
            getRepeat().put("death_reason", new HashSet<>());
            getRepeat().put("py_code", new HashSet<>());
            getRepeat().put("d_code", new HashSet<>());
            getRepeat().put("flag", 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++) {
                    basIcd10ExcelDO = new BasIcd10ExcelDO();
                    basIcd10ExcelDO.setCode(replaceBlank(getCellCont(sheet, i, 0)));
                    basIcd10ExcelDO.setName(replaceBlank(getCellCont(sheet, i, 1)));
                    basIcd10ExcelDO.setDescription(getCellCont(sheet, i, 2));
                    basIcd10ExcelDO.setDisea_reason(replaceBlank(getCellCont(sheet, i, 3)));
                    basIcd10ExcelDO.setDeath_reason(replaceBlank(getCellCont(sheet, i, 4)));
                    basIcd10ExcelDO.setPy_code(replaceBlank(getCellCont(sheet, i, 5)));
                    basIcd10ExcelDO.setD_code(replaceBlank(getCellCont(sheet, i, 6)));
                    basIcd10ExcelDO.setFlag(replaceBlank(getCellCont(sheet, i, 7)));
                    basIcd10ExcelDO.setStatus(getCellCont(sheet, i, 8));
                    basIcd10ExcelDO.setExcelSeq(i);
                    int rs = basIcd10ExcelDO.validate(repeat);
                    if (validate(basIcd10ExcelDO)== 0||rs == 0) {
                        errorLs.add(basIcd10ExcelDO);
                    } else if (rs == 1) {
                        correctLs.add(basIcd10ExcelDO);
                    }

                }
                j++;
            }
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }finally {
            if (rwb != null) {
                rwb.close();
            }
        }
    }
    public int validate(BasIcd10ExcelDO basIcd10ExcelDO) {
        int rs = 1;
        if (StringUtils.isNoneBlank(basIcd10ExcelDO.getCode())){
            List<DictIcd10DO> list  = dictIcd10Dao.findByIcdCode(basIcd10ExcelDO.getCode());
            System.out.println("当前导入数据："+ basIcd10ExcelDO.getCode());
            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().toString());
                    baseTemplateDictDO.setCreateTime(new Date());
                    baseTemplateDictDao.save(baseTemplateDictDO);
                }
                System.out.println("进验证有重复数据"+list.get(0).getCode());
                return 0;
            }
        }

        return rs;
    }
}

