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.TMtKeymaintenanceMapper;
import com.ruoyi.project.plant.domain.TMtKeymaintenance;
import com.ruoyi.project.plant.service.ITMtKeymaintenanceService;

import javax.annotation.Resource;

/**
 * 下周关键维修工作Service业务层处理
 *
 * @author ruoyi
 * @date 2020-12-30
 */
@Service
public class TMtKeymaintenanceServiceImpl implements ITMtKeymaintenanceService
{
    @Resource
    private TMtKeymaintenanceMapper tMtKeymaintenanceMapper;
    @Autowired
    private ISysDictTypeService iSysDictTypeService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询下周关键维修工作
     *
     * @param id 下周关键维修工作ID
     * @return 下周关键维修工作
     */
    @Override
    public TMtKeymaintenance selectTMtKeymaintenanceById(Long id)
    {
        return tMtKeymaintenanceMapper.selectTMtKeymaintenanceById(id);
    }

    /**
     * 查询下周关键维修工作列表
     *
     * @param tMtKeymaintenance 下周关键维修工作
     * @return 下周关键维修工作
     */
    @Override
    public List<TMtKeymaintenance> selectTMtKeymaintenanceList(TMtKeymaintenance tMtKeymaintenance)
    {
        return tMtKeymaintenanceMapper.selectTMtKeymaintenanceList(tMtKeymaintenance);
    }

    /**
     * 新增下周关键维修工作
     *
     * @param tMtKeymaintenance 下周关键维修工作
     * @return 结果
     */
    @Override
    public int insertTMtKeymaintenance(TMtKeymaintenance tMtKeymaintenance)
    {
        return tMtKeymaintenanceMapper.insertTMtKeymaintenance(tMtKeymaintenance);
    }

    /**
     * 修改下周关键维修工作
     *
     * @param tMtKeymaintenance 下周关键维修工作
     * @return 结果
     */
    @Override
    public int updateTMtKeymaintenance(TMtKeymaintenance tMtKeymaintenance)
    {
        return tMtKeymaintenanceMapper.updateTMtKeymaintenance(tMtKeymaintenance);
    }

    /**
     * 批量删除下周关键维修工作
     *
     * @param ids 需要删除的下周关键维修工作ID
     * @return 结果
     */
    @Override
    public int deleteTMtKeymaintenanceByIds(Long[] ids)
    {
        return tMtKeymaintenanceMapper.deleteTMtKeymaintenanceByIds(ids);
    }

    /**
     * 删除下周关键维修工作信息
     *
     * @param id 下周关键维修工作ID
     * @return 结果
     */
    @Override
    public int deleteTMtKeymaintenanceById(Long id)
    {
        return tMtKeymaintenanceMapper.deleteTMtKeymaintenanceById(id);
    }

    @Override
    public void dealKeymaintenancelist(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, "2000", "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, "4000", "Resp.");
        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, "4000", "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, "4000", "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, "4000", "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, "2000", "Remark");
        TMtKeymaintenance tMtKeymaintenance = new TMtKeymaintenance();
        tMtKeymaintenance.setMeetingId(meetingId);
        List<TMtKeymaintenance> list = tMtKeymaintenanceMapper.selectTMtKeymaintenanceList(tMtKeymaintenance);
        List<SysDictData> dictList = iSysDictTypeService.selectDictDataByType("ACTION_STATUS");
        List<SysDictData> dictList2 = iSysDictTypeService.selectDictDataByType("ACTUALPOST");

        for (TMtKeymaintenance 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, "2000", 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, "4000",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, "4000", 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, "4000", 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, "4000", 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, "2000", list.get(i).getRemarks());
            }
        }
    }
}
