package com.corewing.app.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.corewing.app.dto.DeviceActivationRequest;
import com.corewing.app.entity.BizDevice;
import com.corewing.app.entity.BizDeviceActivation;
import com.corewing.app.entity.BizDeviceCategory;
import com.corewing.app.mapper.BizDeviceActivationMapper;
import com.corewing.app.service.BizDeviceActivationService;
import com.corewing.app.service.BizDeviceCategoryService;
import com.corewing.app.service.BizDeviceService;
import com.corewing.app.util.CommonServletUtil;
import com.corewing.app.util.IpUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class BizDeviceActivationServiceImpl extends ServiceImpl<BizDeviceActivationMapper, BizDeviceActivation> implements BizDeviceActivationService {

    @Resource
    private BizDeviceService deviceService;

    @Resource
    private BizDeviceCategoryService categoryService;

    @Override
    public Boolean activation(DeviceActivationRequest deviceActivationRequest) {
        Object loginId = StpUtil.getLoginId();
        LambdaQueryWrapper<BizDeviceActivation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BizDeviceActivation::getDeviceMac, deviceActivationRequest.getMac());
        BizDeviceActivation checkDeviceActivation = getOne(wrapper);
        if (checkDeviceActivation != null) {
            // 该用户已经激活过该设备
            if (checkDeviceActivation.getUserId().equals(loginId)) {
                return true;
            }
            return updateById(checkDeviceActivation);
        }

        // 校验是否为酷翼飞控设备
        LambdaQueryWrapper<BizDevice> checkDeviceWrapper = new LambdaQueryWrapper<>();
        checkDeviceWrapper.eq(BizDevice::getDeviceMac, deviceActivationRequest.getMac());
        List<BizDevice> list = deviceService.list(checkDeviceWrapper);
        if (list.isEmpty()) {
            throw new RuntimeException("该设备不是酷翼官方产品");
        }

        BizDevice bizDevice = list.get(0);

        // 执行新设备绑定激活
        BizDeviceActivation deviceActivation = new BizDeviceActivation();
        deviceActivation.setDeviceMac(bizDevice.getDeviceMac());
        deviceActivation.setActivationIp(IpUtil.getClientIp(CommonServletUtil.getRequest()));
        deviceActivation.setActivationTime(new Date());
        deviceActivation.setUserId(Long.valueOf(loginId.toString()));
        deviceActivation.setDeviceId(bizDevice.getId());
        return this.saveOrUpdate(deviceActivation);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean factoryActivation(DeviceActivationRequest deviceActivationRequest) {
        BizDeviceActivation checkDeviceActivation = getBizDeviceActivationByMac(deviceActivationRequest.getMac());
        if (checkDeviceActivation != null) {
            return false;
        }

        LambdaQueryWrapper<BizDeviceCategory> findWrapper = new LambdaQueryWrapper<>();
        findWrapper.eq(BizDeviceCategory::getModelId, deviceActivationRequest.getModelId());
        BizDeviceCategory deviceCategory = categoryService.getOne(findWrapper);

        BizDevice device = new BizDevice();
        device.setDeviceMac(deviceActivationRequest.getMac());
        device.setStatus("0");
        device.setPublicStatus("1");
        device.setCategoryId(deviceCategory.getId());
        return deviceService.save(device);
    }

    /**
     * 根据mac地址获取激活设备
     *
     * @param mac mac地址
     * @return 激活设备
     */
    private BizDeviceActivation getBizDeviceActivationByMac(String mac) {
        LambdaQueryWrapper<BizDeviceActivation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BizDeviceActivation::getDeviceMac, mac);
        return getOne(wrapper);
    }
}
