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

import com.ruoyi.common.utils.poi.POIConstants;
import com.ruoyi.common.utils.poi.PoiWordTools;
import com.ruoyi.project.document.domain.TPlantproglist;
import com.ruoyi.project.document.mapper.TPlantproglistMapper;
import com.ruoyi.project.document.service.ITPlantproglistService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.xmlbeans.XmlCursor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 装置程序清单Service业务层处理
 *
 * @author ruoyi
 * @date 2020-11-26
 */
@Service
public class TPlantproglistServiceImpl implements ITPlantproglistService
{
    @Resource
    private TPlantproglistMapper tPlantproglistMapper;

    /**
     * 查询装置程序清单
     *
     * @param id 装置程序清单ID
     * @return 装置程序清单
     */
    @Override
    public TPlantproglist selectTPlantproglistById(Long id)
    {
        return tPlantproglistMapper.selectTPlantproglistById(id);
    }

    /**
     * 查询装置程序清单列表
     *
     * @param tPlantproglist 装置程序清单
     * @return 装置程序清单
     */
    @Override
    public List<TPlantproglist> selectTPlantproglistList(TPlantproglist tPlantproglist)
    {
        return tPlantproglistMapper.selectTPlantproglistList(tPlantproglist);
    }

    @Override
    public List<TPlantproglist> countNextReviewDate(TPlantproglist tPlantproglist)
    {
        return tPlantproglistMapper.countNextReviewDate(tPlantproglist);
    }

    @Override
    public List<TPlantproglist> countNextEditDate(TPlantproglist tPlantproglist)
    {
        return tPlantproglistMapper.countNextEditDate(tPlantproglist);
    }

    /**
     * 定时任务查询装置程序清单列表
     *
     * @param tPlantproglist 装置程序清单
     * @return 装置程序清单集合
     */
    @Override
    public List<TPlantproglist> selectList(TPlantproglist tPlantproglist) {
        return tPlantproglistMapper.selectList(tPlantproglist);
    }

    /**
     * 新增装置程序清单
     *
     * @param tPlantproglist 装置程序清单
     * @return 结果
     */
    @Override
    public int insertTPlantproglist(TPlantproglist tPlantproglist)
    {
        return tPlantproglistMapper.insertTPlantproglist(tPlantproglist);
    }

    /**
     * 修改装置程序清单
     *
     * @param tPlantproglist 装置程序清单
     * @return 结果
     */
    @Override
    public int updateTPlantproglist(TPlantproglist tPlantproglist)
    {
        return tPlantproglistMapper.updateTPlantproglist(tPlantproglist);
    }

    /**
     * 批量删除装置程序清单
     *
     * @param ids 需要删除的装置程序清单ID
     * @return 结果
     */
    @Override
    public int deleteTPlantproglistByIds(Long[] ids)
    {
        return tPlantproglistMapper.deleteTPlantproglistByIds(ids);
    }

    /**
     * 删除装置程序清单信息
     *
     * @param id 装置程序清单ID
     * @return 结果
     */
    @Override
    public int deleteTPlantproglistById(Long id)
    {
        return tPlantproglistMapper.deleteTPlantproglistById(id);
    }

    @Override
    public void dealPlantproglist(XWPFDocument doc, XWPFParagraph paragraph, XWPFRun run) {
        run.setText("", 0);
        XmlCursor cursor = paragraph.getCTP().newCursor();
        XWPFTable tableOne = doc.insertNewTbl(cursor);// ---这个是关键
        // 设置表格宽度，第一行宽度就可以了，这个值的单位，目前我也还不清楚，还没来得及研究
        tableOne.setWidth(8500);
        // 表格第一行，对于每个列，必须使用createCell()，而不是getCell()，因为第一行嘛，肯定是属于创建的，没有create哪里来的get呢
        XWPFTableRow tableOneRowOne = tableOne.getRow(0);// 行
        PoiWordTools.setWordCellSelfStyle(tableOneRowOne.getCell(0), POIConstants.FONT_NAME, POIConstants.FONT_SIAZE, POIConstants.FONT_BOLD, POIConstants.ALIGNMENT, POIConstants.VERTICAL,POIConstants.FONT_COLOR, POIConstants.BG_TITLE_COLOR, POIConstants.CELL_WIDTH, "序号");
        PoiWordTools.setWordCellSelfStyle(tableOneRowOne.createCell(), POIConstants.FONT_NAME, POIConstants.FONT_SIAZE, POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.BG_TITLE_COLOR, POIConstants.CELL_WIDTH, "文件编号");
        PoiWordTools.setWordCellSelfStyle(tableOneRowOne.createCell(), POIConstants.FONT_NAME, POIConstants.FONT_SIAZE, POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.BG_TITLE_COLOR, POIConstants.CELL_WIDTH, "文件名称");
        PoiWordTools.setWordCellSelfStyle(tableOneRowOne.createCell(), POIConstants.FONT_NAME, POIConstants.FONT_SIAZE, POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.BG_TITLE_COLOR, POIConstants.CELL_WIDTH, "版次");
        PoiWordTools.setWordCellSelfStyle(tableOneRowOne.createCell(), POIConstants.FONT_NAME, POIConstants.FONT_SIAZE, POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.BG_TITLE_COLOR, POIConstants.CELL_WIDTH, "RC代码");
        PoiWordTools.setWordCellSelfStyle(tableOneRowOne.createCell(), POIConstants.FONT_NAME, POIConstants.FONT_SIAZE, POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.BG_TITLE_COLOR, POIConstants.CELL_WIDTH, "载体");
        List<TPlantproglist> list = tPlantproglistMapper.selectTPlantproglistList(new TPlantproglist());
        if (CollectionUtils.isNotEmpty(list)) {
            for (int i = 0; i < list.size(); i++) {
                // 表格第二行
                XWPFTableRow tableOneRowTwo = tableOne.createRow();// 行
                PoiWordTools.setWordCellSelfStyle(tableOneRowTwo.getCell(0), POIConstants.FONT_NAME, POIConstants.FONT_SIAZE, POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.BG_COLOR, POIConstants.CELL_WIDTH, String.valueOf(i + 1));
                PoiWordTools.setWordCellSelfStyle(tableOneRowTwo.getCell(1), POIConstants.FONT_NAME, POIConstants.FONT_SIAZE, POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.BG_COLOR, POIConstants.CELL_WIDTH, list.get(i).getFileno());
                PoiWordTools.setWordCellSelfStyle(tableOneRowTwo.getCell(2), POIConstants.FONT_NAME, POIConstants.FONT_SIAZE, POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.BG_COLOR, POIConstants.CELL_WIDTH, list.get(i).getFilename());
                PoiWordTools.setWordCellSelfStyle(tableOneRowTwo.getCell(3), POIConstants.FONT_NAME, POIConstants.FONT_SIAZE, POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.BG_COLOR, POIConstants.CELL_WIDTH, list.get(i).getRevision());
                PoiWordTools.setWordCellSelfStyle(tableOneRowTwo.getCell(4), POIConstants.FONT_NAME, POIConstants.FONT_SIAZE, POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.BG_COLOR, POIConstants.CELL_WIDTH, list.get(i).getRccode());
                PoiWordTools.setWordCellSelfStyle(tableOneRowTwo.getCell(5), POIConstants.FONT_NAME, POIConstants.FONT_SIAZE, POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.BG_COLOR, POIConstants.CELL_WIDTH, list.get(i).getCarrier());
            }
        }
    }
}
