package com.ruoyi.project.training.bccnew.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.file.ExcelUtils;
import com.ruoyi.project.cui.domain.TPipe;
import com.ruoyi.project.system.domain.SysDept;
import com.ruoyi.project.system.domain.SysDictData;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.training.bccnew.domain.TTsTrainer;
import com.ruoyi.project.training.bccnew.service.ITTsTrainerService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 蒸汽裂解装置导师带徒导师库Controller
 *
 * @author ssy
 * @date 2024-08-12
 */
@RestController
@RequestMapping("/bccnew/trainer")
public class TTsTrainerController extends BaseController
{
    @Autowired
    private ITTsTrainerService tTsTrainerService;

    /**
     * 查询蒸汽裂解装置导师带徒导师库列表
     */
    @PreAuthorize("@ss.hasPermi('bccnew:trainer:list')")
    @GetMapping("/list")
    public TableDataInfo list(TTsTrainer tTsTrainer)
    {
        startPage();
        List<TTsTrainer> list = tTsTrainerService.selectTTsTrainerList(tTsTrainer);
        return getDataTable(list);
    }

    /**
     * 导出蒸汽裂解装置导师带徒导师库列表
     */
    @PreAuthorize("@ss.hasPermi('bccnew:trainer:export')")
    @Log(title = "蒸汽裂解装置导师带徒导师库", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TTsTrainer tTsTrainer)
    {
        List<TTsTrainer> list = tTsTrainerService.selectTTsTrainerList(tTsTrainer);
        ExcelUtil<TTsTrainer> util = new ExcelUtil<TTsTrainer>(TTsTrainer.class);
        return util.exportExcel(list, "trainer");
    }

    /**
     * 获取蒸汽裂解装置导师带徒导师库详细信息
     */
    @PreAuthorize("@ss.hasPermi('bccnew:trainer:query')")
    @GetMapping(value = "/{trainerId}")
    public AjaxResult getInfo(@PathVariable("trainerId") Long trainerId)
    {
        return AjaxResult.success(tTsTrainerService.selectTTsTrainerById(trainerId));
    }

    /**
     * 新增蒸汽裂解装置导师带徒导师库
     */
    @PreAuthorize("@ss.hasPermi('bccnew:trainer:add')")
    @Log(title = "蒸汽裂解装置导师带徒导师库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TTsTrainer tTsTrainer)
    {
        return toAjax(tTsTrainerService.insertTTsTrainer(tTsTrainer));
    }

    /**
     * 修改蒸汽裂解装置导师带徒导师库
     */
    @PreAuthorize("@ss.hasPermi('bccnew:trainer:edit')")
    @Log(title = "蒸汽裂解装置导师带徒导师库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TTsTrainer tTsTrainer)
    {
        return toAjax(tTsTrainerService.updateTTsTrainer(tTsTrainer));
    }

    /**
     * 删除蒸汽裂解装置导师带徒导师库
     */
    @PreAuthorize("@ss.hasPermi('bccnew:trainer:remove')")
    @Log(title = "蒸汽裂解装置导师带徒导师库", businessType = BusinessType.DELETE)
	@DeleteMapping("/{trainerIds}")
    public AjaxResult remove(@PathVariable Long[] trainerIds)
    {
        return toAjax(tTsTrainerService.deleteTTsTrainerByIds(trainerIds));
    }

    @PreAuthorize("@ss.hasPermi('bccnew:trainer:add')")
    @Log(title = "蒸汽裂解装置导师带徒导师库", businessType = BusinessType.INSERT)
    @PostMapping("/importData")
    public AjaxResult importData(@RequestParam("file") MultipartFile file) throws IOException
    {
        //获取操作人员ID
        Long userId = getUserId();
        Workbook workbook = ExcelUtils.getWorkBook(file);
        Sheet sheet = workbook.getSheetAt(0);
        List<TTsTrainer> list = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        int rowNum = sheet.getPhysicalNumberOfRows();
        int failNumber = 0;
        //报错行数统计
        List<Integer> failRow =new ArrayList<Integer>();
        for (int i = 1; i < rowNum; i++) {
            try {
                logger.info("读取行数：" + i);
                Row row = sheet.getRow(i);
                int cellNum = row.getLastCellNum();
                TTsTrainer entity = new TTsTrainer();
                for (int j = 0; j < cellNum; j++) {
                    Cell cell = row.getCell(j);
                    String cellValue = ExcelUtils.getCellValue(cell);
                    logger.info("cellValue:" + cellValue);
                    if (j == 0) {
                        entity.setStaffId(cellValue);
                    } else if (j == 1) {
                        entity.setTrainerName(cellValue);
                    } else if (j == 2) {
                        entity.setTeam(cellValue);
                    } else if (j == 3) {
                        entity.setPost(cellValue);
                    } else if (j == 4) {
                        entity.setExperience(cellValue);
                    } else if (j == 5) {
                        entity.setApprenticePosition(cellValue);
                    } else if (j == 6) {
                        entity.setRemarks(cellValue);
                    }
                }
                entity.setCreaterCode(userId.toString());
                logger.info("entity:" + entity);
                list.add(entity);
            }catch (Exception e){
                failNumber++;
                failRow.add(i+1);
            }
        }
        int successNumber = 0;
        int failNum = 0;
        for (TTsTrainer t : list
        ) {
            failNum++;
            try {
                add(t);
                successNumber++;
            }catch (Exception e){
                failNumber++;
                failRow.add(failNum+1);
            }
        }
        logger.info("list:" + JSON.toJSONString(list));
        logger.info("successNumber:" + successNumber);
        logger.info("failNumber:" + failNumber);
        logger.info("failRow:" + failRow);
        return AjaxResult.success(String.valueOf(successNumber), failRow);
    }
}
