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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bohuikeji.delivery.backstage.entity.TCourier;
import com.bohuikeji.delivery.backstage.entity.TCourierWithdraw;
import com.bohuikeji.delivery.backstage.entity.TStoreWallet;
import com.bohuikeji.delivery.backstage.mapper.TStoreWalletMapper;
import com.bohuikeji.delivery.backstage.service.ITCourierService;
import com.bohuikeji.delivery.backstage.service.ITStoreWalletDetailService;
import com.bohuikeji.delivery.backstage.service.ITStoreWalletService;
import com.bohuikeji.frame.module.admin.base.ServiceImplBase;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;


/**
 * <p>
 * 加盟商门店钱包 服务实现类
 * </p>
 *
 * @author 10552
 * @since 2019-12-27
 */
@Service
@Slf4j
public class TStoreWalletServiceImpl extends ServiceImplBase<TStoreWalletMapper, TStoreWallet> implements ITStoreWalletService {
    @Autowired
    ITStoreWalletService storeWalletService;
    @Autowired
    ITCourierService courierService;
    @Autowired
    ITStoreWalletDetailService storeWalletDetailService;

    @Override
    public boolean saveWallet(TCourierWithdraw courierWithdraw) {
        TCourier courier=courierService.getById(courierWithdraw.getCourierId());
        if("惩罚".equals(courierWithdraw.getWithdrawType())){
            TStoreWallet storeWallet = storeWalletService.getOne(new QueryWrapper<TStoreWallet>().eq("wallet_type","考核钱包").eq("fran_id",courier.getFranId()));
            if(storeWallet != null){
                storeWallet.setWalletPrice(storeWallet.getWalletPrice().add(courierWithdraw.getApplyAmount()));
                storeWalletService.updateById(storeWallet);
            }else {
                storeWallet = this.fineWallet(courier,courierWithdraw);
            }
            storeWalletDetailService.saveDetail(courierWithdraw,storeWallet);
        }
        if("奖励".equals(courierWithdraw.getWithdrawType())){
            TStoreWallet eaxmWallet = storeWalletService.getOne(new QueryWrapper<TStoreWallet>().eq("wallet_type","考核钱包").eq("fran_id",courier.getFranId()));
            TStoreWallet storeWallet = storeWalletService.getOne(new QueryWrapper<TStoreWallet>().eq("wallet_type","门店钱包").eq("fran_id",courier.getFranId()));
            if(eaxmWallet != null && eaxmWallet.getWalletPrice().compareTo(courierWithdraw.getApplyAmount())>-1){
                eaxmWallet.setWalletPrice(eaxmWallet.getWalletPrice().subtract(courierWithdraw.getApplyAmount()));
                storeWalletService.updateById(eaxmWallet);
                storeWalletDetailService.saveDetail(courierWithdraw,eaxmWallet);
            }else if (storeWallet != null && storeWallet.getWalletPrice().compareTo(courierWithdraw.getApplyAmount())>-1){
                storeWallet.setWalletPrice(storeWallet.getWalletPrice().subtract(courierWithdraw.getApplyAmount()));
                storeWalletService.updateById(storeWallet);
                storeWalletDetailService.saveDetail(courierWithdraw,storeWallet);
            }
        }
        return true;
    }

    @Override
    public TStoreWallet fineWallet(TCourier courier, TCourierWithdraw courierWithdraw) {
        TStoreWallet tStoreWallet = new TStoreWallet();
        tStoreWallet.setFranId(courier.getFranId());
        tStoreWallet.setWalletType("考核钱包");
        tStoreWallet.setWalletState("正常");
        tStoreWallet.setWalletPrice(courierWithdraw.getApplyAmount());
        tStoreWallet.setCreateTime(LocalDateTime.now());
        storeWalletService.save(tStoreWallet);
        return tStoreWallet;
    }


}
