package com.gxwebsoft.open.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.oa.entity.Task;
import com.gxwebsoft.oa.entity.TaskRecord;
import com.gxwebsoft.oa.param.TaskRecordParam;
import com.gxwebsoft.oa.service.TaskRecordService;
import com.gxwebsoft.oa.service.TaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

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

import static com.gxwebsoft.oa.constants.TaskConstants.PROGRESS2;
import static com.gxwebsoft.oa.constants.TaskConstants.PROGRESS3;

/**
 * 工单回复记录表控制器
 *
 * @author 科技小王子
 * @since 2023-03-05 00:52:21
 */
@Api(tags = "工单回复记录表管理")
@RestController
@RequestMapping("/api/open/oa/task-record")
public class OpenTaskRecordController extends BaseController {
    @Resource
    private TaskService taskService;
    @Resource
    private TaskRecordService taskRecordService;

    @ApiOperation("分页查询工单回复记录表")
    @GetMapping("/page")
    public ApiResult<PageResult<TaskRecord>> page(TaskRecordParam param) {
        // 验证签名
        isCheckSign();
        // 使用关联查询
        return success(taskRecordService.pageRel(param));
    }

    @ApiOperation("查询全部工单回复记录表")
    @GetMapping()
    public ApiResult<List<TaskRecord>> list(TaskRecordParam param) {
        // 验证签名
        isCheckSign();
        // 更新查阅状态
        Task task = taskService.getById(param.getTaskId());
        if(!getLoginUserId().equals(task.getLastReadUser())){
          task.setIsRead(1);
        }
        taskService.updateById(task);
        // 使用关联查询
        return success(taskRecordService.listRel(param));
    }

    @ApiOperation("根据id查询工单回复记录表")
    @GetMapping("/{id}")
    public ApiResult<TaskRecord> get(@PathVariable("id") Integer id) {
        // 验证签名
        isCheckSign();
        // 使用关联查询
        return success(taskRecordService.getByIdRel(id));
    }

    @ApiOperation("添加工单回复记录表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody TaskRecord taskRecord) {
        // 验证签名
        isCheckSign();
        taskRecord.setUserId(getLoginUserId());
        int count = taskRecordService.count(new LambdaQueryWrapper<TaskRecord>().eq(TaskRecord::getTaskId,taskRecord.getTaskId()));
        if(count > 100){
          return fail("回复数超过最大限制");
        }
        if (taskRecordService.save(taskRecord)) {
            // 变更工单状态
            Task task = taskService.getById(taskRecord.getTaskId());
            task.setProgress(PROGRESS2);
            task.setIsRead(0);
            task.setLastReadUser(getLoginUserId());
            taskService.updateById(task);
            return success("提交成功");
        }
        return fail("提交失败");
    }

    @ApiOperation("修改工单回复记录表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody TaskRecord taskRecord) {
        // 验证签名
        isCheckSign();
        if (taskRecordService.updateById(taskRecord)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("删除工单回复记录表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        // 验证签名
        isCheckSign();
        if (taskRecordService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @ApiOperation("批量添加工单回复记录表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<TaskRecord> list) {
        // 验证签名
        isCheckSign();
        if (taskRecordService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("批量修改工单回复记录表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<TaskRecord> batchParam) {
        // 验证签名
        isCheckSign();
        if (batchParam.update(taskRecordService, "task_record_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("批量删除工单回复记录表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        // 验证签名
        isCheckSign();
        if (taskRecordService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
