package cn.newfeifan.mall.module.distri.service.applicationforwithdrawalchannel;

import cn.newfeifan.mall.module.distri.enums.WithdrawalChannelTypeEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import cn.newfeifan.mall.module.distri.controller.app.applicationforwithdrawalchannel.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.applicationforwithdrawalchannel.ApplicationForWithdrawalChannelDO;
import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.common.util.object.BeanUtils;

import cn.newfeifan.mall.module.distri.dal.mysql.applicationforwithdrawalchannel.ApplicationForWithdrawalChannelMapper;

import static cn.newfeifan.mall.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.newfeifan.mall.module.distri.enums.ErrorCodeConstants.*;

/**
 * 提现渠道记录 Service 实现类
 *
 * @author 非繁人
 */
@Service
@Validated
public class ApplicationForWithdrawalChannelServiceImpl implements ApplicationForWithdrawalChannelService {

    @Resource
    private ApplicationForWithdrawalChannelMapper applicationForWithdrawalChannelMapper;

    @Override
    public Long createApplicationForWithdrawalChannel(AppApplicationForWithdrawalChannelSaveReqVO createReqVO) {
        // 插入
        ApplicationForWithdrawalChannelDO applicationForWithdrawalChannel = BeanUtils.toBean(createReqVO, ApplicationForWithdrawalChannelDO.class);
        applicationForWithdrawalChannelMapper.insert(applicationForWithdrawalChannel);
        // 返回
        return applicationForWithdrawalChannel.getId();
    }

    @Override
    public void updateApplicationForWithdrawalChannel(AppApplicationForWithdrawalChannelSaveReqVO updateReqVO) {
        // 校验存在
        validateApplicationForWithdrawalChannelExists(updateReqVO.getId());
        // 更新
        ApplicationForWithdrawalChannelDO updateObj = BeanUtils.toBean(updateReqVO, ApplicationForWithdrawalChannelDO.class);
        applicationForWithdrawalChannelMapper.updateById(updateObj);
    }

    @Override
    public void deleteApplicationForWithdrawalChannel(Long id) {
        // 校验存在
        validateApplicationForWithdrawalChannelExists(id);
        // 删除
        applicationForWithdrawalChannelMapper.deleteById(id);
    }

    private void validateApplicationForWithdrawalChannelExists(Long id) {
        if (applicationForWithdrawalChannelMapper.selectById(id) == null) {
            throw exception(APPLICATION_FOR_WITHDRAWAL_CHANNEL_NOT_EXISTS);
        }
    }

    @Override
    public ApplicationForWithdrawalChannelDO getApplicationForWithdrawalChannel(Long id) {
        return applicationForWithdrawalChannelMapper.selectById(id);
    }

    @Override
    public PageResult<ApplicationForWithdrawalChannelDO> getApplicationForWithdrawalChannelPage(AppApplicationForWithdrawalChannelPageReqVO pageReqVO) {
        return applicationForWithdrawalChannelMapper.selectPage(pageReqVO);
    }

    @Override
    public ApplicationForWithdrawalChannelDO getApplicationForWithdrawalChannel(Integer withdrawalType, Long id) {

        // 先查看有没有该渠道的记录
        LambdaQueryWrapper<ApplicationForWithdrawalChannelDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ApplicationForWithdrawalChannelDO::getPtDailyWithdrawalId, id);
        if(WithdrawalChannelTypeEnum.ALIPAY.getType().equals(withdrawalType) || WithdrawalChannelTypeEnum.BANK.getType().equals(withdrawalType)){
            wrapper.and(
                    w -> w.eq(ApplicationForWithdrawalChannelDO::getWithdrawalType, WithdrawalChannelTypeEnum.ALIPAY.getType())
                            .or()
                            .eq(ApplicationForWithdrawalChannelDO::getWithdrawalType, WithdrawalChannelTypeEnum.BANK.getType())
            );
        }
        ApplicationForWithdrawalChannelDO applicationForWithdrawalChannelDO = applicationForWithdrawalChannelMapper.selectOne(wrapper);

        // 没有就插入
        if (applicationForWithdrawalChannelDO == null) {
            applicationForWithdrawalChannelDO = ApplicationForWithdrawalChannelDO.builder()
                    .withdrawalType(withdrawalType)
                    .ptDailyWithdrawalId(id)
                    .build();

            if(WithdrawalChannelTypeEnum.ALIPAY.getType().equals(withdrawalType) || WithdrawalChannelTypeEnum.BANK.getType().equals(withdrawalType)){
                applicationForWithdrawalChannelDO.setWithdrawalName(WithdrawalChannelTypeEnum.getName(WithdrawalChannelTypeEnum.ALIPAY.getType()) + "/" + WithdrawalChannelTypeEnum.getName(WithdrawalChannelTypeEnum.BANK.getType()));
            }

            applicationForWithdrawalChannelMapper.insert(applicationForWithdrawalChannelDO);
            return applicationForWithdrawalChannelMapper.selectById(applicationForWithdrawalChannelDO.getId());
        }

        return applicationForWithdrawalChannelDO;
    }

}