package org.dromara.ems.info.controller;

import org.dromara.common.log.annotation.Log;
import org.dromara.ems.common.web.EmsBaseController;
import org.dromara.common.core.domain.R;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.ems.info.domain.SparePartsUsageRecord;
import org.dromara.ems.info.service.ISparePartsUsageRecordService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 动力能源部行输科备件领用更换记录Controller
 * 
 * @author Yinq
 * @date 2025-06-05
 */
@RestController
@RequiredArgsConstructor

@RequestMapping("/ems/info/sparePartsUsageRecord")
public class SparePartsUsageRecordController extends EmsBaseController
{
    
    private final ISparePartsUsageRecordService sparePartsUsageRecordService;

    /**
     * 查询动力能源部行输科备件领用更换记录列表
     */
    @SaCheckPermission("ems/info:sparePartsUsageRecord:list")
    @GetMapping("/list")
    public TableDataInfo list(SparePartsUsageRecord sparePartsUsageRecord)
    {
        startPage();
        List<SparePartsUsageRecord> list = sparePartsUsageRecordService.selectSparePartsUsageRecordList(sparePartsUsageRecord);
        return getDataTable(list);
    }

    /**
     * 导出动力能源部行输科备件领用更换记录列表
     */
    @SaCheckPermission("ems/info:sparePartsUsageRecord:export")
    @Log(title = "动力能源部行输科备件领用更换记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SparePartsUsageRecord sparePartsUsageRecord)
    {
        List<SparePartsUsageRecord> list = sparePartsUsageRecordService.selectSparePartsUsageRecordList(sparePartsUsageRecord);
        ExcelUtil.exportExcel(list, "动力能源部行输科备件领用更换记录数据", SparePartsUsageRecord.class, response);
    }

    /**
     * 获取动力能源部行输科备件领用更换记录详细信息
     */
    @SaCheckPermission("ems/info:sparePartsUsageRecord:query")
    @GetMapping(value = "/{id}")
    public R<?> getInfo(@PathVariable("id") Long id)
    {
        return success(sparePartsUsageRecordService.selectSparePartsUsageRecordById(id));
    }

    /**
     * 新增动力能源部行输科备件领用更换记录
     */
    @SaCheckPermission("ems/info:sparePartsUsageRecord:add")
    @Log(title = "动力能源部行输科备件领用更换记录", businessType = BusinessType.INSERT)
    @PostMapping
    public R<?> add(@RequestBody SparePartsUsageRecord sparePartsUsageRecord)
    {
        return toAjax(sparePartsUsageRecordService.insertSparePartsUsageRecord(sparePartsUsageRecord));
    }

    /**
     * 修改动力能源部行输科备件领用更换记录
     */
    @SaCheckPermission("ems/info:sparePartsUsageRecord:edit")
    @Log(title = "动力能源部行输科备件领用更换记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<?> edit(@RequestBody SparePartsUsageRecord sparePartsUsageRecord)
    {
        return toAjax(sparePartsUsageRecordService.updateSparePartsUsageRecord(sparePartsUsageRecord));
    }

    /**
     * 删除动力能源部行输科备件领用更换记录
     */
    @SaCheckPermission("ems/info:sparePartsUsageRecord:remove")
    @Log(title = "动力能源部行输科备件领用更换记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public R<?> remove(@PathVariable Long[] ids)
    {
        return toAjax(sparePartsUsageRecordService.deleteSparePartsUsageRecordByIds(ids));
    }

    /**
     * 导入动力能源部行输科备件领用更换记录数据
     */
    @SaCheckPermission("ems/info:sparePartsUsageRecord:add")
    @Log(title = "动力能源部行输科备件领用更换记录", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public R<?> importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        List<SparePartsUsageRecord> recordList = ExcelUtil.importExcel(file.getInputStream(), SparePartsUsageRecord.class);
        String operName = getUsername();
        String message = sparePartsUsageRecordService.importSparePartsUsageRecord(recordList, updateSupport, operName);
        return R.ok(message);
    }

    /**
     * 获取动力能源部行输科备件领用更换记录导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil.exportExcel(new ArrayList<>(), "动力能源部行输科备件领用更换记录数据", SparePartsUsageRecord.class, response);
    }
}
