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.CmsPayAppBo;
import com.propertyManagement.pay.domain.vo.CmsPayAppVo;
import com.propertyManagement.pay.domain.CmsPayApp;
import com.propertyManagement.pay.mapper.PayAppMapper;
import com.propertyManagement.pay.service.IPayAppService;

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

/**
 * 支付应用Service业务层处理
 *
 * @author liushuai
 * @date 2025-05-12
 */
@RequiredArgsConstructor
@Service
public class PayAppServiceImpl implements IPayAppService {

    private final PayAppMapper baseMapper;

    /**
     * 查询支付应用
     */
    @Override
    public CmsPayAppVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询支付应用列表
     */
    @Override
    public TableDataInfo<CmsPayAppVo> queryPageList(CmsPayAppBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CmsPayApp> lqw = buildQueryWrapper(bo);
        Page<CmsPayAppVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询支付应用列表
     */
    @Override
    public List<CmsPayAppVo> queryList(CmsPayAppBo bo) {
        LambdaQueryWrapper<CmsPayApp> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

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

    private LambdaQueryWrapper<CmsPayApp> buildQueryWrapper(CmsPayAppBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CmsPayApp> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getAppId()), CmsPayApp::getAppId, bo.getAppId());
        lqw.eq(StringUtils.isNotBlank(bo.getAppCode()), CmsPayApp::getAppCode, bo.getAppCode());
        lqw.like(StringUtils.isNotBlank(bo.getAppName()), CmsPayApp::getAppName, bo.getAppName());
        lqw.eq(StringUtils.isNotBlank(bo.getTradeType()), CmsPayApp::getTradeType, bo.getTradeType());
        lqw.eq(StringUtils.isNotBlank(bo.getMerchantCode()), CmsPayApp::getMerchantCode, bo.getMerchantCode());
        lqw.eq(StringUtils.isNotBlank(bo.getProviderCode()), CmsPayApp::getProviderCode, bo.getProviderCode());
        lqw.eq(StringUtils.isNotBlank(bo.getAppConfig()), CmsPayApp::getAppConfig, bo.getAppConfig());
        lqw.eq(bo.getIsEnable() != null, CmsPayApp::getIsEnable, bo.getIsEnable());
        lqw.eq(bo.getIsLock() != null, CmsPayApp::getIsLock, bo.getIsLock());
        return lqw;
    }

    /**
     * 新增支付应用
     */
    @Override
    public Boolean insertByBo(CmsPayAppBo bo) {
        CmsPayApp add = BeanUtil.toBean(bo, CmsPayApp.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改支付应用
     */
    @Override
    public Boolean updateByBo(CmsPayAppBo bo) {
        CmsPayApp update = BeanUtil.toBean(bo, CmsPayApp.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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