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.DmsBillsInspectInstance;
import com.aucma.dms.domain.DmsInspectInstanceDetail;
import com.aucma.dms.domain.DmsPlanInspect;
import com.aucma.dms.service.IDmsBillsInspectInstanceService;
import com.aucma.dms.service.IDmsPlanInspectService;
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.Date;
import java.util.List;




/**
 * 巡检工单Controller
 *
 * @author Open Platform
 * @date 2024-01-23
 */
@RestController
@RequestMapping("/dms/dmsBillsInstance")
public class DmsBillsInspectInstanceController extends BaseController
{
    @Autowired
    private IDmsBillsInspectInstanceService dmsBillsInspectInstanceService;
    @Autowired
    private IDmsPlanInspectService dmsPlanInspectService;

    /**
     * 查询巡检工单列表
     */
    //@PreAuthorize("@ss.hasPermi('dms:dmsBillsInstance:list')" )
    @GetMapping("/list")
    public TableDataInfo list(DmsBillsInspectInstance dmsBillsInspectInstance)
    {
        startPage();
        List<DmsBillsInspectInstance> list = dmsBillsInspectInstanceService.selectDmsBillsInspectInstanceList(dmsBillsInspectInstance);
        return getDataTable(list);
    }
    /**
     * 查询点检工单
     * */
    @GetMapping("/listPoint")
    public TableDataInfo listPoint(){
        List<DmsBillsInspectInstance> dmsBillsInspectInstances = dmsBillsInspectInstanceService.selectPoint();
        return getDataTable(dmsBillsInspectInstances);
    }
    /**
     * 查询点检工单
     * */
    @GetMapping("/listSelectInspection")
    public TableDataInfo selectInspection(){
        List<DmsBillsInspectInstance> dmsBillsInspectInstances = dmsBillsInspectInstanceService.selectInspection();
        return getDataTable(dmsBillsInspectInstances);
    }

    /**
     * 导出巡检工单列表
     */
    //@PreAuthorize("@ss.hasPermi('dms:dmsBillsInstance:export')" )
    ////@Log(title = "巡检工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DmsBillsInspectInstance dmsBillsInspectInstance)
    {
        List<DmsBillsInspectInstance> list = dmsBillsInspectInstanceService.selectDmsBillsInspectInstanceList(dmsBillsInspectInstance);
        ExcelUtil<DmsBillsInspectInstance> util = new ExcelUtil<DmsBillsInspectInstance>(DmsBillsInspectInstance.class);
        util.exportExcel(response, list, "巡检工单数据");
    }

    /**
     * 获取巡检工单详细信息
     */
    //@PreAuthorize("@ss.hasPermi('dms:dmsBillsInstance:query')" )
    @GetMapping(value = "/{inspectInstanceId}")
    public AjaxResult getInfo(@PathVariable("inspectInstanceId") Long inspectInstanceId)
    {
        return success(dmsBillsInspectInstanceService.selectDmsBillsInspectInstanceByInspectInstanceId(inspectInstanceId));
    }

    /**
     * 新增巡检工单
     */
    //@PreAuthorize("@ss.hasPermi('dms:dmsBillsInstance:add')" )
    ////@Log(title = "巡检工单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DmsBillsInspectInstance dmsBillsInspectInstance)
    {
        return toAjax(dmsBillsInspectInstanceService.insertDmsBillsInspectInstance(dmsBillsInspectInstance));
    }
    /**
     * 自动创建
     * */
    @PostMapping("/autoAdd")
    public AjaxResult autoAdd(@RequestBody String dmsBillsInstanceId)
    {
        // 使用 replaceAll 方法将双引号替换为空字符串
        String result = dmsBillsInstanceId.replaceAll("\"", "");

        DmsPlanInspect dmsPlanInspect = dmsPlanInspectService.selectDmsPlanInspectByPlanInspectId1(dmsBillsInstanceId);

        DmsBillsInspectInstance dmsBillsInspectInstance = new DmsBillsInspectInstance();


        //        DmsRepairInstance dmsRepairInstance = dmsRepairInstanceService.selectDmsRepairInstanceByRepairInstanceId(2l);

        dmsBillsInspectInstance.setPlanInspectId(dmsPlanInspect.getPlanInspectId());
        // 已废弃审批流功能
        // dmsBillsInspectInstance.setWfProcessId(105l);
        dmsBillsInspectInstance.setInspectType(dmsPlanInspect.getInspectType());
        dmsBillsInspectInstance.setPlanBeginTime(dmsPlanInspect.getPlanTime());
        dmsBillsInspectInstance.setPlanEndTime(new Date(dmsPlanInspect.getPlanTime().getTime()+dmsPlanInspect.getTimeLimit()*1000l));
        dmsBillsInspectInstance.setInspectRouteId(dmsPlanInspect.getInspectRouteId());
        dmsBillsInspectInstance.setDeviceAmount(dmsPlanInspect.getDeviceAmount());
        dmsBillsInspectInstance.setPerformer(dmsPlanInspect.getPerformer());
        return toAjax(dmsBillsInspectInstanceService.insertDmsBillsInspectInstance(dmsBillsInspectInstance));


    }

    /**
     * 修改巡检工单
     */
    //@PreAuthorize("@ss.hasPermi('dms:dmsBillsInstance:edit')" )
    ////@Log(title = "巡检工单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DmsBillsInspectInstance dmsBillsInspectInstance)
    {
        return toAjax(dmsBillsInspectInstanceService.updateDmsBillsInspectInstance(dmsBillsInspectInstance));
    }

    /**
     * 删除巡检工单
     */
    //@PreAuthorize("@ss.hasPermi('dms:dmsBillsInstance:remove')" )
    ////@Log(title = "巡检工单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{inspectInstanceIds}")
    public AjaxResult remove(@PathVariable Long[] inspectInstanceIds)
    {
        return toAjax(dmsBillsInspectInstanceService.deleteDmsBillsInspectInstanceByInspectInstanceIds(inspectInstanceIds));
    }

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

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

}
