package org.dromara.system.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 org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.CaseMessageBo;
import org.dromara.system.domain.vo.CaseMessageVo;
import org.dromara.system.domain.CaseMessage;
import org.dromara.system.mapper.CaseMessageMapper;
import org.dromara.system.service.ICaseMessageService;

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

/**
 * 消息中心Service业务层处理
 *
 * @author 吴志刚
 * @date 2023-12-06
 */
@RequiredArgsConstructor
@Service
public class CaseMessageServiceImpl implements ICaseMessageService {

    private final CaseMessageMapper baseMapper;

    /**
     * 查询消息中心
     */
    @Override
    public CaseMessageVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询消息中心列表
     */
    @Override
    public TableDataInfo<CaseMessageVo> queryPageList(CaseMessageBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CaseMessage> lqw = buildQueryWrapper(bo);
        Page<CaseMessageVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询消息中心列表
     */
    @Override
    public List<CaseMessageVo> queryList(CaseMessageBo bo) {
        LambdaQueryWrapper<CaseMessage> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CaseMessage> buildQueryWrapper(CaseMessageBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CaseMessage> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getMessageType()), CaseMessage::getMessageType, bo.getMessageType());
        lqw.eq(StringUtils.isNotBlank(bo.getMessageContent()), CaseMessage::getMessageContent, bo.getMessageContent());
        lqw.eq(bo.getBusinessId() != null, CaseMessage::getBusinessId, bo.getBusinessId());
        lqw.eq(bo.getUserId() != null, CaseMessage::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getBusinessStatus()), CaseMessage::getBusinessStatus, bo.getBusinessStatus());
        lqw.orderByDesc(CaseMessage::getId);
        return lqw;
    }

    /**
     * 新增消息中心
     */
    @Override
    public Boolean insertByBo(CaseMessageBo bo) {
        CaseMessage add = MapstructUtils.convert(bo, CaseMessage.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改消息中心
     */
    @Override
    public Boolean updateByBo(CaseMessageBo bo) {
        CaseMessage update = MapstructUtils.convert(bo, CaseMessage.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 批量删除消息中心
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 消息批量插入
     *
     * @param messageList
     * @return
     */
    public Boolean insertBatch(List<CaseMessage> messageList) {
        return baseMapper.insertBatch(messageList);
    }
}

