package com.hotwj.platform.operationManagement.smsPackagePurchase.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.smsPackagePurchase.domain.bo.HotSmsPackagePurchaseBo;
import com.hotwj.platform.operationManagement.smsPackagePurchase.domain.vo.HotSmsPackagePurchaseVo;
import com.hotwj.platform.operationManagement.smsPackagePurchase.domain.HotSmsPackagePurchase;
import com.hotwj.platform.operationManagement.smsPackagePurchase.mapper.HotSmsPackagePurchaseMapper;
import com.hotwj.platform.operationManagement.smsPackagePurchase.service.IHotSmsPackagePurchaseService;

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

/**
 * 短信套餐购买记录Service业务层处理
 *
 * @author shihongwei
 * @date 2026-04-28
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class HotSmsPackagePurchaseServiceImpl implements IHotSmsPackagePurchaseService {

    private final HotSmsPackagePurchaseMapper baseMapper;

    /**
     * 查询短信套餐购买记录
     *
     * @param id 主键
     * @return 短信套餐购买记录
     */
    @Override
    public HotSmsPackagePurchaseVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询短信套餐购买记录列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 短信套餐购买记录分页列表
     */
    @Override
    public TableDataInfo<HotSmsPackagePurchaseVo> queryPageList(HotSmsPackagePurchaseBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HotSmsPackagePurchase> lqw = buildQueryWrapper(bo);
        Page<HotSmsPackagePurchaseVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的短信套餐购买记录列表
     *
     * @param bo 查询条件
     * @return 短信套餐购买记录列表
     */
    @Override
    public List<HotSmsPackagePurchaseVo> queryList(HotSmsPackagePurchaseBo bo) {
        LambdaQueryWrapper<HotSmsPackagePurchase> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HotSmsPackagePurchase> buildQueryWrapper(HotSmsPackagePurchaseBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HotSmsPackagePurchase> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(HotSmsPackagePurchase::getId);
        lqw.eq(bo.getCompanyId() != null, HotSmsPackagePurchase::getCompanyId, bo.getCompanyId());
        lqw.eq(bo.getAccountId() != null, HotSmsPackagePurchase::getAccountId, bo.getAccountId());
        lqw.eq(bo.getUserId() != null, HotSmsPackagePurchase::getUserId, bo.getUserId());
        lqw.eq(bo.getConsumerCompanyId() != null, HotSmsPackagePurchase::getConsumerCompanyId, bo.getConsumerCompanyId());
        lqw.like(StringUtils.isNotBlank(bo.getConsumerCompanyName()), HotSmsPackagePurchase::getConsumerCompanyName, bo.getConsumerCompanyName());
        lqw.eq(bo.getOrderId() != null, HotSmsPackagePurchase::getOrderId, bo.getOrderId());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderNo()), HotSmsPackagePurchase::getOrderNo, bo.getOrderNo());
        lqw.eq(bo.getPackageId() != null, HotSmsPackagePurchase::getPackageId, bo.getPackageId());
        lqw.like(StringUtils.isNotBlank(bo.getPackageName()), HotSmsPackagePurchase::getPackageName, bo.getPackageName());
        lqw.eq(bo.getSmsCount() != null, HotSmsPackagePurchase::getSmsCount, bo.getSmsCount());
        lqw.eq(bo.getUnitPrice() != null, HotSmsPackagePurchase::getUnitPrice, bo.getUnitPrice());
        lqw.eq(bo.getQuantity() != null, HotSmsPackagePurchase::getQuantity, bo.getQuantity());
        lqw.eq(bo.getTotalAmount() != null, HotSmsPackagePurchase::getTotalAmount, bo.getTotalAmount());
        lqw.eq(bo.getPayMethod() != null, HotSmsPackagePurchase::getPayMethod, bo.getPayMethod());
        lqw.eq(bo.getPayStatus() != null, HotSmsPackagePurchase::getPayStatus, bo.getPayStatus());
        lqw.eq(bo.getOrderCreateTime() != null, HotSmsPackagePurchase::getOrderCreateTime, bo.getOrderCreateTime());
        lqw.eq(bo.getPayTime() != null, HotSmsPackagePurchase::getPayTime, bo.getPayTime());
        lqw.like(StringUtils.isNotBlank(bo.getCreateByName()), HotSmsPackagePurchase::getCreateByName, bo.getCreateByName());
        lqw.like(StringUtils.isNotBlank(bo.getUpdateByName()), HotSmsPackagePurchase::getUpdateByName, bo.getUpdateByName());
        lqw.eq(bo.getIsDeleted() != null, HotSmsPackagePurchase::getIsDeleted, bo.getIsDeleted());
        return lqw;
    }

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

    /**
     * 修改短信套餐购买记录
     *
     * @param bo 短信套餐购买记录
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HotSmsPackagePurchaseBo bo) {
        HotSmsPackagePurchase update = MapstructUtils.convert(bo, HotSmsPackagePurchase.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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