package com.xy.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xy.annotate.RestMappingController;
import com.xy.dto.DeviceEventMsgDto;
import com.xy.entity.DeviceEventMsg;
import com.xy.entity.SysCodeConfigureRedis;
import com.xy.error.CommRuntimeException;
import com.xy.service.DeviceEventMsgServiceImpl;
import com.xy.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

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

import static com.xy.utils.PlusBeans.toIPage;
import static com.xy.utils.PlusBeans.toPageBean;


@RestMappingController("merc-mini/device-event")
@AllArgsConstructor
@Api(tags = "小程序-设备事件")
public class MercMiniDeviceEventController {

    private DeviceEventMsgServiceImpl deviceEventMsgService;

    @ApiOperation("设备事件分页")
    @PostMapping("page")
    public R<PageBean<DeviceEventMsgDto.Vo>> page(@RequestBody DeviceEventMsgDto.Page page) {
        Long deviceId = page.getDeviceId();
        if (deviceId == null) {
            throw new CommRuntimeException("设备ID不可为空！");
        }
        PageBean<DeviceEventMsgDto.Vo> voPageBean = new PageBean<>();
        Long mercId = MercAuthUtils.getMercId();
        page.setMercId(mercId);
        ArrayList<String> deviceCodes = CollUtil.newArrayList("D01", "M01", "AISHJ");
        Map<String, SysCodeConfigureRedis> deviceCodeMap = MapUtil.newHashMap();
        deviceCodes.forEach(code -> {
            Map<String, SysCodeConfigureRedis> childrenByParentCode = SysCodeConfigureUtils.getChildrenByParentCode(code);
            deviceCodeMap.putAll(childrenByParentCode);
        });
        //获取可展示给商户的设备事件
        List<String> codes = new ArrayList<>();
        deviceCodeMap.forEach((k, v) -> {
            String expand = v.getExpand();
            if (StrUtil.isNotEmpty(expand)) {
                Boolean expShow2Merc = SysCodeConfigureUtils.getExpShow2Merc(expand);
                if (BooleanUtil.isTrue(expShow2Merc)) {
                    codes.add(k);
                }
            }
        });

        if (CollUtil.isEmpty(codes)) {
            //没有可展示给商户看的内容
            return R.ok(voPageBean);
        }

        PageBean pageBean = page.getPage();
        LambdaQueryWrapper<DeviceEventMsg> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(page, DeviceEventMsg.class)
                .in(DeviceEventMsg::getCode, codes)
                .build()
                .ge(ObjectUtil.isNotEmpty(page.getBeginCreateTime()), DeviceEventMsg::getCreateTime, page.getBeginCreateTime())
                .le(ObjectUtil.isNotEmpty(page.getEndCreateTime()), DeviceEventMsg::getCreateTime, page.getEndCreateTime())
                .orderByDesc(!Emptys.check(pageBean.getOrders()), DeviceEventMsg::getCreateTime);
        IPage<DeviceEventMsg> iPage = deviceEventMsgService.page(toIPage(pageBean), lambdaQueryWrapper);
        voPageBean = toPageBean(DeviceEventMsgDto.Vo.class, iPage);
        return R.ok(voPageBean);
    }

}
