package cn.teammodel.controller.frontend;

import cn.teammodel.common.IdRequest;
import cn.teammodel.common.R;
import cn.teammodel.model.dto.weekDuty.*;
import cn.teammodel.model.entity.weekDuty.WeekDuty;
import cn.teammodel.model.vo.weekDuty.DutyRecordVo;
import cn.teammodel.service.DutyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author winter
 * @create 2024-01-03 10:06
 */
@RestController
@RequestMapping("/duty")
@Api(tags = "值周巡检")
public class DutyController {
    @Resource
    private DutyService dutyService;

    @GetMapping("/getTree")
    @ApiOperation("获取值周评价标准树(不存在则拷贝模板)")
    public R<WeekDuty> getTree() {
        WeekDuty weekDuty = dutyService.getTree();
        return R.success(weekDuty);
    }

    @PostMapping("/insertNode")
    @ApiOperation("插入值周评价标准树节点")
    public R<WeekDuty> insertNode(@RequestBody @Valid InsertDutyNodeDto insertDutyNodeDto) {
        WeekDuty weekDuty = dutyService.insertNode(insertDutyNodeDto);
        return R.success(weekDuty);
    }

    @PostMapping("/deleteNode")
    @ApiOperation("删除值周评价标准树节点")
    public R<WeekDuty> deleteNode(@RequestBody @Valid DeleteDutyNodeDto deleteDutyNodeDto) {
        WeekDuty weekDuty = dutyService.deleteNode(deleteDutyNodeDto);
        return R.success(weekDuty);
    }

    @PostMapping("/updateNode")
    @ApiOperation("更新值周评价标准树节点")
    public R<WeekDuty> updateNode(@RequestBody @Valid UpdateDutyNodeDto updateDutyNodeDto) {
        WeekDuty weekDuty = dutyService.updateNode(updateDutyNodeDto);
        return R.success(weekDuty);
    }

    @PostMapping("/vote")
    @ApiOperation("值周评价投票")
    public R<String> vote(@RequestBody @Valid DutyVoteDto dutyVoteDto) {
        dutyService.vote(dutyVoteDto);
        return R.success("评价成功");
    }

    @PostMapping("/recallVote")
    @ApiOperation("撤回评价投票")
    public R<String> recallVote(@RequestBody @Valid RecallDutyVoteDto recallDutyVoteDto) {
        dutyService.recallVote(recallDutyVoteDto);
        return R.success("撤回评价成功");
    }

    @PostMapping("/insertSpot")
    @ApiOperation("插入值周评价地点")
    public R<List<WeekDuty.DutySpot>> insertSpot(@RequestBody @Valid InsertSpotDto insertSpotDto) {
        List<WeekDuty.DutySpot> spots = dutyService.insertSpot(insertSpotDto);
        return R.success(spots);
    }

    @PostMapping("/deleteSpot")
    @ApiOperation("删除值周评价地点")
    public R<List<WeekDuty.DutySpot>> deleteSpot(@RequestBody @Valid IdRequest idRequest) {
        List<WeekDuty.DutySpot> spots = dutyService.deleteSpot(idRequest.getId());
        return R.success(spots);
    }

    @PostMapping("/findRecords")
    @ApiOperation(value = "多条件查询评价明细", notes = "只带 periodId 默认查询当前登录老师在本周的评价明细")
    public R<List<DutyRecordVo>> findRecords(@RequestBody @Valid FindDutyRecordDto findDutyRecordDto) {
        List<DutyRecordVo> items = dutyService.findRecords(findDutyRecordDto);
        return R.success(items);
    }



}
