package com.ruoyi.project.production.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.production.mapper.TAlarmstatisticsMonthMapper;
import com.ruoyi.project.production.domain.TAlarmstatisticsMonth;
import com.ruoyi.project.production.service.ITAlarmstatisticsMonthService;

import javax.annotation.Resource;

/**
 * 报警统计（上月）Service业务层处理
 *
 * @author ruoyi
 * @date 2020-11-26
 */
@Service
public class TAlarmstatisticsMonthServiceImpl implements ITAlarmstatisticsMonthService
{
    @Resource
    private TAlarmstatisticsMonthMapper tAlarmstatisticsMonthMapper;

    /**
     * 查询报警统计（上月）
     *
     * @param id 报警统计（上月）ID
     * @return 报警统计（上月）
     */
    @Override
    public TAlarmstatisticsMonth selectTAlarmstatisticsMonthById(Long id)
    {
        return tAlarmstatisticsMonthMapper.selectTAlarmstatisticsMonthById(id);
    }

    /**
     * 查询报警统计（上月）列表
     *
     * @param tAlarmstatisticsMonth 报警统计（上月）
     * @return 报警统计（上月）
     */
    @Override
    public List<TAlarmstatisticsMonth> selectTAlarmstatisticsMonthList(TAlarmstatisticsMonth tAlarmstatisticsMonth)
    {
        return tAlarmstatisticsMonthMapper.selectTAlarmstatisticsMonthList(tAlarmstatisticsMonth);
    }

    /**
     * 新增报警统计（上月）
     *
     * @param tAlarmstatisticsMonth 报警统计（上月）
     * @return 结果
     */
    @Override
    public int insertTAlarmstatisticsMonth(TAlarmstatisticsMonth tAlarmstatisticsMonth)
    {
        return tAlarmstatisticsMonthMapper.insertTAlarmstatisticsMonth(tAlarmstatisticsMonth);
    }

    /**
     * 修改报警统计（上月）
     *
     * @param tAlarmstatisticsMonth 报警统计（上月）
     * @return 结果
     */
    @Override
    public int updateTAlarmstatisticsMonth(TAlarmstatisticsMonth tAlarmstatisticsMonth)
    {
        return tAlarmstatisticsMonthMapper.updateTAlarmstatisticsMonth(tAlarmstatisticsMonth);
    }

    /**
     * 批量删除报警统计（上月）
     *
     * @param ids 需要删除的报警统计（上月）ID
     * @return 结果
     */
    @Override
    public int deleteTAlarmstatisticsMonthByIds(Long[] ids)
    {
        return tAlarmstatisticsMonthMapper.deleteTAlarmstatisticsMonthByIds(ids);
    }

    /**
     * 删除报警统计（上月）信息
     *
     * @param id 报警统计（上月）ID
     * @return 结果
     */
    @Override
    public int deleteTAlarmstatisticsMonthById(Long id)
    {
        return tAlarmstatisticsMonthMapper.deleteTAlarmstatisticsMonthById(id);
    }

    @Override
    public void dealAlarmstatistics(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<TAlarmstatisticsMonth> list = tAlarmstatisticsMonthMapper.selectTAlarmstatisticsMonthList(new TAlarmstatisticsMonth());
        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).getModulename());
                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).getLevelname());
                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, String.valueOf(list.get(i).getCount()));
                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).getAnalysis());
                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).getInputdate()));
            }
        }
    }
}
