package com.bohuikeji.delivery.backstage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bohuikeji.delivery.backstage.entity.*;
import com.bohuikeji.delivery.backstage.entity.discount.TDiscountMall;
import com.bohuikeji.delivery.backstage.entity.discount.TDiscountRecord;
import com.bohuikeji.delivery.backstage.mapper.TGiveDetailMapper;
import com.bohuikeji.delivery.backstage.service.*;
import com.bohuikeji.frame.module.admin.base.ServiceImplBase;
import com.bohuikeji.frame.module.admin.entity.back.APIEntity;
import com.bohuikeji.frame.module.admin.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * <p>
 * 钱包明细 服务实现类
 * </p>
 *
 * @author 10552
 * @since 2019-12-27
 */
@Service
@Slf4j
public class TGiveDetailServiceImpl extends ServiceImplBase<TGiveDetailMapper, TGiveDetail> implements ITGiveDetailService {

    @Autowired
    ITGiveDetailService giveDetailService;
    @Autowired
    ITCustomerWalletService customerWalletService;
    @Autowired
    ITwalletDetailService twalletDetailService;
    @Autowired
    ITCustomerService customerService;
    @Autowired
    ITDiscountMallService discountMallService;
    @Autowired
    ITDiscountRecordService discountRecordService;
    @Autowired
    ITDiscountPayService discountPayService;

    /**
     * 钱包充值赠金
     * @param tCustomerPay
     * @param tCustomerWallet
     * @param dace
     * @return
     */
    @Override
    public TGiveDetail saveGiveDetail(TCustomerPay tCustomerPay,TCustomerWallet tCustomerWallet, String dace) throws Exception {
        TCustomer customer = customerService.getById(tCustomerWallet.getCustomerId());
        if(customer != null && "商家用户".equals(customer.getCustomerType())){
            if(discountRecordService.getWhetherMember(295)){
                return null;
            }
            TDiscountMall discountMall = discountMallService.getById(295);
            if(discountMall != null){
                TDiscountRecord tDiscountRecord = discountRecordService.getOne(new QueryWrapper<TDiscountRecord>().eq("customer_id",customer.getCustomerId()).eq("dis_type","会员卡").eq("usage_status","使用中"));
                if(tDiscountRecord != null){
                    return null;
                }
                discountPayService.handleDiscountService(null,null,null,discountMall.getDisId(),customer.getCustomerId());
                return null;
            }
        }
        TGiveDetail giveDetail = new TGiveDetail();
        if(tCustomerPay.getPayExpectPrice().compareTo(new BigDecimal("100.00")) < 0){
            return giveDetail;
        }
        giveDetail.setCustomerId(tCustomerWallet.getCustomerId());
        giveDetail.setGiveDace(dace);
        giveDetail.setRechPrincipal(tCustomerPay.getPayExpectPrice());
        giveDetail.setGiftPmount(retailPresentPeice(tCustomerWallet,tCustomerPay.getPayExpectPrice()));
        giveDetail.setFranId(tCustomerWallet.getFranId());
        giveDetail.setUpdateTime(LocalDateTime.now());
        giveDetail.setCreateTime(LocalDateTime.now());
        giveDetail.setGiveStatus("未使用");
        giveDetailService.save(giveDetail);
        customerWalletService.updateById(tCustomerWallet.setWalletPrice(tCustomerWallet.getWalletPrice().add(giveDetail.getGiftPmount())));
        return giveDetail;
    }
    /**
     * 零散商户
     * 赠送代金券
     * @param presentPeice
     * @return
     */
    private BigDecimal retailPresentPeice(TCustomerWallet customerWallet, BigDecimal presentPeice){
        TCustomer customer = customerService.getById(customerWallet.getCustomerId());
        BigDecimal replenishPrice = new BigDecimal("0.00").setScale(2, BigDecimal.ROUND_DOWN);
        BigDecimal merPrice = new BigDecimal("0.1").setScale(2, BigDecimal.ROUND_DOWN);
        BigDecimal retailPrice = new BigDecimal("0.05").setScale(2, BigDecimal.ROUND_DOWN);
        if(presentPeice.compareTo(new BigDecimal(100)) < 0){
            return replenishPrice;
        }
        if(StringUtils.isNotBlank(customer.getTemplateId())){
            replenishPrice = presentPeice.multiply(merPrice);
        }else {
            replenishPrice = presentPeice.multiply(merPrice);
        }
        //否则直接返回差价并且加1元利润
        return replenishPrice;
    }
}
