package com.hw.dms.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.hw.dms.domain.*;
import com.hw.dms.service.IDmsPlanInspectService;
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.RestController;
import com.hw.common.log.annotation.Log;
import com.hw.common.log.enums.BusinessType;
import com.hw.common.security.annotation.RequiresPermissions;
import com.hw.dms.service.IDmsBillsInspectInstanceService;
import com.hw.common.core.web.controller.BaseController;
import com.hw.common.core.web.domain.AjaxResult;
import com.hw.common.core.utils.poi.ExcelUtil;
import com.hw.common.core.web.page.TableDataInfo;

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

    /**
     * 查询巡检工单列表
     */
    @RequiresPermissions("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);
    }

    /**
     * 导出巡检工单列表
     */
    @RequiresPermissions("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, "巡检工单数据");
    }

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

    /**
     * 新增巡检工单
     */
    @RequiresPermissions("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)
    {
        String s = dmsBillsInstanceId.replaceAll("\"", "");

        DmsPlanInspect dmsPlanInspect = dmsPlanInspectService.selectDmsPlanInspectByPlanInspectId(Long.valueOf(s));

        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.setInspectRouteId(dmsPlanInspect.getInspectRouteId());
        dmsBillsInspectInstance.setDeviceAmount(dmsPlanInspect.getDeviceAmount());
        dmsBillsInspectInstance.setPerformer(dmsPlanInspect.getPerformer());
        return toAjax(dmsBillsInspectInstanceService.insertDmsBillsInspectInstance(dmsBillsInspectInstance));


    }

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

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


}
