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

import cn.com.ty.lift.business.capital.dao.entity.CapitalRepair;
import cn.com.ty.lift.business.capital.dao.entity.model.request.CapitalRepairRequest;
import cn.com.ty.lift.business.capital.dao.entity.model.response.CapitalRepairHistoryResponse;
import cn.com.ty.lift.business.capital.dao.entity.model.response.CapitalRepairResponse;
import cn.com.ty.lift.business.capital.service.CapitalRepairHistoryService;
import cn.com.ty.lift.business.capital.service.CapitalRepairLiftRelevanceService;
import cn.com.ty.lift.business.capital.service.CapitalRepairService;
import cn.com.ty.lift.business.capital.service.SignInService;
import cn.com.ty.lift.business.framework.util.MessageUtils;
import cn.com.ty.lift.business.library.dao.entity.model.response.LiftResponse;
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.ty.lift.common.verify.Validator;
import cn.com.xwy.boot.web.dto.RestResponse;
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;
import java.util.Map;

/**
 * @author bieao
 * @date 2020/2/3 12:21 下午
 * @description 大修项目控制层
 */
@RestController
@RequestMapping("capital/repair")
public class CapitalRepairController {

    @Resource
    private CapitalRepairService capitalRepairService;

    @Resource
    private SignInService signInService;

    @Resource
    private ProjectUserService projectUserService;

    @Resource
    private CapitalRepairLiftRelevanceService capitalRepairLiftRelevanceService;

    @Resource
    private CapitalRepairHistoryService capitalRepairHistoryService;

    /**
     * @param request 大修项目列表查询条件
     * @return RestResponse 结果集
     * @description 条件查询大修项目列表
     * @date 2020/2/3 11:38 上午
     */
    @PostMapping("list")
    @Validation(fields = {"mtCompanyId"})
    public RestResponse list(@Val @RequestBody CapitalRepairRequest request) {
        IPage<CapitalRepairResponse> page = capitalRepairService.list(request);
        return RestResponse.success(page, MessageUtils.get("msg.query.success"));
    }

    /**
     * @param request 电梯列表查询条件
     * @return RestResponse 大修项目下电梯列表
     * @description 查询大修项目下电梯列表
     * @date 2020/4/29 3:02 下午
     */
    @PostMapping("lift/list")
    @Validation(fields = {"mtCompanyId", "projectId"})
    public RestResponse liftProjectList(@Val @RequestBody CapitalRepairRequest request) {
        IPage<LiftResponse> page = capitalRepairService.liftProjectList(request);
        return RestResponse.success(page, MessageUtils.get("msg.query.success"));
    }

    /**
     * @param request 大修项目操作记录查询条件
     * @return RestResponse 大修项目操作记录分页列表结果
     * @description 查询大修项目操作记录列表
     * @date 2020/4/28 10:00 上午
     */
    @PostMapping("history/list")
    @Validation(fields = {"mtCompanyId", "projectId"})
    public RestResponse historyList(@Val @RequestBody CapitalRepairRequest request) {
        IPage<CapitalRepairHistoryResponse> page = capitalRepairHistoryService.historyList(request);
        return RestResponse.success(page, MessageUtils.get("msg.query.success"));
    }

    /**
     * @param request mtCompanyId 维保公司ID
     * @return RestResponse 项目ID和名称列表
     * @description 查询项目ID和名称列表
     * @date 2020/4/30 10:41 上午
     */
    @PostMapping("projectIDList")
    @Validation(fields = {"mtCompanyId"})
    public RestResponse projectIDList(@Val @RequestBody CapitalRepairRequest request) {
        List<Map<String, Object>> projectIDList = capitalRepairService.projectIDList(request);
        return RestResponse.success(projectIDList, MessageUtils.get("msg.query.success"));
    }

    /**
     * @param request 选择电梯列表查询条件
     * @return IPage<LiftResponse> 选择电梯列表
     * @description 选择电梯
     * @date 2020/4/29 8:58 下午
     */
    @PostMapping("choose")
    @Validation(fields = {"mtCompanyId"})
    public RestResponse choose(@Val @RequestBody CapitalRepairRequest request) {
        IPage<LiftResponse> page = capitalRepairService.choose(request);
        return RestResponse.success(page, MessageUtils.get("msg.query.success"));
    }

    /**
     * @param request 项目id，项目电梯关联id列表
     * @return 1.成功, 0.失败, 消息描述
     * @description 批量移动电梯
     * @date 2020/4/29 8:53 下午
     */
    @PostMapping("batch/move")
    @Validation(fields = {"projectId", "list"})
    public RestResponse move(@Val @RequestBody CapitalRepairRequest request){
        boolean result = capitalRepairLiftRelevanceService.move(request);
        if (!result) {
            return RestResponse.fail(MessageUtils.get("msg.modify.batch.fail"));
        }
        return RestResponse.success(null, MessageUtils.get("msg.modify.batch.success"));
    }

    /**
     * @param request 公司id,项目状态
     * @return 项目数量、电梯数量、停保电梯数量
     * @description 查询大修项目数量、电梯数量、停保电梯数量
     * @date 2020/4/23 3:31 下午
     */
    @PostMapping("countAll")
    @Validation(fields = {"mtCompanyId", "projectStatus"})
    public RestResponse countAll(@Val @RequestBody CapitalRepairRequest request) {
        Map<String, Object> resultMap = capitalRepairService.countAll(request.getMtCompanyId(), request.getProjectStatus());
        return RestResponse.success(resultMap, MessageUtils.get("msg.query.detail.success"));
    }

    /**
     * @param request 大修项目id
     * @return CapitalRepair 大修项目详情
     * @description 查询大修项目详情
     * @date 2020/2/3 11:31 上午
     */
    @PostMapping("detail")
    @Validation(fields = {"id", "mtCompanyId"})
    public RestResponse detail(@Val @RequestBody CapitalRepairRequest request) {
        Map<String, Object> resultMap = capitalRepairService.detail(request.getId(), request.getMtCompanyId());
        return RestResponse.success(resultMap, MessageUtils.get("msg.query.detail.success"));
    }

    /**
     * @param request 新增大修项目数据项
     * @return Integer 成功1或失败0
     * @description 新增大修项目
     * @date 2020/2/3 11:31 上午
     */
    @PostMapping("add")
    @Validation(fields = {"userList"})
    public RestResponse add(@Val @RequestBody CapitalRepairRequest request) {
        Validator.valid(request.getCapitalRepair(), "mtCompanyId", "ppCompanyId");
        return capitalRepairService.add(request);
    }

    /**
     * @param repair 修改大修项目数据项
     * @return Integer 成功1或失败0
     * @description 修改大修项目
     * @date 2020/2/3 11:36 上午
     */
    @PostMapping("modify")
    @Validation(fields = {"id"})
    public RestResponse modify(@Val @RequestBody CapitalRepair repair) {
        return capitalRepairService.modify(repair);
    }

    /**
     * @param request 是否通过
     * @return Integer 成功1或失败0
     * @description 大修审核
     * @date 2020/2/4 12:46 下午
     */
    @PostMapping("audit")
    @Validation(fields = {"id", "pass", "type"})
    public RestResponse audit(@Val @RequestBody CapitalRepairRequest request) {
        boolean result = capitalRepairService.audit(request);
        if (result) {
            return RestResponse.success(null, MessageUtils.get("msg.modify.success"));
        }
        return RestResponse.fail(MessageUtils.get("msg.modify.fail"));
    }

    /**
     * @param request mtCompanyId 公司id
     * @param request projectId 项目id
     * @param request operate 操作 1:恢复服务，2:停止服务
     * @return 1.成功, 0.失败, 消息描述
     * @description 服务操作
     * @date 2020/4/29 10:16 上午
     */
    @PostMapping("service/operate")
    @Validation(fields = {"mtCompanyId", "projectId", "operate"})
    public RestResponse serviceOperate(@Val @RequestBody CapitalRepairRequest request) {
        return capitalRepairService.serviceOperate(request);
    }

    /**
     * @param request 项目id，公司id
     * @return List<ProjectUserResponse> 大修项目组成员列表
     * @description 查询大修项目组成员
     * @date 2020/4/18 12:45 下午
     */
    @PostMapping("userList")
    @Validation(fields = {"id", "mtCompanyId"})
    public RestResponse userList(@Val @RequestBody CapitalRepairRequest request){
        List<ProjectUserResponse> userList = projectUserService.userList(request.getId(), request.getMtCompanyId()
                , request.getUserId(), request.getCurrentRoleCode());
        return RestResponse.success(userList, MessageUtils.get("msg.query.success"));
    }

    /**
     * @param request 大修签到查询条件
     * @return RestResponse 结果集
     * @description 条件查询大修项目签到列表
     * @date 2020/4/16 7:22 下午
     */
    @PostMapping("signIn/record")
    @Validation(fields = {"id", "mtCompanyId"})
    public RestResponse signInRecord(@Val @RequestBody CapitalRepairRequest request) {
        return signInService.list(request);
    }

    /**
     * @param request 项目电梯关联列表请求
     * @return 1.成功, 0.失败, 消息描述
     * @description 批量插入项目电梯关联数据
     * @date 2020/5/9 10:31 上午
     */
    @PostMapping("batch/insert")
    @Validation(fields = {"list"})
    public RestResponse insertBatch(@Val @RequestBody CapitalRepairRequest request) {
        boolean result = capitalRepairLiftRelevanceService.insertBatch(request.getList());
        if (!result) {
            return RestResponse.fail(MessageUtils.get("msg.add.batch.fail"));
        }
        return RestResponse.success(null, MessageUtils.get("msg.add.batch.success"));
    }
}
