package com.hotwj.platform.operationManagement.smsRechargeRecord.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.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 lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.hotwj.platform.operationManagement.smsRechargeRecord.domain.bo.HotSmsRechargeRecordBo;
import com.hotwj.platform.operationManagement.smsRechargeRecord.domain.vo.HotSmsRechargeRecordVo;
import com.hotwj.platform.operationManagement.smsRechargeRecord.domain.HotSmsRechargeRecord;
import com.hotwj.platform.operationManagement.smsRechargeRecord.mapper.HotSmsRechargeRecordMapper;
import com.hotwj.platform.operationManagement.smsRechargeRecord.service.IHotSmsRechargeRecordService;

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

/**
 * 短信充值记录Service业务层处理
 *
 * @author shihongwei
 * @date 2026-04-28
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class HotSmsRechargeRecordServiceImpl implements IHotSmsRechargeRecordService {

    private final HotSmsRechargeRecordMapper baseMapper;

    /**
     * 查询短信充值记录
     *
     * @param id 主键
     * @return 短信充值记录
     */
    @Override
    public HotSmsRechargeRecordVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询短信充值记录列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 短信充值记录分页列表
     */
    @Override
    public TableDataInfo<HotSmsRechargeRecordVo> queryPageList(HotSmsRechargeRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HotSmsRechargeRecord> lqw = buildQueryWrapper(bo);
        Page<HotSmsRechargeRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的短信充值记录列表
     *
     * @param bo 查询条件
     * @return 短信充值记录列表
     */
    @Override
    public List<HotSmsRechargeRecordVo> queryList(HotSmsRechargeRecordBo bo) {
        LambdaQueryWrapper<HotSmsRechargeRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HotSmsRechargeRecord> buildQueryWrapper(HotSmsRechargeRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HotSmsRechargeRecord> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(HotSmsRechargeRecord::getId);
        lqw.eq(bo.getCompanyId() != null, HotSmsRechargeRecord::getCompanyId, bo.getCompanyId());
        lqw.eq(bo.getEnterpriseId() != null, HotSmsRechargeRecord::getEnterpriseId, bo.getEnterpriseId());
        lqw.like(StringUtils.isNotBlank(bo.getEnterpriseName()), HotSmsRechargeRecord::getEnterpriseName, bo.getEnterpriseName());
        lqw.eq(bo.getRechargeOrderId() != null, HotSmsRechargeRecord::getRechargeOrderId, bo.getRechargeOrderId());
        lqw.eq(StringUtils.isNotBlank(bo.getRechargeOrderNo()), HotSmsRechargeRecord::getRechargeOrderNo, bo.getRechargeOrderNo());
        lqw.eq(bo.getRechargeTime() != null, HotSmsRechargeRecord::getRechargeTime, bo.getRechargeTime());
        lqw.eq(bo.getRechargeAmount() != null, HotSmsRechargeRecord::getRechargeAmount, bo.getRechargeAmount());
        lqw.eq(bo.getRechargeSmsCount() != null, HotSmsRechargeRecord::getRechargeSmsCount, bo.getRechargeSmsCount());
        lqw.eq(bo.getExpireTime() != null, HotSmsRechargeRecord::getExpireTime, bo.getExpireTime());
        lqw.eq(bo.getOperatorId() != null, HotSmsRechargeRecord::getOperatorId, bo.getOperatorId());
        lqw.like(StringUtils.isNotBlank(bo.getOperatorName()), HotSmsRechargeRecord::getOperatorName, bo.getOperatorName());
        lqw.eq(bo.getStatus() != null, HotSmsRechargeRecord::getStatus, bo.getStatus());
        lqw.like(StringUtils.isNotBlank(bo.getCreateByName()), HotSmsRechargeRecord::getCreateByName, bo.getCreateByName());
        lqw.like(StringUtils.isNotBlank(bo.getUpdateByName()), HotSmsRechargeRecord::getUpdateByName, bo.getUpdateByName());
        lqw.eq(bo.getIsDeleted() != null, HotSmsRechargeRecord::getIsDeleted, bo.getIsDeleted());
        return lqw;
    }

    /**
     * 新增短信充值记录
     *
     * @param bo 短信充值记录
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HotSmsRechargeRecordBo bo) {
        HotSmsRechargeRecord add = MapstructUtils.convert(bo, HotSmsRechargeRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改短信充值记录
     *
     * @param bo 短信充值记录
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HotSmsRechargeRecordBo bo) {
        HotSmsRechargeRecord update = MapstructUtils.convert(bo, HotSmsRechargeRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 校验并批量删除短信充值记录信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
