package com.propertyManagement.pay.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.propertyManagement.common.enums.IsEnableType;
import com.propertyManagement.common.utils.StringUtils;
import com.propertyManagement.common.core.page.TableDataInfo;
import com.propertyManagement.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.propertyManagement.pay.domain.bo.CmsPayProviderBo;
import com.propertyManagement.pay.domain.vo.CmsPayProviderVo;
import com.propertyManagement.pay.domain.CmsPayProvider;
import com.propertyManagement.pay.mapper.PayProviderMapper;
import com.propertyManagement.pay.service.IPayProviderService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 支付服务商Service业务层处理
 *
 * @author liushuai
 * @date 2025-05-12
 */
@RequiredArgsConstructor
@Service
public class PayProviderServiceImpl implements IPayProviderService {

    private final PayProviderMapper baseMapper;

    /**
     * 查询支付服务商
     */
    @Override
    public CmsPayProviderVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询支付服务商列表
     */
    @Override
    public TableDataInfo<CmsPayProviderVo> queryPageList(CmsPayProviderBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CmsPayProvider> lqw = buildQueryWrapper(bo);
        Page<CmsPayProviderVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询支付服务商列表
     */
    @Override
    public List<CmsPayProviderVo> queryList(CmsPayProviderBo bo) {
        LambdaQueryWrapper<CmsPayProvider> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    @Override
    public List<CmsPayProvider> listAllUsable() {
        LambdaQueryWrapper<CmsPayProvider> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CmsPayProvider::getIsEnable, IsEnableType.YES.getCode());
        wrapper.eq(CmsPayProvider::getIsLock,IsEnableType.YES.getCode());
        return baseMapper.selectList(wrapper);
    }

    private LambdaQueryWrapper<CmsPayProvider> buildQueryWrapper(CmsPayProviderBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CmsPayProvider> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getProviderCode()), CmsPayProvider::getProviderCode, bo.getProviderCode());
        lqw.like(StringUtils.isNotBlank(bo.getProviderName()), CmsPayProvider::getProviderName, bo.getProviderName());
        lqw.eq(StringUtils.isNotBlank(bo.getLogo()), CmsPayProvider::getLogo, bo.getLogo());
        lqw.eq(bo.getIsEnable() != null, CmsPayProvider::getIsEnable, bo.getIsEnable());
        lqw.eq(bo.getIsLock() != null, CmsPayProvider::getIsLock, bo.getIsLock());
        return lqw;
    }

    /**
     * 新增支付服务商
     */
    @Override
    public Boolean insertByBo(CmsPayProviderBo bo) {
        CmsPayProvider add = BeanUtil.toBean(bo, CmsPayProvider.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改支付服务商
     */
    @Override
    public Boolean updateByBo(CmsPayProviderBo bo) {
        CmsPayProvider update = BeanUtil.toBean(bo, CmsPayProvider.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CmsPayProvider entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除支付服务商
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
