package cn.com.taiji.beidou.web.controller.ship;

import cn.com.taiji.beidou.common.core.controller.BaseController;
import cn.com.taiji.beidou.common.core.domain.R;
import cn.com.taiji.beidou.common.core.domain.model.LoginUser;
import cn.com.taiji.beidou.common.core.page.TableDataInfo;
import cn.com.taiji.beidou.common.utils.SecurityUtils;
import cn.com.taiji.beidou.common.utils.StringUtils;
import cn.com.taiji.beidou.common.utils.uuid.SnowFlakeIdWorker;
import cn.com.taiji.beidou.framework.web.service.TokenService;
import cn.com.taiji.beidou.ship.focus.domain.*;
import cn.com.taiji.beidou.ship.focus.service.FocusShipService;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author CHEN
 * @Date 2022/11/9 18:16
 */
@Api(tags = "重点关注船舶")
@RestController
public class FocusShipController extends BaseController {

    @Resource
    private FocusShipService focusShipService;

    @ApiOperation("新增关注船舶")
    @PostMapping("/addFocusShip")
    public R addFocusShip(@RequestBody FocusShipData focusShipData){
        try{
            String userId = String.valueOf(SecurityUtils.getUserId());
            focusShipData.setFocusId(focusShipData.getDevideNo()+userId);
            focusShipData.setUserId(userId);
            focusShipService.insertFocusShip(focusShipData);
            return R.ok();
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("移除关注船舶")
    @GetMapping("/delFocusShip")
    public R delFocusShip(String devideNo){
        try{
            String id =devideNo+SecurityUtils.getUserId();
            long focusId = Long.parseLong(id);
            int i = focusShipService.deleteFocusShip(focusId);
            if(i<1){
                R.fail("取消关注失败");
            }
            return R.ok();
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("关注船舶详情")
    @GetMapping("/findFocusShip")
    public R<FocusShipResult> findFocusShip(String devideNo){
        try{
            String id =devideNo+SecurityUtils.getUserId();
            long focusId = Long.parseLong(id);
            return R.ok(focusShipService.selectById(focusId));
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("关注船舶列表")
    @PostMapping("/focusShipList")
    public TableDataInfo findFocusShipList(HttpServletRequest request,@RequestBody FocusShipReq focusShipReq){
        try{
            String sortType = focusShipReq.getSortType();
            if ("descending".equals(sortType)){
                focusShipReq.setSortType("DESC");
            }else if("ascending".equals(sortType)){
                focusShipReq.setSortType("ASC");
            }

            startPage(focusShipReq.getPageNum(),focusShipReq.getPageSize());
            List<FocusShipResult> results = focusShipService.selectList(focusShipReq);
            return getDataTable(results);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @ApiOperation(value = "获取船舶ID与关注类型",notes = "根据关注人id获取关注船舶id和类型，默认分页10")
    @PostMapping("/findFocusType")
    public TableDataInfo getShipByUserId(@RequestBody UserIdReq req){

        startPage(req.getPageNum(),req.getPageSize());
        return getDataTable(focusShipService.getShipByUserId(req.getUserId()));
    }

}
