package com.mortals.xhx.module.device.service.impl;

import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.model.PageInfo;
import com.mortals.xhx.module.device.model.DeviceEntity;
import com.mortals.xhx.module.device.service.DeviceService;
import com.mortals.xhx.module.platform.model.PlatformEntity;
import com.mortals.xhx.module.platform.service.PlatformService;
import com.mortals.xhx.module.product.model.ProductEntity;
import com.mortals.xhx.module.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.xhx.module.device.dao.DeviceModuleUseDao;
import com.mortals.xhx.module.device.model.DeviceModuleUseEntity;
import com.mortals.xhx.module.device.service.DeviceModuleUseService;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * DeviceModuleUseService
 * 设备模块使用频率 service实现
 *
 * @author zxfei
 * @date 2022-07-02
 */
@Service("deviceModuleUseService")
public class DeviceModuleUseServiceImpl extends AbstractCRUDServiceImpl<DeviceModuleUseDao, DeviceModuleUseEntity, Long> implements DeviceModuleUseService {

    @Autowired
    private PlatformService platformService;
    @Autowired
    private ProductService productService;
    @Autowired
    private DeviceService deviceService;

    @Override
    protected void findAfter(DeviceModuleUseEntity params, PageInfo pageInfo, Context context, List<DeviceModuleUseEntity> list) throws AppException {
        list.stream().forEach(item -> {
            DeviceEntity deviceEntity = deviceService.getExtCache(item.getDeviceId().toString());
            if (!ObjectUtils.isEmpty(deviceEntity)) {
                PlatformEntity platformEntity = platformService.get(deviceEntity.getPlatformId());
                ProductEntity productEntity = productService.get(deviceEntity.getProductId());
                if (!ObjectUtils.isEmpty(platformEntity) && !ObjectUtils.isEmpty(productEntity)) {
                    item.setPlatformAndProductName(platformEntity.getPlatformName() + "/" + productEntity.getProductName());
                }
            }
        });
        super.findAfter(params, pageInfo, context, list);
    }
}