package com.yiqi.core.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yiqi.common.utils.StringUtils;
import com.yiqi.core.domain.GoodsSpec;
import com.yiqi.core.mapper.GoodsSpecMapper;
import com.yiqi.core.service.IGoodsSpecService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商品管理Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-21
 */
@Service
public class GoodsSpecServiceImpl extends ServiceImpl<GoodsSpecMapper, GoodsSpec> implements IGoodsSpecService {

    private final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public List<GoodsSpec> selectByGoodsId(Long goodsId) {
        List<GoodsSpec> goodsSpecList = baseMapper.selectByGoodsId(goodsId);
        // 设置规格值
        goodsSpecList.forEach(goodsSpec -> {
            //values字符串数组转List
            if (StringUtils.isNotBlank(goodsSpec.getVals())) {
                try {
                    List<String> specValues = objectMapper.readValue(goodsSpec.getVals(), new TypeReference<List<String>>() {});
                    goodsSpec.setSpecValues(specValues);
                } catch (Exception e) {
                    // 处理异常情况
                    e.printStackTrace();
                }
            }
        });
        return goodsSpecList;
    }

}

