package com.ruoyi.project.asset.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.file.ExcelUtils;
import com.ruoyi.project.asset.service.ITLeakagePointsService;
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.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.asset.domain.TLeakagePoints;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 漏点清单Controller
 * 
 * @author ruoyi
 * @date 2024-03-28
 */
@RestController
@RequestMapping("/asset/points")
public class TLeakagePointsController extends BaseController
{
    @Autowired
    private ITLeakagePointsService tLeakagePointsService;

    /**
     * 查询漏点清单列表
     */
    @PreAuthorize("@ss.hasPermi('asset:points:list')")
    @GetMapping("/list")
    public TableDataInfo list(TLeakagePoints tLeakagePoints)
    {
        startPage();
        List<TLeakagePoints> list = tLeakagePointsService.selectTLeakagePointsList(tLeakagePoints);
        return getDataTable(list);
    }

    /**
     * 导出漏点清单列表
     */
    @PreAuthorize("@ss.hasPermi('asset:points:export')")
    @Log(title = "漏点清单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TLeakagePoints tLeakagePoints)
    {
        List<TLeakagePoints> list = tLeakagePointsService.selectTLeakagePointsList(tLeakagePoints);
        ExcelUtil<TLeakagePoints> util = new ExcelUtil<TLeakagePoints>(TLeakagePoints.class);
        util.exportExcel(response, list, "漏点清单数据");
    }

    /**
     * 获取漏点清单详细信息
     */
    @PreAuthorize("@ss.hasPermi('asset:points:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tLeakagePointsService.selectTLeakagePointsById(id));
    }

    /**
     * 新增漏点清单
     */
    @PreAuthorize("@ss.hasPermi('asset:points:add')")
    @Log(title = "漏点清单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TLeakagePoints tLeakagePoints)
    {
        return toAjax(tLeakagePointsService.insertTLeakagePoints(tLeakagePoints));
    }

    /**
     * 修改漏点清单
     */
    @PreAuthorize("@ss.hasPermi('asset:points:edit')")
    @Log(title = "漏点清单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TLeakagePoints tLeakagePoints)
    {
        return toAjax(tLeakagePointsService.updateTLeakagePoints(tLeakagePoints));
    }

    /**
     * 删除漏点清单
     */
    @PreAuthorize("@ss.hasPermi('asset:points:remove')")
    @Log(title = "漏点清单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tLeakagePointsService.deleteTLeakagePointsByIds(ids));
    }


    @Log(title = "漏点清单批量导入", businessType = BusinessType.INSERT)
    @PostMapping("/importData")
    public AjaxResult importData(@RequestParam("file") MultipartFile file) throws IOException {
        //获取操作人员ID
        Long userId = getUserId();
        //报错行数统计
        List<Integer> failRow = new ArrayList<>();
        Workbook workbook = ExcelUtils.getWorkBook(file);
        Sheet sheet = workbook.getSheetAt(0);
        List<TLeakagePoints> list = new ArrayList<>();
        int rowNum = sheet.getPhysicalNumberOfRows();
        int failNumber = 0;
        for (int i = 2; i < rowNum; i++) {
            try {
                logger.info("读取行数：" + i);
                Row row = sheet.getRow(i);
                int cellNum = row.getLastCellNum();
                TLeakagePoints entity = new TLeakagePoints();
                for (int j = 0; j < cellNum; j++) {
                    Cell cell = row.getCell(j);
                    if (cell == null) {
                        continue;
                    }
                    String cellValue = ExcelUtils.getCellValue(cell);
                    logger.info("cellValue:" + cellValue);
                    if (j == 0) {
                        //type
                        entity.setPointNo(cellValue);
                    } else if (j == 1) {
                        //内容
                        entity.setLeakagePosition(cellValue);
                    } else if (j == 2) {
                        //PID图号/管线号
                        entity.setLeakageMedium(cellValue);
                    } else if (j == 3) {
                        //是分解具体位置及描述
                        entity.setDiscoveryTime(DateUtils.parseDate(cellValue));
                    } else if (j == 4) {
                        //According to
                        entity.setDiscoveryName(cellValue);
                    } else if (j == 5) {
                        //资产
                        entity.setLeakageAmount(cellValue);
                    } else if (j == 6) {
                        //DCS操作
                        entity.setSapNo(cellValue);
                    } else if (j == 7) {
                        //现场操作8小时
                        entity.setLeakageEliminationTime(DateUtils.parseDate(cellValue));
                    } else if (j == 8) {
                        //现场操作8小时外
                        entity.setLeakageEliminationFunc(cellValue);
                    } else if (j == 9) {
                        //巡检8小时
                        entity.setListing(cellValue);
                    } else if (j == 10) {
                        //巡检8小时外
                        entity.setPressurePlugging(cellValue);
                    } else if (j == 11) {
                        //维修8小时
                        entity.setLeakageEliminationResult(cellValue);
                    } else if (j == 12) {
                        //维修8小时外
                        entity.setNextCheckDate(DateUtils.parseDate(cellValue));
                    } else if (j == 13) {
                        //维修协调8小时
                        entity.setRemarks(cellValue);
                    }
                }
                entity.setCreaterCode(String.valueOf(userId));
                logger.info("entity:" + entity);
                list.add(entity);
            } catch (Exception e) {
                failNumber++;
                logger.info("e:" + JSON.toJSONString(e));
                failRow.add(i + 1);
            }
        }
        int successNumber = 0;
        int failNum = 0;
        for (TLeakagePoints t : list
        ) {
            failNum++;
            try {
                //根据使用证、注册编号、位号，进行数据更新
                add(t);
                successNumber++;
            } catch (Exception e) {
                failNumber++;
                logger.info("e:" + e);
                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);
    }
}
