package com.xy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xy.annotate.RestMappingController;
import com.xy.annotate.XyEncry;
import com.xy.collections.list.JArrayList;
import com.xy.collections.list.JList;
import com.xy.collections.map.JHashMap;
import com.xy.collections.map.JMap;
import com.xy.dto.ApiDto;
import com.xy.dto.common.MercPlaceDto;
import com.xy.entity.DeviceInfo;
import com.xy.entity.DeviceStatus;
import com.xy.service.DeviceInfoServiceImpl;
import com.xy.service.DeviceStatusServiceImpl;
import com.xy.service.common.MercPlaceService;
import com.xy.utils.Emptys;
import com.xy.utils.PageBean;
import com.xy.utils.PlusBeans;
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.List;

@AllArgsConstructor
@Api(tags = "对外api控制器")
@RestMappingController("api")
public class ApiController {

    private DeviceInfoServiceImpl deviceInfoService;

    private MercPlaceService mercPlaceService;

    private DeviceStatusServiceImpl deviceStatusService;

    @XyEncry
    @PostMapping("devicePages")
    @ApiOperation("设备分页查询")
    public R<ApiDto.DevicePageDto.Vo> devicePages(@RequestBody @Validated ApiDto.DevicePageDto devicePageDto) {
        ApiDto.DevicePageDto.Vo vo = new ApiDto.DevicePageDto.Vo();
        LambdaQueryWrapper<DeviceInfo> lambdaQueryWrapper = new LambdaQueryWrapper<DeviceInfo>()
                .eq(DeviceInfo::getMercId, devicePageDto.getKey())
                .eq(DeviceInfo::getActiveState, 1)
                .eq(Emptys.check(devicePageDto.getDeviceId()), DeviceInfo::getDeviceId, devicePageDto.getDeviceId());
        IPage<DeviceInfo> iPage = deviceInfoService.page(PlusBeans.toIPage(devicePageDto.getPage()), lambdaQueryWrapper);
        PageBean<ApiDto.DevicePageDto.Vo.Info> pageBean = PlusBeans.toPageBean(ApiDto.DevicePageDto.Vo.Info.class, iPage);
        List<DeviceInfo> records = iPage.getRecords();
        if (Emptys.check(records)) {
            JList<DeviceInfo> deviceInfos = new JArrayList<>(records);
            JList<DeviceInfo> placeIds = deviceInfos.filter().isNotNull(DeviceInfo::getPlaceId).list();
            JMap<Long, MercPlaceDto.Vo> mercPlaceJMaps = new JHashMap<>();
            if (Emptys.check(placeIds)) {
                List<MercPlaceDto.Vo> mercPlaces = mercPlaceService.list(new MercPlaceDto.ListDto()
                        .setIds(placeIds.getProperty(DeviceInfo::getPlaceId).comparing())
                ).getData();
                mercPlaceJMaps = new JArrayList<>(mercPlaces).toMap(MercPlaceDto.Vo::getId).cover();
            }
            List<DeviceStatus> deviceStatuses = deviceStatusService.list(new LambdaQueryWrapper<DeviceStatus>()
                    .in(DeviceStatus::getDeviceId, deviceInfos.getProperty(DeviceInfo::getDeviceId))
            );
            JMap<Long, DeviceStatus> deviceStatusJMaps = new JArrayList<>(deviceStatuses).toMap(DeviceStatus::getDeviceId).cover();
            List<ApiDto.DevicePageDto.Vo.Info> infos = pageBean.getRecords();
            for (ApiDto.DevicePageDto.Vo.Info info : infos) {
                MercPlaceDto.Vo mercPlace = mercPlaceJMaps.get(info.getDeviceId());
                DeviceStatus deviceStatus = deviceStatusJMaps.get(info.getDeviceId());
                if (mercPlace != null) {
                    info.setRegionName(mercPlace.getRegionName());
                }
                info.setNetState(deviceStatus.getNetState());
            }
        }
        vo.setResult(pageBean);
        return R.ok(vo);
    }

}
