package com.sundot.airport.web.controller.equipment;

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 com.sundot.airport.common.annotation.Log;
import com.sundot.airport.common.core.controller.BaseController;
import com.sundot.airport.common.core.domain.AjaxResult;
import com.sundot.airport.common.enums.BusinessType;
import com.sundot.airport.equipment.domain.EquipmentLedger;
import com.sundot.airport.equipment.service.IEquipmentLedgerService;
import com.sundot.airport.common.utils.poi.ExcelUtil;
import com.sundot.airport.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 设备台账Controller
 *
 * @author ruoyi
 * @date 2026-05-12
 */
@RestController
@RequestMapping("/equipment/ledger")
public class EquipmentLedgerController extends BaseController {
    @Autowired
    private IEquipmentLedgerService equipmentLedgerService;

    /**
     * 查询设备台账列表
     */
    @PreAuthorize("@ss.hasPermi('equipment:ledger:list')")
    @GetMapping("/list")
    public TableDataInfo list(EquipmentLedger equipmentLedger) {
        startPage();
        List<EquipmentLedger> list = equipmentLedgerService.selectEquipmentLedgerList(equipmentLedger);
        return getDataTable(list);
    }

    /**
     * 查询设备台账列表全量
     */
    @PreAuthorize("@ss.hasPermi('equipment:ledger:list')")
    @GetMapping("/listAll")
    public AjaxResult listAll(EquipmentLedger equipmentLedger) {
        List<EquipmentLedger> list = equipmentLedgerService.selectEquipmentLedgerList(equipmentLedger);
        return success(list);
    }

    /**
     * 导出设备台账列表
     */
    @PreAuthorize("@ss.hasPermi('equipment:ledger:export')")
    @Log(title = "设备台账", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EquipmentLedger equipmentLedger) {
        List<EquipmentLedger> list = equipmentLedgerService.selectEquipmentLedgerList(equipmentLedger);
        ExcelUtil<EquipmentLedger> util = new ExcelUtil<EquipmentLedger>(EquipmentLedger.class);
        util.exportExcel(response, list, "设备台账数据");
    }

    /**
     * 获取设备台账详细信息
     */
    @PreAuthorize("@ss.hasPermi('equipment:ledger:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(equipmentLedgerService.selectEquipmentLedgerById(id));
    }

    /**
     * 新增设备台账
     */
    @PreAuthorize("@ss.hasPermi('equipment:ledger:add')")
    @Log(title = "设备台账", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EquipmentLedger equipmentLedger) {
        equipmentLedger.setCreateBy(getUsername());
        return toAjax(equipmentLedgerService.insertEquipmentLedger(equipmentLedger));
    }

    /**
     * 修改设备台账
     */
    @PreAuthorize("@ss.hasPermi('equipment:ledger:edit')")
    @Log(title = "设备台账", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EquipmentLedger equipmentLedger) {
        equipmentLedger.setUpdateBy(getUsername());
        return toAjax(equipmentLedgerService.updateEquipmentLedger(equipmentLedger));
    }

    /**
     * 删除设备台账
     */
    @PreAuthorize("@ss.hasPermi('equipment:ledger:remove')")
    @Log(title = "设备台账", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(equipmentLedgerService.deleteEquipmentLedgerByIds(ids));
    }

    /**
     * 导入设备台账列表
     */
    @PreAuthorize("@ss.hasPermi('equipment:ledger:import')")
    @Log(title = "导入设备台账列表", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<EquipmentLedger> util = new ExcelUtil<>(EquipmentLedger.class);
        List<EquipmentLedger> list = util.importExcel(file.getInputStream());
        String message = equipmentLedgerService.importData(list, updateSupport);
        return success(message);
    }

    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<EquipmentLedger> util = new ExcelUtil<>(EquipmentLedger.class);
        util.importTemplateExcel(response, "设备台账数据导入模板");
    }

    /**
     * 设备台账通知
     */
    @GetMapping("/notice")
    public AjaxResult notice(EquipmentLedger equipmentLedger) {
        List<EquipmentLedger> list = equipmentLedgerService.notice(equipmentLedger);
        return success(list);
    }
}
