package com.xy.controller;

import com.xy.annotate.RestMappingController;
import com.xy.dto.*;
import com.xy.service.DeviceEventMsgService;
import com.xy.service.DeviceNetRecordService;
import com.xy.service.DeviceRecordsService;
import com.xy.service.DeviceTempRecordsService;
import com.xy.utils.Emptys;
import com.xy.utils.MercAuthUtils;
import com.xy.utils.PageBean;
import com.xy.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

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

/**
 * <p>
 * 小程序设备相关记录
 * </p>
 *
 * @author hechunping
 * @since 2023/3/4
 */
@RestMappingController("merc-mini/deviceRecords")
@AllArgsConstructor
@Api(tags = "小程序-设备记录")
public class MercMiniDeviceRecordsController {

    private final static int INDEX_RECORDS_COUNT = 10;

    private final DeviceRecordsService deviceRecordsService;

    private final DeviceNetRecordService deviceNetRecordService;

    private final DeviceEventMsgService deviceEventMsgService;

    private final DeviceTempRecordsService deviceTempRecordsService;

    @ApiOperation("设备操作记录分页")
    @PostMapping("deviceRecordsPage")
    public R<PageBean<DeviceRecordsDto.Vo>> deviceRecordsPage(@RequestBody @Validated DeviceRecordsDto.Page dto) {
        return deviceRecordsService.page(dto);
    }

    @ApiOperation("设备详情首页-设备操作记录")
    @PostMapping("indexDeviceRecords")
    public R<List<DeviceRecordsDto.Vo>> indexDeviceRecords(@RequestBody @Validated MiniDeviceRecordsDto.DeviceIdDto dto) {
        DeviceRecordsDto.Page page = (DeviceRecordsDto.Page) new DeviceRecordsDto.Page().setMercId(MercAuthUtils.getMercId())
                .setDeviceId(dto.getDeviceId());
        page.setPage(getPageBean(INDEX_RECORDS_COUNT));
        List<DeviceRecordsDto.Vo> recordsList = deviceRecordsPage(page).getData().getRecords();
        return R.ok(recordsList);
    }

    @ApiOperation("联网记录分页")
    @PostMapping("deviceNetRecordPage")
    public R<PageBean<DeviceNetRecordDto.Vo>> deviceNetRecordPage(@RequestBody @Validated DeviceNetRecordDto.Page dto) {
        return deviceNetRecordService.page(dto);
    }

    @ApiOperation("设备详情首页-联网记录")
    @PostMapping("indexDeviceNetRecords")
    public R<List<DeviceNetRecordDto.Vo>> indexDeviceNetRecords(@RequestBody @Validated MiniDeviceRecordsDto.DeviceIdDto dto) {
        DeviceNetRecordDto.Page page =
                (DeviceNetRecordDto.Page) new DeviceNetRecordDto.Page()
                        .setMercId(MercAuthUtils.getMercId());
        if (Emptys.check(dto.getDeviceId())) {
            page.setDeviceId(dto.getDeviceId());
        }
        page.setPage(getPageBean(INDEX_RECORDS_COUNT));
        List<DeviceNetRecordDto.Vo> recordsList = deviceNetRecordPage(page).getData().getRecords();
        return R.ok(recordsList);
    }

    @ApiOperation("故障记录分页")
    @PostMapping("deviceErrorPage")
    public R<PageBean<DeviceEventMsgDto.Vo>> deviceErrorPage(@RequestBody DeviceEventMsgDto.Page page) {
        return deviceEventMsgService.page(page);
    }

    @ApiOperation("设备详情首页-故障记录")
    @PostMapping("indexDeviceError")
    public R<List<DeviceEventMsgDto.Vo>> indexDeviceError(@RequestBody @Validated MiniDeviceRecordsDto.DeviceErrorPageDto dto) {
        DeviceEventMsgDto.Page page = (DeviceEventMsgDto.Page) new DeviceEventMsgDto.Page()
                .setMercId(MercAuthUtils.getMercId())
                .setDeviceId(dto.getDeviceId())
                .setCode(dto.getCode());
        page.setPage(getPageBean(INDEX_RECORDS_COUNT));
        List<DeviceEventMsgDto.Vo> recordsList = deviceErrorPage(page).getData().getRecords();
        return R.ok(recordsList);
    }

    @ApiOperation("温度记录分页")
    @PostMapping("deviceTempRecords")
    public R<PageBean<DeviceTempRecordsDto.Vo>> deviceTempRecords(@RequestBody @Validated DeviceTempRecordsDto.Page dto) {
        return deviceTempRecordsService.page(dto);
    }

    @ApiOperation("设备详情首页-温度记录")
    @PostMapping("indexDeviceTempRecords")
    public R<List<DeviceTempRecordsDto.Vo>> indexDeviceTempRecords(@RequestBody @Validated MiniDeviceRecordsDto.DeviceIdDto dto) {
        DeviceTempRecordsDto.Page page = (DeviceTempRecordsDto.Page) new DeviceTempRecordsDto.Page().setDeviceId(dto.getDeviceId());
        page.setPage(getPageBean(INDEX_RECORDS_COUNT));
        List<DeviceTempRecordsDto.Vo> recordsList = deviceTempRecords(page).getData().getRecords();
        return R.ok(recordsList);
    }

    /**
     * 获取分页信息
     *
     * @param count 显示条数
     * @return {@link PageBean}
     */
    private PageBean getPageBean(int count) {
        List<PageBean.OrderItemBean> orderItemBeans = new ArrayList<>();
        orderItemBeans.add(new PageBean.OrderItemBean().setColumn("id").setAsc(false));
        return new PageBean<>().setSize(count).setSearchCount(false).setOrders(orderItemBeans);
    }
}
