package com.yeejoin.amos.boot.module.jyjc.biz.controller;

import com.alibaba.fastjson.JSONObject;
import com.yeejoin.amos.boot.biz.common.controller.BaseController;
import com.yeejoin.amos.boot.module.jyjc.api.model.InspectionPlanModel;
import com.yeejoin.amos.boot.module.jyjc.biz.service.impl.InspectionPlanServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.typroject.tyboot.component.event.RestEventTrigger;
import org.typroject.tyboot.core.foundation.enumeration.UserType;
import org.typroject.tyboot.core.restful.doc.TycloudOperation;
import org.typroject.tyboot.core.restful.utils.ResponseHelper;
import org.typroject.tyboot.core.restful.utils.ResponseModel;

/**
 * 检验检测计划控制期
 *
 * @author Administrator
 */
@RestController
@RequestMapping(value = "/inspection/plan")
@Api(tags = "计划排期")
@RequiredArgsConstructor
public class InspectionPlanController extends BaseController {

    private final InspectionPlanServiceImpl inspectionPlanService;

    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @GetMapping(value = "/{applicationSeq}/workBenchDetail")
    @ApiOperation(httpMethod = "GET", value = "计划排期详情及单据信息-工作台", notes = "计划排期详情及单据信息-工作台")
    public ResponseModel<JSONObject> getWorkBenchDetail(@PathVariable String applicationSeq) {
        return ResponseHelper.buildResponse(inspectionPlanService.getWorkBenchDetail(applicationSeq));
    }


    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @PostMapping(value = "/flowExecute")
    @ApiOperation(httpMethod = "POST", value = "计划排期执行流程", notes = "计划排期执行流程")
    @RestEventTrigger(value = "operateLogRestEventHandler")
    public ResponseModel<Object> flowExecute(@RequestBody JSONObject map) {
        inspectionPlanService.flowExecute(Long.valueOf(String.valueOf(map.get("applicationSeq"))),
                String.valueOf(map.get("instanceId")),
                String.valueOf(map.get("operate")),
                String.valueOf(map.get("comment")),
                String.valueOf(map.get("nextTaskId")), map, getSelectedOrgInfo());
        return ResponseHelper.buildResponse("ok");
    }


    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @GetMapping(value = "/withdraw")
    @ApiOperation(httpMethod = "POST", value = "计划排期撤回", notes = "撤回")
    @RestEventTrigger(value = "operateLogRestEventHandler")
    public ResponseModel<Boolean> withdraw(@RequestParam("sequenceNbr") String sequenceNbr,
                                           @RequestParam("instanceId") String instanceId,
                                           @RequestParam("nextTaskId") String nextTaskId) {
        return ResponseHelper.buildResponse(inspectionPlanService.withdraw(instanceId, nextTaskId, sequenceNbr));
    }

    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @PostMapping(value = "/{applicationSeq}/save")
    @ApiOperation(httpMethod = "GET", value = "计划排期编辑-列表二次维护使用", notes = "计划排期编辑")
    public ResponseModel<InspectionPlanModel> savePlanData(@PathVariable String applicationSeq,
                                                           @RequestBody InspectionPlanModel model) {
        return ResponseHelper.buildResponse(inspectionPlanService.savePlanData(applicationSeq, model));
    }

    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @GetMapping(value = "/{applicationSeq}/detail")
    @ApiOperation(httpMethod = "GET", value = "计划排期详情-列表二次维护使用", notes = "计划排期详情")
    public ResponseModel<JSONObject> planDataDetail(@PathVariable String applicationSeq) {
        return ResponseHelper.buildResponse(inspectionPlanService.getDetail(applicationSeq));
    }


    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @PutMapping(value = "/workBench/create")
    @ApiOperation(httpMethod = "PUT", value = "人工下发计划排期待办", notes = "人工下发计划排期待办")
    public ResponseModel<Boolean> workBenchCreate(@RequestParam String applicationSeq) {
        return ResponseHelper.buildResponse(inspectionPlanService.workBenchCreate(applicationSeq));
    }

}
