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.AdminPayMerchantRevenueDetailPageReq;
import com.seer.teach.pay.admin.withdrawal.controller.request.AdminPayMerchantRevenueDetailQueryReq;
import com.seer.teach.pay.admin.withdrawal.controller.request.AdminPayMerchantRevenueDetailReq;
import com.seer.teach.pay.admin.withdrawal.controller.response.AdminPayMerchantRevenueDetailResp;
import com.seer.teach.pay.admin.withdrawal.convert.AdminPayMerchantRevenueDetailConvert;
import com.seer.teach.pay.entity.PayMerchantRevenueDetailEntity;
import com.seer.teach.pay.service.IPayMerchantRevenueDetailService;
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 AdminPayMerchantRevenueDetailService {

    private final IPayMerchantRevenueDetailService payMerchantRevenueDetailService;

    public PageListBean<AdminPayMerchantRevenueDetailResp> pageList(AdminPayMerchantRevenueDetailPageReq pageRequest) {
        IPage<PayMerchantRevenueDetailEntity> page = new Page<>(pageRequest.getPageNo(), pageRequest.getPageSize());

        IPage<PayMerchantRevenueDetailEntity> result = payMerchantRevenueDetailService.page(page, new LambdaQueryWrapper<PayMerchantRevenueDetailEntity>()
                .eq(Objects.nonNull(pageRequest.getMerchantUserId()), PayMerchantRevenueDetailEntity::getMerchantUserId, pageRequest.getMerchantUserId())
                .likeRight(StrUtil.isNotBlank(pageRequest.getOrderId()), PayMerchantRevenueDetailEntity::getOrderId, pageRequest.getOrderId())
                .likeRight(StrUtil.isNotBlank(pageRequest.getChildOrderId()), PayMerchantRevenueDetailEntity::getChildOrderId, pageRequest.getChildOrderId())
                .likeRight(StrUtil.isNotBlank(pageRequest.getSkuId()), PayMerchantRevenueDetailEntity::getSkuId, pageRequest.getSkuId())
                .orderByDesc(PayMerchantRevenueDetailEntity::getCreateTime)
        );

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

    public List<AdminPayMerchantRevenueDetailResp> list(AdminPayMerchantRevenueDetailQueryReq queryParam) {
        List<PayMerchantRevenueDetailEntity> result = payMerchantRevenueDetailService.list(new LambdaQueryWrapper<PayMerchantRevenueDetailEntity>()
                .eq(Objects.nonNull(queryParam.getId()), PayMerchantRevenueDetailEntity::getId, queryParam.getId())
                .eq(Objects.nonNull(queryParam.getMerchantUserId()), PayMerchantRevenueDetailEntity::getMerchantUserId, queryParam.getMerchantUserId())
                .likeRight(StrUtil.isNotBlank(queryParam.getOrderId()), PayMerchantRevenueDetailEntity::getOrderId, queryParam.getOrderId())
                .likeRight(StrUtil.isNotBlank(queryParam.getChildOrderId()), PayMerchantRevenueDetailEntity::getChildOrderId, queryParam.getChildOrderId())
                .likeRight(StrUtil.isNotBlank(queryParam.getSkuId()), PayMerchantRevenueDetailEntity::getSkuId, queryParam.getSkuId())
                .likeRight(StrUtil.isNotBlank(queryParam.getUserId()), PayMerchantRevenueDetailEntity::getUserId, queryParam.getUserId())
                .eq(StrUtil.isNotBlank(queryParam.getTenantId()), PayMerchantRevenueDetailEntity::getTenantId, queryParam.getTenantId())
                .orderByDesc(PayMerchantRevenueDetailEntity::getCreateTime)
        );
        return AdminPayMerchantRevenueDetailConvert.INSTANCE.entityListToResponseList(result);
    }

    public AdminPayMerchantRevenueDetailResp getDetail(Long id) {
        PayMerchantRevenueDetailEntity result = payMerchantRevenueDetailService.getById(id);
        log.info("getById:{}", result);
        return AdminPayMerchantRevenueDetailConvert.INSTANCE.entityToResponse(result);
    }

    public AdminPayMerchantRevenueDetailResp getDetail(Long id, Long merchantUserId) {
        PayMerchantRevenueDetailEntity result = payMerchantRevenueDetailService.getOne(new LambdaQueryWrapper<>(PayMerchantRevenueDetailEntity.class)
                .eq(PayMerchantRevenueDetailEntity::getId, id)
                .eq(PayMerchantRevenueDetailEntity::getMerchantUserId, merchantUserId));
        return AdminPayMerchantRevenueDetailConvert.INSTANCE.entityToResponse(result);
    }

    public boolean save(AdminPayMerchantRevenueDetailReq payMerchantRevenueDetail) {
        PayMerchantRevenueDetailEntity entity = AdminPayMerchantRevenueDetailConvert.INSTANCE.reqToEntity(payMerchantRevenueDetail);
        boolean saved = payMerchantRevenueDetailService.save(entity);
        log.info("save:{}", saved);
        return saved;
    }

    public boolean updateById(AdminPayMerchantRevenueDetailReq payMerchantRevenueDetail) {
        PayMerchantRevenueDetailEntity entity = AdminPayMerchantRevenueDetailConvert.INSTANCE.reqToEntity(payMerchantRevenueDetail);
        boolean updated = payMerchantRevenueDetailService.updateById(entity);
        log.info("updateById:{}", updated);
        return updated;
    }

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