package cn.com.ty.lift.business.project.controller;

import cn.com.ty.lift.business.framework.util.MessageUtils;
import cn.com.ty.lift.business.project.dao.entity.model.request.ProjectRequest;
import cn.com.ty.lift.business.project.dao.entity.model.response.ProjectUserResponse;
import cn.com.ty.lift.business.project.service.ProjectUserService;
import cn.com.ty.lift.common.verify.Val;
import cn.com.ty.lift.common.verify.Validation;
import cn.com.xwy.boot.web.dto.RestResponse;
import cn.hutool.core.collection.CollUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @author bieao
 * @date 2020/1/11
 * @description 项目组成员控制层
 */
@RestController
@RequestMapping("project")
public class ProjectUserController {

    @Resource
    private ProjectUserService projectUserService;

    /**
     * @param request 公司id 用户id
     * @return 用户关联项目列表
     * @description 获取用户关联项目列表
     * @date 2020/5/26 3:57 下午
     */
    @PostMapping("relevanceProject")
    @Validation(fields = {"mtCompanyId", "workerId"})
    public RestResponse relevanceProject(@Val @RequestBody ProjectRequest request) {
        return projectUserService.relevanceProject(request);
    }

    /**
     * @param request projectId 项目ID
     * @return RestResponse userList项目组成员列表
     * @description 查询项目组成员
     * @date 2019/12/13 2:40 PM
     */
    @PostMapping("user/list")
    @Validation(fields = {"mtCompanyId", "id"})
    public RestResponse userList(@Val @RequestBody ProjectRequest request) {
        List<ProjectUserResponse> userList = projectUserService.userList(request.getId(), request.getMtCompanyId()
                , request.getUserId(), request.getCurrentRoleCode());
        if (CollUtil.isEmpty(userList)) {
            return RestResponse.success();
        }
        return RestResponse.success(userList, MessageUtils.get("msg.query.success"));
    }


    /**
     * @param request 新增项目组成员
     * @return 1.成功, 0.失败, 消息描述
     * @description 新增项目组成员
     * @date 2019/12/4 4:44 PM
     */
    @PostMapping("/user/batch/add")
    @Validation(fields = {"mtCompanyId", "projectId", "userList"})
    public RestResponse add(@Val @RequestBody ProjectRequest request) {
        boolean result = projectUserService.add(request);
        if (result) {
            return RestResponse.success(null, MessageUtils.get("msg.add.batch.success"));
        }
        return RestResponse.fail(MessageUtils.get("msg.add.batch.fail"));
    }

    /**
     * @param request 项目组成员
     * @return Integer 成功1或失败0
     * @description 修改项目组成员
     * @date 2020/1/11 10:43 上午
     */
    @PostMapping("/user/modify")
    @Validation(fields = {"mtCompanyId", "projectId", "userId"})
    public RestResponse modify(@Val @RequestBody ProjectRequest request) {
        return projectUserService.modify(request);
    }

    /**
     * @param request 项目组成员
     * @return Integer 成功1或失败0
     * @description 删除项目组成员
     * @date 2020/1/11 10:43 上午
     */
    @PostMapping("/user/batch/remove")
    @Validation(fields = {"mtCompanyId", "projectId", "userList"})
    public RestResponse remove(@Val @RequestBody ProjectRequest request) {
        int result = projectUserService.remove(request);
        if (result > 0) {
            return RestResponse.success(null, MessageUtils.get("msg.delete.success"));
        }
        return RestResponse.fail(MessageUtils.get("msg.delete.fail"));
    }
}
