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

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yeejoin.amos.boot.biz.common.bo.ReginParams;
import com.yeejoin.amos.boot.biz.common.controller.BaseController;
import com.yeejoin.amos.boot.module.jg.api.dto.JgEnableDisableDto;
import com.yeejoin.amos.boot.module.jg.biz.service.impl.JgEnableDisableServiceImpl;
import com.yeejoin.amos.boot.module.ymt.api.dto.IdxBizJgProjectContraptionDto;
import com.yeejoin.amos.boot.module.ymt.api.entity.IdxBizJgProjectContraption;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
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;

import java.util.Map;

/**
 * @author system_generator
 * @date 2023-12-25
 */
@RestController
@Api(tags = "Api")
@RequestMapping(value = "/jg-enable-disable")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class JgEnableDisableController extends BaseController {


    private final JgEnableDisableServiceImpl jgEnableDisableServiceImpl;

    /**
     * 新增
     *
     * @return
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @PostMapping(value = "/save")
    @ApiOperation(httpMethod = "POST", value = "停用启用新增", notes = "新增")
    @RestEventTrigger(value = "operateLogRestEventHandler")
    public ResponseModel<Object> save(@RequestBody JSONObject map) {
        ReginParams reginParams = getSelectedOrgInfo();
        return ResponseHelper.buildResponse(jgEnableDisableServiceImpl.saveOrUpdate(map, reginParams));
    }

    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @GetMapping(value = "/page")
    @ApiOperation(httpMethod = "GET", value = "使用登记设备关系表分页查询", notes = "使用登记设备关系表分页查询")
    public ResponseModel<Page<Map<String, Object>>> page(@RequestParam(value = "current") int current,
                                                         @RequestParam(value = "size") int size,
                                                         @RequestParam(value = "sort", required = false) String sort,
                                                         @ApiParam(value = "客户端类型: jgAudit-监管审核;jgLook-监管查看")
                                                         @RequestParam(value = "client", required = false, defaultValue = "jgAudit") String client,
                                                         JgEnableDisableDto dto) {
        Page<Map<String, Object>> page = new Page<>();
        page.setCurrent(current);
        page.setSize(size);
        return ResponseHelper.buildResponse(jgEnableDisableServiceImpl.pageList(page, sort, dto, client, getSelectedOrgInfo()));
    }

    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @PostMapping(value = "/flowExecute")
    @ApiOperation(httpMethod = "POST", value = "停用启用执行流程", notes = "执行流程")
    @RestEventTrigger(value = "operateLogRestEventHandler")
    public ResponseModel<Object> flowExecute(@RequestBody JSONObject map) {
        jgEnableDisableServiceImpl.flowExecute(map, getSelectedOrgInfo());
        return ResponseHelper.buildResponse("ok");
    }


    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @PostMapping(value = "/withdraw")
    @ApiOperation(httpMethod = "POST", value = "停用启用撤回", notes = "撤回")
    @RestEventTrigger(value = "operateLogRestEventHandler")
    public ResponseModel<Object> withdraw(@RequestBody JSONObject map) {
        jgEnableDisableServiceImpl.withdraw(String.valueOf(map.get("instanceId")), String.valueOf(map.get("nextTaskId")));
        return ResponseHelper.buildResponse("ok");
    }

    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @DeleteMapping(value = "/deleteMessage")
    @ApiOperation(httpMethod = "DELETE", value = "停用启用删除", notes = "删除")
    @RestEventTrigger(value = "operateLogRestEventHandler")
    public ResponseModel<Object> deleteMessage(@RequestParam("sequenceNbr") Long sequenceNbr) {
        jgEnableDisableServiceImpl.deleteMessage(sequenceNbr);
        return ResponseHelper.buildResponse("ok");
    }


    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @GetMapping(value = "/detail")
    @ApiOperation(httpMethod = "GET", value = "详情", notes = "详情")
    public ResponseModel<Object> detail(@RequestParam("sequenceNbr") Long sequenceNbr) {
        return ResponseHelper.buildResponse(jgEnableDisableServiceImpl.getDetail(sequenceNbr, getSelectedOrgInfo().getCompany()));
    }

    /**
     * 处理设备启用停用流程结束，业务未结束单子问题
     *
     * @param applyNo 申请单号
     * @return s
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @PostMapping(value = "/handleEnableDisableWrongData")
    @ApiOperation(httpMethod = "POST", value = "处理设备启用停用流程结束，业务未结束单子问题", notes = "处理设备启用停用流程结束，业务未结束单子问题")
    public ResponseModel<Object> handleEnableDisableWrongData(@RequestParam("applyNo") String applyNo) {
        jgEnableDisableServiceImpl.handleEnableDisableWrongData(applyNo);
        return ResponseHelper.buildResponse("ok");
    }

    /**
     * 查询使用登记证状态为已登记的装置列表
     *
     * @param current 当前页
     * @param size    每页数
     * @return page
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @GetMapping(value = "/jgProjectContraptionPage")
    @ApiOperation(value = "查询使用登记证状态为已登记的装置列表", notes = "查询使用登记证状态为已登记的装置列表")
    public ResponseModel<IPage<IdxBizJgProjectContraption>> getJgProjectContraptionPage(@RequestParam(required = false) String sequenceNbr,
                                                                                        @RequestParam(value = "current") int current,
                                                                                        @RequestParam(value = "size") int size) {
        Page<IdxBizJgProjectContraption> page = new Page<>();
        page.setCurrent(current);
        page.setSize(size);
        IPage<IdxBizJgProjectContraption> re = jgEnableDisableServiceImpl.getJgProjectContraptionPage(getSelectedOrgInfo().getCompany().getCompanyCode(), page, sequenceNbr);
        return ResponseHelper.buildResponse(re);
    }

    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @GetMapping(value = "/projectContraption")
    @ApiOperation(value = "装置详情", notes = "装置详情")
    public ResponseModel<IdxBizJgProjectContraptionDto> findOneProjectContraption(@RequestParam("record") Long sequenceNbr) {
        IdxBizJgProjectContraptionDto idxBizJgProjectContraptionDto = jgEnableDisableServiceImpl.findOneProjectContraption(sequenceNbr);
        return ResponseHelper.buildResponse(idxBizJgProjectContraptionDto);
    }

    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @GetMapping(value = "/pipeline/page")
    @ApiOperation(value = "查询装置下可做启停的管道", notes = "查询装置下可做启停的管道")
    public ResponseModel<IPage<Map<String, Object>>> findPipeLinePage(
            @RequestParam(value = "current") int current,
            @RequestParam(value = "size") int size,
            @RequestParam String projectContraptionId,
            @RequestParam String applyType,
            @RequestParam Map<String, Object> filters) {
        Page<Map<String, Object>> page = new Page<>();
        page.setCurrent(current);
        page.setSize(size);
        IPage<Map<String, Object>> re = jgEnableDisableServiceImpl.findPipeLinePage(page, projectContraptionId, applyType, filters);
        return ResponseHelper.buildResponse(re);
    }


}
