package com.ruoyi.framework.task;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.sendEmail.IMailService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.ExcelUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.project.production.domain.TAlarmstatisticsDay;
import com.ruoyi.project.production.service.ITAlarmstatisticsDayService;
import com.ruoyi.project.system.domain.*;
import com.ruoyi.project.system.service.*;
import com.ruoyi.project.training.domain.TWorklicense;
import com.ruoyi.project.training.service.ITWorklicenseService;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 定时任务
 *
 * @author ruoyi
 */
@Component
public class AlarmDayCatchTask extends BaseController
{
    @Autowired
    private ITAlarmstatisticsDayService tAlarmstatisticsDayService;

    @Autowired
    private ISysDictTypeService iSysDictTypeService;

    @Autowired
    private ISysDeptService iSysDeptService;
    /**
     * 抓取
     */
    public void catchTask()
    {
        logger.info("AlarmDayCatchTask start============");
        //报错行数统计
        List<Integer> failRow =new ArrayList<Integer>();
//        File file = new File("D:\\cpmsfile\\alarm.xlsx");
        File file = new File("/data/cpmsfile/alarm.xlsx");
        Workbook workbook = ExcelUtils.getWorkBook(file);
        Sheet sheet = workbook.getSheetAt(0);
        List<TAlarmstatisticsDay> list = new ArrayList<TAlarmstatisticsDay>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //字典查询
        List<SysDictData> plant = iSysDictTypeService.selectDictDataByType("PLANT_DIVIDE");
        //部门查询
        List<SysDept> dept = iSysDeptService.selectAllDeptList(new SysDept());
        int rowNum = sheet.getPhysicalNumberOfRows();
        int failNumber = 0;
        for (int i = 1; i < rowNum; i++) {
            try {
                logger.info("读取行数：" + i);
                Row row = sheet.getRow(i);
                int cellNum = row.getLastCellNum();
                TAlarmstatisticsDay entity = new TAlarmstatisticsDay();
                for (int j = 0; j < cellNum; j++) {
                    Cell cell = row.getCell(j);
                    cell.setCellType(CellType.STRING);
                    String cellValue = ExcelUtils.getCellValue(cell);
                    logger.info("cellValue:" + cellValue);
                    if (j == 0) {
                        for (SysDictData p : plant) {
                            if (p.getDictLabel().equals(cellValue.trim())) {
                                entity.setPlantCode(p.getDictValue());//装置名称
                            }
                        }
                    } else if (j == 1) {
                        entity.setModulename(cellValue);//内容
                    } else if (j == 2) {
                        entity.setLevelname(cellValue);//等级
                    } else if (j == 3) {
                        if (cellValue != ""){
                            entity.setCount(Long.parseLong(cellValue));//次数
                        }
                    } else if (j == 4) {
                        if (cellValue.length() > 3) {
                            entity.setInputdate(new SimpleDateFormat(DateUtils.getDateFormat(cellValue)).parse(cellValue));//录入时间
                        }
                    } else if (j == 5) {
                        entity.setRemarks(cellValue);//备注
                    } else if (j == 6) {
                        for (SysDept d : dept) {
                            if (d.getDeptName().equals(cellValue.trim())) {
                                entity.setDeptId(d.getDeptId());//部门编号
                            }
                        }
                    }
                }
                logger.info("entity:" + entity);
                list.add(entity);
            }catch (Exception e){
                failNumber++;
                failRow.add(i+1);
            }
        }
        int successNumber = 0;
        int failNum = 0;
        for (TAlarmstatisticsDay t : list
        ) {
            failNum++;
            try {
                tAlarmstatisticsDayService.insertTAlarmstatisticsDay(t);
                successNumber++;
            }catch (Exception e){
                failNumber++;
                failRow.add(failNum+1);
            }
        }
        logger.info("list:" + JSON.toJSONString(list));
        logger.info("successNumber:" +String.valueOf(successNumber));
        logger.info("failNumber:" +String.valueOf(failNumber));
        logger.info("failRow:" +String.valueOf(failRow));
    }
}
