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

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;

    /**
     * 查询进组培训列表
     */
//    @PreAuthorize("@ss.hasPermi('bccnew:firstplan:list')")
    @GetMapping("/list")
    public TableDataInfo list(TTsFirstplan tTsFirstplan) {
        startPage();
        List<TTsFirstplan> list = tTsFirstplanService.selectTTsFirstplanList(tTsFirstplan);
        return getDataTable(list);
    }

    @GetMapping("/listAll")
    public AjaxResult listAll(TTsFirstplan tTsFirstplan) {
        return AjaxResult.success(tTsFirstplanService.selectTTsFirstplanList(tTsFirstplan));
    }

    /**
     * 导出进组培训列表
     */
//    @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);
                }
            }
            return toAjax(tTsFirstplanService.updateTTsFirstplan(need));
        }
        return AjaxResult.error("当前登录用户非学习用户!");
    }

    /**
     * 修改进组培训
     */
//    @PreAuthorize("@ss.hasPermi('bccnew:firstplan:edit')")
    @Log(title = "进组培训", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TTsFirstplan tTsFirstplan) {
        tTsFirstplan.setUserId(getUserId());
        tTsFirstplan.setNickName(getNickName());
        if ("1".equals(tTsFirstplan.getTrainerConfirm())) {
            tTsFirstplan.setTrainerConfirm(getNickName());
        }
        if ("1".equals(tTsFirstplan.getStudentConfirm())) {
            tTsFirstplan.setStudentConfirm(getNickName());
            tTsFirstplan.setTrainingPlanDate(new Date());
            tTsFirstplan.setCourseDate(new Date());
            TTsFirstplan firstplan = tTsFirstplanService.selectTTsFirstplanById(tTsFirstplan.getId());
            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));
    }
}
