package com.aucma.base.controller;

import java.util.List;
import java.util.Map;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.aucma.common.annotation.Log;
import com.aucma.common.core.controller.BaseController;
import com.aucma.common.core.domain.AjaxResult;
import com.aucma.common.enums.BusinessType;
import com.aucma.base.domain.ProcessSnapshot;
import com.aucma.base.service.IProcessSnapshotService;
import com.aucma.common.utils.poi.ExcelUtil;
import com.aucma.common.core.page.TableDataInfo;

/**
 * 工艺快照Controller
 *
 * @author Cascade
 * @date 2026-01-19
 */
@RestController
@RequestMapping("/base/processSnapshot")
public class ProcessSnapshotController extends BaseController {
    @Autowired
    private IProcessSnapshotService processSnapshotService;

    /**
     * 查询工艺快照列表
     */
    @PreAuthorize("@ss.hasPermi('base:processSnapshot:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProcessSnapshot processSnapshot) {
        startPage();
        List<ProcessSnapshot> list = processSnapshotService.selectProcessSnapshotList(processSnapshot);
        return getDataTable(list);
    }

    /**
     * 导出工艺快照列表
     */
    @PreAuthorize("@ss.hasPermi('base:processSnapshot:export')")
    @Log(title = "工艺快照", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProcessSnapshot processSnapshot) {
        List<ProcessSnapshot> list = processSnapshotService.selectProcessSnapshotList(processSnapshot);
        ExcelUtil<ProcessSnapshot> util = new ExcelUtil<ProcessSnapshot>(ProcessSnapshot.class);
        util.exportExcel(response, list, "工艺快照数据");
    }

    /**
     * 获取工艺快照详情（包含参数列表）
     */
    @PreAuthorize("@ss.hasPermi('base:processSnapshot:query')")
    @GetMapping("/detail/{snapshotId}")
    public AjaxResult detail(@PathVariable("snapshotId") Long snapshotId) {
        return success(processSnapshotService.selectSnapshotDetail(snapshotId));
    }

    /**
     * 获取工艺快照基本信息
     */
    @PreAuthorize("@ss.hasPermi('base:processSnapshot:query')")
    @GetMapping(value = "/{snapshotId}")
    public AjaxResult getInfo(@PathVariable("snapshotId") Long snapshotId) {
        return success(processSnapshotService.selectProcessSnapshotBySnapshotId(snapshotId));
    }

    /**
     * 创建工艺快照
     */
    @PreAuthorize("@ss.hasPermi('base:processSnapshot:add')")
    @Log(title = "工艺快照", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProcessSnapshot processSnapshot) {
        return toAjax(processSnapshotService.insertProcessSnapshot(processSnapshot));
    }

    /**
     * 修改工艺快照
     */
    @PreAuthorize("@ss.hasPermi('base:processSnapshot:edit')")
    @Log(title = "工艺快照", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProcessSnapshot processSnapshot) {
        return toAjax(processSnapshotService.updateProcessSnapshot(processSnapshot));
    }

    /**
     * 删除工艺快照
     */
    @PreAuthorize("@ss.hasPermi('base:processSnapshot:remove')")
    @Log(title = "工艺快照", businessType = BusinessType.DELETE)
    @DeleteMapping("/{snapshotIds}")
    public AjaxResult remove(@PathVariable Long[] snapshotIds) {
        return toAjax(processSnapshotService.deleteProcessSnapshotBySnapshotIds(snapshotIds));
    }

    /**
     * 对比两个快照的参数差异
     */
    @PreAuthorize("@ss.hasPermi('base:processSnapshot:query')")
    @GetMapping("/compare")
    public AjaxResult compare(@RequestParam("snapshotId1") Long snapshotId1,
                              @RequestParam("snapshotId2") Long snapshotId2) {
        List<Map<String, Object>> result = processSnapshotService.compareSnapshots(snapshotId1, snapshotId2);
        if (result == null) {
            return error("快照不存在或设备不一致，无法对比");
        }
        return success(result);
    }

    /**
     * 创建调试备份
     */
    @PreAuthorize("@ss.hasPermi('base:processSnapshot:createBackup')")
    @Log(title = "调试备份", businessType = BusinessType.INSERT)
    @PostMapping("/createBackup")
    public AjaxResult createBackup(@RequestParam String deviceCode,
                                   @RequestParam String moldCode,
                                   @RequestParam String productCode,
                                   @RequestParam String backupName) {
        int result = processSnapshotService.createDebugBackup(deviceCode, moldCode, productCode, backupName);
        return result > 0 ? success("备份创建成功") : error("备份创建失败");
    }

    /**
     * 查询调试备份列表
     */
    @PreAuthorize("@ss.hasPermi('base:processSnapshot:list')")
    @GetMapping("/backupList")
    public AjaxResult getBackupList(@RequestParam(required = false) String moldCode,
                                    @RequestParam(required = false) String deviceCode,
                                    @RequestParam(required = false) String productCode) {
        return success(processSnapshotService.getDebugBackups(moldCode, deviceCode, productCode));
    }

    /**
     * 获取默认备份
     */
    @PreAuthorize("@ss.hasPermi('base:processSnapshot:query')")
    @GetMapping("/defaultBackup")
    public AjaxResult getDefaultBackup(@RequestParam String moldCode,
                                       @RequestParam String deviceCode,
                                       @RequestParam String productCode) {
        return success(processSnapshotService.getDefaultBackup(moldCode, deviceCode, productCode));
    }

    /**
     * 设为默认备份
     */
    @PreAuthorize("@ss.hasPermi('base:processSnapshot:setDefault')")
    @Log(title = "调试备份", businessType = BusinessType.UPDATE)
    @PutMapping("/setDefault/{snapshotId}")
    public AjaxResult setDefaultBackup(@PathVariable Long snapshotId) {
        int result = processSnapshotService.setDefaultBackup(snapshotId);
        return result > 0 ? success("设置成功") : error("备份不存在");
    }

    /**
     * 应用备份（查看参数）
     */
    @PreAuthorize("@ss.hasPermi('base:processSnapshot:query')")
    @GetMapping("/applyBackup/{snapshotId}")
    public AjaxResult applyBackup(@PathVariable Long snapshotId) {
        return success(processSnapshotService.applyDebugBackup(snapshotId));
    }
}
