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

import java.util.List;

import com.alibaba.fastjson.JSON;
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.TTargetreview;
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.TMtActionlistMapper;
import com.ruoyi.project.plant.domain.TMtActionlist;
import com.ruoyi.project.plant.service.ITMtActionlistService;

import javax.annotation.Resource;

/**
 * 装置会议执行Service业务层处理
 *
 * @author ruoyi
 * @date 2020-12-30
 */
@Service
public class TMtActionlistServiceImpl implements ITMtActionlistService
{
    @Resource
    private TMtActionlistMapper tMtActionlistMapper;

    @Autowired
    private ISysDictTypeService iSysDictTypeService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询装置会议执行
     *
     * @param id 装置会议执行ID
     * @return 装置会议执行
     */
    @Override
    public TMtActionlist selectTMtActionlistById(Long id)
    {
        return tMtActionlistMapper.selectTMtActionlistById(id);
    }

    /**
     * 查询装置会议执行列表
     *
     * @param tMtActionlist 装置会议执行
     * @return 装置会议执行
     */
    @Override
    public List<TMtActionlist> selectTMtActionlistList(TMtActionlist tMtActionlist)
    {
        return tMtActionlistMapper.selectTMtActionlistList(tMtActionlist);
    }

    /**
     * 查询装置会议执行列表
     *
     * @param tMtActionlist 装置会议执行
     * @return 装置会议执行集合
     */
    @Override
    public List<TMtActionlist> selectList(TMtActionlist tMtActionlist) {
        return tMtActionlistMapper.selectList(tMtActionlist);
    }

    /**
     * 新增装置会议执行
     *
     * @param tMtActionlist 装置会议执行
     * @return 结果
     */
    @Override
    public int insertTMtActionlist(TMtActionlist tMtActionlist)
    {
        return tMtActionlistMapper.insertTMtActionlist(tMtActionlist);
    }

    /**
     * 修改装置会议执行
     *
     * @param tMtActionlist 装置会议执行
     * @return 结果
     */
    @Override
    public int updateTMtActionlist(TMtActionlist tMtActionlist)
    {
        return tMtActionlistMapper.updateTMtActionlist(tMtActionlist);
    }

    /**
     * 批量删除装置会议执行
     *
     * @param ids 需要删除的装置会议执行ID
     * @return 结果
     */
    @Override
    public int deleteTMtActionlistByIds(Long[] ids)
    {
        return tMtActionlistMapper.deleteTMtActionlistByIds(ids);
    }

    /**
     * 删除装置会议执行信息
     *
     * @param id 装置会议执行ID
     * @return 结果
     */
    @Override
    public int deleteTMtActionlistById(Long id)
    {
        return tMtActionlistMapper.deleteTMtActionlistById(id);
    }

    @Override
    public void dealActionlist(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 备注");
        TMtActionlist tMtActionlist = new TMtActionlist();
        tMtActionlist.setMeetingId(meetingId);
        List<TMtActionlist> list = tMtActionlistMapper.selectTMtActionlistList(tMtActionlist);
        List<SysDictData> dictList = iSysDictTypeService.selectDictDataByType("ACTUALPOST");

        for (TMtActionlist t : list
        ) {
            //指定人
            if ("1".equals(t.getIsPerson())) {
                if (StringUtils.isNotEmpty(t.getResponsible())) {
                    String[] respArr = t.getResponsible().split(",");
                    for (int i = 0; i < respArr.length; i++) {
                        SysUser user = sysUserService.selectUserByStaffId(respArr[i]);
                        if (user != null) {
                            if (i == 0) {
                                t.setRespName(user.getNickName());
                            } else {
                                t.setRespName(t.getRespName() + "," + 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(dictList ,arr[i]));
                        }else {
                            t.setRespName(t.getRespName() + "," + DictUtils.getDictVale(dictList ,arr[i]));
                        }

                    }

                }
            }

            if (t.getUpdaterCode() != null) {
                SysUser updater = sysUserService.selectUserById(Long.parseLong(t.getUpdaterCode()));
                t.setUpdaterCode(updater.getNickName());
            }
        }

        List<SysDictData> dictList2 = iSysDictTypeService.selectDictDataByType("ACTION_STATUS");
        System.out.println(JSON.toJSONString(dictList));

        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", DateUtils.parseDateToStr("yyyy-MM-dd",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(dictList2 ,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());
            }
        }
    }
}
