package com.kmall.api.service.mk;

import com.google.common.collect.Maps;
import com.kmall.api.dao.ApiGoodsMapper;
import com.kmall.api.dao.ApiStoreMapper;
import com.kmall.api.dao.ApiThirdMerchantBizMapper;
import com.kmall.api.dao.mk.ApiMkStoreTicketDiscountGoodsMapper;
import com.kmall.api.dao.mk.ApiMkStoreTicketDiscountMapper;
import com.kmall.api.entity.CategoryVo;
import com.kmall.api.entity.GoodsVo;
import com.kmall.api.entity.StoreVo;
import com.kmall.api.entity.ThirdMerchantBizVo;
import com.kmall.api.entity.mk.MkStoreTicketDiscountGoodsVo;
import com.kmall.api.entity.mk.MkStoreTicketDiscountVo;
import com.kmall.api.service.ApiCategoryService;
import com.kmall.common.utils.Query;
import com.kmall.common.utils.RRException;
import com.qiniu.util.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 门店优惠券活动商品，参与优惠的商品Service实现类
 *
 * @author emato
 * @email admin@qhdswl.com
 * @date 2019-05-07 16:06:28
 */
@Service
public class ApiMkStoreTicketDiscountGoodsService{
    private Log logger = LogFactory.getLog(ApiMkStoreTicketDiscountGoodsService.class);
    @Autowired
    private ApiMkStoreTicketDiscountGoodsMapper apiMkStoreTicketDiscountGoodsMapper;
    @Autowired
    private ApiGoodsMapper apiGoodsMapper;
    @Autowired
    private ApiThirdMerchantBizMapper apiThirdMerchantBizMapper;
    @Autowired
    private ApiStoreMapper apiStoreMapper;
    @Autowired
    private ApiCategoryService categoryService;
    @Autowired
    private ApiMkStoreTicketDiscountMapper apiMkStoreTicketDiscountMapper;

    public MkStoreTicketDiscountGoodsVo queryObject(Integer discGoodId) {
        return apiMkStoreTicketDiscountGoodsMapper.queryObject(discGoodId);
    }

    public List<MkStoreTicketDiscountGoodsVo> queryList(Map<String, Object> map) {
        return apiMkStoreTicketDiscountGoodsMapper.queryList(map);
    }

    public int queryTotal(Map<String, Object> map) {
        return apiMkStoreTicketDiscountGoodsMapper.queryTotal(map);
    }

    public List<GoodsVo> queryDiscountGoodsList(Map map){
        return apiMkStoreTicketDiscountGoodsMapper.queryDiscountGoodsList(map);
    }

    public Integer queryDiscountGoodsTotal(Map map){
        return apiMkStoreTicketDiscountGoodsMapper.queryDiscountGoodsTotal(map);
    }

    public Map getStoreTicketDiscountGoodsList(Integer storeId, Integer categoryId, Integer tickDiscId,
                                               Integer page, Integer size, String sort, String order){
        MkStoreTicketDiscountVo discountVo = apiMkStoreTicketDiscountMapper.queryObject(tickDiscId);
        if(discountVo == null){
            logger.error("优惠券信息不存在");
            throw new RRException("优惠券信息不存在");
        }
        Map reusltObj = Maps.newHashMap();
        if(storeId == null){
            logger.error("门店id不能为空");
            throw new RRException("门店id不能为空");
        }
        StoreVo storeVo = apiStoreMapper.queryObject(Long.valueOf(storeId));
        ThirdMerchantBizVo thirdMerchantBiz = new ThirdMerchantBizVo();
        if(storeVo != null) {
            thirdMerchantBiz = apiThirdMerchantBizMapper.getThirdMerchangByCode(storeVo.getThirdPartyMerchCode());
            if (null == thirdMerchantBiz) {
                logger.error("第三方商户不存在");
                throw new RRException("第三方商户不存在");
            }
        }

        Map params = Maps.newHashMap();
        //筛选的分类
        List<CategoryVo> filterCategory = new ArrayList();
        CategoryVo rootCategory = new CategoryVo();
        rootCategory.setId(0);
        rootCategory.setName("全部");
        rootCategory.setChecked(false);
        filterCategory.add(rootCategory);
        //
        params.put("fields", "psr1.category_id");
        params.put("store_id", storeId);
        params.put("tickDiscId",tickDiscId);
        params.put("applyType", discountVo.getApplyType());
        params.put("goodsBizType", discountVo.getGoodsBizType());

        List<GoodsVo> goodsEntityList = apiGoodsMapper.queryList(params);
        params.remove("fields");
        filterCategory.addAll(categoryService.getFilterCategory(goodsEntityList, filterCategory));

        params.put("category_parent_id", categoryId);
        params.put("fields", "a.id,a.name,a.goods_brief,a.list_pic_url,psr1.retail_price," +
                "psr1.market_price,b.id as product_id,psr1.stock_num");
        params.put("is_hot", "1");
        params.put("is_delete", 0);
        params.put("store_id", storeId);
        if (!StringUtils.isNullOrEmpty(sort) && !StringUtils.isNullOrEmpty(order)) {
            if (sort.equals("price")) {
                params.put("sidx", "psr1.retail_price");
                params.put("order", order);
            }
        }

        reusltObj.put("filterCategory", filterCategory);
        params.put("isStockShare", thirdMerchantBiz.getIsStockShare());

        params.put("page", page);
        params.put("limit", size);
        Query query = new Query(params);
        List<GoodsVo> mkStoreTicketDiscountList = apiMkStoreTicketDiscountGoodsMapper.queryDiscountGoodsList(query);
        Integer goodsCount = apiMkStoreTicketDiscountGoodsMapper.queryDiscountGoodsTotal(query);
        if (null != mkStoreTicketDiscountList && mkStoreTicketDiscountList.size() > 0) {
            for (GoodsVo goodsVo : mkStoreTicketDiscountList) {
                goodsVo.setCart_num(goodsVo.getCart_num()==null?0:goodsVo.getCart_num());
            }
        }
        reusltObj.put("goodsList", mkStoreTicketDiscountList);
        reusltObj.put("goodsCount", goodsCount);

        return reusltObj;
    }
}
