package com.ruoyi.project.training.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.project.system.domain.SysDictData;
import com.ruoyi.project.system.service.ISysDictTypeService;
import com.ruoyi.project.training.domain.TTrainingRegular;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.training.domain.TTrainingMatrix;
import com.ruoyi.project.training.service.ITTrainingMatrixService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 培训矩阵Controller
 *
 * @author ruoyi
 * @date 2021-06-28
 */
@RestController
@RequestMapping("/training/matrix")
public class TTrainingMatrixController extends BaseController
{
    @Autowired
    private ITTrainingMatrixService tTrainingMatrixService;

    @Autowired
    private ISysDictTypeService iSysDictTypeService;

    /**
     * 查询培训矩阵列表
     */
    @PreAuthorize("@ss.hasPermi('training:matrix:list')")
    @GetMapping("/list")
    public TableDataInfo list(TTrainingMatrix tTrainingMatrix)
    {
        startPage();
        List<TTrainingMatrix> list = tTrainingMatrixService.selectTTrainingMatrixList(tTrainingMatrix);
        List<SysDictData> actualpost = iSysDictTypeService.selectDictDataByType("ACTUALPOST");
        for (TTrainingMatrix t : list) {
            String[] actualpostId = new String[0];;
            if (t.getActualpostId() != null) {
                actualpostId = t.getActualpostId().split(",");
            }
            List<String> postStatu = new ArrayList<>();
            for (SysDictData p : actualpost) {
                int i = 0;
                for (String a : actualpostId) {
                    if (p.getDictValue().equals(a)) {
                        i = 1;
                    }
                }
                if (t.getDesignatedPosition() != null) {
                    String[] designatedPosition = t.getDesignatedPosition().split(",");
                    for (String d : designatedPosition) {
                        if (p.getDictValue().equals(d)) {
                            i = 2;
                        }
                    }
                }
                if (i == 1) {
                    postStatu.add("M");
                }else if (i == 2) {
                    postStatu.add("|");
                }else {
                    postStatu.add("-");
                }
            }
            if (t.getInvolvedMoc().equals("true")) {
                if (t.getDesignatedOther() != null) {
                    String[] designatedOther = t.getDesignatedOther().split(",");
                    for (String d : designatedOther) {
                        if (d.equals("28")) {
                            postStatu.set(postStatu.size() - 3, "(M)");
                        }
                        if (d.equals("30")) {
                            postStatu.set(postStatu.size() - 2, "(M)");
                        }
                        if (d.equals("32")) {
                            postStatu.set(postStatu.size() - 1, "(M)");
                        }
                    }
                }
            }
            t.setActualpost(postStatu);
        }
        return getDataTable(list);
    }

    /**
     * 导出培训矩阵列表
     */
    @PreAuthorize("@ss.hasPermi('training:matrix:export')")
    @Log(title = "培训矩阵", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TTrainingMatrix tTrainingMatrix)
    {
        List<TTrainingMatrix> list = tTrainingMatrixService.selectTTrainingMatrixList(tTrainingMatrix);
        ExcelUtil<TTrainingMatrix> util = new ExcelUtil<TTrainingMatrix>(TTrainingMatrix.class);
        return util.exportExcel(list, "matrix");
    }

    /**
     * 获取培训矩阵详细信息
     */
    @PreAuthorize("@ss.hasPermi('training:matrix:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tTrainingMatrixService.selectTTrainingMatrixById(id));
    }

    /**
     * 新增培训矩阵
     */
    @PreAuthorize("@ss.hasPermi('training:matrix:add')")
    @Log(title = "培训矩阵", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TTrainingMatrix tTrainingMatrix)
    {
        return toAjax(tTrainingMatrixService.insertTTrainingMatrix(tTrainingMatrix));
    }

    /**
     * 修改培训矩阵
     */
    @PreAuthorize("@ss.hasPermi('training:matrix:edit')")
    @Log(title = "培训矩阵", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TTrainingMatrix tTrainingMatrix)
    {
        return toAjax(tTrainingMatrixService.updateTTrainingMatrix(tTrainingMatrix));
    }

    /**
     * 删除培训矩阵
     */
    @PreAuthorize("@ss.hasPermi('training:matrix:remove')")
    @Log(title = "培训矩阵", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tTrainingMatrixService.deleteTTrainingMatrixByIds(ids));
    }
}
