package com.ruoyi.project.production.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.system.domain.SysDictData;
import com.ruoyi.project.system.service.ISysDictTypeService;
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.production.mapper.TAccidentMapper;
import com.ruoyi.project.production.domain.TAccident;
import com.ruoyi.project.production.service.ITAccidentService;

import javax.annotation.Resource;

/**
 * 隐患排查Service业务层处理
 *
 * @author ruoyi
 * @date 2020-11-24
 */
@Service
public class TAccidentServiceImpl implements ITAccidentService
{
    @Resource
    private TAccidentMapper tAccidentMapper;
    @Autowired
    private ISysDictTypeService iSysDictTypeService;
    /**
     * 查询隐患排查
     *
     * @param id 隐患排查ID
     * @return 隐患排查
     */
    @Override
    public TAccident selectTAccidentById(Long id)
    {
        return tAccidentMapper.selectTAccidentById(id);
    }

    /**
     * 查询隐患排查列表
     *
     * @param tAccident 隐患排查
     * @return 隐患排查
     */
    @Override
    public List<TAccident> selectTAccidentList(TAccident tAccident)
    {
        return tAccidentMapper.selectTAccidentList(tAccident);
    }

    /**
     * 新增隐患排查
     *
     * @param tAccident 隐患排查
     * @return 结果
     */
    @Override
    public int insertTAccident(TAccident tAccident)
    {
        return tAccidentMapper.insertTAccident(tAccident);
    }

    /**
     * 修改隐患排查
     *
     * @param tAccident 隐患排查
     * @return 结果
     */
    @Override
    public int updateTAccident(TAccident tAccident)
    {
        return tAccidentMapper.updateTAccident(tAccident);
    }

    /**
     * 批量删除隐患排查
     *
     * @param ids 需要删除的隐患排查ID
     * @return 结果
     */
    @Override
    public int deleteTAccidentByIds(Long[] ids)
    {
        return tAccidentMapper.deleteTAccidentByIds(ids);
    }

    /**
     * 删除隐患排查信息
     *
     * @param id 隐患排查ID
     * @return 结果
     */
    @Override
    public int deleteTAccidentById(Long id)
    {
        return tAccidentMapper.deleteTAccidentById(id);
    }

    /**
     * 首页查询隐患排查s数量
     *
     * @param month 需要查询的月份
     * @return 结果
     */
    @Override
    public int selectHomeData(String month) {
        return tAccidentMapper.selectHomeData(month);
    }

    @Override
    public void dealAccident(XWPFDocument doc, XWPFParagraph paragraph, XWPFRun run) {
        //字典查询
        List<SysDictData> plant = iSysDictTypeService.selectDictDataByType("PLANT_DIVIDE");
        List<SysDictData> area = iSysDictTypeService.selectDictDataByType("AREA_DIVIDE");
        List<SysDictData> teamtype = iSysDictTypeService.selectDictDataByType("TEAM_TYPE");
        List<SysDictData> team = iSysDictTypeService.selectDictDataByType("TEAM_DIVIDE");
        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, "发现者");
        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<TAccident> list = tAccidentMapper.selectTAccidentList(new TAccident());
        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, DateUtils.parseDateToStr("yyyy-MM-dd",list.get(i).getAreadate()));
                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, DictUtils.getDictVale(team ,list.get(i).getTeam()));
                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).getContent());
                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, DictUtils.getDictVale(area ,list.get(i).getArea()));
                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).getDetector());
                PoiWordTools.setWordCellSelfStyle(tableOneRowTwo.getCell(6), 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).getDealer());
            }
        }
    }
}
