package com.hotwj.platform.operationManagement.smsSendDetail.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.smsSendDetail.domain.bo.HotSmsSendDetailBo;
import com.hotwj.platform.operationManagement.smsSendDetail.domain.vo.HotSmsSendDetailVo;
import com.hotwj.platform.operationManagement.smsSendDetail.domain.HotSmsSendDetail;
import com.hotwj.platform.operationManagement.smsSendDetail.mapper.HotSmsSendDetailMapper;
import com.hotwj.platform.operationManagement.smsSendDetail.service.IHotSmsSendDetailService;

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

/**
 * 短信发送明细Service业务层处理
 *
 * @author shihongwei
 * @date 2026-04-28
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class HotSmsSendDetailServiceImpl implements IHotSmsSendDetailService {

    private final HotSmsSendDetailMapper baseMapper;

    /**
     * 查询短信发送明细
     *
     * @param id 主键
     * @return 短信发送明细
     */
    @Override
    public HotSmsSendDetailVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询短信发送明细列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 短信发送明细分页列表
     */
    @Override
    public TableDataInfo<HotSmsSendDetailVo> queryPageList(HotSmsSendDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HotSmsSendDetail> lqw = buildQueryWrapper(bo);
        Page<HotSmsSendDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的短信发送明细列表
     *
     * @param bo 查询条件
     * @return 短信发送明细列表
     */
    @Override
    public List<HotSmsSendDetailVo> queryList(HotSmsSendDetailBo bo) {
        LambdaQueryWrapper<HotSmsSendDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HotSmsSendDetail> buildQueryWrapper(HotSmsSendDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HotSmsSendDetail> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(HotSmsSendDetail::getId);
        lqw.eq(bo.getCompanyId() != null, HotSmsSendDetail::getCompanyId, bo.getCompanyId());
        lqw.eq(bo.getEnterpriseId() != null, HotSmsSendDetail::getEnterpriseId, bo.getEnterpriseId());
        lqw.like(StringUtils.isNotBlank(bo.getEnterpriseName()), HotSmsSendDetail::getEnterpriseName, bo.getEnterpriseName());
        lqw.eq(StringUtils.isNotBlank(bo.getBizType()), HotSmsSendDetail::getBizType, bo.getBizType());
        lqw.eq(StringUtils.isNotBlank(bo.getSmsType()), HotSmsSendDetail::getSmsType, bo.getSmsType());
        lqw.eq(bo.getReceiverId() != null, HotSmsSendDetail::getReceiverId, bo.getReceiverId());
        lqw.like(StringUtils.isNotBlank(bo.getReceiverName()), HotSmsSendDetail::getReceiverName, bo.getReceiverName());
        lqw.eq(StringUtils.isNotBlank(bo.getMobile()), HotSmsSendDetail::getMobile, bo.getMobile());
        lqw.eq(StringUtils.isNotBlank(bo.getReceiverRole()), HotSmsSendDetail::getReceiverRole, bo.getReceiverRole());
        lqw.eq(StringUtils.isNotBlank(bo.getNoticeContent()), HotSmsSendDetail::getNoticeContent, bo.getNoticeContent());
        lqw.eq(bo.getNotifyTime() != null, HotSmsSendDetail::getNotifyTime, bo.getNotifyTime());
        lqw.eq(bo.getNotifyStatus() != null, HotSmsSendDetail::getNotifyStatus, bo.getNotifyStatus());
        lqw.eq(bo.getIsReceived() != null, HotSmsSendDetail::getIsReceived, bo.getIsReceived());
        lqw.eq(bo.getReceiveTime() != null, HotSmsSendDetail::getReceiveTime, bo.getReceiveTime());
        lqw.eq(bo.getSendChannel() != null, HotSmsSendDetail::getSendChannel, bo.getSendChannel());
        lqw.eq(bo.getSmsCount() != null, HotSmsSendDetail::getSmsCount, bo.getSmsCount());
        lqw.eq(bo.getBillableCount() != null, HotSmsSendDetail::getBillableCount, bo.getBillableCount());
        lqw.eq(StringUtils.isNotBlank(bo.getProviderMsgId()), HotSmsSendDetail::getProviderMsgId, bo.getProviderMsgId());
        lqw.like(StringUtils.isNotBlank(bo.getCreateByName()), HotSmsSendDetail::getCreateByName, bo.getCreateByName());
        lqw.like(StringUtils.isNotBlank(bo.getUpdateByName()), HotSmsSendDetail::getUpdateByName, bo.getUpdateByName());
        lqw.eq(bo.getIsDeleted() != null, HotSmsSendDetail::getIsDeleted, bo.getIsDeleted());
        return lqw;
    }

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

    /**
     * 修改短信发送明细
     *
     * @param bo 短信发送明细
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HotSmsSendDetailBo bo) {
        HotSmsSendDetail update = MapstructUtils.convert(bo, HotSmsSendDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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