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

import cn.com.ty.lift.business.library.dao.entity.model.request.LiftRequest;
import cn.com.ty.lift.business.maintenance.dao.entity.MaintenanceOption;
import cn.com.ty.lift.business.maintenance.dao.entity.ScanRecord;
import cn.com.ty.lift.business.maintenance.dao.entity.model.request.MtRecordRequest;
import cn.com.ty.lift.business.maintenance.dao.entity.model.request.ScanRecordRequest;
import cn.com.ty.lift.business.maintenance.dao.entity.model.response.ScanMaintenanceRecordResponse;
import cn.com.ty.lift.business.maintenance.dao.mapper.ScanRecordMapper;
import cn.com.ty.lift.business.maintenance.service.MaintenanceOptionService;
import cn.com.ty.lift.business.maintenance.service.ScanRecordService;
import cn.com.xwy.boot.web.dto.RestResponse;
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;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 电梯二维码扫码Controller
 */
@RestController
@RequestMapping("scan")
@AllArgsConstructor
public class ScanRecordController {
    private final ScanRecordService scanRecordService;
    private final ScanRecordMapper scanRecordMapper;
    private final MaintenanceRecordController maintenanceRecordController;
    private final MaintenanceOptionService maintenanceOptionService;

    /**
     * 根据电梯id查询电梯信息
     *
     * @param request liftId
     * @return 电梯信息
     */
    @PostMapping("lift/detail")
    public RestResponse<?> detail(@RequestBody LiftRequest request) {
        return RestResponse.success(scanRecordMapper.queryLiftInfoByLiftId(request.getLiftId()));
    }

    /**
     * 根据电梯id查询维保
     *
     * @param request liftId
     * @return 分页维保记录列表
     */
    @PostMapping("maintenance/record")
    public RestResponse<Object> maintenanceRecord(@RequestBody ScanRecordRequest request) {
        IPage<ScanMaintenanceRecordResponse> page = new Page<>(request.getPageNum(), request.getPageSize());
        IPage<ScanMaintenanceRecordResponse> record = scanRecordMapper.queryMaintenanceRecordByLiftId(page, request.getLiftId());
        return RestResponse.success(record);
    }

    //维保项list结构
    @PostMapping("maintenance/option/list")
    public RestResponse<?> listOptions(@RequestBody ScanRecordRequest request) {
        List<Integer> sort;
        switch (request.getLiftType()) {
            case 1:
                sort = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
                break;
            case 2:
                sort = new ArrayList<>(Arrays.asList(-1));
                break;
            default:
                sort = null;
        }
        return RestResponse.success(maintenanceOptionService.list(Wrappers.<MaintenanceOption>query().in(sort != null, "sort", sort)));
    }

    //查看保养单
    @PostMapping("maintenance/detail")
    public RestResponse<?> detail(@RequestBody ScanRecordRequest scanRecordRequest) {
        MtRecordRequest request = new MtRecordRequest();
        request.setId(scanRecordRequest.getRecordId());
        return maintenanceRecordController.one(request);
    }

    //查看保养图片
    @PostMapping("maintenance/images")
    public RestResponse<?> images(@RequestBody ScanRecordRequest scanRecordRequest) {
        MtRecordRequest request = new MtRecordRequest();
        request.setId(scanRecordRequest.getRecordId());
        return maintenanceRecordController.images(request);
    }

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

    /**
     * 新增扫码记录
     *
     * @param scanRecord 扫码记录
     * @return 操作结果
     */
    @PostMapping("record/add")
    public RestResponse<?> add(@RequestBody ScanRecord scanRecord) {
        List<ScanRecord> list = scanRecordService.list(Wrappers.<ScanRecord>query()
                .eq(scanRecord.getOpenId() != null, "open_id", scanRecord.getOpenId())
                .eq(scanRecord.getLiftId() != null, "lift_id", scanRecord.getLiftId()));
        if (list.isEmpty()) {
            return RestResponse.success(scanRecordService.save(scanRecord));
        }
        return RestResponse.fail("该电梯扫码记录已存在");
    }
}