package cn.com.taiji.beidou.ship.focus.service.impl;

import cn.com.taiji.beidou.common.exception.GlobalException;
import cn.com.taiji.beidou.common.utils.SecurityUtils;
import cn.com.taiji.beidou.common.utils.spring.SpringUtils;
import cn.com.taiji.beidou.ship.focus.domain.FocusShipData;
import cn.com.taiji.beidou.ship.focus.domain.FocusShipReq;
import cn.com.taiji.beidou.ship.focus.domain.FocusShipResult;
import cn.com.taiji.beidou.ship.focus.domain.SimpleFocusShipResult;
import cn.com.taiji.beidou.ship.focus.mapper.FocusShipMapper;
import cn.com.taiji.beidou.ship.focus.service.FocusShipService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author CHEN
 * @Date 2022/11/9 18:13
 */
@Service
public class FocusShipServiceImpl implements FocusShipService {

    @Resource
    private FocusShipMapper focusShipMapper;


    @Override
    public int insertFocusShip(FocusShipData focusShipData) {

        if(focusShipMapper.selectByDevideNoUserId(focusShipData.getDevideNo(), focusShipData.getUserId())>0){
            throw new GlobalException("该船舶你已关注！");
        }
        return focusShipMapper.insertFocusShip(focusShipData);
    }

    @Override
    public int deleteFocusShip(Long focusId) {
        return focusShipMapper.deleteFocusShip(focusId);
    }

    @Override
    public FocusShipResult selectById(Long focusId) {
        Long device = focusShipMapper.getDeviceByFocusId(focusId);
        return focusShipMapper.selectById(device,focusId);
    }

    @Override
    public List<FocusShipResult> selectList(FocusShipReq focusShipReq) {
        Long userId = SecurityUtils.getUserId();
        focusShipReq.setUserId(userId);
        return focusShipMapper.selectList(focusShipReq);
    }

    @Override
    public List<SimpleFocusShipResult> getShipByUserId(String userId) {
        if (StringUtils.isBlank(userId)){
            userId = SecurityUtils.getUserId().toString();
        }
        return focusShipMapper.getShipByUserId(userId);
    }
}
