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.archives.domain.ShipDisposeData;
import cn.com.taiji.beidou.ship.warning.record.domain.*;
import cn.com.taiji.beidou.ship.warning.record.mapper.WarningRecordMapper;
import cn.com.taiji.beidou.ship.warning.record.service.WarningRecordService;
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.*;

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

/**
 * @Author CHEN
 * @Date 2022/11/22 18:02
 */
@Api(tags = "预警记录信息")
@RestController
public class ShipWarningRecordController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(ShipWarningRecordController.class);
    @Resource
    private WarningRecordService warningRecordService;

    @ApiOperation(value = "按预警类型统计",notes = "简化为预警规则")
    @PostMapping("/statisticsByRule")
    public R statisticsByRule(@RequestBody WarningRecordTotalReq req){
        return R.ok(warningRecordService.statisticsByRule(req.getDeptId(),req.getStartTime(),req.getEndTime()));
    }

    @ApiOperation(value = "预警记录列表",notes = "预警记录列表，默认为最近一天的预警记录")
    @PostMapping("/getWarningList")
    public TableDataInfo getWarningList(@RequestBody WarningRecordReq req){
        startPage(req.getPageNum(),req.getPageSize());
        return getDataTable(warningRecordService.warningRecordList(req));
    }

    @ApiOperation(value = "根据设备号和预警ID获取预警记录ID和时间",notes = "一条船舶有多条预警时，使用此接口展开多条预警进行处理")
    @PostMapping("/getWarningByDeviceId")
    public TableDataInfo getWarningByDeviceId(@RequestBody WarningRecordIdReq req){
        startPage(req.getPageNum(),req.getPageSize());
        return getDataTable(warningRecordService.getWarningRecordByDeviceId(req.getDevideNo(),req.getRuleId(),req.getStartTime(),req.getEndTime()));
    }

    @ApiOperation(value = "导出预警记录",notes = "参照北斗系统的导出，默认导出最新一天的预警记录")
    @PostMapping("/exportWarningRecord")
    public void exportWarningRecord(@RequestBody WarningRecordExcelReq req,HttpServletResponse response){
        List<WarningRecordExcel> excels = warningRecordService.warningRecordExcel(req.getRuleId(), req.getDeptId(),req.getStartTime(),req.getEndTime());
        ExcelUtil<WarningRecordExcel> util = new ExcelUtil<WarningRecordExcel>(WarningRecordExcel.class);
        AjaxResult result = util.exportExcel(excels, "预警记录");
        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);
        }

    }


    @ApiOperation("新增处置")
    @PostMapping("/addDispose")
    public R addDispose(@RequestBody ShipDisposeData shipDisposeData){
        try{
            warningRecordService.insertDispose(shipDisposeData);
            return R.ok();
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("处置状态")
    @GetMapping("/DisposeStatus")
    public R DisposeStatus(){
        return R.ok(warningRecordService.getDisposeStatus());
    }

    @ApiOperation("根据北斗号获取船舶预警")
    @GetMapping("/getRecordByDeviceId")
    public R<List<WarningRecordResult>> getRecordByDeviceId(String deviceId){
        return R.ok(warningRecordService.getRecordByDeviceId(deviceId));
    }


}
