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

import com.ruoyi.common.jpush.JiGuangPushService;
import com.ruoyi.common.utils.StringUtils;
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.TTsFirstplan;
import com.ruoyi.project.training.bccnew.domain.TTsNew;
import com.ruoyi.project.training.bccnew.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 进组培训Controller
 *
 * @author ssy
 * @date 2024-06-17
 */
@RestController
@RequestMapping("/bccnew/firstplan")
public class TTsFirstplanController extends BaseController {
    @Autowired
    private ITTsFirstplanService tTsFirstplanService;
    @Autowired
    private ITTsNewService tTsNewService;
    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ITTsLjplanService tTsLjplanService;

    @Autowired
    private ITTsYsplanService tTsYsplanService;

    @Autowired
    private ITTsFlplanService tTsFlplanService;

    @Autowired
    private ITTsFtplanService tTsFtplanService;

    @Autowired
    private ITTsApproveService tTsApproveService;
    @Autowired
    private JiGuangPushService jiGuangPushService;

    /**
     * 查询进组培训列表
     */
//    @PreAuthorize("@ss.hasPermi('bccnew:firstplan:list')")
    @GetMapping("/list")
    public TableDataInfo list(TTsFirstplan tTsFirstplan) {
        List<TTsFirstplan> list = tTsFirstplanService.selectTTsFirstplanList(tTsFirstplan);
        for (int i = 0; i < list.size(); i++) {
            if (i == 0) {
                list.get(i).setStudy(1);
            }
            if ((list.get(i).getPlanStatus() == 5 || list.get(i).getPlanStatus() == 1) && i <= list.size() - 1) {
                list.get(i).setStudy(1);
                if (i < list.size() - 1) {
                    list.get(i + 1).setStudy(1);
                }
            }
            if (list.get(i).getStudy() == null) {
                list.get(i).setStudy(0);
            }
        }
        for (TTsFirstplan firstplan : list) {
            TTsNew tsNew = tTsNewService.selectTTsNewById(firstplan.getNewId());
            firstplan.setStaffId(tsNew.getStaffId());
        }
        //startPage();
        return getDataTable(list);
    }

    @GetMapping("/listAll")
    public AjaxResult listAll(TTsFirstplan tTsFirstplan) {
        List<TTsFirstplan> list = tTsFirstplanService.selectTTsFirstplanList(tTsFirstplan);
        for (int i = 0; i < list.size(); i++) {
            if (i == 0) {
                list.get(i).setStudy(1);
            }
            if ((list.get(i).getPlanStatus() == 5 || list.get(i).getPlanStatus() == 1) && i <= list.size() - 1) {
                list.get(i).setStudy(1);
                if (i < list.size() - 1) {
                    list.get(i + 1).setStudy(1);
                }
            }
            if (list.get(i).getStudy() == null) {
                list.get(i).setStudy(0);
            }
        }
        for (TTsFirstplan firstplan : list) {
            TTsNew tsNew = tTsNewService.selectTTsNewById(firstplan.getNewId());
            firstplan.setStaffId(tsNew.getStaffId());
        }
        return AjaxResult.success(list);
    }

    @GetMapping("/listAllByTrainer")
    public AjaxResult listAllByTrainer(TTsFirstplan tTsFirstplan) {
        tTsFirstplan.setTrainer(getNickName());
        List<TTsFirstplan> list = tTsFirstplanService.selectTTsFirstplanList(tTsFirstplan);
        for (TTsFirstplan firstplan : list) {
            TTsNew tsNew = tTsNewService.selectTTsNewById(firstplan.getNewId());
            firstplan.setStaffId(tsNew.getStaffId());
        }
        return AjaxResult.success(list);
    }

    /**
     * 导出进组培训列表
     */
//    @PreAuthorize("@ss.hasPermi('bccnew:firstplan:export')")
    @Log(title = "进组培训", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TTsFirstplan tTsFirstplan) {
        List<TTsFirstplan> list = tTsFirstplanService.selectTTsFirstplanList(tTsFirstplan);
        ExcelUtil<TTsFirstplan> util = new ExcelUtil<TTsFirstplan>(TTsFirstplan.class);
        return util.exportExcel(list, "firstplan");
    }

    /**
     * 获取进组培训详细信息
     */
//    @PreAuthorize("@ss.hasPermi('bccnew:firstplan:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tTsFirstplanService.selectTTsFirstplanById(id));
    }

    /**
     * 新增进组培训
     */
//    @PreAuthorize("@ss.hasPermi('bccnew:firstplan:add')")
    @Log(title = "进组培训", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TTsFirstplan tTsFirstplan) {
        return toAjax(tTsFirstplanService.insertTTsFirstplan(tTsFirstplan));
    }


    /**
     * 修改裂解培训
     */
    @Log(title = "进组培训更新学习时长", businessType = BusinessType.UPDATE)
    @PutMapping("/updateFirstLearnTime")
    public AjaxResult updateFirstLearnTime(@RequestBody TTsFirstplan tTsFirstplan) {
        Long userId = getUserId();
        TTsNew tTsNew = tTsNewService.selectTTsNewById(tTsFirstplan.getNewId());
        SysUser sysUser = sysUserService.selectUserByStaffId(tTsNew.getStaffId());
        if (userId.equals(sysUser.getUserId())) {
            TTsFirstplan need = tTsFirstplanService.selectTTsFirstplanById(tTsFirstplan.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);
                    if (need.getExamType().equals("线下考试")){
                        sysUser = new SysUser();
                        sysUser.setNickName(need.getTrainer());
                        for (SysUser trainer : sysUserService.selectUserList(sysUser)) {
                            sysUser = new SysUser();
                            SysUser staff = sysUserService.selectUserByStaffId(tTsNew.getStaffId());
                            jiGuangPushService.send("导师带徒-装置级培训", staff.getNickName() + "的学习已完成，请进行线下考试，并上传成绩。", trainer.getUserId().toString());
                        }
                    }
                }
            }
            tTsFirstplanService.updateTTsFirstplan(need);
            return AjaxResult.success(need.getTimer());
        }
        return AjaxResult.error("当前登录用户非学习用户!");
    }

    /**
     * 修改进组培训
     */
//    @PreAuthorize("@ss.hasPermi('bccnew:firstplan:edit')")
    @Log(title = "进组培训", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TTsFirstplan tTsFirstplan) {
        TTsFirstplan firstplan = tTsFirstplanService.selectTTsFirstplanById(tTsFirstplan.getId());
        if ((StringUtils.isNotEmpty(tTsFirstplan.getScore1()) && StringUtils.isEmpty(firstplan.getScore1())) ||
                (StringUtils.isNotEmpty(tTsFirstplan.getScore2()) && StringUtils.isEmpty(firstplan.getScore2())) ||
                (StringUtils.isNotEmpty(tTsFirstplan.getScore3()) && StringUtils.isEmpty(firstplan.getScore3()))) {
            if (StringUtils.isEmpty(firstplan.getTrainer()) || !firstplan.getTrainer().trim().equals(getNickName())) {
                return AjaxResult.error("仅讲师可评估成绩！");
            }
            tTsFirstplan.setNickName(getNickName());
        }
        tTsFirstplan.setUserId(getUserId());
        tTsFirstplan.setNickName(getNickName());
        if ("1".equals(tTsFirstplan.getTrainerConfirm())) {
            if (StringUtils.isEmpty(firstplan.getTrainer()) || !firstplan.getTrainer().trim().equals(getNickName())) {
                return AjaxResult.error("仅讲师可确认成绩！");
            }
            tTsFirstplan.setTrainerConfirm(getNickName());


            TTsNew tsNew = tTsNewService.selectTTsNewById(firstplan.getNewId());
            SysUser sysUser = new SysUser();
            SysUser staff = sysUserService.selectUserByStaffId(tsNew.getStaffId());
            jiGuangPushService.send("导师带徒-装置级培训", "讲师已确认您"+tTsFirstplan.getTopic()+"的成绩，请您确认成绩后继续下一项培训。", staff.getUserId().toString());

        }
        if ("1".equals(tTsFirstplan.getStudentConfirm())) {
            tTsFirstplan.setStudentConfirm(getNickName());
            tTsFirstplan.setCourseDate(new Date());
            tTsFirstplan.setAssess(StringUtils.isEmpty(firstplan.getScore2()) ? firstplan.getScore1() : firstplan.getScore2());
        }
        return toAjax(tTsFirstplanService.updateTTsFirstplan(tTsFirstplan));
    }

    /**
     * 删除进组培训
     */
//    @PreAuthorize("@ss.hasPermi('bccnew:firstplan:remove')")
    @Log(title = "进组培训", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tTsFirstplanService.deleteTTsFirstplanByIds(ids));
    }
}
