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.ProjectLiftRelevance;
import cn.com.ty.lift.business.project.dao.entity.model.request.ProjectRequest;
import cn.com.ty.lift.business.project.dao.entity.model.response.ProjectRelevanceResponse;
import cn.com.ty.lift.business.project.dao.entity.model.response.ProjectUserResponse;
import cn.com.ty.lift.business.project.service.ProjectLiftRelevanceService;
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 com.baomidou.mybatisplus.core.metadata.IPage;
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/2/12
 * @description 项目电梯关联控制层
 */
@RestController
@RequestMapping("project/relevance")
public class ProjectRelevanceController {

    @Resource
    private ProjectLiftRelevanceService projectLiftRelevanceService;

    /**
     * @param request 用户id
     * @return 1.成功, 0.失败, 消息描述
     * @description 查询用户下的关联项目和电梯信息
     * @date 2019/12/26 4:27 PM
     */
    @PostMapping("list")
    @Validation(fields = {"workerId", "projectId"})
    public RestResponse relevanceList(@Val @RequestBody ProjectRequest request) {
        IPage<ProjectRelevanceResponse> page = projectLiftRelevanceService.relevanceList(request);
        if (CollUtil.isEmpty(page.getRecords())) {
            return RestResponse.success();
        }
        return RestResponse.success(page, MessageUtils.get("msg.query.success"));
    }

    /**
     * @param request 项目ID
     * @return List<ProjectUser> userList项目组维保工列表
     * @description 查询项目组维保工
     * @date 2020/2/12 5:01 下午
     */
    @PostMapping("user/list")
    @Validation(fields = {"projectId"})
    public RestResponse userList(@Val @RequestBody ProjectRequest request) {
        List<ProjectUserResponse> userList = projectLiftRelevanceService.userList(request.getId());
        if (CollUtil.isEmpty(userList)) {
            return RestResponse.success();
        }
        return RestResponse.success(userList, MessageUtils.get("msg.query.success"));
    }

    /**
     * @param request 修改项目电梯关联数据项
     * @return 1.成功, 0.失败, 消息描述
     * @description 修改项目电梯关联
     * @date 2020/2/3 1:29 下午
     */
    @PostMapping("modify")
    @Validation(fields = {"workerId", "list"})
    public RestResponse modify(@Val @RequestBody ProjectRequest request) {
        return projectLiftRelevanceService.batchModify(request);
    }

    /**
     * @param request 项目id 用户id
     * @return 1.成功, 0.失败, 消息描述
     * @description 删除项目关联数据
     * @date 2020/2/12 5:10 下午
     */
    @PostMapping("cancelRelevance")
    public RestResponse cancelRelevance(@RequestBody ProjectRequest request) {
        return projectLiftRelevanceService.cancelRelevance(request);
    }
}
