package com.pj.tb_goods_type;

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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.pj.tb_goods.TbGoods;
import com.pj.tb_goods.TbGoodsMapper;
import com.pj.utils.so.SoMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * Service: tb_goods_type -- 商品分类
 *
 * @author qzy
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class TbGoodsTypeService extends ServiceImpl<TbGoodsTypeMapper, TbGoodsType> implements IService<TbGoodsType> {

    /**
     * 底层 Mapper 对象
     */
    @Autowired
    TbGoodsTypeMapper tbGoodsTypeMapper;
    @Autowired
    TbGoodsMapper tbGoodsMapper;

    /**
     * 增
     */
    void add(TbGoodsType t) {
        save(t);
    }

    /**
     * 删
     */
    void delete(Long id) {
        removeById(id);
    }

    /**
     * 改
     */
    void update(TbGoodsType t) {
        updateById(t);

    }

    /**
     * 查
     */
    TbGoodsType getById(String id) {
        return super.getById(id);
    }

    /**
     * 查集合 - 根据条件（参数为空时代表忽略指定条件）
     */
    List<TbGoodsType> getList(SoMap so) {
        return tbGoodsTypeMapper.getList(so);
    }

    /**
     * 向商品分类添加商品
     *
     * @author loovi
     * @date
     */
    public int addGoodsToGoodsType(Long goodsTypeId, Long[] goodsIds) {
        // 获取当前登录用户id
//		Long loginId = StpUserUtil.getLoginIdAsLong();
        int line = 0;
        // 获取商品类型信息
        com.pj.tb_goods_type.TbGoodsType tbGoodsType = tbGoodsTypeMapper.selectById(goodsTypeId);
        // 如果商品类型不存在则抛异常
        if (Objects.isNull(tbGoodsType)) {
            throw new RuntimeException("该商品类型不存在");
        }
        List<Long> list = Arrays.asList(goodsIds);
        // 把商品与类型关联id写入商品类型商品关系表
        for (Long goodsId : list) {
            TbGoods tbGoods = tbGoodsMapper.selectById(goodsId);
            if (Objects.isNull(tbGoods)) {
                throw new RuntimeException("该商品不存在");
            }
            int i = tbGoodsTypeMapper.addGoodsToGoodsType(goodsTypeId, goodsId);
            line = i + 1;
        }
        return line;

    }

    /**
     * 删除商品分类中的商品
     *
     * @author loovi
     * @date
     */
    public int delGoodsFromGoodsType(Long goodsTypeId, Long[] goodsIds) {

        // 获取当前登录用户id
//		Long loginId = StpUserUtil.getLoginIdAsLong();
        int line = 0;
        // 获取商品类型信息
        com.pj.tb_goods_type.TbGoodsType tbGoodsType = tbGoodsTypeMapper.selectById(goodsTypeId);
        // 如果商品类型不存在则抛异常
        if (Objects.isNull(tbGoodsType)) {
            throw new RuntimeException("该商品类型不存在");
        }
        List<Long> list = Arrays.asList(goodsIds);
        // 从商品类型商品关系表把商品与类型关联删除
        for (Long goodsId : list) {
            TbGoods tbGoods = tbGoodsMapper.selectById(goodsId);
            if (Objects.isNull(tbGoods)) {
                throw new RuntimeException("该商品不存在");
            }
            int i = tbGoodsTypeMapper.delGoodsFromGoodsType(goodsTypeId, goodsId);
            line = i + 1;
        }
        return line;
    }

    public List<Long> getGoodsIds(String typeCode) {
        LambdaQueryWrapper<TbGoods> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TbGoods::getThrItemCode, typeCode);
        List<TbGoods> goodsList = tbGoodsMapper.selectList(wrapper);

        List<Long> goodsIds = goodsList.stream().map(TbGoods::getId).collect(Collectors.toList());
        return goodsIds;
    }

    public TbGoodsType findByCode(String typeCode) {
        LambdaQueryWrapper<TbGoodsType> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TbGoodsType::getNo, typeCode);
        List<TbGoodsType> typeList = this.list(wrapper);
        return typeList.isEmpty() ? null : typeList.get(0);
    }
}
