package com.ruoyi.project.reliability.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.POIConstants;
import com.ruoyi.common.utils.poi.PoiWordTools;
import org.apache.commons.collections.CollectionUtils;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.xmlbeans.XmlCursor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.reliability.mapper.TAbnormalreportMapper;
import com.ruoyi.project.reliability.domain.TAbnormalreport;
import com.ruoyi.project.reliability.service.ITAbnormalreportService;

import javax.annotation.Resource;

/**
 * 异常报告管理Service业务层处理
 *
 * @author ruoyi
 * @date 2020-11-27
 */
@Service
public class TAbnormalreportServiceImpl implements ITAbnormalreportService
{
    @Resource
    private TAbnormalreportMapper tAbnormalreportMapper;

    /**
     * 查询异常报告管理
     *
     * @param id 异常报告管理ID
     * @return 异常报告管理
     */
    @Override
    public TAbnormalreport selectTAbnormalreportById(Long id)
    {
        return tAbnormalreportMapper.selectTAbnormalreportById(id);
    }

    /**
     * 查询异常报告管理列表
     *
     * @param tAbnormalreport 异常报告管理
     * @return 异常报告管理
     */
    @Override
    public List<TAbnormalreport> selectTAbnormalreportList(TAbnormalreport tAbnormalreport)
    {
        return tAbnormalreportMapper.selectTAbnormalreportList(tAbnormalreport);
    }

    /**
     * 新增异常报告管理
     *
     * @param tAbnormalreport 异常报告管理
     * @return 结果
     */
    @Override
    public int insertTAbnormalreport(TAbnormalreport tAbnormalreport)
    {
        return tAbnormalreportMapper.insertTAbnormalreport(tAbnormalreport);
    }

    /**
     * 修改异常报告管理
     *
     * @param tAbnormalreport 异常报告管理
     * @return 结果
     */
    @Override
    public int updateTAbnormalreport(TAbnormalreport tAbnormalreport)
    {
        return tAbnormalreportMapper.updateTAbnormalreport(tAbnormalreport);
    }

    /**
     * 批量删除异常报告管理
     *
     * @param ids 需要删除的异常报告管理ID
     * @return 结果
     */
    @Override
    public int deleteTAbnormalreportByIds(Long[] ids)
    {
        return tAbnormalreportMapper.deleteTAbnormalreportByIds(ids);
    }

    /**
     * 删除异常报告管理信息
     *
     * @param id 异常报告管理ID
     * @return 结果
     */
    @Override
    public int deleteTAbnormalreportById(Long id)
    {
        return tAbnormalreportMapper.deleteTAbnormalreportById(id);
    }

    @Override
    public void dealAbnormalreport(XWPFDocument doc, XWPFParagraph paragraph, XWPFRun run) {
        run.setText("", 0);
        XmlCursor cursor = paragraph.getCTP().newCursor();
        XWPFTable tableOne = doc.insertNewTbl(cursor);// ---这个是关键
        // 设置表格宽度，第一行宽度就可以了，这个值的单位，目前我也还不清楚，还没来得及研究
        tableOne.setWidth(8500);
        // 表格第一行，对于每个列，必须使用createCell()，而不是getCell()，因为第一行嘛，肯定是属于创建的，没有create哪里来的get呢
        XWPFTableRow tableOneRowOne = tableOne.getRow(0);// 行
        PoiWordTools.setWordCellSelfStyle(tableOneRowOne.getCell(0), POIConstants.FONT_NAME, POIConstants.FONT_SIAZE, POIConstants.FONT_BOLD, POIConstants.ALIGNMENT, POIConstants.VERTICAL,POIConstants.FONT_COLOR, POIConstants.BG_TITLE_COLOR, POIConstants.CELL_WIDTH, "序号");
        PoiWordTools.setWordCellSelfStyle(tableOneRowOne.createCell(), POIConstants.FONT_NAME, POIConstants.FONT_SIAZE, POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.BG_TITLE_COLOR, POIConstants.CELL_WIDTH, "班组");
        PoiWordTools.setWordCellSelfStyle(tableOneRowOne.createCell(), POIConstants.FONT_NAME, POIConstants.FONT_SIAZE, POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.BG_TITLE_COLOR, POIConstants.CELL_WIDTH, "报告人");
        PoiWordTools.setWordCellSelfStyle(tableOneRowOne.createCell(), POIConstants.FONT_NAME, POIConstants.FONT_SIAZE, POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.BG_TITLE_COLOR, POIConstants.CELL_WIDTH, "报告号");
        PoiWordTools.setWordCellSelfStyle(tableOneRowOne.createCell(), POIConstants.FONT_NAME, POIConstants.FONT_SIAZE, POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.BG_TITLE_COLOR, POIConstants.CELL_WIDTH, "异常现象");
        PoiWordTools.setWordCellSelfStyle(tableOneRowOne.createCell(), POIConstants.FONT_NAME, POIConstants.FONT_SIAZE, POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.BG_TITLE_COLOR, POIConstants.CELL_WIDTH, "时间");
        List<TAbnormalreport> list = tAbnormalreportMapper.selectTAbnormalreportList(new TAbnormalreport());
        if (CollectionUtils.isNotEmpty(list)) {
            for (int i = 0; i < list.size(); i++) {
                // 表格第二行
                XWPFTableRow tableOneRowTwo = tableOne.createRow();// 行
                PoiWordTools.setWordCellSelfStyle(tableOneRowTwo.getCell(0), POIConstants.FONT_NAME, POIConstants.FONT_SIAZE, POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.BG_COLOR, POIConstants.CELL_WIDTH, String.valueOf(i + 1));
                PoiWordTools.setWordCellSelfStyle(tableOneRowTwo.getCell(1), POIConstants.FONT_NAME, POIConstants.FONT_SIAZE, POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.BG_COLOR, POIConstants.CELL_WIDTH, list.get(i).getTeam());
                PoiWordTools.setWordCellSelfStyle(tableOneRowTwo.getCell(2), POIConstants.FONT_NAME, POIConstants.FONT_SIAZE, POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.BG_COLOR, POIConstants.CELL_WIDTH, list.get(i).getReporter());
                PoiWordTools.setWordCellSelfStyle(tableOneRowTwo.getCell(3), POIConstants.FONT_NAME, POIConstants.FONT_SIAZE, POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.BG_COLOR, POIConstants.CELL_WIDTH, list.get(i).getReportno());
                PoiWordTools.setWordCellSelfStyle(tableOneRowTwo.getCell(4), POIConstants.FONT_NAME, POIConstants.FONT_SIAZE, POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.BG_COLOR, POIConstants.CELL_WIDTH, list.get(i).getAbnormalphen());
                PoiWordTools.setWordCellSelfStyle(tableOneRowTwo.getCell(5), POIConstants.FONT_NAME, POIConstants.FONT_SIAZE, POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.BG_COLOR, POIConstants.CELL_WIDTH, DateUtils.parseDateToStr("yyyy-MM-dd",list.get(i).getTime()));
            }
        }

    }
}
