package com.gxwebsoft.oa.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.oa.entity.Task;
import com.gxwebsoft.oa.service.*;
import com.gxwebsoft.oa.entity.TaskRecord;
import com.gxwebsoft.oa.param.TaskRecordParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

import static com.gxwebsoft.oa.constants.NoticeConstants.LETTER;
import static com.gxwebsoft.oa.constants.TaskConstants.*;

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

    @PreAuthorize("hasAuthority('oa:task:list')")
    @OperationLog
    @ApiOperation("分页查询工单回复记录表")
    @GetMapping("/page")
    public ApiResult<PageResult<TaskRecord>> page(TaskRecordParam param) {
        // 使用关联查询
        return success(taskRecordService.pageRel(param));
    }

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

    @PreAuthorize("hasAuthority('oa:task:list')")
    @OperationLog
    @ApiOperation("根据id查询工单回复记录表")
    @GetMapping("/{id}")
    public ApiResult<TaskRecord> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(taskRecordService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('oa:task:save')")
    @OperationLog
    @ApiOperation("添加工单回复记录表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody TaskRecord taskRecord) {
        // 记录当前登录用户id、租户id、商户编号
        User loginUser = getLoginUser();
        if (loginUser != null) {
          taskRecord.setUserId(loginUser.getUserId());
        }
        if (taskRecordService.save(taskRecord)) {
          Task task = new Task();
          task.setTaskId(taskRecord.getTaskId());
          task.setProgress(PROGRESS2);
          task.setIsRead(0);
          task.setLastReadUser(getLoginUserId());
          taskService.updateById(task);
          // 发送私信
          noticeService.addLetter(taskRecord);
          return success("提交成功");
        }
        return fail("提交失败");
    }

    @PreAuthorize("hasAuthority('oa:task:update')")
    @OperationLog
    @ApiOperation("修改工单回复记录表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody TaskRecord taskRecord) {
        if (taskRecordService.updateById(taskRecord)) {
          Task task = taskService.getById(taskRecord.getTaskId());
          task.setIsRead(0);
          task.setLastReadUser(getLoginUserId());
          return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('oa:task:remove')")
    @OperationLog
    @ApiOperation("删除工单回复记录表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (taskRecordService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('oa:task:save')")
    @OperationLog
    @ApiOperation("批量添加工单回复记录表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<TaskRecord> list) {
        if (taskRecordService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('oa:task:update')")
    @OperationLog
    @ApiOperation("批量修改工单回复记录表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<TaskRecord> batchParam) {
        if (batchParam.update(taskRecordService, "task_record_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('oa:task:remove')")
    @OperationLog
    @ApiOperation("批量删除工单回复记录表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (taskRecordService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
