package com.gxwebsoft.oa.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.core.exception.BusinessException;
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.common.system.entity.User;
import com.gxwebsoft.common.system.service.UserService;
import com.gxwebsoft.oa.entity.Task;
import com.gxwebsoft.oa.entity.TaskUser;
import com.gxwebsoft.oa.param.TaskUserParam;
import com.gxwebsoft.oa.service.TaskService;
import com.gxwebsoft.oa.service.TaskUserService;
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.TaskConstants.PROGRESS1;

/**
 * 工单成员控制器
 *
 * @author 科技小王子
 * @since 2023-03-06 12:00:41
 */
@Api(tags = "工单成员管理")
@RestController
@RequestMapping("/api/oa/task-user")
public class TaskUserController extends BaseController {
    @Resource
    private UserService userService;
    @Resource
    private TaskUserService taskUserService;
    @Resource
    private TaskService taskService;

    @PreAuthorize("hasAuthority('oa:taskUser:list')")
    @OperationLog
    @ApiOperation("分页查询工单成员")
    @GetMapping("/page")
    public ApiResult<PageResult<TaskUser>> page(TaskUserParam param) {
        // 使用关联查询
        return success(taskUserService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('oa:taskUser:list')")
    @OperationLog
    @ApiOperation("查询全部工单成员")
    @GetMapping()
    public ApiResult<List<TaskUser>> list(TaskUserParam param) {
        // 使用关联查询
        return success(taskUserService.listRel(param));
    }

    @PreAuthorize("hasAuthority('oa:taskUser:list')")
    @OperationLog
    @ApiOperation("根据id查询工单成员")
    @GetMapping("/{id}")
    public ApiResult<TaskUser> get(@PathVariable("id") Integer id) {
//        return success(taskUserService.getById(id));
        // 使用关联查询
        return success(taskUserService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('oa:taskUser:save')")
    @OperationLog
    @ApiOperation("添加工单成员")
    @PostMapping()
    public ApiResult<?> save(@RequestBody TaskUser taskUser) {
        User user = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, taskUser.getName()));
        if(user == null){
          throw new BusinessException("找不到该用户");
        }
        int count = taskUserService.count(new LambdaQueryWrapper<TaskUser>().eq(TaskUser::getUserId, user.getUserId()).eq(TaskUser::getTaskId,taskUser.getTaskId()));
        if( count > 0) {
          return fail("请勿重复指派");
        }
        taskUser.setUserId(user.getUserId());
        if (taskUserService.save(taskUser)) {
          Task task = new Task();
          task.setTaskId(taskUser.getTaskId());
          task.setProgress(PROGRESS1);
          task.setIsRead(0);
          task.setLastReadUser(getLoginUserId());
          taskService.updateById(task);
          return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('oa:taskUser:update')")
    @OperationLog
    @ApiOperation("修改工单成员")
    @PutMapping()
    public ApiResult<?> update(@RequestBody TaskUser taskUser) {
        if (taskUserService.updateById(taskUser)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('oa:taskUser:remove')")
    @OperationLog
    @ApiOperation("删除工单成员")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (taskUserService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('oa:taskUser:save')")
    @OperationLog
    @ApiOperation("批量添加工单成员")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<TaskUser> list) {
        if (taskUserService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('oa:taskUser:update')")
    @OperationLog
    @ApiOperation("批量修改工单成员")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<TaskUser> batchParam) {
        if (batchParam.update(taskUserService, "task_user_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

}
