package com.uavps.web.controller.system;

import com.uavps.common.annotation.Log;
import com.uavps.common.core.controller.BaseController;
import com.uavps.common.core.domain.AjaxResult;
import com.uavps.common.core.page.TableDataInfo;
import com.uavps.common.enums.BusinessType;
import com.uavps.common.utils.poi.ExcelUtil;
import com.uavps.system.domain.UavpsUnmannedAerialVehicle;
import com.uavps.system.service.IUavpsUnmannedAerialVehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 无人机信息Controller
 *
 * @author ruoyi
 * @date 2025-02-07
 */
@RestController
@RequestMapping("/uavps/uav")
public class UavpsUnmannedAerialVehicleController extends BaseController {
    @Autowired
    private IUavpsUnmannedAerialVehicleService uavpsUnmannedAerialVehicleService;

    /**
     * 查询无人机信息列表
     */
    @PreAuthorize("@ss.hasPermi('uavps:uav:list')")
    @GetMapping("/list")
    public TableDataInfo list(UavpsUnmannedAerialVehicle uavpsUnmannedAerialVehicle) {
        startPage();
        List<UavpsUnmannedAerialVehicle> list = uavpsUnmannedAerialVehicleService.selectUavpsUnmannedAerialVehicleList(uavpsUnmannedAerialVehicle);
        return getDataTable(list);
    }

    /**
     * 导入无人机信息列表
     */
    @Log(title = "无人机信息", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('uavps:uav:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<UavpsUnmannedAerialVehicle> util = new ExcelUtil<UavpsUnmannedAerialVehicle>(UavpsUnmannedAerialVehicle.class);
        List<UavpsUnmannedAerialVehicle> uavList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = uavpsUnmannedAerialVehicleService.importUavpsUav(uavList, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<UavpsUnmannedAerialVehicle> util = new ExcelUtil<UavpsUnmannedAerialVehicle>(UavpsUnmannedAerialVehicle.class);
        util.importTemplateExcel(response, "无人机信息");
    }

    /**
     * 导出无人机信息列表
     */
    @PreAuthorize("@ss.hasPermi('uavps:uav:export')")
    @Log(title = "无人机信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UavpsUnmannedAerialVehicle uavpsUnmannedAerialVehicle) {
        List<UavpsUnmannedAerialVehicle> list = uavpsUnmannedAerialVehicleService.selectUavpsUnmannedAerialVehicleList(uavpsUnmannedAerialVehicle);
        ExcelUtil<UavpsUnmannedAerialVehicle> util = new ExcelUtil<UavpsUnmannedAerialVehicle>(UavpsUnmannedAerialVehicle.class);
        util.exportExcel(response, list, "无人机信息数据");
    }

    /**
     * 获取无人机信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('uavps:uav:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(uavpsUnmannedAerialVehicleService.selectUavpsUnmannedAerialVehicleById(id));
    }

    /**
     * 新增无人机信息
     */
    @PreAuthorize("@ss.hasPermi('uavps:uav:add')")
    @Log(title = "无人机信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UavpsUnmannedAerialVehicle uavpsUnmannedAerialVehicle) {
        uavpsUnmannedAerialVehicle.setCreateBy(getUsername());
        return toAjax(uavpsUnmannedAerialVehicleService.insertUavpsUnmannedAerialVehicle(uavpsUnmannedAerialVehicle));
    }

    /**
     * 修改无人机信息
     */
    @PreAuthorize("@ss.hasPermi('uavps:uav:edit')")
    @Log(title = "无人机信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UavpsUnmannedAerialVehicle uavpsUnmannedAerialVehicle) {
        uavpsUnmannedAerialVehicle.setUpdateBy(getUsername());
        return toAjax(uavpsUnmannedAerialVehicleService.updateUavpsUnmannedAerialVehicle(uavpsUnmannedAerialVehicle));
    }

    /**
     * 删除无人机信息
     */
    @PreAuthorize("@ss.hasPermi('uavps:uav:remove')")
    @Log(title = "无人机信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(uavpsUnmannedAerialVehicleService.deleteUavpsUnmannedAerialVehicleByIds(ids));
    }
}
