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.PayWithdrawalOperationLogPageReq;
import com.seer.teach.pay.admin.withdrawal.controller.request.PayWithdrawalOperationLogReq;
import com.seer.teach.pay.admin.withdrawal.controller.response.PayWithdrawalOperationLogResp;
import com.seer.teach.pay.admin.withdrawal.convert.PayWithdrawalOperationLogConvert;
import com.seer.teach.pay.entity.PayMerchantWithdrawalOperationLogEntity;
import com.seer.teach.pay.service.IPayMerchantWithdrawalOperationLogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import cn.hutool.core.util.StrUtil;

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

@Slf4j
@RequiredArgsConstructor
@Service
public class AdminPayWithdrawalOperationLogService {

    private final IPayMerchantWithdrawalOperationLogService payWithdrawalOperationLogService;

    public PageListBean<PayWithdrawalOperationLogResp> pageList(PayWithdrawalOperationLogPageReq pageRequest) {
        IPage<PayMerchantWithdrawalOperationLogEntity> page = new Page<>(pageRequest.getPageNo(), pageRequest.getPageSize());

        IPage<PayMerchantWithdrawalOperationLogEntity> result = payWithdrawalOperationLogService.page(page, new LambdaQueryWrapper<PayMerchantWithdrawalOperationLogEntity>()
                .eq(StrUtil.isNotBlank(pageRequest.getWithdrawalNo()), PayMerchantWithdrawalOperationLogEntity::getWithdrawalNo, pageRequest.getWithdrawalNo())
                .eq(Objects.nonNull(pageRequest.getOperatorType()), PayMerchantWithdrawalOperationLogEntity::getOperatorType, pageRequest.getOperatorType())
                .eq(Objects.nonNull(pageRequest.getOperatorId()), PayMerchantWithdrawalOperationLogEntity::getOperatorId, pageRequest.getOperatorId())
                .likeRight(StrUtil.isNotBlank(pageRequest.getOperationContent()), PayMerchantWithdrawalOperationLogEntity::getOperationContent, pageRequest.getOperationContent())
                .likeRight(StrUtil.isNotBlank(pageRequest.getRelatedSettlementIds()), PayMerchantWithdrawalOperationLogEntity::getRelatedSettlementIds, pageRequest.getRelatedSettlementIds())
                .likeRight(StrUtil.isNotBlank(pageRequest.getRelatedOrderNos()), PayMerchantWithdrawalOperationLogEntity::getRelatedOrderNos, pageRequest.getRelatedOrderNos())
                .eq(StrUtil.isNotBlank(pageRequest.getOperationIp()), PayMerchantWithdrawalOperationLogEntity::getOperationIp, pageRequest.getOperationIp())
                .ge(Objects.nonNull(pageRequest.getOperationTime()), PayMerchantWithdrawalOperationLogEntity::getOperationTime, pageRequest.getOperationTime())
                .orderByDesc(PayMerchantWithdrawalOperationLogEntity::getOperationTime)
        );

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

    public List<PayWithdrawalOperationLogResp> list(PayWithdrawalOperationLogReq queryParam) {
        List<PayMerchantWithdrawalOperationLogEntity> result = payWithdrawalOperationLogService.list(new LambdaQueryWrapper<PayMerchantWithdrawalOperationLogEntity>()
                .eq(StrUtil.isNotBlank(queryParam.getWithdrawalNo()), PayMerchantWithdrawalOperationLogEntity::getWithdrawalNo, queryParam.getWithdrawalNo())
                .eq(Objects.nonNull(queryParam.getOperatorType()), PayMerchantWithdrawalOperationLogEntity::getOperatorType, queryParam.getOperatorType())
                .eq(Objects.nonNull(queryParam.getOperatorId()), PayMerchantWithdrawalOperationLogEntity::getOperatorId, queryParam.getOperatorId())
                .likeRight(StrUtil.isNotBlank(queryParam.getRelatedSettlementIds()), PayMerchantWithdrawalOperationLogEntity::getRelatedSettlementIds, queryParam.getRelatedSettlementIds())
                .likeRight(StrUtil.isNotBlank(queryParam.getRelatedOrderNos()), PayMerchantWithdrawalOperationLogEntity::getRelatedOrderNos, queryParam.getRelatedOrderNos())
                .orderByDesc(PayMerchantWithdrawalOperationLogEntity::getOperationTime)
        );
        return PayWithdrawalOperationLogConvert.INSTANCE.entityListToResponseList(result);
    }

    public PayWithdrawalOperationLogResp getDetail(Long id) {
        PayMerchantWithdrawalOperationLogEntity result = payWithdrawalOperationLogService.getById(id);
        log.info("getById:{}", result);
        return PayWithdrawalOperationLogConvert.INSTANCE.entityToResponse(result);
    }

    public boolean save(PayWithdrawalOperationLogReq payWithdrawalOperationLog) {
        PayMerchantWithdrawalOperationLogEntity entity = PayWithdrawalOperationLogConvert.INSTANCE.reqToEntity(payWithdrawalOperationLog);
        boolean saved = payWithdrawalOperationLogService.save(entity);
        log.info("save:{}", saved);
        return saved;
    }

    public Boolean updateById(PayWithdrawalOperationLogReq payWithdrawalOperationLog) {
        PayMerchantWithdrawalOperationLogEntity entity = PayWithdrawalOperationLogConvert.INSTANCE.reqToEntity(payWithdrawalOperationLog);
        boolean updated = payWithdrawalOperationLogService.updateById(entity);
        log.info("updateById:{}", updated);
        return updated;
    }

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