package com.gxwebsoft.oa.controller;

import com.gxwebsoft.common.core.security.JwtUtil;
import com.gxwebsoft.common.core.utils.CommonUtil;
import com.gxwebsoft.common.core.web.*;
import com.gxwebsoft.common.system.entity.User;
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 javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

import static com.gxwebsoft.common.core.constants.TaskConstants.PENDING;
import static com.gxwebsoft.common.core.constants.TaskConstants.PROCESSING;

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

    @ApiOperation("分页查询工单回复记录表")
    @GetMapping("/page")
    public ApiResult<PageResult<TaskRecord>> page(TaskRecordParam param, HttpServletRequest request) {
        param.setToken(JwtUtil.getAccessToken(request));
        param.setTenantId(getTenantId());
        return success(taskRecordService.pageRel(param));
    }

    @ApiOperation("查询全部工单回复记录表")
    @GetMapping()
    public ApiResult<List<TaskRecord>> list(TaskRecordParam param) {
        PageParam<TaskRecord, TaskRecordParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(taskRecordService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(taskRecordService.listRel(param));
    }

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

    @ApiOperation("添加工单回复记录表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody TaskRecord taskRecord) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          taskRecord.setUserId(loginUser.getUserId());
          // 技术人员更新工单状态
          Task task = taskService.getById(taskRecord.getTaskId());
          if (CommonUtil.hasRole(loginUser.getRoles(),"commander") && task.getProgress().equals(PENDING)) {
            task.setProgress(PROCESSING);
          }
          task.setUpdateTime(new Date());
          task.setContent(taskRecord.getContent());
          task.setLastAvatar(loginUser.getAvatar());
          task.setLastReadUser(loginUser.getUserId());
          task.setLastNickname(loginUser.getNickname());
          taskService.updateById(task);
        }
        if (taskRecordService.save(taskRecord)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

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

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

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

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

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

}
