package com.propertyManagement.pay.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
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 com.propertyManagement.pay.domain.CmsPayApp;
import com.propertyManagement.pay.mapper.PayAppMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.propertyManagement.pay.domain.bo.CmsPayMerchantBo;
import com.propertyManagement.pay.domain.vo.CmsPayMerchantVo;
import com.propertyManagement.pay.domain.CmsPayMerchant;
import com.propertyManagement.pay.mapper.PayMerchantMapper;
import com.propertyManagement.pay.service.IPayMerchantService;

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

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

    private final PayMerchantMapper baseMapper;
    private final PayAppMapper payAppMapper;

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

    /**
     * 查询支付商户
     */
    @Override
    public CmsPayMerchantVo getByCode(String code){
        LambdaQueryWrapper<CmsPayMerchant> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(CmsPayMerchant::getMerchantCode, code);
        return baseMapper.selectVoOne(queryWrapper);
    }

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

    /**
     * 查询支付商户列表
     */
    @Override
    public List<CmsPayMerchantVo> queryList(CmsPayMerchantBo bo) {
        LambdaQueryWrapper<CmsPayMerchant> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

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

    private LambdaQueryWrapper<CmsPayMerchant> buildQueryWrapper(CmsPayMerchantBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CmsPayMerchant> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getMerchantCode()), CmsPayMerchant::getMerchantCode, bo.getMerchantCode());
        lqw.like(StringUtils.isNotBlank(bo.getMerchantName()), CmsPayMerchant::getMerchantName, bo.getMerchantName());
        lqw.eq(StringUtils.isNotBlank(bo.getProviderCode()), CmsPayMerchant::getProviderCode, bo.getProviderCode());
        lqw.eq(StringUtils.isNotBlank(bo.getMerchantConfig()), CmsPayMerchant::getMerchantConfig, bo.getMerchantConfig());
        lqw.eq(bo.getIsEnable() != null, CmsPayMerchant::getIsEnable, bo.getIsEnable());
        lqw.eq(bo.getIsLock() != null, CmsPayMerchant::getIsLock, bo.getIsLock());
        return lqw;
    }

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

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

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

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

    /**
     * @param cmsPayMerchant
     */
    @Override
    public void syncEdit(CmsPayMerchantBo cmsPayMerchant) {
        updateByBo(cmsPayMerchant);

        LambdaUpdateWrapper<CmsPayApp> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(CmsPayApp::getAppConfig,cmsPayMerchant.getMerchantConfig());
        wrapper.eq(CmsPayApp::getMerchantCode,cmsPayMerchant.getMerchantCode());
        payAppMapper.update(wrapper);
    }
}
