package com.education.api.controller.admin.education;

import com.education.business.service.education.GoalInfoService;
import com.education.business.service.education.HomeworkInfoService;
import com.education.common.base.BaseController;
import com.education.common.utils.Result;
import com.education.common.utils.ResultCode;
import com.education.model.dto.GoalInfoDto;
import com.education.model.dto.HomeworkInfoReqDto;
import com.education.model.dto.TestPaperInfoDto;
import com.education.model.entity.GoalInfo;
import com.education.model.entity.HomeworkInfo;
import com.education.model.request.PageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 作业布置管理
 */
@RequestMapping("/system/homeworkInfo")
@RestController
public class HomeworkInfoController extends BaseController {

    @Autowired
    private HomeworkInfoService homeworkInfoService;

    /**
     * 添加作业布置
     * @param homeworkInfo
     * @return
     */
    @PostMapping
    //@RequiresPermissions("system:homeworkInfo:save")
    public Result add(@RequestBody  HomeworkInfo homeworkInfo) {
        homeworkInfoService.saveOrUpdate(homeworkInfo);
        return Result.success();
    }

    /**
     * 更新作业布置
     * @param homeworkInfo
     * @return
     */
    @PutMapping
    //@RequiresPermissions("system:homeworkInfo:update")
    public Result update(@RequestBody  HomeworkInfo homeworkInfo) {
        HomeworkInfo hi = homeworkInfoService.getById(homeworkInfo.getId());
        if (hi.getPublishFlag() != 0) {  //0为未发布
            return Result.fail(ResultCode.FAIL,"作业已发布不能修改");
        }
        homeworkInfoService.saveOrUpdate(homeworkInfo);
        return Result.success();
    }

    /**
     * 删除作业(含删除导向目标配置)
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    //@RequiresPermissions("system:homeworkInfo:delete")
    public Result delete(@PathVariable Integer id) {
        HomeworkInfo hi = homeworkInfoService.getById(id);
        if (hi.getPublishFlag() != 0) {  //0为未发布
            return Result.fail(ResultCode.FAIL,"作业已发布不能修改");
        }        homeworkInfoService.removeById(id);
        return Result.success(ResultCode.SUCCESS, "删除成功");
    }

    /**
     * 获取作业列表
     * @param pageParam 分布参数
     * @param req 请求参数
     * @return
     */
    @GetMapping
    //@RequiresPermissions("system:homeworkInfo:list")
    public Result selectPage(PageParam pageParam,  HomeworkInfoReqDto req) {
        HomeworkInfo homeworkInfo = new HomeworkInfo();
        homeworkInfo.setSubjectId(req.getSubjectId());
        homeworkInfo.setGradeInfoId(req.getGradeInfoId());
        return Result.success(homeworkInfoService.selectPageList(pageParam, homeworkInfo));
    }

    /**
     * 获取作业详情
     * @param id 作业布置id
     * @return
     */
    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id) {
        return Result.success(homeworkInfoService.getById(id));
    }

    /**
     * 获取作业关联的导向目标列表
     * @param id 作业id
     * @return
     */
    @GetMapping("/{id}/goals")
    public Result<List<GoalInfoDto>> getGoals(@PathVariable Integer id) {
        List<GoalInfoDto> goals = homeworkInfoService.getGoals(id);
        return Result.success(goals);
    }

    /**
     * 布置作业
     * @param id 作业id
     * @return
     */
    @PostMapping("/{id}/assign")
    public Result assign(@PathVariable Integer id) {
        homeworkInfoService.assign(id);
        return Result.success();
    }

}