package com.gxwebsoft.oa.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.oa.service.TaskUserService;
import com.gxwebsoft.oa.entity.TaskUser;
import com.gxwebsoft.oa.param.TaskUserParam;
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;

/**
 * 工单成员控制器
 *
 * @author 科技小王子
 * @since 2023-10-23 18:53:01
 */
@Api(tags = "工单成员管理")
@RestController
@RequestMapping("/api/oa/task-user")
public class TaskUserController extends BaseController {
    @Resource
    private TaskUserService taskUserService;

    @ApiOperation("分页查询工单成员")
    @GetMapping("/page")
    public ApiResult<PageResult<TaskUser>> page(TaskUserParam param) {
        PageParam<TaskUser, TaskUserParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(taskUserService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(taskUserService.pageRel(param));
    }

    @ApiOperation("查询全部工单成员")
    @GetMapping()
    public ApiResult<List<TaskUser>> list(TaskUserParam param) {
        PageParam<TaskUser, TaskUserParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(taskUserService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(taskUserService.listRel(param));
    }

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

    @ApiOperation("添加工单成员")
    @PostMapping()
    public ApiResult<?> save(@RequestBody TaskUser taskUser) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          taskUser.setUserId(loginUser.getUserId());
        }
        if (taskUserService.save(taskUser)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

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

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

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

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

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

}
