package com.cpop.oam.business.controller.backstage;

import com.cpop.core.base.R;
import com.cpop.oam.business.bo.*;
import com.cpop.oam.business.service.StaffService;
import com.cpop.oam.business.service.TaskService;
import com.cpop.oam.business.service.TaskStaffGroupService;
import com.cpop.oam.business.service.TaskWorkOrderService;
import com.cpop.oam.business.vo.*;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * OAM-任务表 控制层。
 *
 * @author DB
 * @since 2023-09-15
 */
@RestController
@Api(tags = "技术模块-技术")
@RequestMapping("/backstage/taskTechnology")
public class BackstageTaskTechnologyController {

    @Autowired
    private TaskWorkOrderService taskWorkOrderService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private TaskStaffGroupService taskStaffGroupService;

    @Autowired
    private StaffService staffService;

    /**
     * 技术模块-今日事务-获取个人工单列表
     *
     * @author DB
     * @since 2023-11-30 19:41:32
     */
    @PreAuthorize("@aps.hasPermission('oamTask:technology:list')")
    @ApiOperation("技术模块-今日事务-获取个人工单列表")
    @GetMapping("/getPersonWorkOrder")
    public R<List<TaskWorkOrderPersonVo>> getPersonWorkOrder() {
        List<TaskWorkOrderPersonVo> list = taskWorkOrderService.getPersonWorkOrder();
        return R.ok(list);
    }

    /**
     * 工单模块-今日事务-工单记录列表
     *
     * @param workOrderId 工单id
     * @author DB
     * @since 2023-11-29 11:25:28
     */
    @ApiOperation("技术模块-今日事务-工单记录列表")
    @GetMapping("/getWorkOrderRecordList/{workOrderId}")
    public R<List<TaskWorkOrderRecordListVo>> getWorkOrderRecordList(@PathVariable @ApiParam("工单id") String workOrderId) {
        List<TaskWorkOrderRecordListVo> list = taskWorkOrderService.getWorkOrderRecordList(workOrderId);
        return R.ok(list);
    }

    /**
     * 工单模块-今日事务-新增工单记录
     *
     * @param bo 请求参数
     * @author DB
     * @since 2023-11-29 11:33:35
     */
    @ApiOperation("技术模块-今日事务-新增工单记录")
    @PostMapping("/insertWorkOrderRecord")
    public R<Void> insertWorkOrderRecord(@RequestBody @Validated TaskWorkOrderRecordBo bo) {
        //是否是第一个
        taskWorkOrderService.whetherReceive(bo.getTaskWorkOrderId());
        taskWorkOrderService.insertWorkOrderRecord(bo);
        return R.ok();
    }

    /**
     * 技术模块-任务归档-获取任务归档分页
     *
     * @author DB
     * @since 2023-11-30 21:11:47
     */
    @PreAuthorize("@aps.hasPermission('oamTask:technology:list')")
    @ApiOperation("技术模块-任务归档-获取任务归档分页")
    @GetMapping("/getTaskArchivingPage")
    public R<Page<TaskArchivingPagVo>> getTaskArchivingPage(TaskArchivingPagBo bo) {
        Page<TaskArchivingPagVo> page = taskService.getTaskArchivingPage(bo);
        return R.ok(page);
    }

    /**
     * 技术模块-任务归档-设置任务归档
     *
     * @param taskId 任务id
     * @author DB
     * @since 2023-11-30 21:49:05
     */
    @PreAuthorize("@aps.hasPermission('oamTask:technology:deploy')")
    @ApiOperation("技术模块-任务归档-设置任务归档")
    @PutMapping("/setTaskArchiving/{taskId}")
    public R<Void> setTaskArchiving(@PathVariable @ApiParam("任务id") String taskId) {
        taskService.setTaskArchiving(taskId);
        return R.ok();
    }

    /**
     * 技术模块-任务领取-获取领取任务分页
     *
     * @param subject       主体
     * @param taskContent   任务内容
     * @param taskGetStatus 任务领取状态(0:待领取;1:待评估;10:审核不通过)
     * @author DB
     * @since 2023-11-30 22:21:58
     */
    @PreAuthorize("@aps.hasPermission('oamTask:technology:list')")
    @ApiOperation("技术模块-任务领取-获取领取任务分页")
    @GetMapping("/getToBeClaimedPage")
    public R<Page<TaskToBeClaimedPageVo>> getToBeClaimedPage(@ApiParam(value = "主体")
                                                             String subject, @ApiParam(value = "任务内容")
                                                             String taskContent, @RequestParam("taskGetStatus") @ApiParam(value = "任务领取状态(0:待领取;1:待评估;2:审核不通过)", required = true)
                                                             Integer taskGetStatus) {
        Page<TaskToBeClaimedPageVo> list = taskService.getToBeClaimedPage(subject, taskContent, taskGetStatus);
        return R.ok(list);
    }

    /**
     * 技术模块-任务领取-新增迭代任务
     *
     * @param bo 请求
     * @author DB
     * @since 2023-12-01 09:45:08
     */
    //@PreAuthorize("@aps.hasPermission('oamTask:iterateTask:insert')")
    @ApiOperation("技术模块-任务领取-新增迭代任务")
    @PostMapping("/insertIterationTask")
    public R<Void> insertIterationTask(@RequestBody @Validated
                                       TaskIterationBo bo) {
        taskService.insertIterationTask(bo);
        return R.ok();
    }

    /**
     * 技术模块-任务领取-技术人员领取任务
     *
     * @param id 任务id
     * @author DB
     * @since 2023-12-01 10:15:28
     */
    //@PreAuthorize("@aps.hasPermission('oamTask:iterateTask:update')")
    @ApiOperation("技术模块-任务领取-技术人员领取任务")
    @PutMapping("/claimTask/{id}")
    public R<Void> claimTask(@PathVariable @ApiParam("任务id") String id) {
        taskService.claimTask(id);
        return R.ok();
    }

    /**
     * 技术模块-任务领取-任务审核
     *
     * @param bo 请求参数
     * @author DB
     * @since 2023-12-01 10:35:31
     */
    @PreAuthorize("@aps.hasPermission('oamTask:technology:audit')")
    @ApiOperation("技术模块-任务领取-任务审核")
    @PutMapping("/setTaskAuditComments")
    public R<Void> setTaskAuditComments(@RequestBody @Validated TaskAuditCommentsBo bo) {
        taskService.setTaskAuditComments(bo);
        return R.ok();
    }

    /**
     * 技术模块-今日事务-个人任务分页列表
     *
     * @return R<Page < PersonTaskPageVo>>
     * @author DB
     * @since 2023/12/04
     */
    @PreAuthorize("@aps.hasPermission('oamTask:technology:list')")
    @ApiOperation("技术模块-今日事务-个人任务分页列表")
    @GetMapping("/getPersonTaskPage")
    public R<Page<PersonTaskPageVo>> getPersonTaskPage(@RequestParam(value = "staffId", required = false) @ApiParam("员工id") String staffId,
                                                       @RequestParam(value = "startDate", required = false) @ApiParam("开始日期") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
                                                       @RequestParam(value = "endDate", required = false) @ApiParam("结束日期") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
        Page<PersonTaskPageVo> list = taskService.getPersonTaskPage(staffId, startDate, endDate);
        return R.ok(list);
    }

    /**
     * 技术模块-今日事务-任务成员组列表
     *
     * @param taskId 任务id
     * @return R<List < TaskStaffGroupVo>>
     * @author DB
     * @since 2023/12/04
     */
    @ApiOperation("技术模块-今日事务-任务成员组列表")
    @GetMapping("/getTaskStaffGroupList/{taskId}")
    public R<List<TaskStaffGroupVo>> getTaskStaffGroupList(@PathVariable String taskId) {
        List<TaskStaffGroupVo> list = taskStaffGroupService.getTaskStaffGroupList(taskId);
        return R.ok(list);
    }

    /**
     * 技术模块-今日事务-查询技术员工列表
     *
     * @return R<List < StaffVo>>
     * @author DB
     * @since 2023/12/04
     */
    @ApiOperation("技术模块-今日事务-查询技术员工列表")
    @GetMapping("/getTechnologyStaffList")
    public R<List<StaffVo>> getTechnologyStaffList() {
        List<StaffVo> list = staffService.getTechnologyStaffList();
        return R.ok(list);
    }

    /**
     * 技术模块-今日事务-新增任务小组成员
     *
     * @param bo 请求参数
     * @return R<Void>
     * @author DB
     * @since 2023/12/04
     */
    @ApiOperation("技术模块-今日事务-新增任务小组成员")
    @PostMapping("/insertTaskStaffGroup")
    public R<Void> insertTaskStaffGroup(@RequestBody @Validated TaskStaffGroupBo bo) {
        taskStaffGroupService.insertTaskStaffGroup(bo);
        return R.ok();
    }

    /**
     * 技术模块-今日事务-修改任务负责人
     *
     * @param bo 请求参数
     * @return R<Void>
     * @author DB
     * @since 2023/12/04
     */
    @ApiOperation("技术模块-今日事务-修改任务负责人")
    @PutMapping("/updateTaskResponsibleStaff")
    public R<Void> updateTaskResponsibleStaff(@RequestBody @Validated TransferTaskBo bo) {
        taskService.updateTaskResponsibleStaff(bo);
        return R.ok();
    }

    /**
     * 技术模块-今日事务-修改员工任务绩点
     *
     * @param bo 请求参数
     * @return R<Void>
     * @author DB
     * @since 2023/12/04
     */
    @ApiOperation("技术模块-今日事务-修改员工任务绩点")
    @PutMapping("/updateStaffGradePoint")
    public R<Void> updateStaffGradePoint(@RequestBody @Validated TaskStaffGroupBo bo) {
        taskStaffGroupService.updateStaffGradePoint(bo);
        return R.ok();
    }

    /**
     * 技术模块-今日事务-删除成员组
     *
     * @param id 任务组id
     * @return R<Void>
     * @author DB
     * @since 2023/12/04
     */
    @ApiOperation("技术模块-今日事务-删除成员组")
    @DeleteMapping("/removeTaskStaffGroup/{id}")
    public R<Void> removeTaskStaffGroup(@PathVariable String id) {
        taskStaffGroupService.removeTaskStaffGroupById(id);
        return R.ok();
    }

    /**
     * 技术模块-今日事务-更新任务进度
     *
     * @param bo 请求参数
     * @return R<Void>
     * @author DB
     * @since 2023/12/04
     */
    @ApiOperation("技术模块-今日事务-更新任务进度")
    @PutMapping("/updateTaskProgress")
    public R<Void> updateTaskProgress(@RequestBody @Validated TaskProgressBo bo) {
        taskService.updateTaskProgress(bo);
        return R.ok();
    }

    /**
     * "技术模块-今日事务-转交任务
     *
     * @param bo 请求参数
     * @return R<Void>
     * @author DB
     * @since 2023/12/04
     */
    @ApiOperation("技术模块-今日事务-转交任务")
    @PutMapping("/updateTaskItem")
    public R<Void> updateTaskItem(@RequestBody @Validated TaskItemBo bo) {
        taskService.updateTaskItem(bo);
        return R.ok();
    }

    /**
     * 技术模块-数据统计-获取月度任务统计列表
     *
     * @param bo 请求参数
     * @return R<TaskMonthStatisticsVo>
     * @author DB
     * @since 2023/12/04
     */
    @PreAuthorize("@aps.hasPermission('oamTask:technology:list')")
    @ApiOperation("技术模块-数据统计-获取月度任务统计列表")
    @GetMapping("/getTaskMonthStatistics")
    public R<TaskMonthStatisticsVo> getTaskMonthStatistics(TaskMonthStatisticsBo bo) {
        return R.ok(taskService.getTaskMonthStatistics(bo));
    }

    /**
     * 技术模块-数据统计-获取任务日报
     *
     * @return R<TaskMonthStatisticsVo>
     * @author DB
     * @since 2023/12/05
     */
    @ApiOperation("技术模块-数据统计-获取任务日报")
    @GetMapping("/getTaskDailyPaper")
    public R<String> getTaskDailyPaper(TaskDailyPaperBo bo) {
        String dailyPaper = taskService.getTaskDailyPaper(bo);
        return R.ok(dailyPaper);
    }

    /**
     * 技术模块-今日事务-个人绩点
     *
     * @return R<TaskIndividualGpaVo>
     * @author DB
     * @since 2023/12/05
     */
    @PreAuthorize("@aps.hasPermission('oamTask:technology:list')")
    @ApiOperation("技术模块-今日事务-个人绩点")
    @GetMapping("/getIndividualGpa")
    public R<TaskIndividualGpaVo> getIndividualGpa(@RequestParam(value = "staffId", required = false) @ApiParam("员工id") String staffId,
                                                   @RequestParam(value = "startDate", required = false) @ApiParam("开始日期") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
                                                   @RequestParam(value = "endDate", required = false) @ApiParam("结束日期") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
        TaskIndividualGpaVo vo = taskService.getIndividualGpa(staffId, startDate, endDate);
        return R.ok(vo);
    }

    /**
     * 强制办结工单
     * @author DB
     * @since 2024/2/4
     * @param bo 请求
     * @return R<Void>
     */
    @PutMapping("/enforceFinishWorkOrder")
    @ApiOperation("强制办结工单")
    public R<Void> enforceFinishWorkOrder(@RequestBody @Validated EnforceFinishWorkOrderBo bo) {
        taskWorkOrderService.enforceFinishWorkOrder(bo);
        return R.ok();
    }

}
