package cn.pluss.platform.merchantMarket.impl;

import cn.pluss.platform.deviceMerchantBuy.DeviceMerchantBuyService;
import cn.pluss.platform.entity.*;
import cn.pluss.platform.enums.UserRoleEnum;
import cn.pluss.platform.mapper.MerchantMarketMapper;
import cn.pluss.platform.mapper.UserAppMapper;
import cn.pluss.platform.merchantMarket.MerchantMarketService;
import cn.pluss.platform.merchantOrder.MerchantOrderService;
import cn.pluss.platform.merchantStore.MerchantStoreService;
import cn.pluss.platform.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商户积分记录表 服务实现类
 * </p>
 *
 * @author Djh
 * @since 2020-11-02
 */
@Service
@Slf4j
public class MerchantMarketServiceImpl extends ServiceImpl<MerchantMarketMapper, MerchantMarket> implements MerchantMarketService {

    /**
     * 获取奖励金额度
     * @param userId
     * @return
     */
    @Override
    public BigDecimal getCountMarketAmt(Long userId) {
        return this.baseMapper.getCountMarketAmt(userId);
    }

    /**
     * bst自动返机具积分
     * @date: 2022/2/14 10:56
     * @param order:
     */
//    @Override
//    public void bstOrderIntegralReward(MerchantOrder order) {
//        log.info("【倍斯特】积分返操作开始，订单号:{}",order.getOrderNumber());
//        try {
//            String merchantCode = order.getMerchantCode();
//            if(StringUtil.isEmpty(merchantCode)){
//                return;
//            }
//            UserApp userApp = userAppMapper.selectByMerchantCode(merchantCode);
//            if(userApp == null || !UserRoleEnum.ZY_BST.getCode().equals(userApp.getRoleCode())){
//                return;
//            }
//            MerchantStore store = merchantStoreService.getStoreByMerchantCode(merchantCode);
//            if(store == null || !"1".equals(store.getIsMarket())){
//                return;
//            }
//            QueryWrapper<DeviceMerchantBuy> queryDeviceWrapper = new QueryWrapper<DeviceMerchantBuy>()
//                    .eq("merchantCode",merchantCode).eq("status","1").in("orderAmount",BST_LI_BAO_AMT1,BST_LI_BAO_AMT2);
//            List<DeviceMerchantBuy> list = deviceMerchantBuyService.list(queryDeviceWrapper);
//            if(list.isEmpty()){
//                return;
//            }
//            DeviceMerchantBuy deviceMerchantBuy = list.get(0);
//            QueryWrapper<MerchantMarket> queryWrapper = new QueryWrapper<MerchantMarket>()
//                    .eq("userId",userApp.getUserId()).eq("source",2);
//            MerchantMarket merchantMarket = baseMapper.selectOne(queryWrapper);
//            if(merchantMarket != null){
//                return;
//            }
//            QueryWrapper<MerchantOrder> orderQuery = new QueryWrapper<MerchantOrder>()
//                    .eq("merchantCode",userApp.getMerchantCode())
//                    .eq("status","1").select("count(1) as transNum,IFNULL(SUM(consumeFee),0) as transAmt");
//            Map<String, Object> map = merchantOrderService.getMap(orderQuery);
//            if(map == null || map.size() == 0){
//                return;
//            }
//            long transNum = (long)map.get("transNum");
//            double transAmt = (double) map.get("transAmt");
//            log.info("【倍斯特】积分返操作成功，订单号:{},交易笔数：{}，交易总额：{}",order.getOrderNumber(),transNum,transAmt);
//            if(transNum >= BST_INTEGRAL_TRANS_NUM || transAmt >= BST_INTEGRAL_TRANS_AMOUNT){
//                //满足发放积分条件
//
//
////                MerchantMarket market = new MerchantMarket();
////                market.setUserName(userApp.getUserName());
////                market.setUserId(userApp.getUserId());
////                market.setMarketAmt(BigDecimal.valueOf(deviceMerchantBuy.getOrderAmount()));
////                market.setSource("2");
////                baseMapper.insert(market);
////                log.info("【倍斯特】积分返操作成功，订单号:{},用户ID：{}，积分数值：{}",order.getOrderNumber(),userApp.getUserId(),deviceMerchantBuy.getOrderAmount());
//            }
//        }catch (Exception e){
//            e.printStackTrace();
//            log.error("【倍斯特】积分返异常,异常信息:{}",e.getMessage());
//        }
//    }
}
