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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotwj.platform.operationManagement.smsPackage.domain.HotSmsPackage;
import com.hotwj.platform.operationManagement.smsPackage.domain.bo.HotSmsPackageBo;
import com.hotwj.platform.operationManagement.smsPackage.domain.vo.HotSmsPackageVo;
import com.hotwj.platform.operationManagement.smsPackage.mapper.HotSmsPackageMapper;
import com.hotwj.platform.operationManagement.smsPackage.service.IHotSmsPackageService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Service;

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

/**
 * 短信套餐Service业务层处理
 *
 * @author shihongwei
 * @date 2026-05-08
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class HotSmsPackageServiceImpl implements IHotSmsPackageService {

    private final HotSmsPackageMapper baseMapper;

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

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

    /**
     * 查询符合条件的短信套餐列表
     *
     * @param bo 查询条件
     * @return 短信套餐列表
     */
    @Override
    public List<HotSmsPackageVo> queryList(HotSmsPackageBo bo) {
        LambdaQueryWrapper<HotSmsPackage> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HotSmsPackage> buildQueryWrapper(HotSmsPackageBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HotSmsPackage> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(HotSmsPackage::getId);
        lqw.eq(bo.getCompanyId() != null, HotSmsPackage::getCompanyId, bo.getCompanyId());
        lqw.eq(StringUtils.isNotBlank(bo.getPackageCode()), HotSmsPackage::getPackageCode, bo.getPackageCode());
        lqw.like(StringUtils.isNotBlank(bo.getPackageName()), HotSmsPackage::getPackageName, bo.getPackageName());
        lqw.eq(bo.getSmsCount() != null, HotSmsPackage::getSmsCount, bo.getSmsCount());
        lqw.eq(bo.getPackagePrice() != null, HotSmsPackage::getPackagePrice, bo.getPackagePrice());
        lqw.eq(bo.getUnitPrice() != null, HotSmsPackage::getUnitPrice, bo.getUnitPrice());
        lqw.eq(bo.getStatus() != null, HotSmsPackage::getStatus, bo.getStatus());
        lqw.eq(bo.getSortNo() != null, HotSmsPackage::getSortNo, bo.getSortNo());
        lqw.like(StringUtils.isNotBlank(bo.getCreateByName()), HotSmsPackage::getCreateByName, bo.getCreateByName());
        lqw.like(StringUtils.isNotBlank(bo.getUpdateByName()), HotSmsPackage::getUpdateByName, bo.getUpdateByName());
        lqw.eq(bo.getIsDeleted() != null, HotSmsPackage::getIsDeleted, bo.getIsDeleted());
        lqw.eq(bo.getActivityPrice() != null, HotSmsPackage::getActivityPrice, bo.getActivityPrice());
        return lqw;
    }

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

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

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

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