package com.inspect.metadata.robuavtask.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.inspect.metadata.robuavtask.domain.BasedataPatrolRobUavTask;
import com.inspect.metadata.robuavtask.service.IBasedataPatrolRobUavTaskService;
import com.inspect.base.websocket.configure.WsPool;
import com.inspect.base.core.utils.DateUtils;
import com.inspect.base.core.utils.poi.ExcelUtil;
import com.inspect.base.core.web.controller.BaseController;
import com.inspect.base.core.web.domain.AjaxResult;
import com.inspect.base.core.web.page.TableDataInfo;
import com.inspect.common.log.annotation.Log;
import com.inspect.common.log.enums.BizType;
import com.inspect.common.security.annotation.RequiresPermissions;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/robuavtask"})
public class BasedataPatrolRobUavTaskController extends BaseController {
    @Resource
    private IBasedataPatrolRobUavTaskService basedataPatrolRobUavTaskService;

    @RequiresPermissions({"basedata/:robuavtask:list"})
    @GetMapping({"/list"})
    public TableDataInfo list(BasedataPatrolRobUavTask basedataPatrolRobuavTask) {
        this.startPage();
        List<BasedataPatrolRobUavTask> list = this.basedataPatrolRobUavTaskService.selectBasedataPatrolRobUavTaskList(basedataPatrolRobuavTask);
        return this.getDataTable(list);
    }

    @RequiresPermissions({"basedata/:robuavtask:export"})
    @Log(title = "机器人无人机巡检结果", businessType = BizType.EXPORT)
    @PostMapping({"/export"})
    public void export(HttpServletResponse response, BasedataPatrolRobUavTask basedataPatrolRobuavTask) {
        List<BasedataPatrolRobUavTask> list = this.basedataPatrolRobUavTaskService.selectBasedataPatrolRobUavTaskList(basedataPatrolRobuavTask);
        ExcelUtil<BasedataPatrolRobUavTask> util = new ExcelUtil<>(BasedataPatrolRobUavTask.class);
        util.exportExcel(response, list, "机器人无人机巡检结果数据");
    }

    @RequiresPermissions({"basedata/:robuavtask:query"})
    @GetMapping({"/{partolRobuavTaskId}"})
    public AjaxResult getInfo(@PathVariable("partolRobuavTaskId") Long patrolRobUavTaskId) {
        return AjaxResult.success(this.basedataPatrolRobUavTaskService.selectBasedataPatrolRobUavTaskByPatrolRobUavTaskId(patrolRobUavTaskId));
    }

    @RequiresPermissions({"basedata/:robuavtask:add"})
    @Log(title = "机器人无人机巡检结果", businessType = BizType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BasedataPatrolRobUavTask basedataPatrolRobuavTask) {
        return this.toAjax(this.basedataPatrolRobUavTaskService.insertBasedataPatrolRobUavTask(basedataPatrolRobuavTask));
    }

    @RequiresPermissions({"basedata/:robuavtask:edit"})
    @Log(title = "机器人无人机巡检结果", businessType = BizType.UPDATE)
    @PostMapping({"/edit"})
    public AjaxResult edit(@RequestBody BasedataPatrolRobUavTask basedataPatrolRobuavTask) {
        return this.toAjax(this.basedataPatrolRobUavTaskService.updateBasedataPatrolRobUavTask(basedataPatrolRobuavTask));
    }

    @RequiresPermissions({"basedata/:robuavtask:remove"})
    @Log(title = "机器人无人机巡检结果", businessType = BizType.DELETE)
    @PostMapping({"/remove/{partolRobuavTaskIds}"})
    public AjaxResult remove(@PathVariable("partolRobuavTaskIds") Long[] patrolRobUavTaskIds) {
        return this.toAjax(this.basedataPatrolRobUavTaskService.deleteBasedataPatrolRobUavTaskByPatrolRobUavTaskIds(patrolRobUavTaskIds));
    }

    @PostMapping({"/partolRobuavTaskInfoUp"})
    public AjaxResult patrolRobUavTaskInfoUp(@RequestBody BasedataPatrolRobUavTask basedataPatrolRobuavTask) {
        int retFlag = this.basedataPatrolRobUavTaskService.insertBasedataPatrolRobUavTask(basedataPatrolRobuavTask);
        if (retFlag == 1) {
            String dateTimeNow = DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss");
            JSONObject returnJsonObject = new JSONObject();
            new JSONObject();
            returnJsonObject.put("data", basedataPatrolRobuavTask);
            returnJsonObject.put("receiveTime", dateTimeNow);
            returnJsonObject.put("event", "robuavTask/robuavTaskInfoUp");
            String jsonString = JSON.toJSONString(returnJsonObject);
            this.parseMessage(jsonString);
        }

        return this.toAjax(retFlag);
    }

    @GetMapping({"/downloadPartolRobuavTaskFile"})
    public AjaxResult downloadPatrolRobUavTaskFile(@PathVariable("partolRobuavTaskId") Long patrolRobUavTaskId) throws IOException {
        BasedataPatrolRobUavTask basedataPatrolRobuavTask = this.basedataPatrolRobUavTaskService.selectBasedataPatrolRobUavTaskByPatrolRobUavTaskId(patrolRobUavTaskId);
        Map map = new HashMap<>();
        map.put("path", basedataPatrolRobuavTask.getTaskFilePath());
        return AjaxResult.success(map);
    }

    public void parseMessage(String order) {
        WsPool.sendMessageToAll(order);
    }
}
