package com.seer.teach.pay.admin.financial.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.financial.controller.request.AdminPayFinancialReconciliationDetailPageReq;
import com.seer.teach.pay.admin.financial.controller.request.AdminPayFinancialReconciliationDetailQueryReq;
import com.seer.teach.pay.admin.financial.controller.request.AdminPayFinancialReconciliationDetailReq;
import com.seer.teach.pay.admin.financial.controller.response.AdminPayFinancialReconciliationDetailResp;
import com.seer.teach.pay.admin.financial.convert.AdminPayFinancialReconciliationDetailConvert;
import com.seer.teach.pay.entity.PayFinancialReconciliationDetailEntity;
import com.seer.teach.pay.service.IPayFinancialReconciliationDetailService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.collection.CollectionUtil;

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

@Slf4j
@RequiredArgsConstructor
@Service
public class AdminPayFinancialReconciliationDetailService {

    private final IPayFinancialReconciliationDetailService payFinancialReconciliationDetailService;

    public PageListBean<AdminPayFinancialReconciliationDetailResp> pageList(AdminPayFinancialReconciliationDetailPageReq pageRequest) {
        IPage<PayFinancialReconciliationDetailEntity> page = new Page<>(pageRequest.getPageNo(), pageRequest.getPageSize());

        IPage<PayFinancialReconciliationDetailEntity> result = payFinancialReconciliationDetailService.page(page, new LambdaQueryWrapper<PayFinancialReconciliationDetailEntity>()
                .eq(Objects.nonNull(pageRequest.getReconciliationId()), PayFinancialReconciliationDetailEntity::getReconciliationId, pageRequest.getReconciliationId())
                .eq(Objects.nonNull(pageRequest.getTargetType()), PayFinancialReconciliationDetailEntity::getTargetType, pageRequest.getTargetType())
                .eq(Objects.nonNull(pageRequest.getTargetId()), PayFinancialReconciliationDetailEntity::getTargetId, pageRequest.getTargetId())
                .likeRight(StrUtil.isNotBlank(pageRequest.getTargetNo()), PayFinancialReconciliationDetailEntity::getTargetNo, pageRequest.getTargetNo())
                .likeRight(StrUtil.isNotBlank(pageRequest.getThirdPartyNo()), PayFinancialReconciliationDetailEntity::getThirdPartyNo, pageRequest.getThirdPartyNo())
                .in(CollectionUtil.isNotEmpty(pageRequest.getReconciliationResultList()), PayFinancialReconciliationDetailEntity::getReconciliationResult, pageRequest.getReconciliationResultList())
                .orderByDesc(PayFinancialReconciliationDetailEntity::getCreateTime)
        );

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

    public List<AdminPayFinancialReconciliationDetailResp> list(AdminPayFinancialReconciliationDetailQueryReq queryParam) {
        List<PayFinancialReconciliationDetailEntity> result = payFinancialReconciliationDetailService.list(new LambdaQueryWrapper<PayFinancialReconciliationDetailEntity>()
                .eq(Objects.nonNull(queryParam.getReconciliationId()), PayFinancialReconciliationDetailEntity::getReconciliationId, queryParam.getReconciliationId())
                .eq(Objects.nonNull(queryParam.getTargetType()), PayFinancialReconciliationDetailEntity::getTargetType, queryParam.getTargetType())
                .eq(Objects.nonNull(queryParam.getTargetId()), PayFinancialReconciliationDetailEntity::getTargetId, queryParam.getTargetId())
                .likeRight(StrUtil.isNotBlank(queryParam.getTargetNo()), PayFinancialReconciliationDetailEntity::getTargetNo, queryParam.getTargetNo())
                .likeRight(StrUtil.isNotBlank(queryParam.getThirdPartyNo()), PayFinancialReconciliationDetailEntity::getThirdPartyNo, queryParam.getThirdPartyNo())
                .in(CollectionUtil.isNotEmpty(queryParam.getReconciliationResultList()), PayFinancialReconciliationDetailEntity::getReconciliationResult, queryParam.getReconciliationResultList())
                .orderByDesc(PayFinancialReconciliationDetailEntity::getCreateTime)
        );
        return AdminPayFinancialReconciliationDetailConvert.INSTANCE.entityListToResponseList(result);
    }

    public AdminPayFinancialReconciliationDetailResp getDetail(Long id) {
        PayFinancialReconciliationDetailEntity result = payFinancialReconciliationDetailService.getById(id);
        log.info("getById:{}", result);
        return AdminPayFinancialReconciliationDetailConvert.INSTANCE.entityToResponse(result);
    }

    public boolean save(AdminPayFinancialReconciliationDetailReq payFinancialReconciliationDetail) {
        PayFinancialReconciliationDetailEntity entity = AdminPayFinancialReconciliationDetailConvert.INSTANCE.reqToEntity(payFinancialReconciliationDetail);
        boolean saved = payFinancialReconciliationDetailService.save(entity);
        log.info("save:{}", saved);
        return saved;
    }

    public boolean updateById(AdminPayFinancialReconciliationDetailReq payFinancialReconciliationDetail) {
        PayFinancialReconciliationDetailEntity entity = AdminPayFinancialReconciliationDetailConvert.INSTANCE.reqToEntity(payFinancialReconciliationDetail);
        boolean updated = payFinancialReconciliationDetailService.updateById(entity);
        log.info("updateById:{}", updated);
        return updated;
    }

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