package com.ruoyi.project.plant.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.plant.mapper.TTargetreviewMapper;
import com.ruoyi.project.plant.domain.TTargetreview;
import com.ruoyi.project.plant.service.ITTargetreviewService;

import javax.annotation.Resource;

/**
 * 目标回顾Service业务层处理
 *
 * @author ruoyi
 * @date 2020-11-25
 */
@Service
public class TTargetreviewServiceImpl implements ITTargetreviewService
{
    @Resource
    private TTargetreviewMapper tTargetreviewMapper;

    /**
     * 查询目标回顾
     *
     * @param id 目标回顾ID
     * @return 目标回顾
     */
    @Override
    public TTargetreview selectTTargetreviewById(Long id)
    {
        return tTargetreviewMapper.selectTTargetreviewById(id);
    }

    /**
     * 查询目标回顾列表
     *
     * @param tTargetreview 目标回顾
     * @return 目标回顾
     */
    @Override
    public List<TTargetreview> selectTTargetreviewList(TTargetreview tTargetreview)
    {
        return tTargetreviewMapper.selectTTargetreviewList(tTargetreview);
    }

    /**
     * 新增目标回顾
     *
     * @param tTargetreview 目标回顾
     * @return 结果
     */
    @Override
    public int insertTTargetreview(TTargetreview tTargetreview)
    {
        //单元格背景颜色状态整改
        if (tTargetreview.getTotal() == "" || tTargetreview.getTotal() == null) {
            tTargetreview.setTotalStatus(null);
        }else if (tTargetreview.getTotalStatus() != null) {
        }else {
            tTargetreview.setTotalStatus("2");
        }
        if (tTargetreview.getFirstquarter() == "" || tTargetreview.getFirstquarter() == null) {
            tTargetreview.setFirstquarterStatus(null);
        }else if (tTargetreview.getFirstquarterStatus() != null) {
        }else {
            tTargetreview.setFirstquarterStatus("2");
        }
        if (tTargetreview.getHalfyear() == "" || tTargetreview.getHalfyear() == null) {
            tTargetreview.setHalfyearStatus(null);
        }else if (tTargetreview.getHalfyearStatus() != null) {
        }else {
            tTargetreview.setHalfyearStatus("2");
        }
        if (tTargetreview.getThreequarter() == "" || tTargetreview.getThreequarter() == null) {
            tTargetreview.setThreequarterStatus(null);
        }else if (tTargetreview.getThreequarterStatus() != null) {
        }else {
            tTargetreview.setThreequarterStatus("2");
        }
        if (tTargetreview.getAnnual() == "" || tTargetreview.getAnnual() == null) {
            tTargetreview.setAnnualStatus(null);
        }else if (tTargetreview.getAnnualStatus() != null) {
        }else {
            tTargetreview.setAnnualStatus("2");
        }
        return tTargetreviewMapper.insertTTargetreview(tTargetreview);
    }

    /**
     * 修改目标回顾
     *
     * @param tTargetreview 目标回顾
     * @return 结果
     */
    @Override
    public int updateTTargetreview(TTargetreview tTargetreview)
    {
        //单元格背景颜色状态整改
        if (tTargetreview.getTotal() == "" || tTargetreview.getTotal() == null) {
            tTargetreview.setTotalStatus(null);
        }else if (tTargetreview.getTotalStatus() != null) {
        }else {
            tTargetreview.setTotalStatus("2");
        }
        if (tTargetreview.getFirstquarter() == "" || tTargetreview.getFirstquarter() == null) {
            tTargetreview.setFirstquarterStatus(null);
        }else if (tTargetreview.getFirstquarterStatus() != null) {
        }else {
            tTargetreview.setFirstquarterStatus("2");
        }
        if (tTargetreview.getHalfyear() == "" || tTargetreview.getHalfyear() == null) {
            tTargetreview.setHalfyearStatus(null);
        }else if (tTargetreview.getHalfyearStatus() != null) {
        }else {
            tTargetreview.setHalfyearStatus("2");
        }
        if (tTargetreview.getThreequarter() == "" || tTargetreview.getThreequarter() == null) {
            tTargetreview.setThreequarterStatus(null);
        }else if (tTargetreview.getThreequarterStatus() != null) {
        }else {
            tTargetreview.setThreequarterStatus("2");
        }
        if (tTargetreview.getAnnual() == "" || tTargetreview.getAnnual() == null) {
            tTargetreview.setAnnualStatus(null);
        }else if (tTargetreview.getAnnualStatus() != null) {
        }else {
            tTargetreview.setAnnualStatus("2");
        }
        return tTargetreviewMapper.updateTTargetreview(tTargetreview);
    }

    /**
     * 根据listId删除目标回顾
     *
     * @param listId 需要删除的目标listId
     * @return 结果
     */
    @Override
    public int deleteTTargetreviewByListId(Long listId) {
        return tTargetreviewMapper.deleteTTargetreviewByListId(listId);
    }

    /**
     * 批量删除目标回顾
     *
     * @param ids 需要删除的目标回顾ID
     * @return 结果
     */
    @Override
    public int deleteTTargetreviewByIds(Long[] ids)
    {
        return tTargetreviewMapper.deleteTTargetreviewByIds(ids);
    }

    /**
     * 删除目标回顾信息
     *
     * @param id 目标回顾ID
     * @return 结果
     */
    @Override
    public int deleteTTargetreviewById(Long id)
    {
        return tTargetreviewMapper.deleteTTargetreviewById(id);
    }

    @Override
    public void dealTargetreview(XWPFDocument doc, XWPFParagraph paragraph, XWPFRun run) {
        run.setText("", 0);//设置此文本在运行中的文本value -应在文件中显示的文字文本,pos --在文本数组中的位置（NB：从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<TTargetreview> list = tTargetreviewMapper.selectTTargetreviewList(new TTargetreview());
        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, String.valueOf(list.get(i).getYear()));
                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).getTargets());
                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).getFirstquarter());
                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).getHalfyear());
                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).getThreequarter());
                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).getAnnual());
            }
        }
    }
}
