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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.bohuikeji.delivery.backstage.entity.TCourier;
import com.bohuikeji.delivery.backstage.entity.TCourierWithdraw;
import com.bohuikeji.delivery.backstage.entity.TCourierWorkExam;
import com.bohuikeji.delivery.backstage.entity.TStoreWallet;
import com.bohuikeji.delivery.backstage.mapper.TCourierWorkExamMapper;
import com.bohuikeji.delivery.backstage.service.ITCourierWorkExamService;
import com.bohuikeji.delivery.backstage.service.ITStoreWalletService;
import com.bohuikeji.frame.module.admin.base.ServiceImplBase;
import com.bohuikeji.frame.module.admin.entity.sys.SysRes;
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;
import java.util.List;


/**
 * <p>
 * 加盟商门店钱包 服务实现类
 * </p>
 *
 * @author 10552
 * @since 2019-12-27
 */
@Service
@Slf4j
public class TCourierWorkExamServiceImpl extends ServiceImplBase<TCourierWorkExamMapper, TCourierWorkExam> implements ITCourierWorkExamService {
    @Autowired
    ITCourierWorkExamService courierWorkExamService;
    @Autowired
    ITStoreWalletService storeWalletService;

    @Override
    public Object bonusCount(TCourier courier, String codeNum) {
        List<TCourierWorkExam> courierWorkExams = courierWorkExamService.list(new QueryWrapper<TCourierWorkExam>().eq("examine_state","待奖励").eq("examine_type","奖励").eq("courier_id",courier.getCourierId()));
        Object bonus=0;
        if(courierWorkExams.size()>0){
            SysRes res=new SysRes();
            StringBuilder sss=new StringBuilder();
            Object bonusNum=res.sql().selectObj("select IFNULL(sum(examine_price),0) as sumc from t_courier_work_exam where courier_id={0} and examine_state='待奖励' and examine_type='奖励'",courier.getCourierId());
            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(new BigDecimal(bonusNum.toString()))>-1){
                bonus=bonusNum;//判断考核钱包奖励金额
            }else if(storeWallet != null && storeWallet.getWalletPrice().compareTo(new BigDecimal(bonusNum.toString()))>-1){
                bonus=bonusNum;//判断门店钱包奖励金额
            }
            if(bonus.equals(0)){
                return bonus;
            }
            for(TCourierWorkExam courierWorkExam : courierWorkExams){
                sss.append(courierWorkExam.getExamineId());
                sss.append(",");
            }
            TCourierWithdraw cw=new TCourierWithdraw();
            cw.setWithdrawDesc("本次考核奖励金额："+courierWorkExams.size()+"笔");
            cw.setCourierId(courier.getCourierId());
            cw.setOrderIds(sss.toString());
            cw.setCountAmount(new BigDecimal(bonus.toString()));
            cw.setCreateDate(LocalDateTime.now());
            //申请到账金额(收益金额)
            cw.setApplyAmount(new BigDecimal(bonus.toString()));
            cw.setRealityAmount(new BigDecimal(bonus.toString()));
            cw.setFranId(courier.getFranId());
            cw.setWithdrawType("奖励");
            cw.setWithdrawGroup(codeNum);
            cw.insert();
            courierWorkExamService.update(new UpdateWrapper<TCourierWorkExam>().set("examine_state","考核中").in("examine_id",sss));
        }
        return bonus;
    }
    @Override
    public Object fineCount(TCourier courier, String codeNum) {
        List<TCourierWorkExam> courierWorkExams = courierWorkExamService.list(new QueryWrapper<TCourierWorkExam>().eq("examine_state","待惩罚").eq("examine_type","惩罚").eq("courier_id",courier.getCourierId()));
        Object fineNum=0;
        if(courierWorkExams.size()>0){
            SysRes res=new SysRes();
            StringBuilder sss=new StringBuilder();
            fineNum=res.sql().selectObj("select IFNULL(sum(examine_price),0) as sumc from t_courier_work_exam where courier_id={0} and examine_state='待惩罚' and examine_type='惩罚'",courier.getCourierId());
            for(TCourierWorkExam courierWorkExam : courierWorkExams){
                sss.append(courierWorkExam.getExamineId());
                sss.append(",");
            }
            TCourierWithdraw cw=new TCourierWithdraw();
            cw.setWithdrawDesc("本次考核罚款金额："+courierWorkExams.size()+"笔");
            cw.setCourierId(courier.getCourierId());
            cw.setOrderIds(sss.toString());
            cw.setCountAmount(new BigDecimal(fineNum.toString()));
            cw.setCreateDate(LocalDateTime.now());
            //申请到账金额(收益金额)
            cw.setApplyAmount(new BigDecimal(fineNum.toString()));
            cw.setRealityAmount(new BigDecimal(fineNum.toString()));
            cw.setFranId(courier.getFranId());
            cw.setWithdrawType("惩罚");
            cw.setWithdrawGroup(codeNum);
            cw.insert();
            courierWorkExamService.update(new UpdateWrapper<TCourierWorkExam>().set("examine_state","考核中").in("examine_id",sss));
        }
        return fineNum;
    }
}
