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

import java.util.List;

import com.ruoyi.common.utils.poi.POIConstants;
import com.ruoyi.common.utils.poi.PoiWordTools;
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.document.mapper.TPublicdocMapper;
import com.ruoyi.project.document.domain.TPublicdoc;
import com.ruoyi.project.document.service.ITPublicdocService;

import javax.annotation.Resource;

/**
 * 公共盘文档Service业务层处理
 *
 * @author ruoyi
 * @date 2020-12-03
 */
@Service
public class TPublicdocServiceImpl implements ITPublicdocService
{
    @Resource
    private TPublicdocMapper tPublicdocMapper;

    /**
     * 查询公共盘文档
     *
     * @param id 公共盘文档ID
     * @return 公共盘文档
     */
    @Override
    public TPublicdoc selectTPublicdocById(Long id)
    {
        return tPublicdocMapper.selectTPublicdocById(id);
    }

    /**
     * 查询公共盘文档列表
     *
     * @param tPublicdoc 公共盘文档
     * @return 公共盘文档
     */
    @Override
    public List<TPublicdoc> selectTPublicdocList(TPublicdoc tPublicdoc)
    {
        return tPublicdocMapper.selectTPublicdocList(tPublicdoc);
    }

    /**
     * 新增公共盘文档
     *
     * @param tPublicdoc 公共盘文档
     * @return 结果
     */
    @Override
    public int insertTPublicdoc(TPublicdoc tPublicdoc)
    {
        return tPublicdocMapper.insertTPublicdoc(tPublicdoc);
    }

    /**
     * 修改公共盘文档
     *
     * @param tPublicdoc 公共盘文档
     * @return 结果
     */
    @Override
    public int updateTPublicdoc(TPublicdoc tPublicdoc)
    {
        return tPublicdocMapper.updateTPublicdoc(tPublicdoc);
    }

    /**
     * 批量删除公共盘文档
     *
     * @param ids 需要删除的公共盘文档ID
     * @return 结果
     */
    @Override
    public int deleteTPublicdocByIds(Long[] ids)
    {
        return tPublicdocMapper.deleteTPublicdocByIds(ids);
    }

    /**
     * 删除公共盘文档信息
     *
     * @param id 公共盘文档ID
     * @return 结果
     */
    @Override
    public int deleteTPublicdocById(Long id)
    {
        return tPublicdocMapper.deleteTPublicdocById(id);
    }

    @Override
    public void dealPublicdoc(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, "具体行动");
        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<TPublicdoc> list = tPublicdocMapper.selectTPublicdocList(new TPublicdoc());
        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).getMenuName());
                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).getResponsibility());
                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).getAction());
                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).getFrequency());
            }
        }
    }
}
