package com.usky.ems.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.ems.domain.DmpProduct;
import com.usky.ems.domain.EmsDataUploadShanghaiProduct;
import com.usky.ems.enums.ValueTypeEnum;
import com.usky.ems.mapper.DmpProductMapper;
import com.usky.ems.mapper.EmsDataUploadShanghaiProductMapper;
import com.usky.ems.service.DmpProductService;
import com.usky.ems.service.vo.ProductFunctionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 产品信息表 服务实现类（service-ems）
 */
@Service
public class DmpProductServiceImpl extends AbstractCrudService<DmpProductMapper, DmpProduct> implements DmpProductService {

    @Autowired
    private EmsDataUploadShanghaiProductMapper shanghaiProductMapper;

    @Override
    public List<ProductFunctionVO> getFunctions(Long id, ValueTypeEnum valueTypeEnum) {
        // 参照原有实现：valueTypeEnum 为空返回全部，否则按值类型过滤
        if (valueTypeEnum == null) {
            LambdaQueryWrapper<EmsDataUploadShanghaiProduct> wrapper =
                    Wrappers.<EmsDataUploadShanghaiProduct>lambdaQuery()
                            .eq(EmsDataUploadShanghaiProduct::getProductId, id)
                            .orderByAsc(EmsDataUploadShanghaiProduct::getIdentifier)
                            .orderByDesc(EmsDataUploadShanghaiProduct::getUpdateTime);

            List<EmsDataUploadShanghaiProduct> list = shanghaiProductMapper.selectList(wrapper);
            return list.stream()
                    .map(item -> new ProductFunctionVO(
                            item.getId(),
                            item.getProductId(),
                            item.getIdentifier(),
                            item.getCustomIdentifier()
                    ))
                    .collect(Collectors.toList());
        }

        // 预留：按值类型过滤，目前表结构中暂无 value_type 字段，仅根据 productId 查询
        return getByProductIdAndValueType(id, valueTypeEnum.getValue());
    }

    /**
     * 根据产品 ID 与值类型获取功能列表。
     * 当前实现仅按 productId 过滤，后续可在 ems_data_upload_shanghai_product 表中增加 value_type 字段后扩展。
     */
    private List<ProductFunctionVO> getByProductIdAndValueType(Long id, Short valueType) {
        LambdaQueryWrapper<EmsDataUploadShanghaiProduct> wrapper =
                Wrappers.<EmsDataUploadShanghaiProduct>lambdaQuery()
                        .eq(EmsDataUploadShanghaiProduct::getProductId, id)
                        .orderByAsc(EmsDataUploadShanghaiProduct::getIdentifier)
                        .orderByDesc(EmsDataUploadShanghaiProduct::getUpdateTime);

        List<EmsDataUploadShanghaiProduct> list = shanghaiProductMapper.selectList(wrapper);
        return list.stream()
                .map(item -> new ProductFunctionVO(
                        item.getId(),
                        item.getProductId(),
                        item.getIdentifier(),
                        item.getCustomIdentifier()
                ))
                .collect(Collectors.toList());
    }
}

