package com.leromro.core.facade;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.leromro.common.core.domain.R;
import com.leromro.common.utils.SecurityUtils;
import com.leromro.core.domain.VolunteerAccount;
import com.leromro.core.domain.VolunteerAccountChange;
import com.leromro.core.domain.VolunteerTakeRecord;
import com.leromro.core.domain.dto.VolunteerSubmitAmountDTO;
import com.leromro.core.service.IVolunteerAccountChangeService;
import com.leromro.core.service.IVolunteerAccountService;
import com.leromro.core.service.IVolunteerTakeRecordService;
import com.leromro.core.utils.BigDecimalUtil;
import com.leromro.core.utils.NoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Slf4j
@Service
public class VolunteerAccountFacade {

    @Autowired
    private IVolunteerAccountService volunteerAccountService;

    @Autowired
    private IVolunteerTakeRecordService volunteerTakeRecordService;

    @Autowired
    private IVolunteerAccountChangeService accountChangeService;


    /**
     * 提交提现申请
     */
    @Transactional(rollbackFor = Exception.class)
    public R submitAmountApply(VolunteerSubmitAmountDTO dto) {
        //查询志愿者账户，判断账户余额是否足够
        VolunteerAccount account = volunteerAccountService.getOne(new LambdaQueryWrapper<VolunteerAccount>()
                .eq(VolunteerAccount::getVolunteerId, SecurityUtils.getUserId()));
        if (ObjectUtil.isNull(account)){
            return R.fail("当前志愿者账户不存在");
        }
        if (account.getBalance().compareTo(dto.getTakeAmount()) < 0){
            return R.fail("账户余额不足");
        }
        //修改志愿者账户余额
        BigDecimal changeBeforeBalance = account.getBalance();
        account.setBalance(account.getBalance().subtract(dto.getTakeAmount()));
        account.setBeBalance(account.getBeBalance().add(dto.getTakeAmount()));
        volunteerAccountService.updateById(account);
        //生成志愿者提现申请记录
        VolunteerTakeRecord takeRecord = VolunteerTakeRecord.builder()
                .takeSn(NoUtil.getTakeMoneyNo())
                .volunteerAccountId(account.getVolunteerAccountId())
                .volunteerUserId(SecurityUtils.getUserId())
                .takeAmount(dto.getTakeAmount())
                .shoudAmount(dto.getTakeAmount())
                .taxAmount(BigDecimal.ZERO)
                .payType(dto.getPayType())
                .alipayAccountNo(dto.getAlipayAccountNo())
                .alipayName(dto.getAlipayName())
                .build();
        volunteerTakeRecordService.save(takeRecord);
        //修改账户变更记录表
        VolunteerAccountChange accountChange = VolunteerAccountChange.builder()
                .volunteerId(SecurityUtils.getUserId())
                .changeType("0")
                .sourceType("10")
                .changeMoney(dto.getTakeAmount())
                .beforeBalance(changeBeforeBalance)
                .afterBalance(account.getBalance())
                .sourceId(takeRecord.getVolunteerTakeRecordId())
                .build();
        accountChangeService.save(accountChange);
        return R.ok();
    }


    public R getWithdrawStatus() {
        int count = volunteerTakeRecordService.count(new LambdaQueryWrapper<VolunteerTakeRecord>()
                .eq(VolunteerTakeRecord::getVolunteerUserId, SecurityUtils.getUserId())
                .eq(VolunteerTakeRecord::getAppStatus, "1"));
        return count>0?R.fail("提现审批中"):R.ok();
    }
}
