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.CmsPayBizBo;
import com.propertyManagement.pay.domain.vo.CmsPayBizVo;
import com.propertyManagement.pay.domain.CmsPayBiz;
import com.propertyManagement.pay.mapper.PayBizMapper;
import com.propertyManagement.pay.service.IPayBizService;

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

/**
 * 支付业务Service业务层处理
 *
 * @author liushuai
 * @date 2025-05-12
 */
@RequiredArgsConstructor
@Service
public class PayBizServiceImpl implements IPayBizService {

    private final PayBizMapper baseMapper;

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

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

    /**
     * 查询支付业务列表
     */
    @Override
    public List<CmsPayBizVo> queryList(CmsPayBizBo bo) {
        LambdaQueryWrapper<CmsPayBiz> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

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

    private LambdaQueryWrapper<CmsPayBiz> buildQueryWrapper(CmsPayBizBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CmsPayBiz> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBizCode()), CmsPayBiz::getBizCode, bo.getBizCode());
        lqw.like(StringUtils.isNotBlank(bo.getBizName()), CmsPayBiz::getBizName, bo.getBizName());
        lqw.eq(StringUtils.isNotBlank(bo.getBizIntro()), CmsPayBiz::getBizIntro, bo.getBizIntro());
        lqw.eq(bo.getIsEnable() != null, CmsPayBiz::getIsEnable, bo.getIsEnable());
        lqw.eq(bo.getIsLock() != null, CmsPayBiz::getIsLock, bo.getIsLock());
        lqw.eq(StringUtils.isNotBlank(bo.getPayNotifyUrlTmpl()), CmsPayBiz::getPayNotifyUrlTmpl, bo.getPayNotifyUrlTmpl());
        lqw.eq(StringUtils.isNotBlank(bo.getRefundNotifyUrlTmpl()), CmsPayBiz::getRefundNotifyUrlTmpl, bo.getRefundNotifyUrlTmpl());
        return lqw;
    }

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

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

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

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