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

import com.org.hktshop.Interface.product.IHktGoodsCategoryService;
import com.org.hktshop.base.BaseService;
import com.org.hktshop.base.MongoHelper;
import com.org.hktshop.entity.Page;
import com.org.hktshop.entity.hotking.SampleGoodsAttrQuery;
import com.org.hktshop.entity.product.HktGoodItem;
import com.org.hktshop.entity.product.HktGoods;
import com.org.hktshop.entity.product.HktGoodsCategory;
import com.org.hktshop.service.hotking.SampleGoodsAttrQueryService;
import com.org.hktshop.service.hotking.SampleService;
import com.org.hktshop.utils.GoodsUtils;
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.*;

/**
 * @author hotking03
 */
@Service
public class SampleGoodsAttrQueryServiceImpl extends BaseService<SampleGoodsAttrQuery> implements SampleGoodsAttrQueryService {
    @Autowired
    IHktGoodsCategoryService categoryService;

    @Autowired
    MongoHelper<SampleGoodsAttrQuery> sampleGoodsAttrQueryMongoHelper;
    @Override
    public void saveSampleAttr(HktGoods query) {
        HktGoodsCategory hc = new HktGoodsCategory();
        hc.setId(query.getCategoryId());
        HktGoodsCategory hktGoodsCategory = categoryService.queryCategory(hc);

        String brandSql = "select hgc.name from hkt_goods hg LEFT JOIN hkt_brand_desc hgc ON hg.brand_id = hgc.brand_id where hgc.language_id = 1 and hg.id = "+query.getId();

        Map<String, Object> brandName = this.baseJdbcDao.query(brandSql);
        Map<String,String> brandMap = new HashMap<>();
        brandMap.put("nameCh","品牌");
        brandMap.put("attrValue",brandName.get("name").toString());

        String sql = "select hga.language_id,hgca.name_ch,hgca.name_en,hga.attr_value from hkt_goods_attr hga  LEFT JOIN hkt_goods_category_attr hgca\n" +
                "ON hga.attr_id = hgca.id  \n" +
                "where hga.language_id = 1 and  hga.goods_id = '"+query.getKsn()+"'";
        List<Map<String, String>> maps = this.baseJdbcDao.executeForMap(sql);
        maps.add(brandMap);
        for(Map<String,String> map : maps){
            SampleGoodsAttrQuery sampleGoodsAttrQuery = new SampleGoodsAttrQuery();
            String nameCh = map.get("nameCh");
            sampleGoodsAttrQuery.setName(nameCh);
            sampleGoodsAttrQuery.setParentId(hktGoodsCategory.getParentId());
            sampleGoodsAttrQuery.setCategoryId(hktGoodsCategory.getId());

            Criteria a = Criteria.where("categoryId").is(hktGoodsCategory.getId());
            Criteria b = Criteria.where("name").is(nameCh);
            Criteria criteria = new Criteria();
            criteria.andOperator(a,b);
            Query q = new Query(criteria);
            SampleGoodsAttrQuery one = sampleGoodsAttrQueryMongoHelper.findOne(q, SampleGoodsAttrQuery.class);
            sampleGoodsAttrQuery.setId(UUID.randomUUID().toString());
            Set<String> attrs;
            if(one == null){
                attrs = new HashSet<>();
                attrs.add(map.get("attrValue"));
                sampleGoodsAttrQuery.setAttrs(attrs);
                sampleGoodsAttrQueryMongoHelper.save(sampleGoodsAttrQuery);
            }else {
                attrs = one.getAttrs();
                attrs.add(map.get("attrValue"));
                sampleGoodsAttrQuery.setAttrs(attrs);
                sampleGoodsAttrQueryMongoHelper.mongoUpdate(one.getId(),sampleGoodsAttrQuery,SampleGoodsAttrQuery.class);
            }
        }
    }

    @Override
    public List<SampleGoodsAttrQuery> selectSampleAttr(HktGoodsCategory hktGoodsCategory) {
        Criteria criteria;
        if(hktGoodsCategory.getLevel() == 1){
            criteria = Criteria.where("parentId").is(hktGoodsCategory.getId());
        }else {
            criteria = Criteria.where("categoryId").is(hktGoodsCategory.getId());
        }
        List<SampleGoodsAttrQuery> sampleGoodsAttrQueries = sampleGoodsAttrQueryMongoHelper.find(new Query(criteria), SampleGoodsAttrQuery.class);
        Collections.reverse(sampleGoodsAttrQueries);
        return sampleGoodsAttrQueries;
    }

    @Autowired
    private GoodsUtils goodsUtils;

    private MongoHelper<HktGoodItem> hktGoodsPageItemMongoHelper = new MongoHelper<>();

    @Autowired
    private SampleService sampleService;

    @Override
    public void init() {
        Query query = new Query();
        SampleGoodsAttrQuery one = sampleGoodsAttrQueryMongoHelper.findOne(query, SampleGoodsAttrQuery.class);
        if(one == null){
            String collectionName = goodsUtils.getCollectionName("1");
            HktGoodItem hktGoodItem = new HktGoodItem();
            hktGoodItem.setIsSample("1");
            List<HktGoodItem> hktGoodItems = hktGoodsPageItemMongoHelper.find(hktGoodItem, HktGoodItem.class, collectionName);
            for(HktGoodItem hg : hktGoodItems){
                HktGoods hktGoods = new HktGoods();
                hktGoods.setId(hg.getId());
                hktGoods.setIsSample("1");
                sampleService.addSapmle(hktGoods);
            }
        }
    }
}
