package com.gxwebsoft.tower.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gxwebsoft.common.core.annotation.OperationLog;
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.service.UserService;
import com.gxwebsoft.tower.entity.TowerProjectUser;
import com.gxwebsoft.tower.param.TowerProjectUserParam;
import com.gxwebsoft.tower.service.TowerProjectUserService;
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-06-07 15:00:32
 */
@Api(tags = "项目成员管理")
@RestController
@RequestMapping("/api/tower/tower-project-user")
public class TowerProjectUserController extends BaseController {
    @Resource
    private TowerProjectUserService towerProjectUserService;
    @Resource
    private UserService userService;

    @PreAuthorize("hasAuthority('tower:towerProjectUser:list')")
    @OperationLog
    @ApiOperation("分页查询项目成员")
    @GetMapping("/page")
    public ApiResult<PageResult<TowerProjectUser>> page(TowerProjectUserParam param) {
        PageResult<TowerProjectUser> list = towerProjectUserService.pageRel(param);
        for(TowerProjectUser projectUser: list.getList() ){
            projectUser.setUser(userService.getById(projectUser.getUserId()));
        }
        return success(list);
    }

    @PreAuthorize("hasAuthority('tower:towerProjectUser:list')")
    @OperationLog
    @ApiOperation("查询全部项目成员")
    @GetMapping()
    public ApiResult<List<TowerProjectUser>> list(TowerProjectUserParam param) {
        // 使用关联查询
        return success(towerProjectUserService.listRel(param));
    }

    @PreAuthorize("hasAuthority('tower:towerProjectUser:list')")
    @OperationLog
    @ApiOperation("根据id查询项目成员")
    @GetMapping("/{id}")
    public ApiResult<TowerProjectUser> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(towerProjectUserService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('tower:towerProjectUser:save')")
    @OperationLog
    @ApiOperation("添加项目成员")
    @PostMapping()
    public ApiResult<?> save(@RequestBody TowerProjectUser towerProjectUser) {
        if (towerProjectUserService.count(new LambdaQueryWrapper<TowerProjectUser>()
          .eq(TowerProjectUser::getUserId, towerProjectUser.getUserId()).eq(TowerProjectUser::getProjectId,towerProjectUser.getProjectId())) > 0) {
          return fail("该成员已存在");
        }
        if (towerProjectUserService.save(towerProjectUser)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('tower:towerProjectUser:update')")
    @OperationLog
    @ApiOperation("修改项目成员")
    @PutMapping()
    public ApiResult<?> update(@RequestBody TowerProjectUser towerProjectUser) {
        if (towerProjectUserService.updateById(towerProjectUser)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('tower:towerProjectUser:remove')")
    @OperationLog
    @ApiOperation("删除项目成员")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (towerProjectUserService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('tower:towerProjectUser:save')")
    @OperationLog
    @ApiOperation("批量添加项目成员")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<TowerProjectUser> list) {
        if (towerProjectUserService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('tower:towerProjectUser:update')")
    @OperationLog
    @ApiOperation("批量修改项目成员")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<TowerProjectUser> batchParam) {
        if (batchParam.update(towerProjectUserService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

}
