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

import com.education.business.service.education.GoalInfoService;
import com.education.common.base.BaseController;
import com.education.common.utils.Result;
import com.education.common.utils.ResultCode;
import com.education.model.dto.GoalInfoReqDto;
import com.education.model.dto.GoalInfoSaveDto;
import com.education.model.entity.GoalInfo;
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 org.springframework.beans.BeanUtils;

/**
 * 导向目标管理
 */
@RequestMapping("/system/goalInfo")
@RestController
public class GoalInfoController extends BaseController {

    @Autowired
    private GoalInfoService goalInfoService;

    private GoalInfo TransformGoalInfoSaveDto(GoalInfoSaveDto goalInfoSaveDto) {
        GoalInfo goalInfo = new GoalInfo();
        BeanUtils.copyProperties(goalInfoSaveDto, goalInfo);
        return goalInfo;
    }

    /**
     * 添加导向目标
     * @param goalInfoSaveDto
     * @return
     */
    @PostMapping
    //@RequiresPermissions("system:goalInfo:save")
    public Result add(@RequestBody @Validated(GoalInfoSaveDto.Add.class) GoalInfoSaveDto goalInfoSaveDto) {
        GoalInfo goalInfo = TransformGoalInfoSaveDto(goalInfoSaveDto);
        Integer id = goalInfoService.add(goalInfo);
        return Result.success(id);
    }

    /**
     * 更新导向目标
     * @param goalInfoSaveDto
     * @return
     */
    @PutMapping
    //@RequiresPermissions("system:goalInfo:update")
    public Result update(@RequestBody @Validated(GoalInfoSaveDto.Update.class) GoalInfoSaveDto goalInfoSaveDto) {
        GoalInfo goalInfo = TransformGoalInfoSaveDto(goalInfoSaveDto);
        goalInfoService.saveOrUpdate(goalInfo);
        return Result.success();
    }

    /**
     * 删除导向目标
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    //@RequiresPermissions("system:goalInfo:delete")
    public Result delete(@PathVariable Integer id) {
        //todo check goal info's relation and status
        goalInfoService.removeById(id);
        return Result.success(ResultCode.SUCCESS, "删除成功");
    }

    /**
     * 查询导向目标列表
     * @param pageParam
     * @param goalInfoReqDto
     * @return
     */
    @GetMapping
    //@RequiresPermissions("system:extKnowledgePoint:list")
    public Result selectPage(PageParam pageParam, GoalInfoReqDto goalInfoReqDto) {
        GoalInfo goalInfo = new GoalInfo();
        goalInfo.setSchoolType(goalInfoReqDto.getSchoolType());
        goalInfo.setGradeId(goalInfoReqDto.getGradeId());
        goalInfo.setCourseId(goalInfoReqDto.getCourseId());
        goalInfo.setgOriented(goalInfoReqDto.getgOriented());
        return Result.success(goalInfoService.selectPageList(pageParam, goalInfo));
    }
}