package co.yixiang.yshop.module.distributor.service.distributorwageslog;

import co.yixiang.yshop.module.distributor.controller.admin.distributor.vo.DistributorApplyReqVO;
import co.yixiang.yshop.module.distributor.controller.admin.distributorwageslog.vo.DistributorWagesLogUpdateReqVO;
import co.yixiang.yshop.module.distributor.controller.admin.distributorwageslog.vo.DistributorWagesLogVerifyReqVO;
import co.yixiang.yshop.module.distributor.dal.dataobject.distributorwageslog.DistributorWagesLogDO;
import co.yixiang.yshop.module.distributor.enums.DistributorWagesLogTypeEnum;
import co.yixiang.yshop.module.distributor.enums.DistributorWithdrawalMethodTypeEnum;
import co.yixiang.yshop.module.distributor.service.distributorwages.DistributorWagesService;
import co.yixiang.yshop.module.member.api.user.MemberUserNowMoneyApiImpl;
import co.yixiang.yshop.module.member.api.user.dto.IncreaseNowMoneyDTO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class DistributorWagesLogFacadeImpl implements DistributorWagesLogFacade {

    @Resource
    private DistributorWagesLogService distributorWagesLogService;

    @Resource
    private MemberUserNowMoneyApiImpl memberUserNowMoneyApi;

    @Resource
    private DistributorWagesService distributorWagesService;

    @Override
    public void verify(DistributorWagesLogVerifyReqVO reqVO) {
        DistributorWagesLogDO wagesLog = distributorWagesLogService.getWagesLog(reqVO.getId());
        // 更新状态
        DistributorWagesLogUpdateReqVO updateReqVO = new DistributorWagesLogUpdateReqVO();
        updateReqVO.setId(reqVO.getId());
        updateReqVO.setType(reqVO.getType());
        updateReqVO.setContent(reqVO.getContent());
        distributorWagesLogService.updateWagesLog(updateReqVO);

        // 判断是否为拒绝
        if (reqVO.getType().equals(DistributorWagesLogTypeEnum.WITHDRAW_FAIL.getType())) {
            // 退回佣金
            distributorWagesService.increaseAmount(wagesLog.getDistributorId(), wagesLog.getAmount());
        }

        // 判断是否为通过 并且为余额
        if (reqVO.getType().equals(DistributorWagesLogTypeEnum.WITHDRAW_COMPLETE.getType()) && wagesLog.getMethodType().equals(DistributorWithdrawalMethodTypeEnum.BALANCE.getType())) {
            IncreaseNowMoneyDTO increaseNowMoneyDTO = new IncreaseNowMoneyDTO();
            increaseNowMoneyDTO.setId(wagesLog.getUserId());
            increaseNowMoneyDTO.setAmount(wagesLog.getAmount());
            increaseNowMoneyDTO.setLink(wagesLog.getId().toString());
            increaseNowMoneyDTO.setTitle("佣金提现");
            increaseNowMoneyDTO.setMark("佣金提现" + increaseNowMoneyDTO.getAmount() + "元");
            memberUserNowMoneyApi.increaseNowMoney(increaseNowMoneyDTO);
        }
    }
}
