package com.hotwj.platform.operationManagement.smsCompanyStat.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.smsCompanyStat.domain.bo.HotSmsCompanyStatBo;
import com.hotwj.platform.operationManagement.smsCompanyStat.domain.vo.HotSmsCompanyStatVo;
import com.hotwj.platform.operationManagement.smsCompanyStat.domain.HotSmsCompanyStat;
import com.hotwj.platform.operationManagement.smsCompanyStat.mapper.HotSmsCompanyStatMapper;
import com.hotwj.platform.operationManagement.smsCompanyStat.service.IHotSmsCompanyStatService;

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

/**
 * 短信企业统计Service业务层处理
 *
 * @author shihongwei
 * @date 2026-04-28
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class HotSmsCompanyStatServiceImpl implements IHotSmsCompanyStatService {

    private final HotSmsCompanyStatMapper baseMapper;

    /**
     * 查询短信企业统计
     *
     * @param id 主键
     * @return 短信企业统计
     */
    @Override
    public HotSmsCompanyStatVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询短信企业统计列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 短信企业统计分页列表
     */
    @Override
    public TableDataInfo<HotSmsCompanyStatVo> queryPageList(HotSmsCompanyStatBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HotSmsCompanyStat> lqw = buildQueryWrapper(bo);
        Page<HotSmsCompanyStatVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的短信企业统计列表
     *
     * @param bo 查询条件
     * @return 短信企业统计列表
     */
    @Override
    public List<HotSmsCompanyStatVo> queryList(HotSmsCompanyStatBo bo) {
        LambdaQueryWrapper<HotSmsCompanyStat> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HotSmsCompanyStat> buildQueryWrapper(HotSmsCompanyStatBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HotSmsCompanyStat> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(HotSmsCompanyStat::getId);
        lqw.eq(bo.getCompanyId() != null, HotSmsCompanyStat::getCompanyId, bo.getCompanyId());
        lqw.eq(bo.getEnterpriseId() != null, HotSmsCompanyStat::getEnterpriseId, bo.getEnterpriseId());
        lqw.like(StringUtils.isNotBlank(bo.getEnterpriseName()), HotSmsCompanyStat::getEnterpriseName, bo.getEnterpriseName());
        lqw.eq(bo.getTotalRechargeCount() != null, HotSmsCompanyStat::getTotalRechargeCount, bo.getTotalRechargeCount());
        lqw.eq(bo.getTotalSendCount() != null, HotSmsCompanyStat::getTotalSendCount, bo.getTotalSendCount());
        lqw.eq(bo.getTotalSuccessCount() != null, HotSmsCompanyStat::getTotalSuccessCount, bo.getTotalSuccessCount());
        lqw.eq(bo.getTotalBillableCount() != null, HotSmsCompanyStat::getTotalBillableCount, bo.getTotalBillableCount());
        lqw.eq(bo.getTotalRechargeAmount() != null, HotSmsCompanyStat::getTotalRechargeAmount, bo.getTotalRechargeAmount());
        lqw.eq(bo.getRemainingSmsCount() != null, HotSmsCompanyStat::getRemainingSmsCount, bo.getRemainingSmsCount());
        lqw.eq(bo.getLastStatTime() != null, HotSmsCompanyStat::getLastStatTime, bo.getLastStatTime());
        lqw.like(StringUtils.isNotBlank(bo.getCreateByName()), HotSmsCompanyStat::getCreateByName, bo.getCreateByName());
        lqw.like(StringUtils.isNotBlank(bo.getUpdateByName()), HotSmsCompanyStat::getUpdateByName, bo.getUpdateByName());
        lqw.eq(bo.getIsDeleted() != null, HotSmsCompanyStat::getIsDeleted, bo.getIsDeleted());
        return lqw;
    }

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

    /**
     * 修改短信企业统计
     *
     * @param bo 短信企业统计
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HotSmsCompanyStatBo bo) {
        HotSmsCompanyStat update = MapstructUtils.convert(bo, HotSmsCompanyStat.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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