package com.bonus.sgzb.base.controller;

import com.alibaba.fastjson.JSONObject;
import com.bonus.sgzb.base.domain.RepairApplyRecord;
import com.bonus.sgzb.base.domain.RepairPartDetails;
import com.bonus.sgzb.base.domain.RepairTask;
import com.bonus.sgzb.base.domain.RepairTaskDetails;
import com.bonus.sgzb.base.domain.vo.DictVo;
import com.bonus.sgzb.base.service.RepairService;
import com.bonus.sgzb.common.core.utils.poi.ExcelUtil;
import com.bonus.sgzb.common.core.web.controller.BaseController;
import com.bonus.sgzb.common.core.web.domain.AjaxResult;
import com.bonus.sgzb.common.core.web.page.TableDataInfo;
import com.bonus.sgzb.common.log.annotation.Log;
import com.bonus.sgzb.common.log.enums.BusinessType;
import com.bonus.sgzb.common.security.annotation.Logical;
import com.bonus.sgzb.common.security.annotation.RequiresPermissions;
import com.bonus.sgzb.system.api.domain.SysUser;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author c liu
 * @date 2023/12/11
 */
@RestController
@RequestMapping("/base/repair")
public class RepairController extends BaseController {
    @Autowired
    private RepairService service;

    /**
     * 获取维修任务列表
     */
    @ApiOperation(value = "获取维修任务列表")
    @Log(title = "维修任务列表", businessType = BusinessType.QUERY)
    @GetMapping("/getRepairTaskList")
    @RequiresPermissions(value = "repair:manage:list")
    public TableDataInfo getRepairTaskList(RepairTask bean)
    {
        startPage();
        List<RepairTask> list = service.getRepairTaskList(bean);
        return getDataTable(list);
    }

    /**
     * 导出维修任务列表
     */
    @Log(title = "导出维修任务列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @RequiresPermissions("repair:manage:export")
    public void export(HttpServletResponse response, RepairTask bean)
    {
        List<RepairTask> list = service.exportRepairTaskList(bean);
        ExcelUtil<RepairTask> util = new ExcelUtil<RepairTask>(RepairTask.class);
        util.exportExcel(response, list, "维修任务列表");
    }

    /**
     * 获取维修任务列表
     */
    @ApiOperation(value = "获取维修任务列表")
    @Log(title = "维修任务列表", businessType = BusinessType.QUERY)
    @GetMapping("/getAppRepairTaskList")
    @RequiresPermissions("repair:manage:list")
    public AjaxResult getAppRepairTaskList(RepairTask bean)
    {
        List<RepairTask> list = service.getRepairTaskList(bean);
        return AjaxResult.success(list);
    }

    /**
     * 获取维修任务机具列表
     */
    @ApiOperation(value = "获取维修任务机具列表")
    @Log(title = "维修任务机具列表", businessType = BusinessType.QUERY)
    @GetMapping("/getRepairMaTypeList")
    @RequiresPermissions("repair:manage:preview")
    public TableDataInfo getRepairMaTypeList(RepairTaskDetails bean)
    {
        startPage();
        List<RepairTaskDetails> list = service.getRepairMaTypeList(bean);
        return getDataTable(list);
    }

    /**
     * 获取维修任务机具列表
     */
    @ApiOperation(value = "获取维修任务机具列表")
    @Log(title = "维修任务机具列表", businessType = BusinessType.QUERY)
    @GetMapping("/getAppRepairMaTypeList")
    public AjaxResult getAppRepairMaTypeList(RepairTaskDetails bean)
    {
        List<RepairTaskDetails> list = service.getRepairMaTypeList(bean);
        return AjaxResult.success(list);
    }

    /**
     * 新增维修记录
     */
    @ApiOperation(value = "新增维修记录")
    @Log(title = "新增维修记录", businessType = BusinessType.INSERT)
    @PostMapping("/submitRepairApply")
    public AjaxResult submitRepairApply(@RequestBody RepairApplyRecord bean) {
        String partStrList = bean.getPartStrList();
        List<RepairPartDetails> repairPartDetails = JSONObject.parseArray(partStrList, RepairPartDetails.class);
        bean.setPartList(repairPartDetails);
        AjaxResult ajaxResult = service.submitRepairApply(bean);
        return ajaxResult;
    }

    /**
     * 快捷维修记录
     */
    @ApiOperation(value = "快捷维修记录")
    @Log(title = "快捷维修记录", businessType = BusinessType.INSERT)
    @PostMapping("/fastRepairApply")
    public AjaxResult fastRepairApply(@RequestBody List<RepairTaskDetails> list) {
        AjaxResult ajaxResult = service.fastRepairApply(list);
        return ajaxResult;
    }

    /**
     * 完成维修
     */
    @ApiOperation(value = "完成维修")
    @Log(title = "完成维修", businessType = BusinessType.INSERT)
    @PostMapping("/completeRepair")
    public AjaxResult completeRepair(@RequestBody ArrayList<Long> ids) {
        return toAjax(service.completeRepair(ids));
    }

    /**
     * 提交审核
     */
    @ApiOperation(value = "提交审核")
    @Log(title = "提交审核", businessType = BusinessType.INSERT)
    @PostMapping("/endRepairTask")
    public AjaxResult endRepairTask(@RequestBody ArrayList<RepairTask> taskList) {
        return service.endRepairTask(taskList);
    }

    /**
     * 获取维修员下拉选
     */
    @GetMapping("/getUserSelect")
    public AjaxResult getUserSelect()
    {
        List<SysUser> list = service.selectUserList();
        return AjaxResult.success(list);
    }

    /**
     * 字典下拉选
     */
    @GetMapping("/getDicSelect")
    public AjaxResult getDicSelect(@RequestParam String value)
    {
        List<DictVo> list = service.getDicSelect(value);
        return AjaxResult.success(list);
    }


}
