package com.ruoyi.project.training.bccnew.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysUserService;
import com.ruoyi.project.training.bccnew.domain.TTsFlplan;
import com.ruoyi.project.training.bccnew.domain.TTsFlplanContent;
import com.ruoyi.project.training.bccnew.domain.TTsNew;
import com.ruoyi.project.training.bccnew.service.*;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 分离培训Controller
 *
 * @author ssy
 * @date 2024-06-13
 */
@RestController
@RequestMapping("/bccnew/flplandsdt")
public class TTsFlplanController extends BaseController {
    @Autowired
    private ITTsFlplanService tTsFlplanService;
    @Autowired
    private ITTsFlplanContentService tTsFlplanContentService;
    @Autowired
    private ITTsNewService tTsNewService;
    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ITTsFirstplanService tTsFirstplanService;

    @Autowired
    private ITTsApproveService tTsApproveService;

    /**
     * 查询分离培训列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TTsFlplan tTsFlplan) {
        List<TTsFlplan> list = new ArrayList<>();
        List<TTsFlplan> tTsFlplans = tTsFlplanService.selectTTsFlplanList(tTsFlplan);
        StringBuilder content = null;
        StringBuilder planDate = null;
        TTsFlplan entity = null;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 0; i < tTsFlplans.size(); i++) {
            TTsFlplan item = tTsFlplans.get(i);
            if (i == 0) {
                entity = item;
                content = new StringBuilder();
                planDate = new StringBuilder();
                if (item.getPlanDate() != null)
                    planDate.append(format.format(item.getPlanDate()));
                content.append(item.getCourseDay()).append("：").append(item.getContent()).append("；");
            } else {
                if (entity.getTopic().equals(item.getTopic())) {
                    content.append(item.getCourseDay()).append("：").append(item.getContent()).append("；");
                } else {
                    if (tTsFlplans.get(i - 1).getPlanDate() != null)
                        planDate.append(" ~ ").append(format.format(tTsFlplans.get(i - 1).getPlanDate()));
                    entity.setContent(content.toString());
                    entity.setPlanStartData(planDate.toString());
                    list.add(entity);
                    entity = item;
                    content = new StringBuilder();
                    planDate = new StringBuilder();
                    if (item.getPlanDate() != null)
                        planDate.append(format.format(item.getPlanDate()));
                    content.append(item.getCourseDay()).append("：").append(item.getContent()).append("；");
                }
            }
            if (i + 1 == tTsFlplans.size()) {
                if (item.getPlanDate() != null)
                    planDate.append(" ~ ").append(format.format(item.getPlanDate()));
                entity.setContent(content.toString());
                entity.setPlanStartData(planDate.toString());
                list.add(entity);
            }
        }

        startPage();
        return getDataTable(list);
    }


    /**
     * 查询分离培训列表
     */
    @GetMapping("/listAll")
    public AjaxResult listAll(TTsFlplan tTsFlplan) {
        return AjaxResult.success(tTsFlplanService.selectTTsFlplanList(tTsFlplan));
    }

    /**
     * 导出分离培训列表
     */
    @Log(title = "分离培训", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TTsFlplan tTsFlplan) {
        List<TTsFlplan> list = tTsFlplanService.selectTTsFlplanList(tTsFlplan);
        ExcelUtil<TTsFlplan> util = new ExcelUtil<TTsFlplan>(TTsFlplan.class);
        return util.exportExcel(list, "flplandsdt");
    }

    /**
     * 获取分离培训详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tTsFlplanService.selectTTsFlplanById(id));
    }

    /**
     * 新增分离培训
     */
    @Log(title = "分离培训", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TTsFlplan tTsFlplan) {
        int i = tTsFlplanService.insertTTsFlplan(tTsFlplan);
        if (CollectionUtils.isNotEmpty(tTsFlplan.getContents())) {
            for (TTsFlplanContent content : tTsFlplan.getContents()) {
                content.setPid(tTsFlplan.getId());
                tTsFlplanContentService.insertTTsFlplanContent(content);
            }
        }
        return toAjax(i);
    }


    @Log(title = "分离培训", businessType = BusinessType.UPDATE)
    @PutMapping("/updateFlLearnTime")
    public AjaxResult updateFlLearnTime(@RequestBody TTsFlplan tTsFlplan) {
        Long userId = getUserId();
        TTsNew tTsNew = tTsNewService.selectTTsNewById(tTsFlplan.getNewId());
        SysUser sysUser = sysUserService.selectUserByStaffId(tTsNew.getStaffId());
        if (userId.equals(sysUser.getUserId())) {
            TTsFlplan need = tTsFlplanService.selectTTsFlplanById(tTsFlplan.getId());
            long timer = need.getTimer() == null ? 0 : need.getTimer();
            if (timer < need.getTimerNeed()) {
                need.setTimer(timer + 1);
                if (need.getTimerNeed() == null)
                    need.setTimerNeed(0.0);
                if (need.getTimerNeed() != null && need.getTimerNeed() <= timer + 1 && need.getPlanStatus() == 0) {
                    need.setPlanStatus(2L);
                }
            }
            tTsFlplanService.updateTTsFlplan(need);
            return AjaxResult.success(need.getTimer());
        }
        return AjaxResult.error("当前登录用户非学习用户!");
    }

    /**
     * 修改分离培训
     */
    @Log(title = "分离培训", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TTsFlplan tTsFlplan) {
        if (CollectionUtils.isNotEmpty(tTsFlplan.getContents())) {
            for (TTsFlplanContent content : tTsFlplan.getContents()) {
                if (content.getId() == null) {
                    content.setPid(tTsFlplan.getId());
                    tTsFlplanContentService.insertTTsFlplanContent(content);
                } else {
                    tTsFlplanContentService.updateTTsFlplanContent(content);
                }
            }
        }
        TTsFlplan flplan = tTsFlplanService.selectTTsFlplanById(tTsFlplan.getId());
        if ("好".equals(tTsFlplan.getScore1())) {
            tTsFlplan.setPlanStatus(5L);
        }
        if ((("满意".equals(tTsFlplan.getScore1()) || "差".equals(tTsFlplan.getScore1())) && tTsFlplan.getScore2() == null) ||
                (("满意".equals(tTsFlplan.getScore2()) || "差".equals(tTsFlplan.getScore2())) && tTsFlplan.getScore3() == null)) {
            tTsFlplan.setScorePass("0");
            tTsApproveService.doApprove(flplan.getId(), "3", getUserId(), getNickName(), "dsdtNoPass");
        }
        if (("满意".equals(tTsFlplan.getScore1()) || "满意".equals(flplan.getScore1())) && "好".equals(tTsFlplan.getScore2())) {
            tTsFlplan.setPlanStatus(5L);
        }
        if (("满意".equals(tTsFlplan.getScore1()) || "满意".equals(flplan.getScore1())) && ("满意".equals(tTsFlplan.getScore2()) || "满意".equals(flplan.getScore2())) && "好".equals(tTsFlplan.getScore3())) {
            tTsFlplan.setPlanStatus(5L);
        }
        if (("满意".equals(tTsFlplan.getScore1()) || "满意".equals(flplan.getScore1())) && ("差".equals(tTsFlplan.getScore2()) || "差".equals(flplan.getScore2())) && "好".equals(tTsFlplan.getScore3())) {
            tTsFlplan.setPlanStatus(5L);
        }
        if (("满意".equals(tTsFlplan.getScore1()) || "满意".equals(flplan.getScore1())) && ("满意".equals(tTsFlplan.getScore2()) || "满意".equals(flplan.getScore2())) && "差".equals(tTsFlplan.getScore3())) {
            tTsFlplan.setPlanStatus(4L);
        }
        if (("满意".equals(tTsFlplan.getScore1()) || "满意".equals(flplan.getScore1())) && ("差".equals(tTsFlplan.getScore2()) || "差".equals(flplan.getScore2())) && "满意".equals(tTsFlplan.getScore3())) {
            tTsFlplan.setPlanStatus(4L);
        }
        if (("满意".equals(tTsFlplan.getScore1()) || "满意".equals(flplan.getScore1())) && ("满意".equals(tTsFlplan.getScore2()) || "满意".equals(flplan.getScore2())) && "满意".equals(tTsFlplan.getScore3())) {
            tTsFlplan.setPlanStatus(4L);
        }
        if (("满意".equals(tTsFlplan.getScore1()) || "满意".equals(flplan.getScore1())) && ("差".equals(tTsFlplan.getScore2()) || "差".equals(flplan.getScore2())) && "差".equals(tTsFlplan.getScore3())) {
            tTsFlplan.setPlanStatus(4L);
        }
        if (("差".equals(tTsFlplan.getScore1()) || "差".equals(flplan.getScore1())) && "好".equals(tTsFlplan.getScore2())) {
            tTsFlplan.setPlanStatus(5L);
        }
        if (("差".equals(tTsFlplan.getScore1()) || "差".equals(flplan.getScore1())) && "满意".equals(tTsFlplan.getScore2())) {
            tTsFlplan.setPlanStatus(4L);
        }
        if (("差".equals(tTsFlplan.getScore1()) || "差".equals(flplan.getScore1())) && "差".equals(tTsFlplan.getScore2())) {
            tTsFlplan.setPlanStatus(4L);
        }
        tTsFlplanService.updateTTsFlplan(tTsFlplan);
        if (tTsFlplan.getPlanStatus() != null) {
            if (tTsFlplan.getPlanStatus() == 5) {
                TTsNew tTsNew = new TTsNew();
                tTsNew.setPlanStatus(5L);
                tTsNew.setPlanType(3L);
                tTsNew.setNewId(tTsFlplan.getNewId());
                tTsNewService.updateTTsNew(tTsNew, getUserId(), getNickName());
            } else if (tTsFlplan.getPlanStatus() == 4) {
                TTsNew tTsNew = new TTsNew();
                tTsNew.setPlanStatus(4L);
                tTsNew.setPlanType(3L);
                tTsNew.setNewId(tTsFlplan.getNewId());
                tTsNewService.updateTTsNew(tTsNew, getUserId(), getNickName());
            }
        }
        return AjaxResult.success();
    }

    /**
     * 删除分离培训
     */
    @Log(title = "分离培训", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tTsFlplanService.deleteTTsFlplanByIds(ids));
    }
}
