package com.bonus.material.repair.controller;

import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.poi.ExcelUtil;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.web.page.TableDataInfo;
import com.bonus.common.security.annotation.RequiresPermissions;
import com.bonus.material.repair.domain.RepairApplyRecord;
import com.bonus.material.repair.domain.RepairPartDetails;
import com.bonus.material.repair.domain.RepairTask;
import com.bonus.material.repair.domain.RepairTaskDetails;
import com.bonus.material.repair.domain.vo.RepairDeviceListVo;
import com.bonus.material.repair.domain.vo.RepairDeviceVO;
import com.bonus.material.repair.service.RepairService;
import com.bonus.system.api.domain.SysUser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * @author syruan
 */
@RestController
@RequestMapping("/repair")
@Api(tags = "维修管理")
public class RepairController extends BaseController {

    @Resource
    private RepairService service;

    /**
     * Jackson -- 避免反序列化漏洞
     */
    private final ObjectMapper objectMapper = new ObjectMapper();

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

    /**
     * 获取维修任务列表--不分页--NO_PAGE
     */
    @ApiOperation(value = "获取维修任务列表---不分页")
    @GetMapping("/getAppRepairTaskList")
    @RequiresPermissions("repair:manage:list")
    public AjaxResult getAppRepairTaskList(RepairTask bean) {
        List<RepairTask> list = service.getRepairTaskList(bean);
        return AjaxResult.success(list);
    }

    /**
     * 获取维修任务物资设备列表--不分页
     */
    @ApiOperation(value = "获取维修物资设备列表---分页")
    @GetMapping("/getRepairMaTypeList")
//    @RequiresPermissions("repair:manage:preview")
    public TableDataInfo getRepairMaTypeList(RepairTaskDetails bean) {
        startPage();
        List<RepairDeviceListVo> list = service.getRepairMaTypeList(bean);
        return getDataTable(list);
    }

    /**
     * 获取维修任务机具列表--Ⅱ级页面详情列表
     */
    @ApiOperation(value = "获取维修物资设备列表---不分页")
    @GetMapping("/getAppRepairMaTypeList")
    public AjaxResult getAppRepairMaTypeList(RepairTaskDetails bean) {
        List<RepairDeviceListVo> list = service.getRepairMaTypeList(bean);
        return AjaxResult.success(list);
    }

    /**
     * 新增维修记录
     */
    @ApiOperation(value = "新增维修记录")
    @PostMapping("/submitRepairApply")
    public AjaxResult submitRepairApply(@RequestBody @NotNull RepairApplyRecord bean) {
        try {
            String partStrList = bean.getPartStrList();
            if (StringUtils.isNoneBlank(partStrList)) {
                List<RepairPartDetails> repairPartDetails = objectMapper.readValue(partStrList, new TypeReference<List<RepairPartDetails>>() {});
                bean.setPartList(Optional.ofNullable(repairPartDetails).orElse(new ArrayList<>()));
            }
            return service.submitRepairApply(bean);
        } catch (JsonProcessingException e) {
            throw new ServiceException("Jackson反序列化异常:" + e.getMessage());
        }
    }

    /**
     * 快捷维修 1.把维修都设置为内部维修，增加维修配件明细
     */
    @ApiOperation(value = "快捷维修--批量--默认内部维修")
    @PostMapping("/fastRepairApply")
    public AjaxResult fastRepairApply(@RequestBody List<RepairTaskDetails> list) {
        return service.fastRepairApply(list);
    }

    /**
     * 批量维修 1.判断维修方式，增加维修配件明细
     */
    @ApiOperation(value = "批量维修--批量--按照传参维修方式进行维修")
    @PostMapping("/batchRepairApply")
    public AjaxResult batchRepairApply(@RequestBody @NotNull List<RepairDeviceVO> list) {
        return service.batchRepairApply(list);
    }

    /**
     * 维修明细--批量审核--更新明细status
     */
    @ApiOperation(value = "维修明细更新status--批量")
    @PostMapping("/completeRepair")
    public AjaxResult completeRepair(@RequestBody ArrayList<Long> ids) {
        return toAjax(service.completeRepair(ids));
    }

    /**
     * 任务维修完成--提交修饰审核
     */
    @ApiOperation(value = "任务维修完成--提交至修饰审核")
    @PostMapping("/endRepairTask")
    public AjaxResult endRepairTask(@RequestBody ArrayList<RepairTask> taskList) {
        return service.endRepairTask(taskList);
    }

    /**
     * 驳回退料--批量
     */
    @ApiOperation(value = "驳回退料--批量")
    @DeleteMapping("/{ids}")
    public AjaxResult rejectRepair(@PathVariable Long[] ids) {
        return service.rejectRepair(Arrays.asList(ids));
    }

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

        /**
     * 导出维修任务列表--外层--壹级列表
     */
    @PostMapping("/export")
    @RequiresPermissions("repair:manage:export")
    @ApiOperation(value = "导出维修任务列表")
    public void export(HttpServletResponse response, RepairTask bean) {
        List<RepairTask> list = service.exportRepairTaskList(bean);
        ExcelUtil<RepairTask> util = new ExcelUtil<>(RepairTask.class);
        util.exportExcel(response, list, "维修任务列表");
    }


}
