package com.hotwj.platform.operationManagement.smsDailyStat.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.smsDailyStat.domain.bo.HotSmsDailyStatBo;
import com.hotwj.platform.operationManagement.smsDailyStat.domain.vo.HotSmsDailyStatVo;
import com.hotwj.platform.operationManagement.smsDailyStat.domain.HotSmsDailyStat;
import com.hotwj.platform.operationManagement.smsDailyStat.mapper.HotSmsDailyStatMapper;
import com.hotwj.platform.operationManagement.smsDailyStat.service.IHotSmsDailyStatService;

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

/**
 * 短信按日统计Service业务层处理
 *
 * @author shihongwei
 * @date 2026-04-28
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class HotSmsDailyStatServiceImpl implements IHotSmsDailyStatService {

    private final HotSmsDailyStatMapper baseMapper;

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

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

    /**
     * 查询符合条件的短信按日统计列表
     *
     * @param bo 查询条件
     * @return 短信按日统计列表
     */
    @Override
    public List<HotSmsDailyStatVo> queryList(HotSmsDailyStatBo bo) {
        LambdaQueryWrapper<HotSmsDailyStat> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HotSmsDailyStat> buildQueryWrapper(HotSmsDailyStatBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HotSmsDailyStat> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(HotSmsDailyStat::getId);
        lqw.eq(bo.getCompanyId() != null, HotSmsDailyStat::getCompanyId, bo.getCompanyId());
        lqw.eq(bo.getEnterpriseId() != null, HotSmsDailyStat::getEnterpriseId, bo.getEnterpriseId());
        lqw.like(StringUtils.isNotBlank(bo.getEnterpriseName()), HotSmsDailyStat::getEnterpriseName, bo.getEnterpriseName());
        lqw.eq(bo.getStatDate() != null, HotSmsDailyStat::getStatDate, bo.getStatDate());
        lqw.eq(bo.getTotalSendCount() != null, HotSmsDailyStat::getTotalSendCount, bo.getTotalSendCount());
        lqw.eq(bo.getSuccessCount() != null, HotSmsDailyStat::getSuccessCount, bo.getSuccessCount());
        lqw.eq(bo.getBillableCount() != null, HotSmsDailyStat::getBillableCount, bo.getBillableCount());
        lqw.eq(bo.getFailCount() != null, HotSmsDailyStat::getFailCount, bo.getFailCount());
        lqw.like(StringUtils.isNotBlank(bo.getCreateByName()), HotSmsDailyStat::getCreateByName, bo.getCreateByName());
        lqw.like(StringUtils.isNotBlank(bo.getUpdateByName()), HotSmsDailyStat::getUpdateByName, bo.getUpdateByName());
        lqw.eq(bo.getIsDeleted() != null, HotSmsDailyStat::getIsDeleted, bo.getIsDeleted());
        return lqw;
    }

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

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

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

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