package com.goomood.phm.controller;

import java.util.HashMap;
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.*;
import com.goomood.common.annotation.Log;
import com.goomood.common.core.controller.BaseController;
import com.goomood.common.core.domain.AjaxResult;
import com.goomood.common.enums.BusinessType;
import com.goomood.phm.domain.FaultRecord;
import com.goomood.phm.service.IFaultRecordService;
import com.goomood.common.utils.poi.ExcelUtil;
import com.goomood.common.core.page.TableDataInfo;

/**
 * 故障记录Controller
 * 
 * @author goomood
 * @date 2024-11-17
 */
@RestController
@RequestMapping("/fault/faultRecord")
public class FaultRecordController extends BaseController
{
    @Autowired
    private IFaultRecordService faultRecordService;

    /**
     * 查询故障记录列表
     */
    @PreAuthorize("@ss.hasPermi('fault:faultRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(FaultRecord faultRecord)
    {
        startPage();
        List<FaultRecord> list = faultRecordService.selectFaultRecordList(faultRecord);
        return getDataTable(list);
    }

    /**
     * 导出故障记录列表
     */
    @PreAuthorize("@ss.hasPermi('fault:faultRecord:export')")
    @Log(title = "故障记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FaultRecord faultRecord)
    {
        List<FaultRecord> list = faultRecordService.selectFaultRecordList(faultRecord);
        ExcelUtil<FaultRecord> util = new ExcelUtil<FaultRecord>(FaultRecord.class);
        util.exportExcel(response, list, "故障记录数据");
    }

    /**
     * 获取故障记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('fault:faultRecord:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(faultRecordService.selectFaultRecordById(id));
    }

    /**
     * 新增故障记录
     */
    @PreAuthorize("@ss.hasPermi('fault:faultRecord:add')")
    @Log(title = "故障记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FaultRecord faultRecord)
    {
        return toAjax(faultRecordService.insertFaultRecord(faultRecord));
    }

    /**
     * 修改故障记录
     */
    @PreAuthorize("@ss.hasPermi('fault:faultRecord:edit')")
    @Log(title = "故障记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FaultRecord faultRecord)
    {
        return toAjax(faultRecordService.updateFaultRecord(faultRecord));
    }

    /**
     * 删除故障记录
     */
    @PreAuthorize("@ss.hasPermi('fault:faultRecord:remove')")
    @Log(title = "故障记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(faultRecordService.deleteFaultRecordByIds(ids));
    }


    /**
     * 获取所有机型
     * @return
     */
    @GetMapping("/aircraftTypes")
    public AjaxResult getAircraftTypes() {
        return success(faultRecordService.getAircraftTypes());
    }

    /**
     * 获取虽有飞机号列表
     * @return
     */
    @GetMapping("/aircraftNos")
    public AjaxResult getAircraftNos() {
        return success(faultRecordService.getAircraftNos());
    }


    /**
     * 获取专业列表
     * @return
     */
    @GetMapping("/specialties")
    public AjaxResult getSpecialties() {
        return success(faultRecordService.getSpecialties());
    }




    /**
     * 专业故障统计
     * @param aircraftType
     * @param aircraftNo
     * @param startDate
     * @param endDate
     * @return
     */
    @GetMapping("/specialtyFaultStatsByStandAlone")
    public AjaxResult getSpecialtyFaultStatsByStandAlone(
            @RequestParam(required = false) String aircraftType,
            @RequestParam(required = false) String aircraftNo,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate
    ) {
        return success(faultRecordService.getSpecialtyFaultStatsByStandAlone(aircraftType, aircraftNo, startDate, endDate));
    }


    /**
     * 机件故障统计
     * @param aircraftType
     * @param aircraftNo
     * @param startDate
     * @param endDate
     * @return
     */
    @GetMapping("/partFaultStatsByStandAlone")
    public AjaxResult getPartFaultStatsByStandAlone(
            @RequestParam(required = false) String aircraftType,
            @RequestParam(required = false) String aircraftNo,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate
    ) {
        return success(faultRecordService.getPartFaultStatsByStandAlone(aircraftType, aircraftNo, startDate, endDate));
    }


    /**
     * 故障地点统计
     * @param aircraftType
     * @param aircraftNo
     * @param startDate
     * @param endDate
     * @return
     */
    @GetMapping("/locationFaultStatsByStandAlone")
    public AjaxResult getLocationFaultStatsByStandAlone(
            @RequestParam(required = false) String aircraftType,
            @RequestParam(required = false) String aircraftNo,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate
    ) {
        return success(faultRecordService.getLocationFaultStatsByStandAlone(aircraftType, aircraftNo, startDate, endDate));
    }


    /**
     * 单机查询
     * @param aircraftType
     * @param aircraftNo
     * @param startDate
     * @param endDate
     * @return
     */
    @GetMapping("/getFaultRecordByStandAlone")
    public AjaxResult getFaultRecordByStandAlone(
            @RequestParam(required = false) String aircraftType,
            @RequestParam(required = false) String aircraftNo,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate
    ) {
        Map<String, Object> result = new HashMap<>();
        result.put("systemFaultStatsByStandAlone", faultRecordService.getSystemFaultStatsByStandAlone(aircraftType, aircraftNo, startDate, endDate));
        result.put("specialtyFaultStatsByStandAlone", faultRecordService.getSpecialtyFaultStatsByStandAlone(aircraftType, aircraftNo, startDate, endDate));
        result.put("partFaultStatsByStandAlone", faultRecordService.getPartFaultStatsByStandAlone(aircraftType,aircraftNo, startDate, endDate));
        result.put("locationFaultStatsByStandAlone", faultRecordService.getLocationFaultStatsByStandAlone(aircraftType, aircraftNo, startDate, endDate));
        return success(result);
    }


    /**
     * 专业查询
     * @param aircraftType
     * @param specialty
     * @param startDate
     * @param endDate
     * @return
     */
    @GetMapping("/getFaultRecordBySpecialty")
    public AjaxResult getFaultRecordBySpecialty(
            @RequestParam(required = false) String aircraftType,
            @RequestParam(required = false) String specialty,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate
    ) {
        Map<String, Object> result = new HashMap<>();
        result.put("aircraftFaultStatsBySpecialty", faultRecordService.getAircraftFaultStatsBySpecialty(aircraftType, specialty, startDate, endDate));
        result.put("partFaultStatsBySpecialty", faultRecordService.getPartFaultStatsBySpecialty(aircraftType, specialty, startDate, endDate));
        result.put("specialtyFaultStatsBySpecialty", faultRecordService.getSpecialtyFaultStatsBySpecialty(aircraftType,specialty, startDate, endDate));
        result.put("systemFaultStatsBySpecialty", faultRecordService.getSystemFaultStatsBySpecialty(aircraftType, specialty, startDate, endDate));
        return success(result);
    }
}
