package com.org.hktshop.service.hotking.impl;

import com.alibaba.fastjson.JSON;
import com.org.hktshop.Interface.brand.IHktBrandService;
import com.org.hktshop.Interface.portal.product.IPortalProductService;
import com.org.hktshop.base.BaseService;
import com.org.hktshop.base.MongoHelper;
import com.org.hktshop.entity.Page;
import com.org.hktshop.entity.hotking.BrandCategory;
import com.org.hktshop.entity.hotking.HktBrandGoodsAttrQueryItem;
import com.org.hktshop.entity.product.HktGoodItem;
import com.org.hktshop.entity.product.HktGoodsAttrItem;
import com.org.hktshop.entity.vo.HktBrandInfo;
import com.org.hktshop.service.hotking.BrandService;
import com.org.hktshop.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author hotking03
 */
@Service
public class BrandServiceImpl extends BaseService<BrandCategory> implements BrandService {
    @Autowired
    private IHktBrandService iHktBrandService;
    @Autowired
    private MongoHelper<BrandCategory> brandCategoryMongoHelper;
    @Autowired
    private IPortalProductService portalProductService;

    MongoHelper<HktBrandGoodsAttrQueryItem> hktBrandGoodsAttrQueryItemMongoHelper = new MongoHelper<>();
    @Override
    public Object queryBrandList(BrandCategory brandCategory) {
        List<BrandCategory> brandCategories;
        Criteria criteria = new Criteria();
        if(!StringUtils.isNull(brandCategory.getParentId())){
            criteria.andOperator(Criteria.where("parentId").is(brandCategory.getParentId()));
        }else {
            criteria.andOperator(Criteria.where("parentId").is(0));
        }
        if(!StringUtils.isNull(brandCategory.getBrandHeader())){
            criteria.and("brandHeader").regex(brandCategory.getBrandHeader()+".*","i");
        }
            Query query = new Query(criteria);
            brandCategories = brandCategoryMongoHelper.find(query, BrandCategory.class);


        List<BrandCategory> collect = brandCategories.stream().sorted(Comparator.comparingInt(x -> x.getList().size())).collect(Collectors.toList());
        Collections.reverse(collect);
        return collect;
    }

    @Override
    public void init() {
        List<BrandCategory> brandCategories = brandCategoryMongoHelper.find(new BrandCategory(), BrandCategory.class);
        if(brandCategories != null && brandCategories.size() > 0){
            return;
        }
        List<HktBrandInfo> hktBrandInfos = iHktBrandService.queryLists(true);
        for(HktBrandInfo hb : hktBrandInfos){
            BrandCategory bc = new BrandCategory();
            String info = hb.getBrandDesc_ch().getBrandId();
            HktGoodItem hktGoodItem = new HktGoodItem();
            hktGoodItem.setLanguage("1");
            hktGoodItem.setPageNum(1);
            hktGoodItem.setPageSize(1000);
            hktGoodItem.setBrandId(info);
            BrandCategory bc2 = new BrandCategory();
            bc2.setBrandId(info);
            bc2.setBrandHeader(hb.getBrandDesc_ch().getBrandHead());
            bc2.setBrandName(hb.getBrandDesc_ch().getName());
            bc.setBrandId(info);
            bc.setBrandHeader(hb.getBrandDesc_ch().getBrandHead());
            bc.setBrandName(hb.getBrandDesc_ch().getName());
            List<Map<String, Object>> mapList = portalProductService.prodectCategory(hktGoodItem);
            List<Map<String,Object>> list = new ArrayList<>();
            for(Map<String, Object> map : mapList){
                List<Map<String,Object>> list2 = new ArrayList<>();
                Object sub = map.get("sub");
                List<Map> mapList1 = JSON.parseArray(JSON.toJSONString(sub), Map.class);
                for(Map m : mapList1){
                    Map<String,Object> map1 = new HashMap<>();
                    map1.put("id",m.get("id"));
                    map1.put("count",m.get("count"));
                    map1.put("name",m.get("name"));
                    map1.put("bcId",map.get("id").toString());
                    list.add(map1);
                    list2.add(map1);
                }
                bc.setBcId(map.get("id").toString());
                bc.setParentId(0);
                bc2.setParentId(Integer.parseInt(map.get("id").toString()));
                bc2.setList(list2);
                bc2.setId(UUID.randomUUID().toString());
                if(bc2.getList() != null && bc2.getList().size() > 0){
                    brandCategoryMongoHelper.save(bc2);
                }
            }
            bc.setList(list);
            bc.setId(UUID.randomUUID().toString());
            if(bc.getList() != null && bc.getList().size() > 0){
                brandCategoryMongoHelper.save(bc);
            }
        }
    }

    @Override
    public Object branList(BrandCategory brandCategory) {
        return brandCategoryMongoHelper.find(brandCategory, BrandCategory.class);
    }


    @Override
    public void initBrandGoodsAttr() {
        /*List<BrandCategory> brandCategories = brandCategoryMongoHelper.find(new BrandCategory(), BrandCategory.class);
        if(brandCategories != null && brandCategories.size() > 0){
            return;
        }*/
        List<HktBrandInfo> hktBrandInfos = iHktBrandService.queryLists(true);
        List<HktBrandGoodsAttrQueryItem> hktBrandGoodsAttrQueryItems = hktBrandGoodsAttrQueryItemMongoHelper.find(new HktBrandGoodsAttrQueryItem(), HktBrandGoodsAttrQueryItem.class);
        if(hktBrandGoodsAttrQueryItems != null && hktBrandGoodsAttrQueryItems.size() > 0){
            return;
        }

        for(HktBrandInfo hb : hktBrandInfos) {
            String info = hb.getBrandDesc_ch().getBrandId();

            HktGoodItem hktGoodItem = new HktGoodItem();
            hktGoodItem.setLanguage("1");
            hktGoodItem.setBrandId(info);
            List<HktGoodItem> records = portalProductService.selectProductListByBrandId(hktGoodItem);
            if(records.size() == 0){
                continue;
            }
            List<HktBrandGoodsAttrQueryItem> list = new ArrayList<>();
            Map<String,List<HktGoodsAttrItem>> map = new HashMap<>();

            for(HktGoodItem hg : records){
                HktGoodsAttrItem hktGoodsAttrItem = new HktGoodsAttrItem();
                hktGoodsAttrItem.setAttrId("封装");
                hktGoodsAttrItem.setAttrValue(hg.getPackageSpecification());
                hktGoodsAttrItem.setSort("0");
                hg.getAttrs().add(hktGoodsAttrItem);
                if(map.containsKey(hg.getCategoryId())){
                    map.get(hg.getCategoryId()).addAll(hg.getAttrs());
                }else {
                    List<HktGoodsAttrItem> objects = new ArrayList<>(hg.getAttrs());
                    map.put(hg.getCategoryId(),objects);
                }
            }
            Iterator<Map.Entry<String, List<HktGoodsAttrItem>>> iterator = map.entrySet().iterator();
            while (iterator.hasNext()){
                Map.Entry<String, List<HktGoodsAttrItem>> next = iterator.next();
                List<HktGoodsAttrItem> value = next.getValue();
                Map<String,Set<String>> mapSet = new TreeMap<>();
                for(HktGoodsAttrItem hgai : value){
                    if(mapSet.containsKey(hgai.getAttrId())){
                        mapSet.get(hgai.getAttrId()).add(hgai.getAttrValue());
                    }else {
                        Set<String> set = new TreeSet<>();
                        set.add(hgai.getAttrValue());
                        mapSet.put(hgai.getAttrId(),set);
                    }
                }
                Iterator<Map.Entry<String, Set<String>>> iterator1 = mapSet.entrySet().iterator();
                while (iterator1.hasNext()){
                    Map.Entry<String, Set<String>> next1 = iterator1.next();
                    HktBrandGoodsAttrQueryItem hktBrandGoodsAttrQueryItem = new HktBrandGoodsAttrQueryItem();
//                    hktBrandGoodsAttrQueryItem.setCategoryId(hg);
                    hktBrandGoodsAttrQueryItem.setName(next1.getKey());
                    hktBrandGoodsAttrQueryItem.setLanguageId("1");
                    hktBrandGoodsAttrQueryItem.setBrandId(info);
                    hktBrandGoodsAttrQueryItem.setCategoryId(next.getKey());
                    hktBrandGoodsAttrQueryItem.setId(UUID.randomUUID().toString().replace("-",""));
                    hktBrandGoodsAttrQueryItem.setAttrs(next1.getValue());
                    list.add(hktBrandGoodsAttrQueryItem);
                }
            }
            hktBrandGoodsAttrQueryItemMongoHelper.save(list);
        }
    }
}
