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

import cn.com.ty.lift.business.framework.util.MessageUtils;
import cn.com.ty.lift.business.library.dao.entity.model.request.LiftExtensionRequest;
import cn.com.ty.lift.business.library.dao.entity.model.request.LiftRequest;
import cn.com.ty.lift.business.library.dao.entity.model.response.LiftHistoryResponse;
import cn.com.ty.lift.business.library.dao.entity.model.response.LiftResponse;
import cn.com.ty.lift.business.library.service.LiftHistoryService;
import cn.com.ty.lift.business.library.service.LiftService;
import cn.com.ty.lift.business.library.service.PlatformCompanyLiftRelevanceService;
import cn.com.ty.lift.business.project.service.ProjectLiftRelevanceService;
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.Map;

/**
 * @author bieao
 * @date 2019/11/27
 * @description 电梯控制层
 */
@RestController
@RequestMapping("lift")
public class LiftController {

    @Resource
    private LiftService liftService;

    @Resource
    private LiftHistoryService liftHistoryService;

    @Resource
    private PlatformCompanyLiftRelevanceService platformService;

    @Resource
    private ProjectLiftRelevanceService projectLiftRelevanceService;

    /**
     * @param request 电梯列表查询条件
     * @return RestResponse 电梯分页列表结果
     * @description 查询电梯列表
     * @date 2019/11/27 10:03 AM
     */
    @PostMapping("list")
    @Validation(fields = {"mtCompanyId", "liftStatus"})
    public RestResponse list(@Val @RequestBody LiftRequest request) {
        IPage<LiftResponse> page = liftService.list(request);
        return RestResponse.success(page, MessageUtils.get("msg.query.success"));
    }

    /**
     * @param request 电梯操作列表查询条件
     * @return RestResponse 电梯操作记录分页列表结果
     * @description 查询电梯操作记录列表
     * @date 2020/4/27 2:42 下午
     */
    @PostMapping("history/list")
    @Validation(fields = {"mtCompanyId", "liftId"})
    public RestResponse historyList(@Val @RequestBody LiftRequest request) {
        IPage<LiftHistoryResponse> page = liftHistoryService.historyList(request);
        return RestResponse.success(page, MessageUtils.get("msg.query.success"));
    }

    /**
     * @param request 公司id
     * @return 数量集合
     * @description 根据公司id查询服务中电梯和维修中的电梯数量
     * @date 2020/1/19 2:34 下午
     */
    @PostMapping("countAll")
    @Validation(fields = {"mtCompanyId"})
    public RestResponse countAll(@Val @RequestBody LiftRequest request) {
        Map<String, Object> resultMap = platformService.countAll(request.getMtCompanyId());
        return RestResponse.success(resultMap, MessageUtils.get("msg.query.detail.success"));
    }

    /**
     * @param request 电梯列表查询条件
     * @return RestResponse 项目下电梯列表
     * @description 查询项目下电梯列表
     * @date 2019/12/17 11:04 AM
     */
    @PostMapping("project/list")
    @Validation(fields = {"mtCompanyId", "projectId"})
    public RestResponse liftProjectList(@Val @RequestBody LiftRequest request) {
        IPage<LiftResponse> page = liftService.liftProjectList(request);
        return RestResponse.success(page, MessageUtils.get("msg.query.success"));
    }

    /**
     * @param request 选择电梯列表查询条件
     * @return IPage<LiftResponse> 选择电梯列表
     * @description 选择电梯
     * @date 2019/12/30 10:42 AM
     */
    @PostMapping("choose")
    @Validation(fields = {"mtCompanyId"})
    public RestResponse choose(@Val @RequestBody LiftRequest request) {
        IPage<LiftResponse> page = liftService.choose(request);
        return RestResponse.success(page, MessageUtils.get("msg.query.success"));
    }

    /**
     * @param request 项目id，项目电梯关联id列表
     * @return 1.成功, 0.失败, 消息描述
     * @description 批量移动电梯
     * @date 2020/1/7 7:57 下午
     */
    @PostMapping("batch/move")
    @Validation(fields = {"projectId", "list"})
    public RestResponse move(@Val @RequestBody LiftRequest request){
        boolean result = projectLiftRelevanceService.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 项目电梯关联列表请求
     * @return 1.成功, 0.失败, 消息描述
     * @description 批量插入项目电梯关联数据
     * @date 2020/1/2 2:59 下午
     */
    @PostMapping("batch/insert")
    @Validation(fields = {"list"})
    public RestResponse insertBatch(@Val @RequestBody LiftRequest request) {
        boolean result = projectLiftRelevanceService.insertBatch(request.getList());
        if (!result) {
            return RestResponse.fail(MessageUtils.get("msg.add.batch.fail"));
        }
        return RestResponse.success(null, MessageUtils.get("msg.add.batch.success"));
    }

    /**
     * @param request 电梯id
     * @return RestResponse 电梯详情
     * @description 查询电梯详情
     * @date 2019/12/6 10:51 AM
     */
    @PostMapping("detail")
    @Validation(fields = {"id"})
    public RestResponse detail(@Val @RequestBody LiftRequest request) {
        Object response = liftService.detail(request.getId(), request.getProjectId());
        if (ObjectUtil.isEmpty(response)) {
            return RestResponse.success();
        }
        return RestResponse.success(response, MessageUtils.get("msg.query.detail.success"));
    }

    /**
     * @param request 公司id，电梯注册代码
     * @return RestResponse 判断结果
     * @description 电梯校验
     * @date 2019/12/20 2:31 PM
     */
    @PostMapping("preJudgment")
    @Validation(fields = {"mtCompanyId", "code"})
    public RestResponse preJudgment(@Val @RequestBody LiftRequest request) {
        return liftService.preJudgment(request.getMtCompanyId(), request.getCode());
    }

    /**
     * @param request 新增电梯数据项
     * @return 1.成功, 0.失败, 消息描述
     * @description 新增电梯
     * @date 2019/11/27 10:03 AM
     */
    @PostMapping("add")
    @Validation(fields = {"mtCompanyId", "source", "mark"})
    public RestResponse add(@Val @RequestBody LiftExtensionRequest request) {
        Validator.valid(request.getLift(), "registrationCode");
        return liftService.add(request);
    }

    /**
     * @param request 修改电梯数据项
     * @return 1.成功, 0.失败, 消息描述
     * @description 修改电梯
     * @date 2019/11/27 10:03 AM
     */
    @PostMapping("modify")
    @Validation(fields = {"mtCompanyId"})
    public RestResponse modify(@Val @RequestBody LiftRequest request) {
        Validator.valid(request.getLift(), "id");
       return liftService.modify(request);
    }

    /**
     * @param request updateLiftList 项目id列表
     * @return 1.成功, 0.失败, 消息描述
     * @description 电梯批量锁定、解锁
     * @date 2019/11/27 10:03 AM
     */
    @PostMapping("lockOrUnlock")
    @Validation(fields = {"mtCompanyId", "lock", "liftList"})
    public RestResponse lockOrUnlock(@Val @RequestBody LiftRequest request) {
        return projectLiftRelevanceService.liftLockOrUnLock(request);
    }


    /**
     * @param request 关联表主键id
     * @return 1.成功, 0.失败, 消息描述
     * @description 删除电梯
     * @date 2019/11/27 10:03 AM
     */
    @PostMapping("delete")
    @Validation(fields = {"id", "relevanceId"})
    public RestResponse delete(@Val @RequestBody LiftRequest request) {
        return projectLiftRelevanceService.delete(request);
    }

    /**
     * @param request idList 电梯id列表
     * @return 1.成功, 0.失败, 消息描述
     * @description 更新电梯状态为停保或恢复为正常
     * @date 2019/12/5 5:24 PM
     */
    @PostMapping("stop/recover")
    @Validation(fields = {"mtCompanyId", "relevanceId", "liftId", "operate"})
    public RestResponse stopOrRecover(@Val @RequestBody LiftRequest request) {
        return platformService.stopOrRecover(request);
    }

    /**
     * @param request idList 电梯id列表
     * @return 1.成功, 0.失败, 消息描述
     * @description 更新电梯状态为停保
     * @date 2019/12/5 5:24 PM
     */
    @PostMapping("batch/stop/insurance")
    @Validation(fields = {"mtCompanyId", "liftList", "relevanceIdList"})
    public RestResponse batchStopInsurance(@Val @RequestBody LiftRequest request) {
        return platformService.batchStopInsurance(request);
    }

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