package com.ygtx.gxt.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
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 com.ygtx.common.annotation.Log;
import com.ygtx.common.core.controller.BaseController;
import com.ygtx.common.core.domain.AjaxResult;
import com.ygtx.common.enums.BusinessType;
import com.ygtx.gxt.domain.GxtEquipment;
import com.ygtx.gxt.service.IGxtEquipmentService;
import com.ygtx.common.utils.poi.ExcelUtil;
import com.ygtx.common.core.page.TableDataInfo;

/**
 * 设备管理Controller
 * 
 * @author ouyj
 * @date 2025-10-28
 */
@RestController
@RequestMapping("/gxt/equipment")
public class GxtEquipmentController extends BaseController
{
    @Autowired
    private IGxtEquipmentService gxtEquipmentService;

    /**
     * 查询设备管理列表
     */
    @PreAuthorize("@ss.hasPermi('gxt:equipment:list')")
    @GetMapping("/list")
    public TableDataInfo list(GxtEquipment gxtEquipment)
    {
        startPage();
        List<GxtEquipment> list = gxtEquipmentService.selectGxtEquipmentList(gxtEquipment);
        return getDataTable(list);
    }

    /**
     * 获取所有品牌列表（去重）
     */
    @PreAuthorize("@ss.hasPermi('gxt:equipment:list')")
    @GetMapping("/brands")
    public AjaxResult listBrands()
    {
        List<String> list = gxtEquipmentService.selectBrandList();
        return AjaxResult.success(list);
    }

    /**
     * 根据品牌获取机型列表（去重）
     */
    @PreAuthorize("@ss.hasPermi('gxt:equipment:list')")
    @GetMapping("/models/{brand}")
    public AjaxResult listModelsByBrand(@PathVariable("brand") String brand)
    {
        List<String> list = gxtEquipmentService.selectModelListByBrand(brand);
        return AjaxResult.success(list);
    }

    /**
     * 导出设备管理列表
     */
    @PreAuthorize("@ss.hasPermi('gxt:equipment:export')")
    @Log(title = "设备管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GxtEquipment gxtEquipment)
    {
        List<GxtEquipment> list = gxtEquipmentService.selectGxtEquipmentList(gxtEquipment);
        ExcelUtil<GxtEquipment> util = new ExcelUtil<GxtEquipment>(GxtEquipment.class);
        util.exportExcel(response, list, "设备管理数据");
    }

    /**
     * 获取设备管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('gxt:equipment:query')")
    @GetMapping(value = "/{equipmentId}")
    public AjaxResult getInfo(@PathVariable("equipmentId") Long equipmentId)
    {
        return success(gxtEquipmentService.selectGxtEquipmentByEquipmentId(equipmentId));
    }

    /**
     * 新增设备管理
     */
    @PreAuthorize("@ss.hasPermi('gxt:equipment:add')")
    @Log(title = "设备管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GxtEquipment gxtEquipment)
    {
        return toAjax(gxtEquipmentService.insertGxtEquipment(gxtEquipment));
    }

    /**
     * 修改设备管理
     */
    @PreAuthorize("@ss.hasPermi('gxt:equipment:edit')")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GxtEquipment gxtEquipment)
    {
        return toAjax(gxtEquipmentService.updateGxtEquipment(gxtEquipment));
    }

    /**
     * 删除设备管理
     */
    @PreAuthorize("@ss.hasPermi('gxt:equipment:remove')")
    @Log(title = "设备管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{equipmentIds}")
    public AjaxResult remove(@PathVariable Long[] equipmentIds)
    {
        return toAjax(gxtEquipmentService.deleteGxtEquipmentByEquipmentIds(equipmentIds));
    }

    /**
     * 导入设备管理数据
     */
    @PreAuthorize("@ss.hasPermi('gxt:equipment:import')")
    @Log(title = "设备管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<GxtEquipment> util = new ExcelUtil<GxtEquipment>(GxtEquipment.class);
        List<GxtEquipment> equipmentList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = gxtEquipmentService.importGxtEquipment(equipmentList, updateSupport, operName);
        return success(message);
    }

    /**
     * 下载设备管理导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<GxtEquipment> util = new ExcelUtil<GxtEquipment>(GxtEquipment.class);
        util.importTemplateExcel(response, "设备管理数据");
    }
}