package com.zksy.web.controller.WaterSupply.DeviceMaintain;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksy.base.domain.vo.EquipmentMaintainDataVO;
import com.zksy.base.domain.vo.MaintainOrderVO;
import com.zksy.base.service.WaterSupplyDeviceService;
import com.zksy.common.annotation.Log;
import com.zksy.common.core.domain.AjaxResult;
import com.zksy.common.enums.BusinessType;
import com.zksy.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/waterSupply/device/maintain")
@Api(tags = "供水设备-维修派单管理")
public class DeviceMaintainController {

    @Autowired
    private WaterSupplyDeviceService waterSupplyDeviceService;

    @PostMapping("/order")
    @ApiOperation("创建维修派单")
    @Log(title = "设备维修派单", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('waterSupply:device:maintain')")
    public AjaxResult createMaintainOrder(@RequestBody MaintainOrderVO vo) {
        try {
            boolean result = waterSupplyDeviceService.createMaintainOrder(vo);
            return result ? AjaxResult.success("派单成功") : AjaxResult.error("派单失败");
        } catch (Exception e) {
            log.error("创建维修派单失败", e);
            return AjaxResult.error("派单失败：" + e.getMessage());
        }
    }

    @GetMapping("/record/page")
    @ApiOperation("维修记录分页查询")
    @PreAuthorize("@ss.hasPermi('waterSupply:device:maintain:list')")
    public AjaxResult getMaintainRecordPage(
            @ApiParam("页码") @RequestParam(defaultValue = "1") long pageNum,
            @ApiParam("每页数量") @RequestParam(defaultValue = "10") long pageSize,
            @ApiParam("设备名称") @RequestParam(required = false) String equipmentName,
            @ApiParam("维修人员") @RequestParam(required = false) String maintainPerson,
            @ApiParam("维修类型") @RequestParam(required = false) String maintainType,
            @ApiParam("工单状态") @RequestParam(required = false) String status,
            @ApiParam("开始时间") @RequestParam(required = false) String startTime,
            @ApiParam("结束时间") @RequestParam(required = false) String endTime) {
        Page<EquipmentMaintainDataVO> page = waterSupplyDeviceService.getMaintainRecordPage(
                pageNum, pageSize, equipmentName, maintainPerson, maintainType, status, startTime, endTime);
        return AjaxResult.success(page);
    }

    @GetMapping("/record/myPage")
    @ApiOperation("我的维修单（根据当前登录人查询）")
    @PreAuthorize("@ss.hasPermi('waterSupply:device:maintain:list')")
    public AjaxResult getMyMaintainRecordPage(
            @ApiParam("页码") @RequestParam(defaultValue = "1") long pageNum,
            @ApiParam("每页数量") @RequestParam(defaultValue = "10") long pageSize,
            @ApiParam("维修类型") @RequestParam(required = false) String maintainType,
            @ApiParam("工单状态") @RequestParam(required = false) String status,
            @ApiParam("开始时间") @RequestParam(required = false) String startTime,
            @ApiParam("结束时间") @RequestParam(required = false) String endTime) {
        String username = SecurityUtils.getUsername();
        Page<EquipmentMaintainDataVO> page = waterSupplyDeviceService.getMyMaintainRecordPage(
                pageNum, pageSize, username, maintainType, status, startTime, endTime);
        return AjaxResult.success(page);
    }

    @GetMapping("/record/list/{equipmentId}")
    @ApiOperation("查询设备维修记录列表")
    @PreAuthorize("@ss.hasPermi('waterSupply:device:maintain:list')")
    public AjaxResult getMaintainRecordList(@PathVariable String equipmentId) {
        List<Map<String, Object>> list = waterSupplyDeviceService.getMaintainRecordList(equipmentId);
        return AjaxResult.success(list);
    }

    @PostMapping("/record")
    @ApiOperation("新增维修记录（历史补录）")
    @Log(title = "新增维修记录", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('waterSupply:device:maintain:add')")
    public AjaxResult saveMaintainRecord(@RequestBody EquipmentMaintainDataVO vo) {
        try {
            boolean result = waterSupplyDeviceService.saveMaintainRecord(vo);
            return result ? AjaxResult.success("新增成功") : AjaxResult.error("新增失败");
        } catch (Exception e) {
            log.error("新增维修记录失败", e);
            return AjaxResult.error("新增失败：" + e.getMessage());
        }
    }

    @PutMapping("/record")
    @ApiOperation("修改维修记录")
    @Log(title = "修改维修记录", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('waterSupply:device:maintain:edit')")
    public AjaxResult updateMaintainRecord(@RequestBody EquipmentMaintainDataVO vo) {
        try {
            boolean result = waterSupplyDeviceService.updateMaintainRecord(vo);
            return result ? AjaxResult.success("修改成功") : AjaxResult.error("修改失败");
        } catch (Exception e) {
            log.error("修改维修记录失败", e);
            return AjaxResult.error("修改失败：" + e.getMessage());
        }
    }

    @DeleteMapping("/record/{maintainId}")
    @ApiOperation("删除维修记录")
    @Log(title = "删除维修记录", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('waterSupply:device:maintain:remove')")
    public AjaxResult deleteMaintainRecord(@PathVariable String maintainId) {
        boolean result = waterSupplyDeviceService.deleteMaintainRecord(maintainId);
        return result ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
    }

    @PostMapping("/record/import")
    @ApiOperation("批量导入维修记录")
    @Log(title = "批量导入维修记录", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('waterSupply:device:maintain:import')")
    public AjaxResult importMaintainRecords() {
        return AjaxResult.success("导入功能待对接EasyExcel");
    }

    @PostMapping("/record/export")
    @ApiOperation("批量导出维修记录")
    @Log(title = "导出维修记录", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('waterSupply:device:maintain:export')")
    public AjaxResult exportMaintainRecords() {
        return AjaxResult.success("导出功能待对接EasyExcel");
    }
}
