package com.yc.ship.module.ota.service.distributorselfrecharge;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.tenant.core.util.TenantUtils;
import com.yc.ship.module.ota.controller.admin.distributor.vo.LoginTypeRespVO;
import com.yc.ship.module.ota.controller.admin.distributorselfrecharge.vo.DistributorSelfRechargePageReqVO;
import com.yc.ship.module.ota.controller.admin.distributorselfrecharge.vo.DistributorSelfRechargeSaveReqVO;
import com.yc.ship.module.ota.controller.admin.distributorselfrecharge.vo.DistributorSelfRechargeSimpleRespVO;
import com.yc.ship.module.ota.dal.dataobject.distributorselfrecharge.DistributorSelfRechargeDO;
import com.yc.ship.module.ota.dal.mysql.distributorselfrecharge.DistributorSelfRechargeMapper;
import com.yc.ship.module.ota.service.distributor.DistributorService;
import com.yc.ship.module.ota.service.tradelog.TradeLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.concurrent.atomic.AtomicReference;

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception0;


/**
 * 自助充值明细 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
@Slf4j
public class DistributorSelfRechargeServiceImpl implements DistributorSelfRechargeService {


    @Resource
    private DistributorSelfRechargeMapper distributorSelfRechargeMapper;

    @Resource
    private DistributorService distributorService;

    @Resource
    private TradeLogService tradeLogService;



    @Override
    public DistributorSelfRechargeSimpleRespVO createDistributorSelfRecharge(DistributorSelfRechargeSaveReqVO createReqVO) {
        LoginTypeRespVO loginType = distributorService.getLoginType();

        if (loginType.getType() == 3) {
            throw exception0(11_028, "当前为平台端，不能进行自助充值操作");
        }

        // 插入
        DistributorSelfRechargeDO distributorSelfRecharge = BeanUtils.toBean(createReqVO, DistributorSelfRechargeDO.class);
        Long id = IdWorker.getId(distributorSelfRecharge);
        distributorSelfRecharge.setId(id);
        distributorSelfRecharge.setDistributorId(loginType.getDistributorId());
        distributorSelfRecharge.setRechargeState(0);
        distributorSelfRecharge.setTenantId(loginType.getTenantId());
        TenantUtils.execute(loginType.getTenantId(), () -> {
            distributorSelfRechargeMapper.insert(distributorSelfRecharge);
        });
        DistributorSelfRechargeSimpleRespVO respVO = BeanUtils.toBean(distributorSelfRecharge, DistributorSelfRechargeSimpleRespVO.class);
        return respVO;
    }


    @Override
    public void updateDistributorSelfRecharge(DistributorSelfRechargeSaveReqVO updateReqVO) {
        // 校验存在
        validateDistributorSelfRechargeExists(updateReqVO.getId());
        // 更新
        DistributorSelfRechargeDO updateObj = BeanUtils.toBean(updateReqVO, DistributorSelfRechargeDO.class);
        distributorSelfRechargeMapper.updateById(updateObj);
    }

    @Override
    public void deleteDistributorSelfRecharge(Long id) {
        LoginTypeRespVO loginType = distributorService.getLoginType();
        if (loginType.getType() == 3) {
            throw exception0(11_028, "当前为平台端，不能进行自助充值删除");
        }
        TenantUtils.execute(loginType.getTenantId(), () -> {
            // 校验存在
            validateDistributorSelfRechargeExists(id);
            // 删除
            distributorSelfRechargeMapper.deleteById(id);
        });
    }

    private void validateDistributorSelfRechargeExists(Long id) {
        if (distributorSelfRechargeMapper.selectById(id) == null) {
            throw exception0(11_028, "不存在");
        }
    }

    @Override
    public DistributorSelfRechargeDO getDistributorSelfRecharge(Long id) {
        LoginTypeRespVO loginType = distributorService.getLoginType();
        if (loginType.getType() == 3) {
            return distributorSelfRechargeMapper.selectById(id);
        }
        AtomicReference<DistributorSelfRechargeDO> rechargeReference = new AtomicReference<>();
        TenantUtils.execute(loginType.getTenantId(), () -> {
            rechargeReference.set(distributorSelfRechargeMapper.selectById(id));
        });
        return rechargeReference.get();
    }

    @Override
    public PageResult<DistributorSelfRechargeDO> getDistributorSelfRechargePage(DistributorSelfRechargePageReqVO pageReqVO) {
        LoginTypeRespVO loginType = distributorService.getLoginType();
        if (loginType.getType() != 3) {
            pageReqVO.setDistributorId(loginType.getDistributorId());
            AtomicReference<PageResult<DistributorSelfRechargeDO>> pageReference = new AtomicReference<>();
            TenantUtils.execute(loginType.getTenantId(), () -> {
                pageReference.set(distributorSelfRechargeMapper.selectPage(pageReqVO));
            });
            return pageReference.get();
        }
        return distributorSelfRechargeMapper.selectPage(pageReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long distributorSelfRecharge(Long id, String payOrderId, Integer payStatus) {
        log.info("自助充值支付完成：{}，{},{}", id, payOrderId, payStatus);
        AtomicReference<Long> tradeLogId = new AtomicReference<>();
        DistributorSelfRechargeDO distributorSelfRecharge = getDistributorSelfRecharge(id);
        if (distributorSelfRecharge.getRechargeState() == 1) {
            throw exception0(11_029, "当前自助充值已处理");
        }
        TenantUtils.execute(distributorSelfRecharge.getTenantId(), () -> {
            distributorSelfRecharge.setRechargeState(payStatus == 1 ? 1 : -1);
            tradeLogId.set(tradeLogService.distributorRecharge(distributorSelfRecharge.getDistributorId(), id, 11, distributorSelfRecharge.getAccountId(), distributorSelfRecharge.getAccountName(), distributorSelfRecharge.getMoney()));
            distributorSelfRechargeMapper.updateById(distributorSelfRecharge);
        });
        return tradeLogId.get();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long distributorCMBRecharge(Long id, String payOrderId, Integer payStatus) {
        log.info("对公转账充值支付完成：{}，{},{}", id, payOrderId, payStatus);
        AtomicReference<Long> tradeLogId = new AtomicReference<>();
        DistributorSelfRechargeDO distributorSelfRecharge = getDistributorSelfRecharge(id);
        if (distributorSelfRecharge.getRechargeState() == 1) {
            throw exception0(11_029, "当前自助充值已处理");
        }
        TenantUtils.execute(distributorSelfRecharge.getTenantId(), () -> {
            distributorSelfRecharge.setRechargeState(payStatus == 1 ? 1 : -1);
            tradeLogId.set(tradeLogService.distributorRecharge(distributorSelfRecharge.getDistributorId(), id, 11, distributorSelfRecharge.getAccountId(), distributorSelfRecharge.getAccountName(), distributorSelfRecharge.getMoney()));
            distributorSelfRechargeMapper.updateById(distributorSelfRecharge);
        });
        return tradeLogId.get();
    }

}
