package com.aucma.dms.controller;

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.utils.poi.ExcelUtil;
import com.aucma.dms.domain.DmsBillsMaintDetail;
import com.aucma.dms.domain.DmsBillsMaintInstance;
import com.aucma.dms.service.IDmsBillsMaintInstanceService;
import com.aucma.dms.service.IDmsPlanMaintService;
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;




/**
 * 保养工单Controller
 *
 * @author Open Platform
 * @date 2024-02-29
 */
@RestController
@RequestMapping("/dms/dmsBillsMaintInstance")
public class DmsBillsMaintInstanceController extends BaseController
{
    @Autowired
    private IDmsBillsMaintInstanceService dmsBillsMaintInstanceService;
    @Autowired
    private IDmsPlanMaintService dmsPlanMaintService;

    /**
     * 查询保养工单列表
     */
    //@PreAuthorize("@ss.hasPermi('dms:dmsBillsMaintInstance:list')" )
    @GetMapping("/list")
    public TableDataInfo list(DmsBillsMaintInstance dmsBillsMaintInstance)
    {
        startPage();
        List<DmsBillsMaintInstance> list = dmsBillsMaintInstanceService.selectDmsBillsMaintInstanceList(dmsBillsMaintInstance);
        return getDataTable(list);
    }

    /**
     * 查询保养工单列表（不分页）
     */
    //@PreAuthorize("@ss.hasPermi('dms:dmsBillsMaintInstance:list')" )
    @GetMapping("/listAll")
    public AjaxResult listAll(DmsBillsMaintInstance dmsBillsMaintInstance)
    {
        List<DmsBillsMaintInstance> list = dmsBillsMaintInstanceService.selectDmsBillsMaintInstanceList(dmsBillsMaintInstance);
        return success(list);
    }

    /**
     * 导出保养工单列表
     */
    //@PreAuthorize("@ss.hasPermi('dms:dmsBillsMaintInstance:export')" )
    ////@Log(title = "保养工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DmsBillsMaintInstance dmsBillsMaintInstance)
    {
        List<DmsBillsMaintInstance> list = dmsBillsMaintInstanceService.selectDmsBillsMaintInstanceList(dmsBillsMaintInstance);
        ExcelUtil<DmsBillsMaintInstance> util = new ExcelUtil<DmsBillsMaintInstance>(DmsBillsMaintInstance.class);
        util.exportExcel(response, list, "保养工单数据");
    }

    /**
     * 获取保养工单详细信息
     */
    //@PreAuthorize("@ss.hasPermi('dms:dmsBillsMaintInstance:query')" )
    @GetMapping(value = "/{maintInstanceId}")
    public AjaxResult getInfo(@PathVariable("maintInstanceId") Long maintInstanceId)
    {
        return success(dmsBillsMaintInstanceService.selectDmsBillsMaintInstanceByMaintInstanceId(maintInstanceId));
    }

    /**
     * 新增保养工单
     */
    //@PreAuthorize("@ss.hasPermi('dms:dmsBillsMaintInstance:add')" )
    ////@Log(title = "保养工单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DmsBillsMaintInstance dmsBillsMaintInstance)
    {
        return null;
//        return toAjax(dmsBillsMaintInstanceService.insertDmsBillsMaintInstance(dmsBillsMaintInstance));
    }

//    //@InnerAuth
    @PostMapping("/autoAdd")
    public AjaxResult autoAdd(@RequestBody String planMaintCode)
    {
        return toAjax(dmsBillsMaintInstanceService.insertDmsBillsMaintInstance(planMaintCode));
    }

    /**
     * 修改保养工单
     */
    //@PreAuthorize("@ss.hasPermi('dms:dmsBillsMaintInstance:edit')" )
    ////@Log(title = "保养工单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DmsBillsMaintInstance dmsBillsMaintInstance)
    {
        return toAjax(dmsBillsMaintInstanceService.updateDmsBillsMaintInstance(dmsBillsMaintInstance));
    }

    /**
     * 删除保养工单
     */
    //@PreAuthorize("@ss.hasPermi('dms:dmsBillsMaintInstance:remove')" )
    ////@Log(title = "保养工单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{maintInstanceIds}")
    public AjaxResult remove(@PathVariable Long[] maintInstanceIds)
    {
        return toAjax(dmsBillsMaintInstanceService.deleteDmsBillsMaintInstanceByMaintInstanceIds(maintInstanceIds));
    }

    /**
     * 获取保养工单设备列表（用于网页版保养执行）
     */
    @GetMapping("/devices/{maintInstanceId}")
    public AjaxResult getDeviceList(@PathVariable("maintInstanceId") Long maintInstanceId)
    {
        return success(dmsBillsMaintInstanceService.getDeviceList(maintInstanceId));
    }

    /**
     * 更新设备保养状态（简化版，只更新合格/不合格）
     */
    @PutMapping("/device/status")
    public AjaxResult updateDeviceStatus(@RequestBody DmsBillsMaintDetail detail)
    {
        return toAjax(dmsBillsMaintInstanceService.updateDeviceStatus(detail));
    }
}
