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(String goodsId) {
        List<RelationGoodsType> relationGoodsTypes = relationGoodsTypeService.findByGoodsId(goodsId);
        if (relationGoodsTypes.isEmpty()) {
            return Collections.emptyList();
        }
        List<TbItemType> itemTypeList = new ArrayList<>();
        for (RelationGoodsType relationGoodsType : relationGoodsTypes) {
            String typeId = relationGoodsType.getTypeId();
            List<RelationTypeItem> list = relationTypeItemService.findByTypeId(typeId);
            List<String> itemIds = list.stream().map(RelationTypeItem::getItemId).collect(Collectors.toList());

            TbItemType tbItemType = this.getById(typeId);
            tbItemType.setNeed(relationGoodsType.getNeedSelect());
            List<TbItem> items =new ArrayList<>();
            if (!itemIds.isEmpty()){
                items= tbItemService.listByIds(itemIds);
            }
            for (TbItem item : items) {
                list.stream()
                        .filter(relationTypeItem -> relationTypeItem.getItemId().equals(item.getId()) && relationTypeItem.getTypeId().equals(typeId))
                        .findFirst().ifPresent(relationTypeItem -> {
                    item.setInc(relationTypeItem.getInc()).setNeedRemark(relationTypeItem.getNeedRemark()).setTypeId(relationGoodsType.getTypeId());
                });
            }
            tbItemType.setItems(items);
            itemTypeList.add(tbItemType);
        }
        return itemTypeList;
    }
}
