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

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.poi.POIConstants;
import com.ruoyi.common.utils.poi.PoiWordTools;
import com.ruoyi.project.plant.domain.TMtActionlist;
import com.ruoyi.project.system.domain.SysDictData;
import com.ruoyi.project.system.service.ISysDictTypeService;
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.plant.mapper.TMtEhsopenitemMapper;
import com.ruoyi.project.plant.domain.TMtEhsopenitem;
import com.ruoyi.project.plant.service.ITMtEhsopenitemService;

/**
 * EHS开项跟踪Service业务层处理
 *
 * @author ruoyi
 * @date 2021-01-04
 */
@Service
public class TMtEhsopenitemServiceImpl implements ITMtEhsopenitemService
{
    @Autowired
    private TMtEhsopenitemMapper tMtEhsopenitemMapper;
    @Autowired
    private ISysDictTypeService iSysDictTypeService;
    /**
     * 查询EHS开项跟踪
     *
     * @param id EHS开项跟踪ID
     * @return EHS开项跟踪
     */
    @Override
    public TMtEhsopenitem selectTMtEhsopenitemById(Long id)
    {
        return tMtEhsopenitemMapper.selectTMtEhsopenitemById(id);
    }

    /**
     * 查询EHS开项跟踪列表
     *
     * @param tMtEhsopenitem EHS开项跟踪
     * @return EHS开项跟踪
     */
    @Override
    public List<TMtEhsopenitem> selectTMtEhsopenitemList(TMtEhsopenitem tMtEhsopenitem)
    {
        return tMtEhsopenitemMapper.selectTMtEhsopenitemList(tMtEhsopenitem);
    }

    /**
     * 新增EHS开项跟踪
     *
     * @param tMtEhsopenitem EHS开项跟踪
     * @return 结果
     */
    @Override
    public int insertTMtEhsopenitem(TMtEhsopenitem tMtEhsopenitem)
    {
        return tMtEhsopenitemMapper.insertTMtEhsopenitem(tMtEhsopenitem);
    }

    /**
     * 修改EHS开项跟踪
     *
     * @param tMtEhsopenitem EHS开项跟踪
     * @return 结果
     */
    @Override
    public int updateTMtEhsopenitem(TMtEhsopenitem tMtEhsopenitem)
    {
        return tMtEhsopenitemMapper.updateTMtEhsopenitem(tMtEhsopenitem);
    }

    /**
     * 批量删除EHS开项跟踪
     *
     * @param ids 需要删除的EHS开项跟踪ID
     * @return 结果
     */
    @Override
    public int deleteTMtEhsopenitemByIds(Long[] ids)
    {
        return tMtEhsopenitemMapper.deleteTMtEhsopenitemByIds(ids);
    }

    /**
     * 删除EHS开项跟踪信息
     *
     * @param id EHS开项跟踪ID
     * @return 结果
     */
    @Override
    public int deleteTMtEhsopenitemById(Long id)
    {
        return tMtEhsopenitemMapper.deleteTMtEhsopenitemById(id);
    }

    @Override
    public void dealEhsopenitemlist(XWPFDocument doc, XWPFParagraph paragraph, XWPFRun run, Long meetingId) {
        run.setText("", 0);//设置此文本在运行中的文本value -应在文件中显示的文字文本,pos --在文本数组中的位置（NB：从0开始）
        XmlCursor cursor = paragraph.getCTP().newCursor();
        XWPFTable tableOne = doc.insertNewTbl(cursor);// ---这个是关键

        // 设置表格宽度，第一行宽度就可以了，这个值的单位，目前我也还不清楚，还没来得及研究
        tableOne.setWidth(10000);

        // 表格第一行，对于每个列，必须使用createCell()，而不是getCell()，因为第一行嘛，肯定是属于创建的，没有create哪里来的get呢
        XWPFTableRow tableOneRowOne = tableOne.getRow(0);// 行
        PoiWordTools.setWordCellSelfStyle(tableOneRowOne.getCell(0), POIConstants.FONT_NAME, POIConstants.MT_FONT_SIZE,    POIConstants.FONT_BOLD_C, POIConstants.ALIGNMENT, POIConstants.VERTICAL,POIConstants.FONT_COLOR, POIConstants.MT_BG_TITLE_COLOR, "40", "开项类别");
        PoiWordTools.setWordCellSelfStyle(tableOneRowOne.createCell(), POIConstants.FONT_NAME, POIConstants.MT_FONT_SIZE,  POIConstants.FONT_BOLD_C, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.MT_BG_TITLE_COLOR, "40", "未关闭开项数量");
        PoiWordTools.setWordCellSelfStyle(tableOneRowOne.createCell(), POIConstants.FONT_NAME, POIConstants.MT_FONT_SIZE,  POIConstants.FONT_BOLD_C, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.MT_BG_TITLE_COLOR, "40", "逾期数量");
        TMtEhsopenitem tMtEhsopenitem = new TMtEhsopenitem();
        tMtEhsopenitem.setMeetingId(meetingId);
        List<TMtEhsopenitem> list = tMtEhsopenitemMapper.selectTMtEhsopenitemList(tMtEhsopenitem);

        if (CollectionUtils.isNotEmpty(list)) {
            for (int i = 0; i < list.size(); i++) {
                String fontColor = POIConstants.FONT_COLOR;
                if (list.get(i).getIsNew() == 1l) {
                    fontColor = POIConstants.FONT_COLOR_BLUE;
                }
                // 表格第二行
                XWPFTableRow tableOneRowTwo = tableOne.createRow();// 行
                PoiWordTools.setWordCellSelfStyle(tableOneRowTwo.getCell(0), POIConstants.FONT_NAME, POIConstants.MT_FONT_SIZE, POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, fontColor, POIConstants.BG_COLOR, "40", list.get(i).getCategory());
                PoiWordTools.setWordCellSelfStyle(tableOneRowTwo.getCell(1), POIConstants.FONT_NAME, POIConstants.MT_FONT_SIZE, POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, fontColor, POIConstants.BG_COLOR, "40", list.get(i).getNotopen() + "");
                PoiWordTools.setWordCellSelfStyle(tableOneRowTwo.getCell(2), POIConstants.FONT_NAME, POIConstants.MT_FONT_SIZE, POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, fontColor, POIConstants.BG_COLOR, "40", list.get(i).getExpired() + "");
            }
        }
    }
}
