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

import cn.com.ty.lift.business.framework.util.MessageUtils;
import cn.com.ty.lift.business.library.dao.entity.model.request.LiftRequest;
import cn.com.ty.lift.business.library.service.LiftService;
import cn.com.ty.lift.business.maintenance.dao.entity.MaintenanceRecord;
import cn.com.ty.lift.business.maintenance.dao.entity.ScanRecord;
import cn.com.ty.lift.business.maintenance.dao.entity.model.request.MaintenanceRecordRequest;
import cn.com.ty.lift.business.maintenance.dao.entity.model.request.ScanRecordRequest;
import cn.com.ty.lift.business.maintenance.dao.entity.model.response.ScanRecordResponse;
import cn.com.ty.lift.business.maintenance.dao.mapper.MaintenanceRecordMapper;
import cn.com.ty.lift.business.maintenance.service.MaintenanceRecordService;
import cn.com.ty.lift.business.maintenance.service.ScanRecordService;
import cn.com.xwy.boot.web.dto.RestResponse;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
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;

/**
 * 电梯二维码扫码Controller
 */
@RestController
@RequestMapping("scan")
@AllArgsConstructor
public class ScanRecordController {
    private final MaintenanceRecordMapper maintenanceRecordMapper;
    private final MaintenanceRecordService maintenanceRecordService;
    private final ScanRecordService scanRecordService;
    private final LiftService liftService;

    /**
     * 根据电梯id查询电梯信息
     *
     * @param request id
     * @return 电梯信息
     */
    @PostMapping("lift/detail")
    public RestResponse detail(@RequestBody LiftRequest request) {
        Object response = liftService.detail(request.getLiftId(), null);
        if (ObjectUtil.isEmpty(response)) {
            return RestResponse.success();
        }
        return RestResponse.success(response, MessageUtils.get("msg.query.detail.success"));
    }

    /**
     * 根据电梯id查询已完成的维保记录
     *
     * @param request liftId
     * @return 分页维保记录列表
     */
    @PostMapping("maintenance/success/record")
    public RestResponse maintenanceRecord(@RequestBody ScanRecordRequest request) {
        Page<ScanRecordResponse> page = new Page<>(request.getPageNum(), request.getPageSize());
        IPage<ScanRecordResponse> record = maintenanceRecordMapper.findRecordByLiftId(page, request);
        if (record.getRecords().isEmpty()) {
            return RestResponse.success();
        }
        for (ScanRecordResponse scanRecord : record.getRecords()) {
            if (scanRecord.getLiftType() != null) {
                int liftType = scanRecord.getLiftType();
                if (liftType == 1) {
                    scanRecord.setLiftTypeName("直梯");
                } else if (liftType == 2) {
                    scanRecord.setLiftTypeName("扶梯");
                } else {
                    scanRecord.setLiftTypeName("未设置电梯类型");
                }
            }
        }
        return RestResponse.success(record);
    }

    /**
     * 维保详情记录
     *
     * @param request 维保id
     * @return 维保记录详情
     */
    @PostMapping("maintenance/record/detail")
    public RestResponse detail(@RequestBody MaintenanceRecordRequest request) {
        MaintenanceRecord record = maintenanceRecordService.detail(request.getId());
        if (ObjectUtil.isEmpty(record)) {
            return RestResponse.success();
        }
        return RestResponse.success(record, MessageUtils.get("msg.query.detail.success"));
    }

    /**
     * 根据电梯id查询电梯扫码记录列表
     *
     * @param record pageNum,pageSize,liftId
     * @return 扫码记录
     */
    @PostMapping("record/list")
    public RestResponse list(@RequestBody ScanRecordRequest record) {
        Page<ScanRecord> scanRecordPage = new Page<>(record.getPageNum(), record.getPageNum());
        IPage<ScanRecord> page = scanRecordService.page(scanRecordPage, Wrappers
                .<ScanRecord>query()
                .eq(record.getLiftId() != null, "lift_id", record.getLiftId()));
        return RestResponse.success(page);
    }

    /**
     * 新增扫码记录
     *
     * @param scanRecord 扫码记录
     * @return 操作结果
     */
    @PostMapping("record/add")
    public RestResponse add(@RequestBody ScanRecord scanRecord) {
        return RestResponse.success(scanRecordService.save(scanRecord));
    }
}