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

import cn.com.taiji.beidou.common.config.RuoYiConfig;
import cn.com.taiji.beidou.common.core.controller.BaseController;
import cn.com.taiji.beidou.common.core.domain.AjaxResult;
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.StringUtils;
import cn.com.taiji.beidou.common.utils.file.FileUtils;
import cn.com.taiji.beidou.common.utils.poi.ExcelUtil;
import cn.com.taiji.beidou.ship.track.domain.ShipContainsReq;
import cn.com.taiji.beidou.ship.track.domain.ShipTrackReq;
import cn.com.taiji.beidou.ship.track.domain.ShipTrackResult;
import cn.com.taiji.beidou.ship.track.service.ShipTrackService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @Author CHEN
 * @Date 2023/1/12 18:09
 */
@Api(tags = "北斗轨迹")
@RestController
public class ShipTrackController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(ShipWarningRecordController.class);
    @Resource
    private ShipTrackService shipTrackService;

    @ApiOperation("北斗轨迹详情")
    @PostMapping("/getTrackDetail")
    public TableDataInfo getTrackDetail(@RequestBody ShipTrackReq req){

        startPage(req.getPageNum(),req.getPageSize());
        return getDataTable(shipTrackService.selectShipTrackDetail(req));

    }

    @ApiOperation("海域圈选历史查询")
    @PostMapping("/getContainsDetail")
    public TableDataInfo getContainsDetail(@RequestBody ShipContainsReq req){
        return getDataTable(shipTrackService.selectShipContainsResults(req));

    }

    @ApiOperation("航速分析")
    @PostMapping("/speedAnalysis")
    public R<List<ShipTrackResult>> speedAnalysis(@RequestBody ShipTrackReq req){

        return R.ok(shipTrackService.selectShipTrackDetail(req));

    }

    @ApiOperation("轨迹导出")
    @PostMapping("/getTrackExcel")
    public void getTrackDetailExcel(@RequestBody ShipTrackReq req, HttpServletResponse response){

            List<ShipTrackResult> shipTrackResults = shipTrackService.selectShipTrackDetail(req);
            ExcelUtil<ShipTrackResult> util = new ExcelUtil<ShipTrackResult>(ShipTrackResult.class);
            AjaxResult result = util.exportExcel(shipTrackResults, "北斗轨迹-"+req.getDeviceId(),
                    "北斗ID："+req.getDeviceId()+"在"+req.getStartTime()+" - "+req.getEndTime()+" 期间详细轨迹");
            String fileName = (String) result.get("msg");
            try {
                if (!FileUtils.checkAllowDownload(fileName)){
                    throw new Exception(StringUtils.format("文件名称({})非法，不允许下载。 ", fileName));
                }
                String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
                String filePath = RuoYiConfig.getDownloadPath() + fileName;
                response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
                FileUtils.setAttachmentResponseHeader(response, realFileName);
                FileUtils.writeBytes(filePath, response.getOutputStream());
                //是否删除已下载文件
                FileUtils.deleteFile(filePath);
            }
            catch (Exception e)
            {
                log.error("下载文件失败", e);
            }
    }

}
