package com.aucma.dms.controller;

import com.aucma.common.annotation.Log;
import com.aucma.common.core.controller.BaseController;
import com.aucma.common.core.domain.AjaxResult;
import com.aucma.common.core.page.TableDataInfo;
import com.aucma.common.enums.BusinessType;
import com.aucma.common.utils.poi.ExcelUtil;
import com.aucma.dms.domain.DmsRepairRecord;
import com.aucma.dms.service.IDmsRepairRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 维修记录Controller
 * 
 * @author Cascade AI
 * @date 2025-01-10
 */
@RestController
@RequestMapping("/dms/repairRecord")
public class DmsRepairRecordController extends BaseController {
    
    @Autowired
    private IDmsRepairRecordService dmsRepairRecordService;

    /**
     * 查询维修记录列表
     */
    //@PreAuthorize("@ss.hasPermi('dms:repairRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(DmsRepairRecord dmsRepairRecord) {
        startPage();
        List<DmsRepairRecord> list = dmsRepairRecordService.selectDmsRepairRecordList(dmsRepairRecord);
        return getDataTable(list);
    }

    /**
     * 查询维修记录列表（不分页）
     */
    //@PreAuthorize("@ss.hasPermi('dms:repairRecord:list')")
    @GetMapping("/listAll")
    public AjaxResult listAll(DmsRepairRecord dmsRepairRecord) {
        List<DmsRepairRecord> list = dmsRepairRecordService.selectDmsRepairRecordList(dmsRepairRecord);
        return AjaxResult.success(list);
    }

    /**
     * 导出维修记录列表
     */
    //@PreAuthorize("@ss.hasPermi('dms:repairRecord:export')")
    @Log(title = "维修记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DmsRepairRecord dmsRepairRecord) {
        List<DmsRepairRecord> list = dmsRepairRecordService.selectDmsRepairRecordList(dmsRepairRecord);
        ExcelUtil<DmsRepairRecord> util = new ExcelUtil<DmsRepairRecord>(DmsRepairRecord.class);
        util.exportExcel(response, list, "维修记录数据");
    }

    /**
     * 获取维修记录详细信息
     */
    //@PreAuthorize("@ss.hasPermi('dms:repairRecord:query')")
    @GetMapping(value = "/{recordId}")
    public AjaxResult getInfo(@PathVariable("recordId") Long recordId) {
        return AjaxResult.success(dmsRepairRecordService.selectDmsRepairRecordByRecordId(recordId));
    }

    /**
     * 新增维修记录
     */
    //@PreAuthorize("@ss.hasPermi('dms:repairRecord:add')")
    @Log(title = "维修记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DmsRepairRecord dmsRepairRecord) {
        return toAjax(dmsRepairRecordService.insertDmsRepairRecord(dmsRepairRecord));
    }

    /**
     * 修改维修记录
     */
    //@PreAuthorize("@ss.hasPermi('dms:repairRecord:edit')")
    @Log(title = "维修记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DmsRepairRecord dmsRepairRecord) {
        return toAjax(dmsRepairRecordService.updateDmsRepairRecord(dmsRepairRecord));
    }

    /**
     * 删除维修记录
     */
    //@PreAuthorize("@ss.hasPermi('dms:repairRecord:remove')")
    @Log(title = "维修记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{recordIds}")
    public AjaxResult remove(@PathVariable Long[] recordIds) {
        return toAjax(dmsRepairRecordService.deleteDmsRepairRecordByRecordIds(recordIds));
    }

    /**
     * 存档维修记录
     */
    //@PreAuthorize("@ss.hasPermi('dms:repairRecord:archive')")
    @Log(title = "维修记录存档", businessType = BusinessType.UPDATE)
    @PostMapping("/archive")
    public AjaxResult archive(@RequestBody Map<String, Object> params) {
        Long[] recordIds = ((List<Integer>) params.get("recordIds")).stream()
                .map(Long::valueOf)
                .toArray(Long[]::new);
        return toAjax(dmsRepairRecordService.archiveRepairRecords(recordIds));
    }

    /**
     * 查询设备维修历史
     */
    //@PreAuthorize("@ss.hasPermi('dms:repairRecord:query')")
    @GetMapping("/history/{deviceId}")
    public AjaxResult getHistory(@PathVariable("deviceId") Long deviceId) {
        List<DmsRepairRecord> list = dmsRepairRecordService.selectRepairHistoryByDeviceId(deviceId);
        return AjaxResult.success(list);
    }
}
