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

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.TStaffmgr;
import com.ruoyi.project.plant.mapper.TStaffmgrMapper;
import com.ruoyi.project.system.domain.SysDictData;
import com.ruoyi.project.system.service.ISysDictTypeService;
import com.ruoyi.project.training.domain.TTraining;
import com.ruoyi.project.training.mapper.TTrainingMapper;
import com.ruoyi.project.training.service.ITTrainingService;
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 javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 培训计划Service业务层处理
 *
 * @author ruoyi
 * @date 2020-12-02
 */
@Service
public class TTrainingServiceImpl implements ITTrainingService
{
    @Resource
    private TTrainingMapper tTrainingMapper;
    @Resource
    private TStaffmgrMapper tStaffmgrMapper;
    @Autowired
    private ISysDictTypeService iSysDictTypeService;
    /**
     * 查询培训计划
     *
     * @param id 培训计划ID
     * @return 培训计划
     */
    @Override
    public TTraining selectTTrainingById(Long id)
    {
        return tTrainingMapper.selectTTrainingById(id);
    }

    /**
     * 查询培训计划
     *
     * @param regularId 装置培训计划ID
     * @return 培训计划
     */
    @Override
    public TTraining selectTTrainingByRegularId(Long regularId) {
        return tTrainingMapper.selectTTrainingByRegularId(regularId);
    }

    /**
     * 查询培训计划列表
     *
     * @param tTraining 培训计划
     * @return 培训计划
     */
    @Override
    public List<TTraining> selectTTrainingList(TTraining tTraining)
    {
        return tTrainingMapper.selectTTrainingList(tTraining);
    }

    @Override
    public List<TTraining> countTTraining(TTraining tTraining)
    {
        return tTrainingMapper.countTTraining(tTraining);
    }

    /**
     * 新增培训计划
     *
     * @param tTraining 培训计划
     * @return 结果
     */
    @Override
    public int insertTTraining(TTraining tTraining)
    {
        return tTrainingMapper.insertTTraining(tTraining);
    }

    /**
     * 修改培训计划
     *
     * @param tTraining 培训计划
     * @return 结果
     */
    @Override
    public int updateTTraining(TTraining tTraining)
    {
        return tTrainingMapper.updateTTraining(tTraining);
    }

    /**
     * 批量删除培训计划
     *
     * @param ids 需要删除的培训计划ID
     * @return 结果
     */
    @Override
    public int deleteTTrainingByIds(Long[] ids)
    {
        return tTrainingMapper.deleteTTrainingByIds(ids);
    }

    /**
     * 删除培训计划信息
     *
     * @param id 培训计划ID
     * @return 结果
     */
    @Override
    public int deleteTTrainingById(Long id)
    {
        return tTrainingMapper.deleteTTrainingById(id);
    }

    @Override
    public void dealTraininglist(XWPFDocument doc, XWPFParagraph paragraph, XWPFRun run, Long trainingId) {
        run.setText("", 0);//设置此文本在运行中的文本value -应在文件中显示的文字文本,pos --在文本数组中的位置（NB：从0开始）
        XmlCursor cursor = paragraph.getCTP().newCursor();
        XWPFTable tableOne = doc.insertNewTbl(cursor);// ---这个是关键

        // 设置表格宽度，第一行宽度就可以了，这个值的单位，目前我也还不清楚，还没来得及研究
        tableOne.setWidth(10000);
        List<SysDictData> dictList = iSysDictTypeService.selectDictDataByType("STAFF_UNIT");

        // 表格第一行，对于每个列，必须使用createCell()，而不是getCell()，因为第一行嘛，肯定是属于创建的，没有create哪里来的get呢
        XWPFTableRow tableOneRowOne = tableOne.getRow(0);// 行
        PoiWordTools.setWordCellSelfStyle(tableOneRowOne.getCell(0), "Arial", "11",  POIConstants.FONT_BOLD, POIConstants.ALIGNMENT, POIConstants.VERTICAL,POIConstants.FONT_COLOR, POIConstants.MT_BG_TITLE_COLOR, "700", "序号");
        PoiWordTools.setWordCellSelfStyle(tableOneRowOne.createCell(), "Arial", "10",  POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.MT_BG_TITLE_COLOR, "1300", " 姓名 ");
        PoiWordTools.setWordCellSelfStyle(tableOneRowOne.createCell(), "Arial", "10",  POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.MT_BG_TITLE_COLOR, "500", "部门");
        PoiWordTools.setWordCellSelfStyle(tableOneRowOne.createCell(), "Arial", "11",  POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.MT_BG_TITLE_COLOR, "1500", "签名");
        PoiWordTools.setWordCellSelfStyle(tableOneRowOne.createCell(), "Arial", "11",  POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.MT_BG_TITLE_COLOR, "1000", "日期");
        PoiWordTools.setWordCellSelfStyle(tableOneRowOne.createCell(), "Arial", "11",  POIConstants.FONT_BOLD, POIConstants.ALIGNMENT, POIConstants.VERTICAL,POIConstants.FONT_COLOR, POIConstants.MT_BG_TITLE_COLOR, "700", "序号");
        PoiWordTools.setWordCellSelfStyle(tableOneRowOne.createCell(), "Arial", "10",  POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.MT_BG_TITLE_COLOR, "1300", "姓名");
        PoiWordTools.setWordCellSelfStyle(tableOneRowOne.createCell(), "Arial", "10",  POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.MT_BG_TITLE_COLOR, "500", "部门");
        PoiWordTools.setWordCellSelfStyle(tableOneRowOne.createCell(), "Arial", "11",  POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.MT_BG_TITLE_COLOR, "1500", "签名");
        PoiWordTools.setWordCellSelfStyle(tableOneRowOne.createCell(), "Arial", "11",  POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.MT_BG_TITLE_COLOR, "1000", "日期");

        TTraining tTraining = tTrainingMapper.selectTTrainingById(trainingId);
        TStaffmgr tStaffmgr = new TStaffmgr();
        tStaffmgr.setActualposts(tTraining.getPosition());
        tStaffmgr.setDeptId(tTraining.getDeptId());
        List<TStaffmgr> staffmgrList =  tStaffmgrMapper.selectAllTStaffmgrList(tStaffmgr);
        try {
            //判断离职
            if (!org.apache.commons.lang.StringUtils.isEmpty(tTraining.getYear())) {
                Iterator<TStaffmgr> iterator = staffmgrList.iterator();
                while (iterator.hasNext()) {
                    TStaffmgr t = iterator.next();
                    if (t.getLeftDate() != null && t.getDelFlag() == 9){
                        if (t.getLeftDate().getTime() - tTraining.getStartDate().getTime() < 0l ) {
                            iterator.remove();
                        }
                    }
                }
            }
        }catch (Exception e) {
        }
        List<TStaffmgr> staffmgrs = new ArrayList<TStaffmgr>();
        if (tTraining.getDesignatedStaff() != null) {
            String[] staffIds = tTraining.getDesignatedStaff().split(",");
            for (int i = 0; i <staffIds.length ; i++) {
                TStaffmgr staffmgr = tStaffmgrMapper.selectTStaffmgrByStaffId(staffIds[i]);
                staffmgrs.add(staffmgr);
            }
            staffmgrList.addAll(staffmgrs);
        }
        List<TStaffmgr> list = new ArrayList();
        list = staffmgrList;
        System.out.println(list.size());
        for (int i = 0; i < list.size(); i++) {
            TStaffmgr t1 = list.get(i);
            for (int j = i+1; j < list.size(); j++) {
                TStaffmgr t2 = list.get(j);
                if (t1.getName().equals(t2.getName())) {
                    t1.setName(t1.getName() +"1");
                    t2.setName(t2.getName() + "2");
                }
            }
        }

        if (CollectionUtils.isNotEmpty(list)) {
            for (int i = 0; i < list.size(); i++) {
                // 表格第二行
                XWPFTableRow tableOneRowTwo = tableOne.createRow();// 行
                PoiWordTools.setWordCellSelfStyle(tableOneRowTwo.getCell(0), "Arial", "11", POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.BG_COLOR, "20", String.valueOf(i + 1));
                PoiWordTools.setWordCellSelfStyle(tableOneRowTwo.getCell(1), "Arial", "10", POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.BG_COLOR, "30", list.get(i).getName());
                PoiWordTools.setWordCellSelfStyle(tableOneRowTwo.getCell(2), "Arial", "10", POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.BG_COLOR, "10", DictUtils.getDictVale(dictList ,list.get(i).getUnit()));
                PoiWordTools.setWordCellSelfStyle(tableOneRowTwo.getCell(3), "Arial", "11", POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.BG_COLOR, "20", "");
                PoiWordTools.setWordCellSelfStyle(tableOneRowTwo.getCell(4), "Arial", "11", POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.BG_COLOR, "20", "");
                if (i+1 < list.size() ){
                    PoiWordTools.setWordCellSelfStyle(tableOneRowTwo.getCell(5), "Arial", "11", POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.BG_COLOR, "20", String.valueOf(i + 2));
                    PoiWordTools.setWordCellSelfStyle(tableOneRowTwo.getCell(6), "Arial", "10", POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.BG_COLOR, "30", list.get(i+1).getName());
                    PoiWordTools.setWordCellSelfStyle(tableOneRowTwo.getCell(7), "Arial", "10", POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.BG_COLOR, "10", DictUtils.getDictVale(dictList ,list.get(i+1).getUnit()));
                    PoiWordTools.setWordCellSelfStyle(tableOneRowTwo.getCell(8), "Arial", "11", POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.BG_COLOR, "20", "");
                    PoiWordTools.setWordCellSelfStyle(tableOneRowTwo.getCell(9), "Arial", "11", POIConstants.FONT_BOLD, POIConstants.ALIGNMENT,POIConstants.VERTICAL, POIConstants.FONT_COLOR, POIConstants.BG_COLOR, "20", "");
                }
                i++;
            }
        }
    }
}
