package com.org.hktshop.service.product;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.org.hktshop.Interface.product.IHktPositiveEnergyService;
import com.org.hktshop.Interface.product.IHktPriceRegulationService;
import com.org.hktshop.base.BaseService;
import com.org.hktshop.base.MongoHelper;
import com.org.hktshop.entity.Page;
import com.org.hktshop.entity.product.HktGoodItem;
import com.org.hktshop.entity.product.HktPositivePrice;
import com.org.hktshop.utils.GoodsUtils;
import com.org.hktshop.utils.HttpUtil;
import com.org.hktshop.utils.PositiveEnergyUtils;
import com.org.hktshop.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hotking03
 */
@Service
public class HktPositiveEnergyServiceImpl extends BaseService<HktGoodItem> implements IHktPositiveEnergyService {
    @Autowired
    private GoodsUtils goodsUtils;

    @Autowired
    private MongoHelper<HktGoodItem> mongoHelper;

    @Autowired
    private PositiveEnergyUtils positiveEnergyUtils;

    @Autowired
    private IHktPriceRegulationService hktPriceRegulationService;

    @Value("${hkt_yt_auth_id}")
    private String hktYtAuthId;

    @Value("${hkt_yt_secret}")

    private String hktYtSecret;

    @Value("${hkt_yt_url}")
    private String hktYtUrl;

    @Value("${hkt_client_url}")
    private String hktClientUrl;

    private MongoHelper<HktPositivePrice> hktPositivePriceMongoHelper = new MongoHelper<>();

    @Override
    public Object queryPositiveByPage(HktGoodItem hktGoodItem) {
        if(hktGoodItem.getType() == 1){
            return getHktProduct(hktGoodItem);
        }else {
           return getPositiveProduct(hktGoodItem);
        }

    }

    private Page<HktGoodItem> getHktProduct(HktGoodItem hktGoodItem){
        Criteria criteria = new Criteria();
        Criteria a = Criteria.where("materialSn").regex(hktGoodItem.getMaterialSn());
        criteria.andOperator(a);
        Query query = new Query(criteria);
        String collectionName = goodsUtils.getCollectionName(hktGoodItem.getLanguage());
        Page<HktGoodItem> page = mongoHelper.findPage(hktGoodItem, query, HktGoodItem.class, collectionName);
        if(page != null && page.getRecords().size() > 0){
            hktPriceRegulationService.callPriceRegulation(page.getRecords());
        }
        return page;
    }

    @Override
    public JSONObject getPositiveProduct(HktGoodItem hktGoodItem){
        String positive = positiveEnergyUtils.getPositive();
        String url = hktClientUrl;
        Map<String,Object> map = new HashMap<>();
        map.put("StockType","0");
        map.put("access_token",positive);
        map.put("PartNo",hktGoodItem.getMaterialSn());
        map.put("PageSize",hktGoodItem.getPageSize());
        map.put("PageIndex",hktGoodItem.getPageNum());
        String string = JSON.toJSONString(map);
        JSONObject jsonObject = JSONObject.parseObject(string);
        String str = HttpUtil.sendPostJson(url, jsonObject);
        JSONObject jsonProduct = JSON.parseObject(str);
        return jsonProduct;
    }

    @Override
    public JSONObject getPositiveTyProduct(HktGoodItem hktGoodItem) {
        StringBuffer sf = new StringBuffer();
        sf.append(hktYtUrl);
        sf.append("?auth_id="+hktYtAuthId);
        sf.append("&secret="+hktYtSecret);
        sf.append("&page="+hktGoodItem.getPageNum());
        sf.append("&page_size="+hktGoodItem.getPageSize());
        String str = HttpUtil.get(sf.toString());
        return JSONObject.parseObject(str);
    }

    @Override
    public void updatePositive(HktPositivePrice hktPositivePrice) {
        if(StringUtils.isNull(hktPositivePrice.getId())){
            hktPositivePrice.setId("1");
            hktPositivePriceMongoHelper.save(hktPositivePrice);
        }else {
            hktPositivePriceMongoHelper.mongoUpdate(hktPositivePrice.getId(),hktPositivePrice,HktPositivePrice.class);
        }

    }

    @Override
    public HktPositivePrice getPositive(HktPositivePrice hktPositivePrice) {
       return hktPositivePriceMongoHelper.find(hktPositivePrice,HktPositivePrice.class).get(0);
    }

    @Override
    public Object findById(HktPositivePrice hktPositivePrice) {
        Criteria criteria = Criteria.where("_id").is(hktPositivePrice.getId());
        Query query = new Query(criteria);
        return hktPositivePriceMongoHelper.findOne(query,HktPositivePrice.class);
    }
}
