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


import com.yihu.jw.base.endpoint.common.excel.AExcelReader;
import com.yihu.jw.dict.dao.BaseDrugUseDictDao;
import com.yihu.jw.dict.dao.BaseFrequencyDictDao;
import com.yihu.jw.dict.dao.BaseTemplateDictDao;
import com.yihu.jw.entity.base.dict.BaseDrugUseDictDO;
import com.yihu.jw.entity.base.dict.BaseFrequencyDictDO;
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 BaseFrequencyExcelDOReader extends AExcelReader {
    @Autowired
    private BaseFrequencyDictDao baseFrequencyDictDao;
    @Autowired
    private BaseTemplateDictDao baseTemplateDictDao;

    @Override
    public void read(Workbook rwb) throws Exception {
        try{
            Iterator<Sheet> sheets = rwb.sheetIterator();
            int j = 0, rows;
            BasFrequencyExcelDO basFrequencyExcelDO;
            getRepeat().put("freq_code", new HashSet<>());
            getRepeat().put("freq_name", new HashSet<>());
            getRepeat().put("freq_sp", new HashSet<>());
            getRepeat().put("daily_times", new HashSet<>());
            getRepeat().put("weekly_times", new HashSet<>());
            getRepeat().put("hosp_code", new HashSet<>());
            getRepeat().put("is_op_ip", 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++) {
                    basFrequencyExcelDO = new BasFrequencyExcelDO();
                    basFrequencyExcelDO.setFreq_code(replaceBlank(getCellCont(sheet, i, 0)));
                    basFrequencyExcelDO.setFreq_name(replaceBlank(getCellCont(sheet, i, 1)));
                    basFrequencyExcelDO.setFreq_sp(getCellCont(sheet, i, 2));
                    basFrequencyExcelDO.setDaily_times(replaceBlank(getCellCont(sheet, i, 3)));
                    basFrequencyExcelDO.setWeekly_times(replaceBlank(getCellCont(sheet, i, 4)));
                    basFrequencyExcelDO.setHosp_code(replaceBlank(getCellCont(sheet, i, 5)));
                    basFrequencyExcelDO.setIs_op_ip(replaceBlank(getCellCont(sheet, i, 6)));
                    basFrequencyExcelDO.setStatus(replaceBlank(getCellCont(sheet, i, 7)));
                    basFrequencyExcelDO.setExcelSeq(i);
                    int rs = basFrequencyExcelDO.validate(repeat);
                    if (validate(basFrequencyExcelDO)== 0||rs == 0) {
                        errorLs.add(basFrequencyExcelDO);
                    } else if (rs == 1) {
                        correctLs.add(basFrequencyExcelDO);
                    }

                }
                j++;
            }
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }finally {
            if (rwb != null) {
                rwb.close();
            }
        }
    }
    public int validate(BasFrequencyExcelDO basFrequencyExcelDO) {
        int rs = 1;
        if (StringUtils.isNoneBlank(basFrequencyExcelDO.getFreq_code())){
            List<BaseFrequencyDictDO> list  = baseFrequencyDictDao.findByFrqCode(basFrequencyExcelDO.getFreq_code());
            System.out.println("当前导入数据："+ basFrequencyExcelDO.getFreq_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).getFreq_code());
                return 0;
            }
        }

        return rs;
    }
}

