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.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.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;


    @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("船舶档案统计")
    @GetMapping("/getShipTotal")
    public R<ShipArchivesTotal> getShipTotal(String shipId,String deptId){
        try {
            ShipArchivesTotal total = shipArchivesService.getShipTotal(shipId,deptId);
            Double shipTotal = Double.parseDouble(total.getShipTotal());

            total.setOnlineTotalRate(MathUtils.getHalfUp(Double.parseDouble(total.getOnlineTotal())/shipTotal*100,2));
            total.setOfflineTotalRate(MathUtils.getHalfUp(Double.parseDouble(total.getOfflineTotal())/shipTotal*100,2));
            total.setInPortRate(MathUtils.getHalfUp(Double.parseDouble(total.getInPort())/shipTotal*100,2));
            total.setOffPortRate(MathUtils.getHalfUp(Double.parseDouble(total.getOffPort())/shipTotal*100,2));
            total.setMarkRate(MathUtils.getHalfUp(Double.parseDouble(total.getMark())/shipTotal*100,2));
            return R.ok(total);
        }catch (Exception e){
            return R.fail(e.getMessage());
        }

    }


}
