package com.storlead.sales.service.impl.sku;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.storlead.framework.common.constant.DSConstants;
import com.storlead.framework.common.result.Result;
import com.storlead.sales.pojo.entity.sku.SsdProductType;
import com.storlead.sales.mapper.sku.SsdProductTypeMapper;
import com.storlead.sales.service.sku.SsdProductTypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@DS(DSConstants.DATASOURCE_MANAGEMENT)
public class SsdProductTypeServiceImpl extends ServiceImpl<SsdProductTypeMapper, SsdProductType> implements SsdProductTypeService {

    @Resource
    private SsdProductServiceImpl ssdProductService;
    /**
     * 分页查询SSD 产品列表
     *
     * @param ssdProductType 请求参数
     * @return com.storlead.common.util.Result
     * @author YPZ
     * @since 2024-07-31 下午 3:50
     */
    @Override
    public Result queryProductTypeList(SsdProductType ssdProductType) {

        List<SsdProductType> list = baseMapper.queryProductTypeListAll();
        //更新在线SSD产品数量
        list.forEach(ssdProductTypePageQueryVO -> {
            Integer subProductCount = ssdProductService.getSubProductCount(ssdProductTypePageQueryVO.getSsdProductTypeId());
            ssdProductTypePageQueryVO.setSubProductCount(subProductCount);
        });

        Map<String, Object> resultMap = new HashMap<>(2);
        resultMap.put("totalCount", list.size());
        resultMap.put("productTypeList", list);

        return Result.result(resultMap);
    }

    @Override
    public Result getProductType(Integer productTypeId) throws IllegalAccessError, RuntimeException {
        SsdProductType ssdProduct = getById(productTypeId);

        if (ssdProduct == null) {
            return Result.ok();
        }
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("productType", ssdProduct);

        return Result.result(resultMap);
    }

}
