package com.usky.transfer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.transfer.domain.DmpDevice;
import com.usky.transfer.domain.DmpProduct;
import com.usky.transfer.mapper.DmpProductMapper;
import com.usky.transfer.service.DmpDeviceService;
import com.usky.transfer.service.DmpProductService;
import com.usky.transfer.service.vo.DeviceMapVO;
import com.usky.transfer.service.vo.ProductMapVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 产品信息表 服务实现类
 * </p>
 *
 * @author ya
 * @since 2024-09-19
 */
@Service
public class DmpProductServiceImpl extends AbstractCrudService<DmpProductMapper, DmpProduct> implements DmpProductService {

    @Autowired
    private DmpDeviceService dmpDeviceService;

    //清除产品缓存
    @CacheEvict(cacheNames = "productList")
    public void deleteProductCache(){
        System.out.println("deleteProductCache");
    }

    //清除设备缓存
    @CacheEvict(cacheNames = "deviceList",key = "#productCode")
    public void deleteDeviceCache(String productCode){
        System.out.println("deleteDeviceCache");
    }

    @Cacheable(cacheNames = "productList",sync = true)
    public Map<String, ProductMapVO> getProductMap(){
        Map<String,ProductMapVO> productMap = new HashMap<>();
        LambdaQueryWrapper<DmpProduct> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DmpProduct::getDeleteFlag,0)
                .orderByDesc(DmpProduct::getId);
        List<DmpProduct> list = this.list(queryWrapper);
        if(CollectionUtils.isNotEmpty(list)){
            for (int i = 0; i < list.size(); i++) {
                String productCode = list.get(i).getProductCode();
                ProductMapVO mapVO = new ProductMapVO();
                mapVO.setProductId(list.get(i).getId());
                mapVO.setProductCode(list.get(i).getProductCode());
                mapVO.setCreatedBy(list.get(i).getCreatedBy());
                mapVO.setTenantId(list.get(i).getTenantId());
                mapVO.setDeviceType(list.get(i).getDeviceType());

                productMap.put(productCode,mapVO);
            }
        }


        return productMap;
    }

    @Cacheable(cacheNames = "deviceList",key = "#productCode",sync = true)
    public Map<String, DeviceMapVO> getDeviceMap(String productCode){
        Map<String,DeviceMapVO> deviceMap = new HashMap<>();
        LambdaQueryWrapper<DmpDevice> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DmpDevice::getDeleteFlag,0)
                .eq(DmpDevice::getProductCode,productCode)
                .orderByDesc(DmpDevice::getId);
        List<DmpDevice> list = dmpDeviceService.list(queryWrapper);
        if(CollectionUtils.isNotEmpty(list)){
            for (int i = 0; i < list.size(); i++) {
                String deviceId = list.get(i).getDeviceId();
                DeviceMapVO mapVO = new DeviceMapVO();
                mapVO.setProductCode(list.get(i).getProductCode());
                mapVO.setDeviceId(list.get(i).getDeviceId());
                mapVO.setDeviceUuid(list.get(i).getDeviceUuid());
                mapVO.setDeviceStatus(list.get(i).getServiceStatus());

                deviceMap.put(deviceId,mapVO);
            }
        }

        return deviceMap;
    }
}
