package com.pj.project.relation_type_item;

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.relation_goods_type.RelationGoodsType;
import com.pj.project.tb_item.TbItem;
import com.pj.project.tb_item.TbItemMapper;
import com.pj.project.tb_item_type.TbItemType;
import com.pj.project.tb_item_type.TbItemTypeService;
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_type_item --
 *
 * @author qzy
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class RelationTypeItemService extends ServiceImpl<RelationTypeItemMapper, RelationTypeItem> implements IService<RelationTypeItem> {

    /**
     * 底层 Mapper 对象
     */
    @Autowired
    RelationTypeItemMapper relationTypeItemMapper;
    @Resource
    private TbItemTypeService tbItemTypeService;


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

    public List<RelationTypeItem> findByTypeId(String typeId) {
        QueryWrapper<RelationTypeItem> ew = new QueryWrapper<>();
        ew.eq("type_id", typeId);
        return list(ew);
    }


    public void setBatch(String typeId, String itemIds) {
        List<String> itemIdList = StrUtil.splitTrim(itemIds, ",");
        List<RelationTypeItem> list = findByTypeId(typeId);
        TbItemType tbItemType = tbItemTypeService.getById(typeId);
        List<String> removeIds = list.stream().filter(relationTypeItem -> !itemIdList.contains(relationTypeItem.getItemId())).map(RelationTypeItem::getId).collect(Collectors.toList());
        this.removeByIds(removeIds);
        List<String> dbItemIdList = list.stream().map(RelationTypeItem::getItemId).collect(Collectors.toList());
        itemIdList.stream().filter(id -> !dbItemIdList.contains(id)).
                forEach(id -> {
                    RelationTypeItem relationTypeItem = new RelationTypeItem();
                    relationTypeItem.setItemId(id).setTypeId(typeId).setNeed(1).setTypeName(tbItemType.getName());
                    this.save(relationTypeItem);
                });

    }

    public void removeByTypeId(String typeId) {
        QueryWrapper<RelationTypeItem> ew = new QueryWrapper<>();
        ew.eq("type_id", typeId);
        this.remove(ew);
    }

    public void setSingle(String typeId, String itemId) {
        RelationTypeItem relationTypeItem = new RelationTypeItem();
        TbItemType tbItemType = tbItemTypeService.getById(typeId);
        relationTypeItem.setNeed(1).setItemId(itemId).setTypeId(typeId).setTypeName(tbItemType.getName());
        this.save(relationTypeItem);
    }

    public RelationTypeItem findByTypeIdAndItemId(String typeId, String itemId) {
        QueryWrapper<RelationTypeItem> ew = new QueryWrapper<>();
        ew.eq("type_id", typeId);
        ew.eq("item_id", itemId);
        return getOne(ew);
    }

    public void updateStatus(String typeId, String itemId, int need) {
        RelationTypeItem relationTypeItem = this.findByTypeIdAndItemId(typeId, itemId);
        relationTypeItem.setNeed(need);
        this.updateById(relationTypeItem);
    }

    public void updateRemarkMust(String typeId, String itemId, int mustRemark) {
        RelationTypeItem relationTypeItem = this.findByTypeIdAndItemId(typeId, itemId);
        relationTypeItem.setMustRemark(mustRemark);
        this.updateById(relationTypeItem);
    }

    public void updateRemarkNeed(String typeId, String itemId, int needRemark) {
        RelationTypeItem relationTypeItem = this.findByTypeIdAndItemId(typeId, itemId);
        relationTypeItem.setNeedRemark(needRemark);
        this.updateById(relationTypeItem);
    }

    public void updateNum(String typeId, String itemId, int inc) {
        RelationTypeItem relationTypeItem = this.findByTypeIdAndItemId(typeId, itemId);
        relationTypeItem.setInc(inc);
        this.updateById(relationTypeItem);
    }

    public void removeRelation(String id, String typeId) {
        QueryWrapper<RelationTypeItem>ew=new QueryWrapper<>();
        ew.eq("type_id",typeId);
        ew.eq("item_id",id);
        this.remove(ew);
    }

    public List<RelationTypeItem> findByTypeIds(List<String> typeIds) {
        QueryWrapper<RelationTypeItem>ew=new QueryWrapper<>();
        ew.in("type_id",typeIds);
        return list(ew);
    }


}
