package com.djhk.uav.uav.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.djhk.common.annotation.Log;
import com.djhk.common.api.domain.vo.FileVo;
import com.djhk.common.core.controller.BaseController;
import com.djhk.common.core.domain.R;
import com.djhk.common.core.page.TableDataInfo;
import com.djhk.common.enums.BizCodeEnum;
import com.djhk.common.enums.BusinessType;
import com.djhk.common.utils.StringUtils;
import com.djhk.common.utils.poi.ExcelUtil;
import com.djhk.uav.airspace.domain.UavInfo;
import com.djhk.uav.equ.service.FileUploadService;
import com.djhk.uav.uav.service.IUavInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @description
 * @author jiachengshuai
 * @date 2023/2/13 13:50:13
 * @version 1.0
 */
@RestController
@RequestMapping("/uav/info")
public class UavInfoController extends BaseController
{
    @Autowired
    private IUavInfoService uavInfoService;

//    @Autowired
//    private FdDealerUtil fdDealerUtil;

//    @Autowired
//    private RemoteGpsService remoteGpsService;

    @Autowired
    private FileUploadService fileUploadService;

    /**
     * 查询列表
     */
//    @RequiresPermissions("uav:info:list")
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam Map<String, Object> param)
    {
        param.remove("uavTypeNameList");
        return uavInfoService.selectUavTableData(param);
    }

    /**
     * 查询所有的无人机信息列表
     */
//    @RequiresPermissions("uav:info:list")
    @GetMapping("/selectUavInfoList")
    public R<List<UavInfo>> selectUavInfoList(@RequestParam Map<String, Object> param)
    {
        return uavInfoService.selectUavInfoList(param);
    }

    /**
     * 导出列表
     */
//    @RequiresPermissions("uav:info:export")
//    @Log(title = "无人机信息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,Map<String, Object> param)
    {
        R<List<UavInfo>> r = uavInfoService.selectUavInfoList(param);
        if(r.getCode() == R.SUCCESS){
            List<UavInfo> list = r.getData();
            ExcelUtil<UavInfo> util = new ExcelUtil<UavInfo>(UavInfo.class);
            util.exportExcel(response, list, "无人机信息数据");
        }else{
            response.setStatus(500);
        }
    }

    /**
     * 获取详细信息
     */
//    @RequiresPermissions("uav:info:query")
    @GetMapping(value = "/{id}")
    public R<UavInfo> getInfo(@PathVariable("id") Long id)
    {
        return uavInfoService.selectUavInfoById(id);
    }

    /**
     * 新增
     */
//    @RequiresPermissions("uav:info:add")
    @Log(title = "无人机信息管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody UavInfo uavInfo)
    {
        if(StringUtils.isEmpty(uavInfo.getUavNum())){
            return R.fail("无人机编号不能为空");
        }
        if(StringUtils.isEmpty(uavInfo.getUavType())){
            return R.fail("无人机机型不能为空");
        }
        if(StringUtils.isEmpty(uavInfo.getMachineNum())){
            return R.fail("无人机机号不能为空");
        }
        return uavInfoService.insertUavInfo(uavInfo);
    }

    /**
     * 修改
     */
//    @RequiresPermissions("uav:info:edit")
    @Log(title = "无人机信息管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody UavInfo uavInfo)
    {
        if(StringUtils.isEmpty(uavInfo.getUavName())){
            return R.fail("无人机名称不能为空");
        }
        if(StringUtils.isEmpty(uavInfo.getUavNum())){
            return R.fail("无人机编号不能为空");
        }
        if(StringUtils.isEmpty(uavInfo.getUavType())){
            return R.fail("无人机机型不能为空");
        }
        if(StringUtils.isEmpty(uavInfo.getMachineNum())){
            return R.fail("无人机机号不能为空");
        }
        return uavInfoService.updateUavInfo(uavInfo);
    }

    /**
     * 修改
     */
    @PutMapping("/uavName")
    public R editInfo(@RequestBody Map<String,Object> map)
    {
        return uavInfoService.editInfo(map);
    }


    /**
     * 删除
     */
//    @RequiresPermissions("uav:info:remove")
    @Log(title = "无人机信息管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable Long[] ids)
    {
        return uavInfoService.deleteUavInfoByIds(ids);
    }

    /**
     * 根据无人机编号 获取无人机信息
     */
//    @RequiresPermissions("uav:order:query")
    @GetMapping(value = "/uavNum/{uavNum}")
    public R<UavInfo> getInfoByUavNum(@PathVariable("uavNum") String uavNum)
    {
        return uavInfoService.getInfoByUavNum(uavNum);
    }

    /**
     * 查询机场在线列表
     */
////    @RequiresPermissions("uav:order:query")
//    @GetMapping(value = "/online/docks")
//    public R<List<UavInfo>> getOnlineDocks()
//    {
//        return uavInfoService.getOnlineDocks();
//    }


//    /**
//     * @Description 查询无人机
//     **/
//    @Log(title = "查询飞行计划信息")
//    @GetMapping(value = "/online/{uavNum}")
//    public R<UavInfo> findUav(@PathVariable String uavNum)
//    {
//        return uavInfoService.getUavOnline(uavNum);
//    }

    /**
     * 推送无人机姿态信息
     * @param jsonObject
     * @return
     */
//    @PostMapping("/push/uav/pose")
//    public R pushUavPose(@RequestBody JSONObject jsonObject)
//    {
//        logger.info("/wsMsg begin,pushUavPose:"+ JSON.toJSONString(jsonObject));
//        // 发送到FdDealer做数据库存储
//        fdDealerUtil.sendMsg(jsonObject);
//
//        try {
//            //上传无人机轨迹数据到公安网
//            remoteGpsService.putGpsMsg(jsonObject);
//        }catch (Exception e){
//            logger.error("无人机轨迹数据上送服务GPS未启动！");
//        }
//
//        uavInfoService.pushUavPose(jsonObject);
//        return R.ok();
//    }

    /**
//     * @Description 查询机场最近一条设备属性
//     **/
//    @GetMapping(value = "/dock/latest/{dockNums}")
//    public R getDocksInfo(@PathVariable String[] dockNums)
//    {
//        return uavInfoService.getDocksInfo(dockNums);
//    }

    @PostMapping("/file")
    public R<FileVo> add(@RequestParam("file") MultipartFile file)
    {
        FileVo vo =  fileUploadService.fileUpload(file, "", "uav", BizCodeEnum.UAV_INFO.getCode(), "", "");
        return R.ok(vo);
    }
}
