package com.fzm.mall.server.admin.goods_center.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzm.mall.server.admin.constant.MallResponseError;
import com.fzm.mall.server.admin.exception.MyException;
import com.fzm.mall.server.admin.goods_center.enums.status.CommonStatusEnum;
import com.fzm.mall.server.admin.goods_center.model.CategorySpu;
import com.fzm.mall.server.admin.goods_center.mapper.CategorySpuMapper;
import com.fzm.mall.server.admin.goods_center.service.ICategoryService;
import com.fzm.mall.server.admin.goods_center.service.ICategorySpuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @author wangp
 * @date 2021/3/4 16:06
 * @description 商品类别与商品对应表 服务实现类
 * @since JDK 1.8
 */
@Service
@Transactional
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CategorySpuServiceImpl extends ServiceImpl<CategorySpuMapper, CategorySpu> implements ICategorySpuService {

    private final ICategoryService categoryService;

    @Override
    public void add(JSONArray ids, String goodsId, String merchantId) {
        Integer status = CommonStatusEnum.EFFECTIVE.getStatus();

        List<CategorySpu> categorySpuList = ids.stream().map(v -> {
            CategorySpu categorySpu = new CategorySpu();
            categorySpu.setGoodsId(goodsId);
            categorySpu.setCategoryId(v.toString());
            categorySpu.setStatus(status);
            categorySpu.setMerchantId(merchantId);
            return categorySpu;
        }).collect(Collectors.toList());

        if (!saveBatch(categorySpuList)) {
            throw new MyException(MallResponseError.SAVE_ERROR);
        }
    }

    @Override
    public void delByGoodsIds(String goodsId) {
        LambdaQueryWrapper<CategorySpu> qw = new LambdaQueryWrapper<>();
        qw.eq(CategorySpu::getGoodsId, goodsId);
        remove(qw);
    }

    @Override
    public String getByGoodsId(String goodsId) {
        Integer status = CommonStatusEnum.EFFECTIVE.getStatus();

        QueryWrapper<CategorySpu> qw = new QueryWrapper<>();
        qw.lambda()
                .eq(CategorySpu::getGoodsId, goodsId)
                .eq(CategorySpu::getStatus, status);
        List<CategorySpu> categorySpuList = list(qw);

        StringBuilder categoryString = new StringBuilder();

        categorySpuList.forEach(v ->
                categoryString
                        .append(categoryService.getById(v.getCategoryId()).getName())
                        .append(">"));

        return StringUtils.isBlank(categoryString) ? "" : categoryString.substring(0, categoryString.lastIndexOf(">"));
    }

    @Override
    public String getByGoodsIdForId(String goodsId) {
        Integer status = CommonStatusEnum.EFFECTIVE.getStatus();

        QueryWrapper<CategorySpu> qw = new QueryWrapper<>();
        qw.lambda()
                .eq(CategorySpu::getGoodsId, goodsId)
                .eq(CategorySpu::getStatus, status);
        List<CategorySpu> categorySpuList = list(qw);

        StringBuilder categoryString = new StringBuilder();

        JSONArray jsonArray = new JSONArray();

        categorySpuList.forEach(v -> jsonArray.add(v.getCategoryId()));

        return jsonArray.toJSONString();
    }

    @Override
    public void removeByGoodsId(String goodsId) {
        LambdaQueryWrapper<CategorySpu> qw = new LambdaQueryWrapper<>();
        qw.eq(CategorySpu::getGoodsId, goodsId);
        remove(qw);
    }
}
