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.AdminPayFinancialReconciliationPageReq;
import com.seer.teach.pay.admin.financial.controller.request.AdminPayFinancialReconciliationQueryReq;
import com.seer.teach.pay.admin.financial.controller.request.AdminPayFinancialReconciliationReq;
import com.seer.teach.pay.admin.financial.controller.response.AdminPayFinancialReconciliationResp;
import com.seer.teach.pay.admin.financial.convert.AdminPayFinancialReconciliationConvert;
import com.seer.teach.pay.entity.PayFinancialReconciliationEntity;
import com.seer.teach.pay.service.IPayFinancialReconciliationService;
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 AdminPayFinancialReconciliationService {

    private final IPayFinancialReconciliationService payFinancialReconciliationService;

    public PageListBean<AdminPayFinancialReconciliationResp> pageList(AdminPayFinancialReconciliationPageReq pageRequest) {
        IPage<PayFinancialReconciliationEntity> page = new Page<>(pageRequest.getPageNo(), pageRequest.getPageSize());

        IPage<PayFinancialReconciliationEntity> result = payFinancialReconciliationService.page(page, new LambdaQueryWrapper<PayFinancialReconciliationEntity>()
                .likeRight(StrUtil.isNotBlank(pageRequest.getReconciliationNo()), PayFinancialReconciliationEntity::getReconciliationNo, pageRequest.getReconciliationNo())
                .eq(Objects.nonNull(pageRequest.getReconciliationType()), PayFinancialReconciliationEntity::getReconciliationType, pageRequest.getReconciliationType())
                .eq(StrUtil.isNotBlank(pageRequest.getReconciliationCycle()), PayFinancialReconciliationEntity::getReconciliationCycle, pageRequest.getReconciliationCycle())
                .ge(Objects.nonNull(pageRequest.getStartTime()), PayFinancialReconciliationEntity::getStartTime, pageRequest.getStartTime())
                .le(Objects.nonNull(pageRequest.getEndTime()), PayFinancialReconciliationEntity::getEndTime, pageRequest.getEndTime())
                .in(CollectionUtil.isNotEmpty(pageRequest.getReconciliationStatusList()), PayFinancialReconciliationEntity::getReconciliationStatus, pageRequest.getReconciliationStatusList())
                .eq(Objects.nonNull(pageRequest.getReconciliationUserId()), PayFinancialReconciliationEntity::getReconciliationUserId, pageRequest.getReconciliationUserId())
                .ge(Objects.nonNull(pageRequest.getReconciliationTime()), PayFinancialReconciliationEntity::getReconciliationTime, pageRequest.getReconciliationTime())
                .orderByDesc(PayFinancialReconciliationEntity::getCreateTime)
        );

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

    public List<AdminPayFinancialReconciliationResp> list(AdminPayFinancialReconciliationQueryReq queryParam) {
        List<PayFinancialReconciliationEntity> result = payFinancialReconciliationService.list(new LambdaQueryWrapper<PayFinancialReconciliationEntity>()
                .likeRight(StrUtil.isNotBlank(queryParam.getReconciliationNo()), PayFinancialReconciliationEntity::getReconciliationNo, queryParam.getReconciliationNo())
                .eq(Objects.nonNull(queryParam.getReconciliationType()), PayFinancialReconciliationEntity::getReconciliationType, queryParam.getReconciliationType())
                .eq(StrUtil.isNotBlank(queryParam.getReconciliationCycle()), PayFinancialReconciliationEntity::getReconciliationCycle, queryParam.getReconciliationCycle())
                .ge(Objects.nonNull(queryParam.getStartTime()), PayFinancialReconciliationEntity::getStartTime, queryParam.getStartTime())
                .le(Objects.nonNull(queryParam.getEndTime()), PayFinancialReconciliationEntity::getEndTime, queryParam.getEndTime())
                .in(CollectionUtil.isNotEmpty(queryParam.getReconciliationStatusList()), PayFinancialReconciliationEntity::getReconciliationStatus, queryParam.getReconciliationStatusList())
                .eq(Objects.nonNull(queryParam.getReconciliationUserId()), PayFinancialReconciliationEntity::getReconciliationUserId, queryParam.getReconciliationUserId())
                .ge(Objects.nonNull(queryParam.getReconciliationTime()), PayFinancialReconciliationEntity::getReconciliationTime, queryParam.getReconciliationTime())
                .orderByDesc(PayFinancialReconciliationEntity::getCreateTime)
        );
        return AdminPayFinancialReconciliationConvert.INSTANCE.entityListToResponseList(result);
    }

    public AdminPayFinancialReconciliationResp getDetail(Long id) {
        PayFinancialReconciliationEntity result = payFinancialReconciliationService.getById(id);
        log.info("getById:{}", result);
        return AdminPayFinancialReconciliationConvert.INSTANCE.entityToResponse(result);
    }

    public boolean save(AdminPayFinancialReconciliationReq payFinancialReconciliation) {
        PayFinancialReconciliationEntity entity = AdminPayFinancialReconciliationConvert.INSTANCE.reqToEntity(payFinancialReconciliation);
        boolean saved = payFinancialReconciliationService.save(entity);
        log.info("save:{}", saved);
        return saved;
    }

    public boolean updateById(AdminPayFinancialReconciliationReq payFinancialReconciliation) {
        PayFinancialReconciliationEntity entity = AdminPayFinancialReconciliationConvert.INSTANCE.reqToEntity(payFinancialReconciliation);
        boolean updated = payFinancialReconciliationService.updateById(entity);
        log.info("updateById:{}", updated);
        return updated;
    }

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