package com.propertyManagement.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
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.business.domain.bo.WxPayRecordBo;
import com.propertyManagement.business.domain.vo.WxPayRecordVo;
import com.propertyManagement.business.domain.WxPayRecord;
import com.propertyManagement.business.mapper.WxPayRecordMapper;
import com.propertyManagement.business.service.IWxPayRecordService;

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

/**
 * 缴费记录Service业务层处理
 *
 * @author liushuai
 * @date 2025-05-14
 */
@RequiredArgsConstructor
@Service
public class WxPayRecordServiceImpl implements IWxPayRecordService {

    private final WxPayRecordMapper baseMapper;

    /**
     * 查询缴费记录
     */
    @Override
    public WxPayRecordVo queryById(Long recordId){
        return baseMapper.selectVoById(recordId);
    }

    /**
     * 查询缴费记录列表
     */
    @Override
    public TableDataInfo<WxPayRecordVo> queryPageList(WxPayRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WxPayRecord> lqw = buildQueryWrapper(bo);
        Page<WxPayRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询缴费记录列表
     */
    @Override
    public List<WxPayRecordVo> queryList(WxPayRecordBo bo) {
        LambdaQueryWrapper<WxPayRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WxPayRecord> buildQueryWrapper(WxPayRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WxPayRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, WxPayRecord::getUserId, bo.getUserId());
        lqw.like(StringUtils.isNotBlank(bo.getUserName()), WxPayRecord::getUserName, bo.getUserName());
        lqw.like(bo.getCommunityName() != null, WxPayRecord::getCommunityName, bo.getCommunityName());
        lqw.like(StringUtils.isNotBlank(bo.getProprietorName()), WxPayRecord::getProprietorName, bo.getProprietorName());
        lqw.eq(StringUtils.isNotBlank(bo.getRoom()), WxPayRecord::getRoom, bo.getRoom());
        lqw.eq(StringUtils.isNotBlank(bo.getPlateNumber()), WxPayRecord::getPlateNumber, bo.getPlateNumber());
        lqw.eq(bo.getBillId() != null, WxPayRecord::getBillId, bo.getBillId());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderType()), WxPayRecord::getOrderType, bo.getOrderType());
        lqw.eq(StringUtils.isNotBlank(bo.getPayChannelCode()), WxPayRecord::getPayChannelCode, bo.getPayChannelCode());
        lqw.eq(StringUtils.isNotBlank(bo.getPayMchCode()), WxPayRecord::getPayMchCode, bo.getPayMchCode());
        lqw.eq(StringUtils.isNotBlank(bo.getPayTradeType()), WxPayRecord::getPayTradeType, bo.getPayTradeType());
        lqw.eq(StringUtils.isNotBlank(bo.getPayAppCode()), WxPayRecord::getPayAppCode, bo.getPayAppCode());
        lqw.eq(StringUtils.isNotBlank(bo.getPayBizCode()), WxPayRecord::getPayBizCode, bo.getPayBizCode());
        lqw.eq(bo.getPayAmount() != null, WxPayRecord::getPayAmount, bo.getPayAmount());
        lqw.eq(bo.getPayState() != null, WxPayRecord::getPayState, bo.getPayState());
        lqw.eq(StringUtils.isNotBlank(bo.getOutTradeNo()), WxPayRecord::getOutTradeNo, bo.getOutTradeNo());
        lqw.eq(StringUtils.isNotBlank(bo.getTradeNo()), WxPayRecord::getTradeNo, bo.getTradeNo());
        return lqw;
    }

    /**
     * 新增缴费记录
     */
    @Override
    public Boolean insertByBo(WxPayRecordBo bo) {
        WxPayRecord add = BeanUtil.toBean(bo, WxPayRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setRecordId(add.getRecordId());
        }
        return flag;
    }

    /**
     * 修改缴费记录
     */
    @Override
    public Boolean updateByBo(WxPayRecordBo bo) {
        WxPayRecord update = BeanUtil.toBean(bo, WxPayRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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