package com.bonus.material.repair.controller;

import cn.hutool.core.convert.Convert;
import com.bonus.common.biz.config.ListPagingUtil;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.ServletUtils;
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.log.annotation.SysLog;
import com.bonus.common.log.enums.OperaType;
import com.bonus.common.security.annotation.RequiresPermissions;
import com.bonus.material.back.domain.vo.BackApplyVo;
import com.bonus.material.common.annotation.PreventRepeatSubmit;
import com.bonus.material.ma.domain.LabelBind;
import com.bonus.material.repair.domain.*;
import com.bonus.material.repair.domain.vo.*;
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.aspectj.weaver.loadtime.Aj;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSONObject;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.*;

/**
 * @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 AjaxResult getRepairTaskList(RepairTask bean) {
        Integer pageIndex = Convert.toInt(ServletUtils.getParameter("pageNum"), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter("pageSize"), 10);
        List<RepairTask> list = service.getRepairTaskList(bean);
        return AjaxResult.success(ListPagingUtil.paging(pageIndex, pageSize, list));
    }

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

    /**
     * 获取维修任务列表--app
     *
     * @param bean
     * @return
     */
    @ApiOperation(value = "获取维修任务详情列表--分页")
    @GetMapping("/getDetailsListApp")
    //@RequiresPermissions(value = "repair:manage:list")
    public TableDataInfo getDetailsListApp(RepairPartDetails bean) {
        startPage();
        List<RepairPartDetails> list = service.getDetailsListApp(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 = "获取维修物资设备列表---不分页")
    //@RequiresPermissions(value = "repair:manage:list")
    @GetMapping("/getAppRepairMaTypeList")
    public AjaxResult getAppRepairMaTypeList(RepairTaskDetails bean) {
        List<RepairDeviceSummaryVo> list = service.getRepairDeviceSummary(bean);
        return AjaxResult.success(list);
    }

    /**
     * 维修已完成--Ⅱ级页面详情列表
     */
    @ApiOperation(value = "维修已完成--Ⅱ级页面详情列表")
    @GetMapping("/getAppRepairMaTypeListNew")
    public AjaxResult getAppRepairMaTypeListNew(RepairTaskDetails bean) {
        List<RepairDeviceSummaryVo> list = service.getAppRepairMaTypeListNew(bean);
        return AjaxResult.success(list);
    }

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

    /**
     * 获取维修任务机具列表--三级页面详情列表--编码
     */
    @ApiOperation(value = "获取维修任务机具列表--三级页面详情列表--编码")
    @GetMapping("/getRepairCodeList")
    public AjaxResult getRepairCodeList(RepairTaskDetails bean) {
        List<RepairTaskDetails> list = service.getRepairCodeList(bean);
        return AjaxResult.success(list);
    }

    /**
     * 查询试验记录表数据
     */
    @ApiOperation(value = "查询试验记录表数据")
    @GetMapping("/getTestRecordData")
    public AjaxResult getTestRecordData(RepairTaskDetails bean) {
        List<RepairTaskDetails> list = service.getTestRecordData(bean);
        return AjaxResult.success(list);
    }

    /**
     * 查询维修单
     */
    @ApiOperation(value = "获取维修单详情")
    //@RequiresPermissions(value = "repair:manage:query")
    @GetMapping("/getRepairDocumentInfo")
    public AjaxResult getRepairDocumentInfo(Long taskId) {
        return service.getRepairDocumentInfo(taskId);
    }

    /**
     * 新增维修记录
     */
    @ApiOperation(value = "新增维修记录")
    //@RequiresPermissions(value = "repair:manage:submit")
    @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，以及repairedNum
     */
    @ApiOperation(value = "维修明细更新status--批量合格")
    @PostMapping("/batchQualified")
    public AjaxResult batchQualified(@RequestBody ArrayList<Long> ids) {
        return toAjax(service.batchQualified(ids));
    }

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

    /**
     * 驳回退料--批量
     */
    @ApiOperation(value = "驳回退料--批量")
    //@RequiresPermissions(value = "repair:manage:reject")
    @PostMapping("/rejectRepair")
    public AjaxResult rejectRepair(@RequestBody RepairTask bean) {
        return service.rejectRepair(bean);
    }

    /**
     * 获取维修员下拉选
     */
    @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 = new ArrayList<>();
        try {
            list = service.exportRepairTaskList(bean);
        } catch (Exception e) {
            list = new ArrayList<>();
        }
        ExcelUtil<RepairTask> util = new ExcelUtil<>(RepairTask.class);
        util.exportExcel(response, list, "维修任务列表");
    }

    @ApiOperation(value = "获取修试后入库列表")
    @GetMapping("getRepairedList")
    //@RequiresPermissions("warehousing:repair:list")
    public TableDataInfo getRepairedList(RepairInputDetailsVo dto) {
        startPage();
        List<RepairInputDetailsVo> list = service.getRepairedList(dto);
        return getDataTable(list);
    }

    @ApiOperation(value = "获取修试后入库列表-详情")
    @GetMapping("getRepairedDetailList")
    //@RequiresPermissions("warehousing:repair:list")
    public TableDataInfo getRepairedDetailList(RepairInputDetailsVo dto) {
        startPage();
        List<RepairInputDetailsVo> list = service.getRepairedDetailList(dto);
        return getDataTable(list);
    }

    @ApiOperation(value = "获取Ocr code")
    @RequestMapping(value = "getOcrCode", method = RequestMethod.POST)
    //@RequiresPermissions("repair:manage:list")
    @ResponseBody
    public JSONObject getOcrCode(OCRBean o) {
        String url = "http://10.138.55.102:10000/api_gqj/pre";
        JSONObject js = null;
        try {
            String jsonStr = JSONObject.toJSONString(o);
            String data = "{\"code\": \"fack_code\"}";     //HttpHelper.sendHttpPostForToken(url, jsonStr);
            js = JSONObject.parseObject(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return js;
    }

    /**
     * 查询修试查询-维修配件查询列表
     *
     * @param dto
     * @return
     */
    @ApiOperation(value = "查询修试查询-维修配件查询列表")
    @GetMapping("/selectRepairPartList")
    public AjaxResult selectRepairPartList(RepairPartVo dto) {
        Integer pageIndex = Convert.toInt(ServletUtils.getParameter("pageNum"), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter("pageSize"), 10);
        List<RepairPartVo> list = service.selectRepairPartList(dto);
        return AjaxResult.success(ListPagingUtil.paging(pageIndex, pageSize, list));
    }

    /**
     * 导出修试查询-维修配件查询列表
     *
     * @param response
     * @param dto
     */
    @PostMapping("/exportRepairPartList")
    public void exportBackApplyList(HttpServletResponse response, RepairPartVo dto) {
        List<RepairPartVo> list = service.selectRepairPartList(dto);
        ExcelUtil<RepairPartVo> util = new ExcelUtil<RepairPartVo>(RepairPartVo.class);
        util.exportExcel(response, list, "退维修配件查询数据");
    }

    /**
     * 查询修试查询-维修配件查询二级列表
     *
     * @param dto
     * @return
     */
    @ApiOperation(value = "查询修试查询-维修配件查询二级列表")
    @GetMapping("/selectSecondRepairPartList")
    public AjaxResult selectSecondRepairPartList(RepairPartInfo dto) {
        Integer pageIndex = Convert.toInt(ServletUtils.getParameter("pageNum"), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter("pageSize"), 10);
        List<RepairPartInfo> list = service.selectSecondRepairPartList(dto);
        return AjaxResult.success(ListPagingUtil.paging(pageIndex, pageSize, list));
    }

    /**
     * 获取定损配件
     */
    @ApiOperation(value = "获取定损配件")
    @GetMapping("/getLossPart")
    public AjaxResult getLossPart(RepairPartInfo bean) {
        List<RepairPartInfo> list = service.getLossPart(bean);
        return AjaxResult.success(list);
    }


    @ApiOperation(value = "维修检验试验报表")
    @GetMapping("/getRepairInspection")
    public TableDataInfo getRepairInspection(RepairInspection dto) {
        try {
            startPage();
            List<RepairInspection> list = service.getRepairInspection(dto);
            return getDataTable(list);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return getDataTable(Collections.emptyList());
        }
    }

    /**
     * 导出机具设备标签列表
     */
    @ApiOperation(value = "导出维修检验试验报表")
    @PreventRepeatSubmit
    @SysLog(title = "机具设备标签", businessType = OperaType.EXPORT, logType = 1, module = "仓储管理->导出维修检验试验报表")
    @PostMapping("/getRepairInspectionExport")
    public void getRepairInspectionExport(HttpServletResponse response, RepairInspection dto) {
        List<RepairInspection> list = service.getRepairInspection(dto);
        ExcelUtil<RepairInspection> util = new ExcelUtil<RepairInspection>(RepairInspection.class);
        util.exportExcel(response, list, "机具设备标签数据");
    }


    @ApiOperation(value = "维修检验试验报表")
    @GetMapping("/getTestRecord")
    public TableDataInfo getTestRecord(TestRecord dto) {
        try {
            startPage();
            List<TestRecord> list = service.getTestRecord(dto);
            return getDataTable(list);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return getDataTable(Collections.emptyList());
        }
    }


    /**
     * 导出机具设备标签列表
     */
    @ApiOperation(value = "导出维修检验试验报表")
    @PreventRepeatSubmit
    @SysLog(title = "机具设备标签", businessType = OperaType.EXPORT, logType = 1, module = "仓储管理->导出维修检验试验报表")
    @PostMapping("/getTestRecordExport")
    public void getTestRecordExport(HttpServletResponse response, TestRecord dto) {
        List<TestRecord> list = service.getTestRecord(dto);
        ExcelUtil<TestRecord> util = new ExcelUtil<TestRecord>(TestRecord.class);
        util.exportExcel(response, list, "机具设备标签数据");
    }

    /**
     * 查询试验记录表数据
     */
    @ApiOperation(value = "查询试验记录表数据")
    @GetMapping("/getTestRecordDetails")
    public AjaxResult getTestRecordDetails(TestRecord bean) {
        List<RepairTaskDetails> list = service.getTestRecordDetails(bean);
        return AjaxResult.success(list);
    }

    /**
     * 查询在修工程维修列表
     * @param bean
     * @return
     */
    @ApiOperation(value = "查询在修工程维修列表")
    @GetMapping("/getRepairProjectList")
    public AjaxResult getRepairProjectList(RepairProjectInfo bean) {
        startPage();
        List<RepairProjectInfo> list = service.getRepairProjectList(bean);
        return AjaxResult.success(getDataTable(list));
    }

    /**
     * 导出在修工程维修列表
     * @param response
     * @param bean
     */
    @ApiOperation(value = "导出在修工程维修列表")
    @PostMapping("/exportRepairProjectList")
    public void exportRepairProjectList(HttpServletResponse response, RepairProjectInfo bean) {
        List<RepairProjectInfo> list = service.getRepairProjectList(bean);
        ExcelUtil<RepairProjectInfo> util = new ExcelUtil<RepairProjectInfo>(RepairProjectInfo.class);
        util.exportExcel(response, list, "在修工程维修列表");
    }


}
