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

import cn.com.ty.lift.business.framework.util.MessageUtils;
import cn.com.ty.lift.business.maintenance.dao.entity.MaintenancePlan;
import cn.com.ty.lift.business.maintenance.dao.entity.model.request.MaintenancePlanRequest;
import cn.com.ty.lift.business.maintenance.dao.entity.model.response.MaintenancePlanPrintResponse;
import cn.com.ty.lift.business.maintenance.dao.entity.model.response.MaintenancePlanResponse;
import cn.com.ty.lift.business.maintenance.service.MaintenancePlanService;
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.xwy.boot.web.dto.RestResponse;
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;

/**
 * @author bieao
 * @date 2019/12/12
 * @description 维保管理控制层
 */
@RestController
@RequestMapping("maintenance")
public class MaintenanceController {

    @Resource
    private MaintenancePlanService maintenancePlanService;

    /**
     * @param request 维保计划列表
     * @return 1.成功, 0.失败, 消息描述
     * @description 查询维保计划
     * @date 2019/12/24 10:47 AM
     */
    @PostMapping("plan/list")
    @Validation(fields = {"mtCompanyId"})
    public RestResponse list(@Val @RequestBody MaintenancePlanRequest request) {
        IPage<MaintenancePlanResponse> page = maintenancePlanService.list(request);
        return RestResponse.success(page, MessageUtils.get("msg.query.success"));
    }

    /**
     * @param request 公司id，项目id，电梯id
     * @return 1.成功, 0.失败, 消息描述
     * @description 查询维保计划详情列表
     * @date 2020/1/2 9:36 上午
     */
    @PostMapping("plan/detail")
    @Validation(fields = {"mtCompanyId", "projectId", "liftId", "beginTime", "endTime"})
    public RestResponse detailList(@Val @RequestBody MaintenancePlanRequest request) {
        boolean result = request.getBeginTime().isAfter(request.getEndTime());
        if (result) return RestResponse.success(null, MessageUtils.get("msg.time.error"));

        List<MaintenancePlan> detailList = maintenancePlanService.detailList(request);
        return RestResponse.success(detailList, MessageUtils.get("msg.query.success"));
    }

    /**
     * @param request 电梯列表
     * @return 1.成功, 0.失败, 消息描述
     * @description 制定维保计划
     * @date 2019/12/16 2:36 PM
     */
    @PostMapping("generate/plan")
    @Validation(fields = {"planList", "relevanceList", "firstTime", "periods", "interval"})
    public RestResponse generatePlan(@Val @RequestBody MaintenancePlanRequest request) {
        return maintenancePlanService.insertBatch(request);
    }

    /**
     * @param request 修改维保计划请求
     * @return 1.成功, 0.失败, 消息描述
     * @description 修改维保计划
     * @date 2019/12/16 2:36 PM
     */
    @PostMapping("modify/plan")
    @Validation(fields = {"mtCompanyId", "projectId", "relevanceId", "liftId", "workerId", "periods"})
    public RestResponse modifyPlan(@Val @RequestBody MaintenancePlanRequest request) {
        boolean result = maintenancePlanService.modifyPlan(request);
        if (result) {
            return RestResponse.success(null, MessageUtils.get("msg.modify.success"));
        } else {
            return RestResponse.fail(MessageUtils.get("msg.modify.fail"));
        }
    }

    /**
     * @param request 公司id，项目id，电梯id
     * @return 是否成功
     * @description 清空维保计划
     * @date 2019/12/17 3:14 PM
     */
    @PostMapping("batch/clean")
    @Validation(fields = {"mtCompanyId", "liftIdList"})
    public RestResponse batchCleanPlan(@Val @RequestBody MaintenancePlanRequest request) {
        boolean result = maintenancePlanService.removeMaintenancePlan(request.getMtCompanyId(), request.getLiftIdList());
        if (result) {
            return RestResponse.success(null, MessageUtils.get("msg.modify.success"));
        } else {
            return RestResponse.fail(MessageUtils.get("msg.modify.fail"));
        }
    }

    /**
     * @param request 导出维保计划数据列表
     * @return 1.成功, 0.失败, 消息描述
     * @description 导出维保计划
     * @date 2019/12/19 11:28 AM
     */
    @PostMapping("export/plan")
    public void export(@RequestBody ExportRequest request, HttpServletResponse response) {
        maintenancePlanService.export(request,response);
    }

    /**
     * @param request 电梯列表
     * @return 1.成功, 0.失败, 消息描述
     * @description 维保计划打印列表
     * @date 2020/2/13 3:39 下午
     */
    @PostMapping("plan/print")
    @Validation(fields = {"mtCompanyId", "liftList", "beginTime"})
    public RestResponse printPlan(@Val @RequestBody MaintenancePlanRequest request){
        List<MaintenancePlanPrintResponse> detailList = maintenancePlanService.planPrint(request);
        return RestResponse.success(detailList, MessageUtils.get("msg.query.success"));
    }
}
