package com.zkqy.web.controller.dragForm;

import com.zkqy.common.core.controller.BaseController;
import com.zkqy.common.core.domain.AjaxResult;
import com.zkqy.common.utils.form.FromUtils;
import com.zkqy.system.entity.CommonEntity;
import com.zkqy.system.entity.DragForm;
import com.zkqy.system.entity.DragTableBtn;
import com.zkqy.system.mapper.DragTableMapper;
import com.zkqy.system.service.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author hanzihang
 * @date 2023/11/16 5:49 PM
 */
@RestController
@RequestMapping("/dragform/commonbtn")
public class CommonBtnController extends BaseController {
    @Resource   // 共通crud
    private ICommonService commonService;

    @Resource  // 动态表单
    private IDragFormService dragFormService;

    @Resource  // 表格按钮
    private IDragTableBtnService iDragTableBtnService;

    @Resource
    private FromUtils fromUtils;

    @Resource
    private DragTableMapper dragTableMapper;

    @Resource
    private IDragTableGroupService dragTableGroupService;

    /**
     * 处理按钮通用接口（新增、修改、删除...）
     * 判断什么类型走什么接口: visible  false代表弹窗未开启时走回显表单模板及数据信息接口；true代表弹窗开启时走保存数据接口
     * 是否提交表单操作 true是 false回显表单数据  commonEntity.getBasicMap().get("visible").toString()
     */
    @PostMapping("/commonBtnHandle")
    public AjaxResult commonBtnHandle(@RequestBody CommonEntity commonEntity) {
        // 获取到按钮类型
        String btnType = commonEntity.getBasicMap().get("btnType").toString();
        /**
         * 发起流程类型(需要特定参数)，
         * 启动脚本类型(执行特定脚本无需传递参数)
         */
        switch (btnType) {
            case "INSERT":
                return insertBtn(commonEntity);
            case "UPDATE":
                return updateBtn(commonEntity);
            case "DELETE":
                return deleteBtn(commonEntity);
            case "EXECUTE":  // 执行脚本
                return executeBtn(commonEntity);
            case "INITIATED":  // 发起流程
                return initiatedBtn(commonEntity);
            default:
                return warn("暂不支持该操作！");
        }
    }

    /**
     * 新增类型按钮
     *
     * @param commonEntity
     * @return
     */

    public AjaxResult insertBtn(CommonEntity commonEntity) {
        if (commonEntity.getBasicMap().get("visible").toString().equals("true")) {
            return AjaxResult.btnMessage(commonService.batchInsert(commonEntity));
        } else if (commonEntity.getBasicMap().get("visible").toString().equals("false")) {
            String sqlKey = commonEntity.getBasicMap().get("sqlKey").toString();
            return success(dragFormService.selectDragFormBySqlKey(sqlKey));
        } else {
            return warn("暂不支持该操作！");
        }
    }


    /**
     * 修改类型按钮
     *
     * @param commonEntity
     * @return
     */
    public AjaxResult updateBtn(CommonEntity commonEntity) {
        //是否开启弹窗
        if (commonEntity.getBasicMap().get("visible").toString().equals("true")) {
            if (commonEntity.getBasicMap().containsKey("btnKey")) {  // 按钮提交时会触发别的操作
                // 根据按钮别名查询按钮详细信息
                DragTableBtn dragTableBtn = iDragTableBtnService.selectDragTableBtnByBtnKey(commonEntity.getBasicMap().get("btnKey").toString());
                // 根据流程的key判断当前按钮是否执行触发流程
                if (!dragTableBtn.getBtnProcessKey().isEmpty()) {
                    // 调用发起流程接口
                    fromUtils.runBpmProductionScheduling(commonEntity);
//                    if (dragTableBtn.getBtnFormKey().isEmpty()) {  // 校验当前按钮是否是直接触发流程 或者脚本 （新增按钮类型）
//                        fromUtils.runBpmProductionScheduling(commonEntity);
//                    }
                } else if (!dragTableBtn.getBtnScriptKey().isEmpty()) {
                    // 调用执行脚本接口
                    fromUtils.triggerScript(commonEntity);
                }
            }
            return toAjax(commonService.edit(commonEntity));
        } else if (commonEntity.getBasicMap().get("visible").toString().equals("false")) {
            Map<String, Object> map = new HashMap<>();
            // 单纯是表格还是弹窗里有表格
            String sqlKey = commonEntity.getBasicMap().get("sqlKey").toString();
            DragForm dragForm = dragFormService.selectDragFormBySqlKey(sqlKey);
            map.put("template", dragForm);
            // 返回结果
            map.put("result", commonService.getInfoById(commonEntity));
            return success(map);
        } else {
            return warn("暂不支持该操作！");
        }
    }

    /**
     * 删除类型按钮
     *
     * @param commonEntity
     * @return
     */
    public AjaxResult deleteBtn(CommonEntity commonEntity) {
        return toAjax(commonService.batchDelete(commonEntity));
    }

    /**
     * 按钮执行脚本
     *
     * @param commonEntity
     * @return
     */
    public AjaxResult executeBtn(CommonEntity commonEntity) {
        if (commonEntity.getBasicMap().get("visible").toString().equals("true")) {
            if (commonEntity.getBasicMap().containsKey("btnKey")) {  // 按钮提交时会触发别的操作
                // 根据按钮别名查询按钮详细信息
                DragTableBtn dragTableBtn = iDragTableBtnService.selectDragTableBtnByBtnKey(commonEntity.getBasicMap().get("btnKey").toString());
                if (!dragTableBtn.getBtnScriptKey().isEmpty()) {
                    // 调用执行脚本接口
                    fromUtils.triggerScript(commonEntity);
                }
            }
            commonService.edit(commonEntity);
            return AjaxResult.success("执行脚本成功！");
        } else if (commonEntity.getBasicMap().get("visible").toString().equals("false")) {
            Map<String, Object> map = new HashMap<>();
            String sqlKey = commonEntity.getBasicMap().get("sqlKey").toString();
            DragForm dragForm = dragFormService.selectDragFormBySqlKey(sqlKey);
            map.put("template", dragForm);
            map.put("result", commonService.getInfoById(commonEntity));
            return success(map);
        } else {
            return AjaxResult.error("执行脚本失败！");
        }
    }

    /**
     * 发起流程按钮
     *
     * @param commonEntity
     * @return
     */
    public AjaxResult initiatedBtn(CommonEntity commonEntity) {
        //是否开启弹窗
        if (commonEntity.getBasicMap().get("visible").toString().equals("true")) {
            if (commonEntity.getBasicMap().containsKey("btnKey")) {  // 按钮提交时会触发别的操作
                // 根据按钮别名查询按钮详细信息
                DragTableBtn dragTableBtn = iDragTableBtnService.selectDragTableBtnByBtnKey(commonEntity.getBasicMap().get("btnKey").toString());
                // 根据流程的key判断当前按钮是否执行触发流程
                if (!dragTableBtn.getBtnProcessKey().isEmpty()) {
                    //  表示对当前操作是及时选择的流程模板
                    if (dragTableBtn.getBtnProcessKey().equals("unknown")) {
                        // 调用发起流程接口
                        fromUtils.runBpmProductionScheduling(commonEntity);
                    } else {
                        // 执行当前按钮绑定的流程-》暂无直接执行流程
                    }
                }
            }
            toAjax(commonService.edit(commonEntity));
            return AjaxResult.success("启动流程成功！");
        } else if (commonEntity.getBasicMap().get("visible").toString().equals("false")) {
            Map<String, Object> map = new HashMap<>();
            // 单纯是表格还是弹窗里有表格
            String sqlKey = commonEntity.getBasicMap().get("sqlKey").toString();
            DragForm dragForm = dragFormService.selectDragFormBySqlKey(sqlKey);
            map.put("template", dragForm);
            // 返回结果
            map.put("result", commonService.getInfoById(commonEntity));
            return success(map);
        } else {
            // return warn("暂不支持该操作！");
            return AjaxResult.error("发起流程失败！");
        }
    }


    /**
     * 动态表格绑定的弹窗表格结构信息
     */
    @GetMapping(value = "/getProcessPopupTableInfo")
    public AjaxResult getProcessPopupTableInfo(CommonEntity commonEntity) {
        return AjaxResult.success(dragTableGroupService.getProcessPopupTableInfo(commonEntity));
    }

    /**
     * 动态表格弹窗表格信息
     */
    @GetMapping(value = "/getProcessPopupTableList")
    public AjaxResult getProcessPopupTableList(CommonEntity commonEntity) {
        return AjaxResult.success(dragTableGroupService.getProcessPopupTableList(commonEntity));
    }


}
