package com.pj.project.tb_item_type;

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

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pj.constants.RoleEnum;
import com.pj.constants.UserTypeEnum;
import com.pj.current.satoken.StpUserUtil;
import com.pj.project.relation_goods_type.RelationGoodsType;
import com.pj.project.relation_goods_type.RelationGoodsTypeService;
import com.pj.project.relation_type_item.RelationTypeItem;
import com.pj.project.relation_type_item.RelationTypeItemService;
import com.pj.project.tb_costomer.TbCostomer;
import com.pj.project.tb_costomer.TbCostomerService;
import com.pj.project.tb_item.TbItem;
import com.pj.project.tb_item.TbItemService;
import com.pj.utils.so.SoMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pj.utils.sg.*;

import javax.annotation.Resource;

/**
 * Service: tb_item_type -- 业务项类型
 *
 * @author qzy
 */
@Service
public class TbItemTypeService extends ServiceImpl<TbItemTypeMapper, TbItemType> implements IService<TbItemType> {

    /**
     * 底层 Mapper 对象
     */
    @Autowired
    TbItemTypeMapper tbItemTypeMapper;
    @Resource
    TbItemService tbItemService;
    @Resource
    private RelationTypeItemService relationTypeItemService;

    @Resource
    private TbCostomerService tbCostomerService;

    @Resource
    private RelationGoodsTypeService relationGoodsTypeService;


    /**
     * 删
     */
    void delete(String id) {
        this.removeById(id);
        relationTypeItemService.removeByTypeId(id);
    }


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

    /**
     * 查集合 - 根据条件（参数为空时代表忽略指定条件）
     */
    public List<TbItemType> getList(SoMap so) {
        List<TbItemType> list = tbItemTypeMapper.getList(so);
        String customerId = StpUserUtil.getCustomerId();
        if (!UserTypeEnum.PLATFORM_ADMIN.getCustomerId().equals(customerId)) {
            TbCostomer tbCostomer = tbCostomerService.getById(customerId);
            if (!TbCostomer.CustomerEnum.BUSINESS_TYPE.getType().equals(tbCostomer.getType())) {
                list = list.stream().filter(tbItemType -> {
                    List<TbItem> items = tbItemService.getItemTypeById(tbItemType.getId());
                    List<String> businessTypeList = items.stream().map(TbItem::getBusinessType).collect(Collectors.toList());
                    List<String> typeList = StrUtil.splitTrim(tbCostomer.getType(), ",");
                    return businessTypeList.stream().anyMatch(typeList::contains);
                }).collect(Collectors.toList());
            }
        }
        return list;
    }


    public TbItemType getItemTypeById(String id) {
        TbItemType tbItemType = super.getById(id);
        List<TbItem> items = tbItemService.getItemTypeById(id);
        tbItemType.setItems(items);
        return tbItemType;
    }

    public void updateItemType(TbItemType t) {
        List<RelationTypeItem> relationTypeItems = relationTypeItemService.findByTypeId(t.getId());
        relationTypeItems.forEach(relationTypeItem -> {
            relationTypeItem.setTypeName(t.getName());
            relationTypeItemService.updateById(relationTypeItem);
        });

        super.updateById(t);
    }

    public List<TbItemType> getTypeByGoodsId(Long goodsId) {
        List<RelationGoodsType> relationGoodsTypes = relationGoodsTypeService.findByGoodsId(goodsId);
        if (relationGoodsTypes.isEmpty()) {
            return Collections.emptyList();
        }
        List<String> typeIds = relationGoodsTypes.stream().map(RelationGoodsType::getTypeId).collect(Collectors.toList());
        List<RelationTypeItem> relationTypeItems = relationTypeItemService.findByTypeIds(typeIds);
        List<TbItemType>itemTypeList=new ArrayList<>();
        relationTypeItems.stream().collect(Collectors.groupingBy(RelationTypeItem::getTypeId))
                .forEach((typeId, list) -> {
                    TbItemType tbItemType = this.getById(typeId);
                    List<String> itemIds = list.stream().map(RelationTypeItem::getItemId).collect(Collectors.toList());
                    List<TbItem> items = tbItemService.listByIds(itemIds);
                    for (TbItem item:items){
                        relationTypeItems.stream()
                                .filter(relationTypeItem -> relationTypeItem.getItemId().equals(item.getId())&&relationTypeItem.getTypeId().equals(typeId))
                                .findFirst().ifPresent(relationTypeItem -> {
                            System.out.println(1111);
                            item.setInc(relationTypeItem.getInc());
                        });
                    }
                    if (items.size()==1){
                        TbItem tbItem=items.get(0);
                        tbItemType.setItemId(tbItem.getId()).setItemName(tbItem.getItemName());
                    }
                    tbItemType.setItems(items);
                    itemTypeList.add(tbItemType);
                });

        return itemTypeList;
    }
}
