package com.qmth.sop.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.sop.business.bean.result.FlowTaskMsgResult;
import com.qmth.sop.business.bean.result.FlowTaskSmsResult;
import com.qmth.sop.business.entity.TFCustomFlowEntity;
import com.qmth.sop.business.mapper.TFCustomFlowEntityMapper;
import com.qmth.sop.business.service.TFCustomFlowEntityService;
import com.qmth.sop.common.enums.ExceptionResultEnum;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 自定义流程实体数据 服务实现类
 * </p>
 *
 * @author wangliang
 * @since 2023-07-17
 */
@Service
public class TFCustomFlowEntityServiceImpl extends ServiceImpl<TFCustomFlowEntityMapper, TFCustomFlowEntity>
        implements TFCustomFlowEntityService {

    /**
     * 根据id和用户id获取流程消息信息
     *
     * @param id
     * @param userId
     * @return
     */
    @Override
    public FlowTaskMsgResult getFlowTaskRemindMsgInfo(Long id, Long userId) {
        return this.baseMapper.getFlowTaskRemindMsgInfo(id, userId);
    }

    /**
     * 获取流程实体id发送短信内容
     *
     * @param id
     * @param userId
     * @return
     */
    @Override
    public FlowTaskSmsResult getFlowTaskRemindSmsInfo(Long id, Long userId) {
        return this.baseMapper.getFlowTaskRemindSmsInfo(id, userId);
    }

    /**
     * 获取crm单号发送短信内容
     *
     * @param crmNo
     * @param userId
     * @return
     */
    @Override
    public FlowTaskSmsResult getFlowTaskRemindSmsInfo(String crmNo, Long userId) {
        return this.baseMapper.getFlowTaskRemindSmsInfoByCrmNo(crmNo, userId);
    }

    /**
     * 根据code查询数据
     *
     * @param code
     * @return
     */
    @Override
    public TFCustomFlowEntity findByCode(String code) {
        TFCustomFlowEntity tfCustomFlowEntity = this.getOne(
                new QueryWrapper<TFCustomFlowEntity>().lambda().eq(TFCustomFlowEntity::getCode, code));
        Optional.ofNullable(tfCustomFlowEntity).orElseThrow(() -> ExceptionResultEnum.FLOW_ENTITY_NO_DATA.exception());
        return tfCustomFlowEntity;
    }

    /**
     * 根据派单号查询数据
     *
     * @param crmNo
     * @return
     */
    @Override
    public List<TFCustomFlowEntity> findByCrmNo(String crmNo) {
        List<TFCustomFlowEntity> tfCustomFlowEntityList = this.list(
                new QueryWrapper<TFCustomFlowEntity>().lambda().eq(TFCustomFlowEntity::getCrmNo, crmNo));
        if (CollectionUtils.isEmpty(tfCustomFlowEntityList)) {
            throw ExceptionResultEnum.FLOW_ENTITY_NO_DATA.exception();
        }
        return tfCustomFlowEntityList;
    }

    /**
     * 根据流程id查询数据
     *
     * @param flowId
     * @return
     */
    @Override
    public TFCustomFlowEntity findByFlowId(Long flowId) {
        TFCustomFlowEntity tfCustomFlowEntity = this.getOne(
                new QueryWrapper<TFCustomFlowEntity>().lambda().eq(TFCustomFlowEntity::getFlowId, flowId));
        Optional.ofNullable(tfCustomFlowEntity).orElseThrow(() -> ExceptionResultEnum.FLOW_ENTITY_NO_DATA.exception());
        return tfCustomFlowEntity;
    }
}
