package com.xy.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.xy.annotate.RestMappingController;
import com.xy.dto.DeviceInfoDto;
import com.xy.dto.MemberDeviceDTO;
import com.xy.dto.MemberOrdersDTO;
import com.xy.error.CommRuntimeException;
import com.xy.service.DeviceInfoServiceImpl;
import com.xy.service.OrdersMemberService;
import com.xy.utils.AuthorizeUtils;
import com.xy.utils.Emptys;
import com.xy.utils.PageBean;
import com.xy.utils.R;
import com.xy.utils.enums.DeviceBusySateType;
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;

/***
 * 会员-设备
 * @author 谭斌
 * @date 2023/3/18 10:24
 */
@RestMappingController("member/device")
@AllArgsConstructor
@Api(tags = "会员-设备")
public class MemberDeviceController {

    private DeviceInfoServiceImpl deviceInfoService;
    private OrdersMemberService ordersMemberService;

    @PostMapping("nearbyPage")
    @ApiOperation("附近设备列表")
    public R<List<MemberDeviceDTO.NearlyDeviceVO>> nearbyPage(@RequestBody MemberDeviceDTO.QueryNearlyDevice dto) {
        if (!Emptys.check(dto.getLon()) || !Emptys.check(dto.getLat())) {
            throw new CommRuntimeException("经纬度不能为空");
        }
        String deviceName = dto.getDeviceName();
        MemberDeviceDTO.NearlyDeviceVO vo = new MemberDeviceDTO.NearlyDeviceVO();
        DeviceInfoDto.Page page = new DeviceInfoDto.Page();
        PageBean pageBean = new PageBean().setSize(-1L);
        page.setPage(pageBean);
        page.setDeviceName(deviceName);
        page.setLimitNum(dto.getLimitNum());
        //冻结状态 1正常  2已冻结
        page.setFreezeStatus(1);
        page.setNearby(dto.getNearby());
        page.setLat(dto.getLat());
        page.setLon(dto.getLon());
        PageBean<DeviceInfoDto.Vo2> pages = deviceInfoService.queryPage(page);
        List<DeviceInfoDto.Vo2> records = pages.getRecords();
        if (CollUtil.isEmpty(records)) {
            return R.ok(CollUtil.newArrayList());
        }
        List<MemberDeviceDTO.NearlyDeviceVO> nearlyDeviceVOS = BeanUtil.copyToList(records, MemberDeviceDTO.NearlyDeviceVO.class);
        nearlyDeviceVOS.forEach(v -> {
            v.setBusyStateDesc(DeviceBusySateType.getEnumByCode(v.getBusyState()).getDescription());
        });
        return R.ok(nearlyDeviceVOS);
    }


    @PostMapping("tradeDevices")
    @ApiOperation("最近交易过的设备")
    public R<List<DeviceInfoDto.Vo>> tradeDevices(@RequestBody MemberDeviceDTO.TradeDevices dto) {
        List<DeviceInfoDto.Vo> voList = new ArrayList<>();
        List<Long> deviceIds = R.feignCheckData(ordersMemberService.memberTradeDevices(new MemberOrdersDTO.MemberTradeDevices().setMemberId(AuthorizeUtils.getLoginId(Long.class))));
        if (CollUtil.isEmpty(deviceIds)) {
            return R.ok(voList);
        }
        List<DeviceInfoDto.Vo> vos = R.feignCheckData(deviceInfoService.list(new DeviceInfoDto.ListDto().setDeviceIds(deviceIds)));
        return R.ok(vos);
    }


    @PostMapping("mercDevices")
    @ApiOperation("商户设备")
    public R<List<DeviceInfoDto.MercNormalListVo>> mercDevices(@RequestBody DeviceInfoDto.MercNormalListDto dto){
        if(!Emptys.check(dto.getMercId())){
            return R.ok(new ArrayList<>());
        }
        return deviceInfoService.mercNormalList(new DeviceInfoDto.MercNormalListDto().setMercId(dto.getMercId()));
    }

}
