package com.seer.teach.pay.admin.sys.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.PayWithdrawalConfigPageReq;
import com.seer.teach.pay.admin.withdrawal.controller.request.PayWithdrawalConfigReq;
import com.seer.teach.pay.admin.withdrawal.controller.response.PayWithdrawalConfigResp;
import com.seer.teach.pay.admin.withdrawal.convert.PayWithdrawalConfigConvert;
import com.seer.teach.pay.entity.PayWithdrawalConfigEntity;
import com.seer.teach.pay.service.IPayWithdrawalConfigService;
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 AdminPayWithdrawalConfigService {

    private final IPayWithdrawalConfigService payWithdrawalConfigService;

    public PageListBean<PayWithdrawalConfigResp> pageList(PayWithdrawalConfigPageReq pageRequest) {
        IPage<PayWithdrawalConfigEntity> page = new Page<>(pageRequest.getPageNo(), pageRequest.getPageSize());

        IPage<PayWithdrawalConfigEntity> result = payWithdrawalConfigService.page(page, new LambdaQueryWrapper<PayWithdrawalConfigEntity>()
                .eq(Objects.nonNull(pageRequest.getUserType()), PayWithdrawalConfigEntity::getUserType, pageRequest.getUserType())
                .eq(StrUtil.isNotBlank(pageRequest.getWithdrawalCycle()), PayWithdrawalConfigEntity::getWithdrawalCycle, pageRequest.getWithdrawalCycle())
                .eq(Objects.nonNull(pageRequest.getStatus()), PayWithdrawalConfigEntity::getStatus, pageRequest.getStatus())
        );

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

    public List<PayWithdrawalConfigResp> list(PayWithdrawalConfigReq queryParam) {
        List<PayWithdrawalConfigEntity> result = payWithdrawalConfigService.list(new LambdaQueryWrapper<PayWithdrawalConfigEntity>()
                .eq(Objects.nonNull(queryParam.getUserType()), PayWithdrawalConfigEntity::getUserType, queryParam.getUserType())
                .eq(Objects.nonNull(queryParam.getStatus()), PayWithdrawalConfigEntity::getStatus, queryParam.getStatus())
        );
        return PayWithdrawalConfigConvert.INSTANCE.entityListToResponseList(result);
    }

    public PayWithdrawalConfigResp getDetail(Long id) {
        PayWithdrawalConfigEntity result = payWithdrawalConfigService.getById(id);
        log.info("getById:{}", result);
        return PayWithdrawalConfigConvert.INSTANCE.entityToResponse(result);
    }

    public boolean save(PayWithdrawalConfigReq payWithdrawalConfig) {
        PayWithdrawalConfigEntity entity = PayWithdrawalConfigConvert.INSTANCE.reqToEntity(payWithdrawalConfig);
        boolean saved = payWithdrawalConfigService.save(entity);
        log.info("save:{}", saved);
        return saved;
    }

    public Boolean updateById(PayWithdrawalConfigReq payWithdrawalConfig) {
        PayWithdrawalConfigEntity entity = PayWithdrawalConfigConvert.INSTANCE.reqToEntity(payWithdrawalConfig);
        boolean updated = payWithdrawalConfigService.updateById(entity);
        log.info("updateById:{}", updated);
        return updated;
    }

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