package com.djhk.uav.uav.controller;

import com.djhk.common.annotation.Log;
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.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.uav.model.UavRepair;
import com.djhk.uav.uav.service.IUavRepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * @description 维护记录管理
 * @author mayongkai
 * @date 2023/3/11 19:16:58
 * @version 1.0
 */
@RestController
@RequestMapping("/repair")
public class UavRepairController extends BaseController
{
    @Autowired
    private IUavRepairService uavRepairService;

    /**
     * 查询列表
     */
//    @RequiresPermissions("uav:info:list")
    @GetMapping("/sub/list")
    public TableDataInfo subList(@RequestParam Map<String, Object> param)
    {
        return uavRepairService.selectUavRepairSubData(param);
    }

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

    /**
     * 查询所有的维护记录信息列表
     */
//    @RequiresPermissions("uav:info:list")
    @GetMapping("/selectUavRepairList")
    public R<List<UavRepair>> selectUavRepairList(@RequestParam Map<String, Object> param)
    {
        return uavRepairService.selectUavRepairList(param);
    }

    /**
     * 导出列表
     */
//    @RequiresPermissions("uav:Repair:export")
    @Log(title = "维护记录信息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,@RequestBody Map<String, Object> param)
    {
        R<List<UavRepair>> r = uavRepairService.selectUavRepairList(param);
        if(r.getCode() == R.SUCCESS){
            List<UavRepair> list = r.getData();
            ExcelUtil<UavRepair> util = new ExcelUtil<UavRepair>(UavRepair.class);
            util.exportExcel(response, list, "维护记录信息数据");
        }else{
            response.setStatus(500);
        }
    }

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

    /**
     * 新增
     */
//    @RequiresPermissions("uav:Repair:add")
    @Log(title = "维护记录信息管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody UavRepair uavRepair)
    {
        if(StringUtils.isEmpty(uavRepair.getUavNum())){
            return R.fail("无人机编号不能为空");
        }

        return uavRepairService.insertUavRepair(uavRepair);
    }

    /**
     * 修改
     */
//    @RequiresPermissions("uav:Repair:edit")
    @Log(title = "维护记录信息管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody UavRepair uavRepair)
    {
        if(StringUtils.isEmpty(uavRepair.getUavNum())){
            return R.fail("无人机编号不能为空");
        }

        return uavRepairService.updateUavRepair(uavRepair);
    }

    /**
     * 删除
     */
//    @RequiresPermissions("uav:Repair:remove")
    @Log(title = "维护记录信息管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable Long[] ids)
    {
        return uavRepairService.deleteUavRepairByIds(ids);
    }

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

}
