package com.seer.teach.pay.admin.withdrawal.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.utils.PageConverterUtils;
import com.seer.teach.pay.admin.withdrawal.controller.request.PayWithdrawalBalancePageReq;
import com.seer.teach.pay.admin.withdrawal.controller.request.PayWithdrawalBalanceReq;
import com.seer.teach.pay.admin.withdrawal.controller.response.PayWithdrawalBalanceResp;
import com.seer.teach.pay.admin.withdrawal.convert.PayWithdrawalBalanceConvert;
import com.seer.teach.pay.entity.PayMerchantAccountEntity;
import com.seer.teach.pay.service.IPayMerchantAccountService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Slf4j
@RequiredArgsConstructor
@Service
public class AdminPayMerchantAccountService {

    private final IPayMerchantAccountService payUserWithdrawalBalanceService;

    public PageListBean<PayWithdrawalBalanceResp> pageList(PayWithdrawalBalancePageReq pageRequest) {
        IPage<PayMerchantAccountEntity> page = new Page<>(pageRequest.getPageNo(), pageRequest.getPageSize());

        IPage<PayMerchantAccountEntity> result = payUserWithdrawalBalanceService.page(page, new LambdaQueryWrapper<PayMerchantAccountEntity>()
                .eq(Objects.nonNull(pageRequest.getUserId()), PayMerchantAccountEntity::getUserId, pageRequest.getUserId())
                .eq(Objects.nonNull(pageRequest.getAvailableBalance()), PayMerchantAccountEntity::getAvailableBalance, pageRequest.getAvailableBalance())
                .eq(Objects.nonNull(pageRequest.getFrozenBalance()), PayMerchantAccountEntity::getFrozenBalance, pageRequest.getFrozenBalance())
                .eq(Objects.nonNull(pageRequest.getTotalSettledAmount()), PayMerchantAccountEntity::getTotalSettledAmount, pageRequest.getTotalSettledAmount())
        );

        return PageConverterUtils.convertPageListBean(result, PayWithdrawalBalanceConvert.INSTANCE::entityListToResponseList);
    }

    public List<PayWithdrawalBalanceResp> list(PayWithdrawalBalancePageReq queryParam) {
        List<PayMerchantAccountEntity> result = payUserWithdrawalBalanceService.list(new LambdaQueryWrapper<PayMerchantAccountEntity>()
                .eq(Objects.nonNull(queryParam.getUserId()), PayMerchantAccountEntity::getUserId, queryParam.getUserId()));
        return PayWithdrawalBalanceConvert.INSTANCE.entityListToResponseList(result);
    }

    public PayWithdrawalBalanceResp getDetail(Integer id) {
        PayMerchantAccountEntity result = payUserWithdrawalBalanceService.getById(id);
        log.info("getById:{}", result);
        return PayWithdrawalBalanceConvert.INSTANCE.entityToResponse(result);
    }

    public boolean save(PayWithdrawalBalanceReq payUserWithdrawalBalance) {
        PayMerchantAccountEntity entity = PayWithdrawalBalanceConvert.INSTANCE.reqToEntity(payUserWithdrawalBalance);
        boolean saved = payUserWithdrawalBalanceService.save(entity);
        log.info("save:{}", saved);
        return saved;
    }

    public Boolean updateById(PayWithdrawalBalanceReq payUserWithdrawalBalance) {
        PayMerchantAccountEntity entity = PayWithdrawalBalanceConvert.INSTANCE.reqToEntity(payUserWithdrawalBalance);
        boolean updated = payUserWithdrawalBalanceService.updateById(entity);
        log.info("updateById:{}", updated);
        return updated;
    }

    public Boolean deleteById(Integer id) {
        boolean updated = payUserWithdrawalBalanceService.removeById(id);
        log.info("deleteById:{}", updated);
        return updated;
    }

    public PayWithdrawalBalanceResp getMyAccountDetail(Long userId) {
        PayMerchantAccountEntity withdrawalBalance = payUserWithdrawalBalanceService.getMyAccount(userId);
        log.info("用户[{}]获取账户余额:{}", userId, withdrawalBalance);
        return PayWithdrawalBalanceConvert.INSTANCE.entityToResponse(withdrawalBalance);
    }
}