package com.pj.project.relation_goods_type;

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

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pj.project.tb_business.TbBusiness;
import com.pj.project.tb_business.TbBusinessMapper;
import com.pj.project.tb_goods.TbGoods;
import com.pj.project.tb_goods.TbGoodsService;
import com.pj.project.tb_item_type.TbItemType;
import com.pj.project.tb_item_type.TbItemTypeService;
import com.pj.project4sp.global.BusinessException;
import com.pj.utils.so.SoMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pj.utils.sg.*;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * Service: relation_goods_type -- 业务类型与作业项
 *
 * @author qzy
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class RelationGoodsTypeService extends ServiceImpl<RelationGoodsTypeMapper, RelationGoodsType> implements IService<RelationGoodsType> {

    /**
     * 底层 Mapper 对象
     */
    @Autowired
    RelationGoodsTypeMapper relationGoodsTypeMapper;

    @Resource
    private TbGoodsService tbGoodsService;

    @Resource
    private TbItemTypeService tbItemTypeService;


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


    public List<RelationGoodsType> findByGoodsId(String goodsId) {
        QueryWrapper<RelationGoodsType> ew = new QueryWrapper<>();
        ew.eq("goods_id", goodsId);
        return list(ew);
    }

    public void saveBatch(String goodsId, String typeIds) {
        if (StrUtil.isEmpty(typeIds)) {
            throw new BusinessException("请选择业务类型");
        }
        List<String> newTypeIds = StrUtil.splitTrim(typeIds, ",");
        List<RelationGoodsType> exitList = this.findByGoodsId(goodsId);
        List<String> exitTypeIdList = exitList.stream().map(RelationGoodsType::getTypeId).collect(Collectors.toList());
        exitList.stream().filter(relationGoodsType -> !newTypeIds.contains(relationGoodsType.getTypeId()))
                .forEach(relationGoodsType -> this.removeById(relationGoodsType.getId()));
        List<String> list = newTypeIds.stream().filter(typeId -> !exitTypeIdList.contains(typeId)).collect(Collectors.toList());
        if (list.isEmpty()) {
            return;
        }
        TbGoods tbGoods = tbGoodsService.getById(goodsId);
        List<TbItemType> tbItemTypes = tbItemTypeService.listByIds(list);
        tbItemTypes.forEach(tbItemType -> {
            RelationGoodsType relationGoodsType = new RelationGoodsType();
            relationGoodsType.setGoodsName(tbGoods.getName())
                    .setGoodsId(tbGoods.getId())
                    .setTypeId(tbItemType.getId()).setTypeName(tbItemType.getName())
                    .setNeedSelect(1);
            this.save(relationGoodsType);
        });
    }

    public void saveSingle(String goodsId, String typeId) {
        RelationGoodsType relationGoodsType = this.findByGoodsIdAndTypeId(goodsId, typeId);
        if (relationGoodsType != null) {
            throw new BusinessException("已存在关联");
        }
        relationGoodsType = new RelationGoodsType();
        TbItemType tbItemType = tbItemTypeService.getById(typeId);
        TbGoods tbGoods = tbGoodsService.getById(goodsId);
        relationGoodsType.setGoodsName(tbGoods.getName())
                .setGoodsId(tbGoods.getId())
                .setTypeId(tbItemType.getId()).setTypeName(tbItemType.getName())
                .setNeedSelect(1);
        this.save(relationGoodsType);
    }

    private RelationGoodsType findByGoodsIdAndTypeId(String goodsId, String typeId) {
        QueryWrapper<RelationGoodsType> ew = new QueryWrapper<>();
        ew.eq("goods_id", goodsId);
        ew.eq("type_id", typeId);
        return getOne(ew);
    }
}
