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

import cn.com.ty.lift.business.framework.util.MessageUtils;
import cn.com.ty.lift.business.library.service.PlatformCompanyLiftRelevanceService;
import cn.com.ty.lift.business.maintenance.dao.entity.model.response.MaintenancePlanPrintResponse;
import cn.com.ty.lift.business.project.dao.entity.Project;
import cn.com.ty.lift.business.project.dao.entity.model.request.ProjectRequest;
import cn.com.ty.lift.business.project.dao.entity.model.response.ProjectHistoryResponse;
import cn.com.ty.lift.business.project.dao.entity.model.response.ProjectResponse;
import cn.com.ty.lift.business.project.service.ProjectHistoryService;
import cn.com.ty.lift.business.project.service.ProjectService;
import cn.com.ty.lift.common.base.ExportRequest;
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 cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author bieao
 * @date 2019/11/28
 * @description 项目管理控制层
 */
@RestController
@RequestMapping("project")
public class ProjectController {

    @Resource
    private ProjectService projectService;

    @Resource
    private ProjectHistoryService projectHistoryService;

    @Resource
    private PlatformCompanyLiftRelevanceService platformService;

    /**
     * @param request 项目列表查询条件
     * @return RestResponse 项目分页列表结果
     * @description 查询项目列表
     * @date 2019/11/28 4:18 PM
     */
    @PostMapping("list")
    @Validation(fields = {"mtCompanyId", "projectStatus"})
    public RestResponse list(@Val @RequestBody ProjectRequest request) {
        IPage<ProjectResponse> page = projectService.list(request);
        return RestResponse.success(page, MessageUtils.get("msg.query.success"));
    }

    /**
     * @param request 项目名称
     * @return 项目列表
     * @description 获取物业端数据接口
     * @date 2020/5/11 10:21 上午
     */
    @PostMapping("propertyList")
    public RestResponse propertyList(@RequestBody ProjectRequest request) {
        IPage<Project> page = projectService.propertyList(request);
        return RestResponse.success(page, "获取项目列表成功");
    }

    /**
     * @param request 公司id,项目状态
     * @return 项目数量、电梯数量、停保电梯数量
     * @description 查询项目数量、电梯数量、停保电梯数量
     * @date 2020/1/19 11:30 上午
     */
    @PostMapping("countAll")
    @Validation(fields = {"mtCompanyId", "projectStatus"})
    public RestResponse countAll(@Val @RequestBody ProjectRequest request) {
        Map<String, Object> resultMap = projectService.countAll(request.getMtCompanyId(), request.getProjectStatus());
        return RestResponse.success(resultMap, MessageUtils.get("msg.query.detail.success"));
    }

    /**
     * @param request 公司项目查询条件
     * @return RestResponse 公司项目分页列表结果
     * @description 查询公司项目列表
     * @date 2019/12/9 10:32 AM
     */
    @PostMapping("companyList")
    @Validation(fields = {"ppCompanyId", "projectStatus"})
    public RestResponse companyList(@Val @RequestBody ProjectRequest request) {
        IPage<ProjectResponse> page = projectService.companyList(request);
        return RestResponse.success(page, MessageUtils.get("msg.query.success"));
    }

    /**
     * @param request 项目操作记录查询条件
     * @return RestResponse 项目操作记录分页列表结果
     * @description 查询项目操作记录列表
     * @date 2019/12/2 11:41 AM
     */
    @PostMapping("history/list")
    @Validation(fields = {"mtCompanyId", "projectId"})
    public RestResponse historyList(@Val @RequestBody ProjectRequest request) {
        IPage<ProjectHistoryResponse> page = projectHistoryService.historyList(request);
        return RestResponse.success(page, MessageUtils.get("msg.query.success"));
    }

    /**
     * @param request regionId 区域id
     * @return RestResponse userList项目组成员列表
     * @description 查询项目id和名称列表
     * @date 2019/12/13 2:40 PM
     */
    @PostMapping("projectIDList")
    public RestResponse projectIDList(@RequestBody ProjectRequest request) {
        List<Map<String, Object>> projectIDList = projectService.projectIDList(request);
        return RestResponse.success(projectIDList, MessageUtils.get("msg.query.success"));
    }

    /**
     * @param request 项目id
     * @return RestResponse 项目详情
     * @description 查询项目详情
     * @date 2019/12/6 10:51 AM
     */
    @PostMapping("detail")
    @Validation(fields = {"id"})
    public RestResponse detail(@Val @RequestBody ProjectRequest request) {
        Map<String, Object> resultMap = projectService.detail(request.getId());
        return RestResponse.success(resultMap, MessageUtils.get("msg.query.detail.success"));
    }

    /**
     * @param projectRequest 项目id
     * @return 项目详情
     * @description
     * @date 2020/5/13 8:57 上午
     */
    @PostMapping("propertyDetail")
    @Validation(fields = {"id"})
    public RestResponse propertyDetail(@Val @RequestBody ProjectRequest projectRequest) {
        Optional<Project> project = projectService.getOne(projectRequest.getId());
        return RestResponse.success(project, "获取项目详情成功");
    }

    /**
     * @param request 新增项目数据项
     * @return 1.成功, 0.失败, 消息描述
     * @description 新增项目
     * @date 2019/11/28 4:19 PM
     */
    @PostMapping("add")
    @Validation(fields = {"userList"})
    public RestResponse add(@Val @RequestBody ProjectRequest request) {
        Validator.valid(request.getProject(), "mtCompanyId", "ppCompanyId", "ppContactId", "projectName",
                "address", "num", "startDate", "endDate", "regionId", "projectUsage");
        return projectService.add(request);
    }

    /**
     * @param project 修改项目数据项
     * @return 1.成功, 0.失败, 消息描述
     * @description 修改项目
     * @date 2019/11/28 4:19 PM
     */
    @PostMapping("modify")
    @Validation(fields = {"id"})
    public RestResponse modify(@Val @RequestBody Project project) {
        return projectService.modify(project);
    }

    /**
     * @param request 删除项目数据项
     * @return 1.成功, 0.失败, 消息描述
     * @description 删除项目，并删除项目组成员，删除项目关联数据
     * @date 2020/5/26 5:21 下午
     */
    @PostMapping("remove")
    @Validation(fields = {"id"})
    public RestResponse remove(@Val @RequestBody ProjectRequest request){
        return projectService.remove(request.getId(),request.getMtCompanyId());
    }

    /**
     * @param request mtCompanyId 公司id
     * @param request projectId 项目id
     * @param request operate 操作 1:恢复服务，2:停止服务，3:继续服务
     * @return 1.成功, 0.失败, 消息描述
     * @description 服务操作
     * @date 2019/12/10 5:42 PM
     */
    @PostMapping("service/operate")
    @Validation(fields = {"mtCompanyId", "projectId", "operate"})
    public RestResponse serviceOperate(@Val @RequestBody ProjectRequest request) {
        return platformService.serviceOperate(request);
    }

    /**
     * @param request projectId 项目id
     * @return 1.成功, 0.失败, 消息描述
     * @description 项目锁定、解锁
     * @date 2019/11/27 10:03 AM
     */
    @PostMapping("lockOrUnlock")
    @Validation(fields = {"mtCompanyId", "projectId", "lock"})
    public RestResponse lockOrUnlock(@Val @RequestBody ProjectRequest request) {
        return projectService.projectLockOrUnLock(request);
    }

    /**
     * @param request 公司id，项目id
     * @return 1.成功, 0.失败, 消息描述
     * @description 项目管理-维保计划打印列表
     * @date 2020/3/2 4:55 下午
     */
    @PostMapping("plan/print")
    @Validation(fields = {"mtCompanyId", "id", "beginTime"})
    public RestResponse printPlan(@Val @RequestBody ProjectRequest request) {
        MaintenancePlanPrintResponse detail = projectService.planPrint(request);
        if (ObjectUtil.isEmpty(detail)) {
            return RestResponse.success();
        }
        return RestResponse.success(detail, MessageUtils.get("msg.query.success"));
    }

    /**
     * @param request 导出项目数据列表
     * @return 1.成功, 0.失败, 消息描述
     * @description 导出项目
     * @date 2019/11/28 4:19 PM
     */
    @PostMapping("export")
    public void export(@RequestBody ExportRequest request, HttpServletResponse response) {
        projectService.export(request, response);
    }
}