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 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.TMtRiskreminderMapper;
import com.ruoyi.project.plant.domain.TMtRiskreminder;
import com.ruoyi.project.plant.service.ITMtRiskreminderService;

/**
 * 风险提醒Service业务层处理
 *
 * @author ruoyi
 * @date 2021-01-04
 */
@Service
public class TMtRiskreminderServiceImpl implements ITMtRiskreminderService
{
    @Autowired
    private TMtRiskreminderMapper tMtRiskreminderMapper;

    /**
     * 查询风险提醒
     *
     * @param id 风险提醒ID
     * @return 风险提醒
     */
    @Override
    public TMtRiskreminder selectTMtRiskreminderById(Long id)
    {
        return tMtRiskreminderMapper.selectTMtRiskreminderById(id);
    }

    /**
     * 查询风险提醒列表
     *
     * @param tMtRiskreminder 风险提醒
     * @return 风险提醒
     */
    @Override
    public List<TMtRiskreminder> selectTMtRiskreminderList(TMtRiskreminder tMtRiskreminder)
    {
        return tMtRiskreminderMapper.selectTMtRiskreminderList(tMtRiskreminder);
    }

    /**
     * 新增风险提醒
     *
     * @param tMtRiskreminder 风险提醒
     * @return 结果
     */
    @Override
    public int insertTMtRiskreminder(TMtRiskreminder tMtRiskreminder)
    {
        return tMtRiskreminderMapper.insertTMtRiskreminder(tMtRiskreminder);
    }

    /**
     * 修改风险提醒
     *
     * @param tMtRiskreminder 风险提醒
     * @return 结果
     */
    @Override
    public int updateTMtRiskreminder(TMtRiskreminder tMtRiskreminder)
    {
        return tMtRiskreminderMapper.updateTMtRiskreminder(tMtRiskreminder);
    }

    /**
     * 批量删除风险提醒
     *
     * @param ids 需要删除的风险提醒ID
     * @return 结果
     */
    @Override
    public int deleteTMtRiskreminderByIds(Long[] ids)
    {
        return tMtRiskreminderMapper.deleteTMtRiskreminderByIds(ids);
    }

    /**
     * 删除风险提醒信息
     *
     * @param id 风险提醒ID
     * @return 结果
     */
    @Override
    public int deleteTMtRiskreminderById(Long id)
    {
        return tMtRiskreminderMapper.deleteTMtRiskreminderById(id);
    }

    @Override
    public void dealRiskreminder(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, "20", "No.");
        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", "           Main Contents       ");
        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", "  Risk  ");
        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", "        Assessment        ");
        TMtRiskreminder tMtRiskreminder = new TMtRiskreminder();
        tMtRiskreminder.setMeetingId(meetingId);
        List<TMtRiskreminder> list = tMtRiskreminderMapper.selectTMtRiskreminderList(tMtRiskreminder);

        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, "20", String.valueOf(i + 1));
                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).getMaincontents());
                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).getRisk());
                PoiWordTools.setWordCellSelfStyle(tableOneRowTwo.getCell(3), POIConstants.FONT_NAME, POIConstants.MT_FONT_SIZE, POIConstants.FONT_BOLD, POIConstants.ALIGNMENT, POIConstants.VERTICAL, fontColor, POIConstants.BG_COLOR, "40", list.get(i).getAssessment());
            }
        }
    }
}
