package com.backendsys.service.B2c;

import com.backendsys.entity.B2c.B2cGoodSpecSkuDTO;
import com.backendsys.mapper.B2c.B2cGoodSpecMapper;
import com.backendsys.mapper.B2c.B2cGoodSpecSkuMapper;
import com.backendsys.utils.response.PageInfoResult;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class B2CGoodSpecSkuServiceImpl implements B2cGoodSpecSkuService {

    @Autowired
    private B2cGoodSpecMapper b2cGoodSpecMapper;

    @Autowired
    private B2cGoodSpecSkuMapper b2cGoodSpecSkuMapper;

    /**
     * 查询 规格列表
     */
    public Map<String, Object> queryGoodSpecSkuList(Integer pageNum, Integer pageSize, B2cGoodSpecSkuDTO b2cGoodSpecSkuDTO) {
        // 分页查询
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
            PageHelper.getLocalPage().setPageSizeZero(true);
        }
        // 分页输出 (自定义)
        //  List<Map<String, Object>> list = b2cGoodSpecMapper.queryGoodSpecList(b2cGoodSpecDTO);
        List<Map<String, Object>> list = b2cGoodSpecSkuMapper.queryGoodSpecSkuList(b2cGoodSpecSkuDTO);

        // 格式化可能不能在这里做
        for (Map<String, Object> map : list) {
            String spec_ids_string = (String) map.get("spec_ids");
            List<Integer> spec_ids = Arrays.stream(spec_ids_string.split(","))
                    .map(Integer::valueOf)
                    .collect(Collectors.toList());
            List<Map<String, Object>> spec_sku = b2cGoodSpecMapper.queryGoodSpecByIds(spec_ids);
            map.put("spec_sku", spec_sku);
        }
        //

        PageInfoResult pageInfoResult = new PageInfoResult(list);
        return pageInfoResult.toMap();
    }

    /**
     * 批量查询 规格列表 (根据 sku_ids)
     */
    public List<Map<String, Object>> queryGoodSpecSkuBatch(Long[] sku_ids) {
        return b2cGoodSpecSkuMapper.queryGoodSpecSkuBatch(sku_ids);
    }

    /**
     * 查询 详情
     */
    @Override
    public Map<String, Object> queryGoodSpecSkuDetail(Long good_id) {
        return b2cGoodSpecSkuMapper.queryGoodSpecSkuDetail(good_id);
    }

    /**
     * 创建
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> insertGoodSpecSku(B2cGoodSpecSkuDTO b2cGoodSpecSkuDTO) {
        b2cGoodSpecSkuMapper.insertGoodSpecSku(b2cGoodSpecSkuDTO);
        return Map.of("good_spec_sku_id", b2cGoodSpecSkuDTO.getGood_spec_sku_id());
    }

    /**
     * 更新
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> updateGoodSpecSku(B2cGoodSpecSkuDTO b2cGoodSpecSkuDTO) {
        b2cGoodSpecSkuMapper.updateGoodSpecSku(b2cGoodSpecSkuDTO);
        return Map.of("good_spec_sku_id", b2cGoodSpecSkuDTO.getGood_spec_sku_id());
    }

    /**
     * 删除
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> deleteGoodSpecSku(B2cGoodSpecSkuDTO b2cGoodSpecSkuDTO) {
        b2cGoodSpecSkuMapper.deleteGoodSpecSku(b2cGoodSpecSkuDTO);
        return Map.of("good_spec_sku_id", b2cGoodSpecSkuDTO.getGood_spec_sku_id());
    }
}
