package com.xjrsoft.module.assessment.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjrsoft.common.exception.MyException;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.module.assessment.dto.AddAssessmentTemplatePlanDto;
import com.xjrsoft.module.assessment.dto.AssessmentTemplatePlanChangeStatusDto;
import com.xjrsoft.module.assessment.dto.AssessmentTemplatePlanPageDto;
import com.xjrsoft.module.assessment.dto.UpdateAssessmentTemplatePlanDto;
import com.xjrsoft.module.assessment.dto.UpdateAssessmentTemplatePlanTimeDto;
import com.xjrsoft.module.assessment.entity.AssessmentTemplatePlan;
import com.xjrsoft.module.assessment.service.IAssessmentTemplatePlanService;
import com.xjrsoft.module.assessment.vo.AssessmentTemplatePlanPageVo;
import com.xjrsoft.module.assessment.vo.AssessmentTemplatePlanVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
* @title: 考核计划
* @Author dzx
* @Date: 2024-04-01
* @Version 1.0
*/
@RestController
@RequestMapping("/assessment" + "/assessmentTemplatePlan")
@Api(value = "/assessment"  + "/assessmentTemplatePlan",tags = "考核计划代码")
@AllArgsConstructor
public class AssessmentTemplatePlanController {


    private final IAssessmentTemplatePlanService planService;

    @GetMapping(value = "/page")
    @ApiOperation(value="考核计划列表(分页)")
    @SaCheckPermission("assessmenttemplateplan:detail")
    public RT<PageOutput<AssessmentTemplatePlanPageVo>> page(@Valid AssessmentTemplatePlanPageDto dto){

        Page<AssessmentTemplatePlanPageVo> page = planService.getPage(new Page<>(dto.getLimit(), dto.getSize()), dto);
        PageOutput<AssessmentTemplatePlanPageVo> pageOutput = ConventPage.getPageOutput(page, AssessmentTemplatePlanPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询考核计划信息")
    @SaCheckPermission("assessmenttemplateplan:detail")
    public RT<AssessmentTemplatePlanVo> info(@RequestParam Long id){
        AssessmentTemplatePlan assessmentTemplatePlan = planService.getById(id);
        if (assessmentTemplatePlan == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(assessmentTemplatePlan, AssessmentTemplatePlanVo.class));
    }

    @GetMapping(value = "/semester-class")
    @ApiOperation(value="根据学期id查询被选择过的班级")
    @SaCheckPermission("assessmenttemplateplan:detail")
    public RT<List<Long>> getSemesterClass(@RequestParam Long id){
        List<Long> semesterClass = planService.getSemesterClass(id);
        return RT.ok(semesterClass);
    }


    @PostMapping
    @ApiOperation(value = "新增考核计划")
    @SaCheckPermission("assessmenttemplateplan:add")
    public RT<Boolean> add(@Valid @RequestBody AddAssessmentTemplatePlanDto dto){
        boolean isSuccess = planService.add(dto);
        return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改考核计划")
    @SaCheckPermission("assessmenttemplateplan:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateAssessmentTemplatePlanDto dto){

        return RT.ok(planService.update(dto));

    }

    @PutMapping(value = "/update-time")
    @ApiOperation(value = "修改考核计划时间")
    @SaCheckPermission("assessmenttemplateplan:edit")
    public RT<Boolean> updateTime(@Valid @RequestBody UpdateAssessmentTemplatePlanTimeDto dto){

        return RT.ok(planService.updateTime(dto));

    }

    @DeleteMapping
    @ApiOperation(value = "删除考核计划")
    @SaCheckPermission("assessmenttemplateplan:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(planService.delete(ids));
    }

    @PostMapping(value = "/change-status")
    @ApiOperation(value = "修改状态")
    @SaCheckPermission("assessmenttemplateplan:change-status")
    public RT<Boolean> changeStatus(@Valid @RequestBody AssessmentTemplatePlanChangeStatusDto dto){
        AssessmentTemplatePlan templatePlan = planService.getById(dto.getId());
        if(templatePlan == null){
            throw new MyException("未能找到场所信息");
        }
        if(dto.getStatus() == 1){
            templatePlan.setStartTime(new Date());
        }else if(dto.getStatus() == 2){
            templatePlan.setEndTime(new Date());
        }
        templatePlan.setStatus(dto.getStatus());
        planService.updateById(templatePlan);
        return RT.ok(true);
    }


}