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.page.TableDataInfo;
import cn.com.taiji.beidou.common.core.redis.RedisCache;
import cn.com.taiji.beidou.common.utils.MathUtils;
import cn.com.taiji.beidou.ship.archives.domain.*;
import cn.com.taiji.beidou.ship.archives.service.ShipArchivesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

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

/**
 * @Author CHEN
 * @Date 2022/11/8 18:55
 */
@Api(tags = "船舶档案")
@RestController
public class ShipArchivesController extends BaseController {

    @Resource
    private ShipArchivesService shipArchivesService;

    @Resource
    private RedisCache redisCache;

    @ApiOperation("获取船舶档案列表")
    @PostMapping("/shipList")
    public TableDataInfo getShip(@RequestBody ShipArchivesREQ req){
        req.setDevideNo(req.getShipId());
        startPage(req.getPageNum(),req.getPageSize());
        List<ShipArchivesData> shipList = shipArchivesService.findShipList(req);
        return getDataTable(shipList);

    }

    @ApiOperation("获取船舶详情")
    @GetMapping("/shipDetail")
    public R<ShipArchivesData> findShipDetail(String devideNo){

        try {
            ShipArchivesData shipById = shipArchivesService.findShipById(devideNo);
            return R.ok(shipById);
        }catch (Exception e){
            return R.fail(e.getMessage());
        }

    }

    @ApiOperation(value = "处置过程详情")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "devideNo",value = "船舶编号，当只传这个条件时获取该船的最新处置"),
            @ApiImplicitParam(name = "warningId",value = "预警ID")
    })
    @GetMapping("/disposeDetail")
    public R<ShipDisposeResult> disposeDetail(@RequestParam(value = "devideNo",required = false) String devideNo,
                                              @RequestParam(value = "warningId",required = false)String warningId){

        try {
            ShipDisposeResult shipDisposeData = shipArchivesService.selectDisposeById(devideNo,warningId);

            return R.ok(shipDisposeData);
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "船舶档案统计",notes = "speed参数为前端拼的sql语句")
    @GetMapping("/getShipTotal")
    public R<ShipArchivesTotal> getShipTotal(String deptId,String speed){
        try {
            ShipArchivesTotal total = null;
            if (StringUtils.isBlank(deptId) || "4635".equals(deptId)){
                if(StringUtils.isBlank(speed)){
                    total = redisCache.getCacheObject("SHIP_TOTAL_4635");
                    if(total == null){
                        total = shipArchivesService.getShipTotal(deptId,null);
                    }
                }else {
                    total = shipArchivesService.getShipTotal(deptId,speed);
                }
            }else {
                 total = shipArchivesService.getShipTotal(deptId,speed);
            }
            return R.ok(total);
        }catch (Exception e){
            return R.fail(e.getMessage());
        }

    }


}
