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.domain.SysUser;
import com.ruoyi.project.system.service.ISysDictTypeService;
import com.ruoyi.project.system.service.ISysUserService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
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.TMtMessageattentionMapper;
import com.ruoyi.project.plant.domain.TMtMessageattention;
import com.ruoyi.project.plant.service.ITMtMessageattentionService;

import javax.annotation.Resource;

/**
 * 信息提醒Service业务层处理
 *
 * @author ruoyi
 * @date 2021-01-04
 */
@Service
public class TMtMessageattentionServiceImpl implements ITMtMessageattentionService
{
    @Resource
    private TMtMessageattentionMapper tMtMessageattentionMapper;
    @Autowired
    private ISysDictTypeService iSysDictTypeService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询信息提醒
     *
     * @param id 信息提醒ID
     * @return 信息提醒
     */
    @Override
    public TMtMessageattention selectTMtMessageattentionById(Long id)
    {
        return tMtMessageattentionMapper.selectTMtMessageattentionById(id);
    }

    /**
     * 查询信息提醒列表
     *
     * @param tMtMessageattention 信息提醒
     * @return 信息提醒
     */
    @Override
    public List<TMtMessageattention> selectTMtMessageattentionList(TMtMessageattention tMtMessageattention)
    {
        return tMtMessageattentionMapper.selectTMtMessageattentionList(tMtMessageattention);
    }

    /**
     * 新增信息提醒
     *
     * @param tMtMessageattention 信息提醒
     * @return 结果
     */
    @Override
    public int insertTMtMessageattention(TMtMessageattention tMtMessageattention)
    {
        return tMtMessageattentionMapper.insertTMtMessageattention(tMtMessageattention);
    }

    /**
     * 修改信息提醒
     *
     * @param tMtMessageattention 信息提醒
     * @return 结果
     */
    @Override
    public int updateTMtMessageattention(TMtMessageattention tMtMessageattention)
    {
        return tMtMessageattentionMapper.updateTMtMessageattention(tMtMessageattention);
    }

    /**
     * 批量删除信息提醒
     *
     * @param ids 需要删除的信息提醒ID
     * @return 结果
     */
    @Override
    public int deleteTMtMessageattentionByIds(Long[] ids)
    {
        return tMtMessageattentionMapper.deleteTMtMessageattentionByIds(ids);
    }

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

    @Override
    public void dealMessageattentionlist(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", "序号");
        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", "Resp.\n" +"责任人");
        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", "Action 行动");
        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", "Deadline 截止日期");
        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", "Status 状态");
        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, "20", "Remark 备注");
        TMtMessageattention tMtActionlist = new TMtMessageattention();
        tMtActionlist.setMeetingId(meetingId);
        List<TMtMessageattention> list = tMtMessageattentionMapper.selectTMtMessageattentionList(tMtActionlist);
        List<SysDictData> dictList = iSysDictTypeService.selectDictDataByType("ACTION_STATUS");
        List<SysDictData> dictList2 = iSysDictTypeService.selectDictDataByType("ACTUALPOST");

        for (TMtMessageattention t : list
        ) {
            //指定人
            if ("1".equals(t.getIsPerson())) {
                if (StringUtils.isNotEmpty(t.getResponsible())) {
                    t.setRespName(t.getResponsible());
                    SysUser user = sysUserService.selectUserByStaffId(t.getResponsible());
                    if (user != null) {
                        t.setRespName(user.getNickName());
                    }
                }
            }
            //指定岗位
            else if ("2".equals(t.getIsPerson())) {
                if (StringUtils.isNotEmpty(t.getPosition())) {
                    String[] arr = t.getPosition().split(",");
                    for (int i = 0; i < arr.length; i++) {
                        if (i == 0) {
                            t.setRespName(DictUtils.getDictVale(dictList2, arr[i]));
                        } else {
                            t.setRespName(t.getRespName() + "," + DictUtils.getDictVale(dictList2, arr[i]));
                        }
                    }
                }
            }
        }
        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).getRespName());
                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).getAction());
                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).getDeadline());
                PoiWordTools.setWordCellSelfStyle(tableOneRowTwo.getCell(4), POIConstants.FONT_NAME, POIConstants.MT_FONT_SIZE, POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, fontColor, POIConstants.BG_COLOR, "40", DictUtils.getDictVale(dictList ,list.get(i).getStatus()));
                PoiWordTools.setWordCellSelfStyle(tableOneRowTwo.getCell(5), POIConstants.FONT_NAME, POIConstants.MT_FONT_SIZE, POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, fontColor, POIConstants.BG_COLOR, "20", list.get(i).getRemarks());
            }
        }
    }
}
