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

import java.util.List;

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.ehs.mapper.TEhskpinewMapper;
import com.ruoyi.project.ehs.domain.TEhskpinew;
import com.ruoyi.project.ehs.service.ITEhskpinewService;

import javax.annotation.Resource;

/**
 * KPI统计及格Service业务层处理
 *
 * @author ruoyi
 * @date 2020-12-01
 */
@Service
public class TEhskpinewServiceImpl implements ITEhskpinewService
{
    @Resource
    private TEhskpinewMapper tEhskpinewMapper;

    /**
     * 查询KPI统计及格
     *
     * @param id KPI统计及格ID
     * @return KPI统计及格
     */
    @Override
    public TEhskpinew selectTEhskpinewById(Long id)
    {
        return tEhskpinewMapper.selectTEhskpinewById(id);
    }

    /**
     * 查询KPI统计及格列表
     *
     * @param tEhskpinew KPI统计及格
     * @return KPI统计及格
     */
    @Override
    public List<TEhskpinew> selectTEhskpinewList(TEhskpinew tEhskpinew)
    {
        return tEhskpinewMapper.selectTEhskpinewList(tEhskpinew);
    }

    /**
     * 新增KPI统计及格
     *
     * @param tEhskpinew KPI统计及格
     * @return 结果
     */
    @Override
    public int insertTEhskpinew(TEhskpinew tEhskpinew)
    {
        return tEhskpinewMapper.insertTEhskpinew(tEhskpinew);
    }

    /**
     * 修改KPI统计及格
     *
     * @param tEhskpinew KPI统计及格
     * @return 结果
     */
    @Override
    public int updateTEhskpinew(TEhskpinew tEhskpinew)
    {
        return tEhskpinewMapper.updateTEhskpinew(tEhskpinew);
    }

    /**
     * 批量删除KPI统计及格
     *
     * @param ids 需要删除的KPI统计及格ID
     * @return 结果
     */
    @Override
    public int deleteTEhskpinewByIds(Long[] ids)
    {
        return tEhskpinewMapper.deleteTEhskpinewByIds(ids);
    }

    /**
     * 删除KPI统计及格信息
     *
     * @param id KPI统计及格ID
     * @return 结果
     */
    @Override
    public int deleteTEhskpinewById(Long id)
    {
        return tEhskpinewMapper.deleteTEhskpinewById(id);
    }

    @Override
    public void dealKPI(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, "KPI名称");
        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, "KPI英文名");
        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<TEhskpinew> list = tEhskpinewMapper.selectTEhskpinewList(new TEhskpinew());
        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, String.valueOf(list.get(i).getYear()));
                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).getKpiname());
                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).getKpinameen());
                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, String.valueOf(list.get(i).getSpring()));
                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, String.valueOf(list.get(i).getSummer()));
                PoiWordTools.setWordCellSelfStyle(tableOneRowTwo.getCell(6), POIConstants.FONT_NAME, POIConstants.FONT_SIAZE, POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.BG_COLOR, POIConstants.CELL_WIDTH, String.valueOf(list.get(i).getAutumn()));
                PoiWordTools.setWordCellSelfStyle(tableOneRowTwo.getCell(7), POIConstants.FONT_NAME, POIConstants.FONT_SIAZE, POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.BG_COLOR, POIConstants.CELL_WIDTH, String.valueOf(list.get(i).getWinter()));
            }
        }
    }
}
