package com.reyun.saas.mob.openapi.service.impl;

import com.reyun.saas.common.exception.CustomException;
import com.reyun.saas.common.restResult.ResultCode;
import com.reyun.saas.mob.openapi.dao.CollaborativeProjectMapper;
import com.reyun.saas.mob.openapi.domain.CollaborativeProject;
import com.reyun.saas.mob.openapi.service.CollaborativeProjectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

/**
 * @author: hanzepeng
 * @Date: 2022/02/09/15:02
 * @Description: openApi 合作项目业务层
 */
@Slf4j
@Service
public class CollaborativeProjectServiceImpl implements CollaborativeProjectService {

    @Autowired
    private CollaborativeProjectMapper projectMapper;

    /**
     * 根据token获取合作项目信息
     * @param token token
     * @return 合作项目实体对象
     * @throws CustomException token不可用会抛出自定义异常
     */
    @Override
    public CollaborativeProject findProjectByToken(String token) throws CustomException {
        if(StringUtils.isEmpty(token)){
            throw new CustomException(ResultCode.TOKEN_NOT_AVAILABLE);
        }
        Example example = new Example(CollaborativeProject.class);
        example.and().orEqualTo("token",token);
        example.and().orEqualTo("delFlag",false);
        CollaborativeProject collaborativeProject = projectMapper.selectOneByExample(example);
        if(collaborativeProject == null){
            throw new CustomException(ResultCode.TOKEN_NOT_AVAILABLE);
        }
        return collaborativeProject;
    }
}
