package com.fuyuanshen.app.service.impl.equipment;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fuyuanshen.app.domain.APPDevice;
import com.fuyuanshen.app.domain.APPDeviceType;
import com.fuyuanshen.app.domain.dto.APPUnbindDTO;
import com.fuyuanshen.app.domain.query.APPDeviceQueryCriteria;
import com.fuyuanshen.app.mapper.equipment.APPDeviceMapper;
import com.fuyuanshen.app.service.equipment.APPDeviceService;
import com.fuyuanshen.common.core.domain.PageResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: WY
 * @Date: 2025/5/16
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class APPDeviceServiceImpl extends ServiceImpl<APPDeviceMapper, APPDevice> implements APPDeviceService {

    private final APPDeviceMapper appDeviceMapper;


    /**
     * APP用户设备列表
     *
     * @param criteria
     */
    @Override
    public PageResult<APPDevice> appDeviceList(Page<APPDevice> page, APPDeviceQueryCriteria criteria) {
        // criteria.setCustomerId(SecurityUtils.getCurrentUserId());
        // IPage<APPDevice> devices = appDeviceMapper.appDeviceList(page, criteria);
        // return new PageResult<>(devices.getRecords(), devices.getTotal());
        return null;
    }


    /**
     * APP用户设备类型列表
     *
     * @param criteria
     * @return
     */
    @Override
    public List<APPDeviceType> appTypeList(APPDeviceQueryCriteria criteria) {
        // criteria.setCustomerId(SecurityUtils.getCurrentUserId());
        // return appDeviceTypeMapper.appTypeList(criteria);
        return null;
    }


    /**
     * APP/小程序用户设备绑定
     *
     * @param criteria
     */
    @Override
    @Transactional
    public void appBindDevice(APPDeviceQueryCriteria criteria) {
        //
        // List<Device> devices = new ArrayList<>();
        //
        // if (criteria.getCommunicationMode().equals(CommunicationModeEnum.BLUETOOTH.getValue())) {
        //     devices = deviceMapper.selectList(new QueryWrapper<Device>().eq("device_mac", criteria.getDeviceMac()));
        //     if (CollectionUtil.isEmpty(devices)) {
        //         throw new BadRequestException("请先将设备入库！！！");
        //     }
        //     List<APPDevice> appDevices = appDeviceMapper.selectList(new QueryWrapper<APPDevice>()
        //             .eq("device_mac", criteria.getDeviceMac()).eq("binding_type", UserType.APP.getValue()));
        //     if (CollectionUtil.isNotEmpty(appDevices)) {
        //         throw new BadRequestException("该设备已绑定！！！");
        //     }
        // }
        //
        // if (criteria.getCommunicationMode().equals(CommunicationModeEnum.FOUR_G.getValue())) {
        //     devices = deviceMapper.selectList(new QueryWrapper<Device>().eq("device_imei", criteria.getDeviceImei()));
        //     if (CollectionUtil.isEmpty(devices)) {
        //         throw new BadRequestException("请先将设备入库！！！");
        //     }
        //     List<APPDevice> appDevices = appDeviceMapper.selectList(new QueryWrapper<APPDevice>()
        //             .eq("device_imei", criteria.getDeviceImei()).eq("binding_type", UserType.APP.getValue()));
        //     if (CollectionUtil.isNotEmpty(appDevices)) {
        //         throw new BadRequestException("该设备已绑定！！！");
        //     }
        // }
        //
        // Device device = devices.get(0);
        // device.setBindingStatus(BindingStatusEnum.BOUND.getCode());
        // deviceMapper.updateById(device);
        //
        // APPDevice appDevice = new APPDevice();
        // BeanUtil.copyProperties(device, appDevice);
        // appDevice.setBindingType(UserType.APP.getValue());
        // appDevice.setBindingStatus(BindingStatusEnum.BOUND.getCode());
        // Long currentUserId = SecurityUtils.getCurrentUserId();
        // appDevice.setCustomerId(currentUserId);
        // appDevice.setCreateTime(new Timestamp(System.currentTimeMillis()));
        // // 设备类型名称
        // appDevice.setDeviceTypeName(device.getTypeName());
        // appDeviceMapper.insert(appDevice);
        //
        // APPDeviceType appDeviceType = appDeviceTypeMapper.selectById(device.getDeviceType());
        // if (appDeviceType == null) {
        //     DeviceType deviceType = deviceTypeMapper.selectById(device.getDeviceType());
        //     APPDeviceType type = new APPDeviceType();
        //     BeanUtil.copyProperties(deviceType, type);
        //     type.setCustomerId(currentUserId);
        //     appDeviceTypeMapper.insert(type);
        // }

    }


    /**
     * 分页查询APP/小程序设备
     *
     * @param criteria
     * @param page
     * @return
     */
    @Override
    public PageResult<APPDevice> queryAll(Page<APPDevice> page, APPDeviceQueryCriteria criteria) {
        // IPage<APPDevice> devices = appDeviceMapper.queryAll(page, criteria);
        // return new PageResult<>(devices.getRecords(), devices.getTotal());

        return null;
    }


    /**
     * APP/小程序用户设备解绑
     *
     * @param deviceForm
     */
    @Override
    @Transactional
    public void unbindAPPDevice(APPUnbindDTO deviceForm) {
        // QueryWrapper<APPDevice> queryWrapper = new QueryWrapper<>();
        // QueryWrapper<Device> qw = new QueryWrapper<>();
        // if (StringUtils.isNotEmpty(deviceForm.getDeviceMac())) {
        //     queryWrapper.eq("device_mac", deviceForm.getDeviceMac());
        //     qw.eq("device_mac", deviceForm.getDeviceMac());
        // }
        // if (StringUtils.isNotEmpty(deviceForm.getDeviceImei())) {
        //     queryWrapper.eq("device_imei", deviceForm.getDeviceImei());
        //     qw.eq("device_imei", deviceForm.getDeviceImei());
        // }
        // queryWrapper.eq("binding_type", UserType.APP.getValue());
        // APPDevice appDevice = appDeviceMapper.selectOne(queryWrapper);
        // appDeviceMapper.delete(queryWrapper);
        //
        // List<Device> devices = deviceMapper.selectList(qw);
        // List<Long> ids = devices.stream()
        //         .map(Device::getId)
        //         .collect(Collectors.toList());
        // if (CollectionUtil.isNotEmpty(ids)) {
        //     appDeviceTypeMapper.deleteBatchIds(ids);
        // }
        // Device device = new Device();
        // device.setId(appDevice.getId());
        // device.setBindingStatus(BindingStatusEnum.UNBOUND.getCode());
        // deviceMapper.updateById(device);

    }


}
