package com.product.mobile.core.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.product.admin.service.SystemButtonsService;
import com.product.common.lang.StringUtils;
import com.product.core.entity.DataTableEntity;
import com.product.core.entity.FieldSetEntity;
import com.product.core.service.support.AbstractBaseService;
import com.product.mobile.core.config.MobileCoreConst;
import com.product.util.BaseUtil;
import com.product.util.SystemParamReplace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author cheng
 * @Date 2022/3/8 16:33
 * @Desc 鍔熻兘鐩稿叧 service
 */
@Service
public class SystemFunctionService extends AbstractBaseService {

    private SystemButtonsService systemButtonsService;

    @Autowired
    public SystemFunctionService(SystemButtonsService systemButtonsService) {
        this.systemButtonsService = systemButtonsService;
    }

    /**
     * 鑾峰彇褰撳墠杩涘叆褰撳墠椤甸潰鐨勬寜閽拰褰撳墠椤甸潰杩炴帴锛坙ink) 鐨勬寜閽�
     *
     * @return
     */
    public Object getFunctionButton(FieldSetEntity fse) {
        String functionUuid = fse.getString("function_uuid");
        String buttonUuid = fse.getString("button_uuid");
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT ");
        sql.append(" a.*,JSON_object('face_uuid',c.face_uuid,'flow_title',flow_title,'uuid',c.uuid,'page_type',c.page_type,'tree_data',c.tree_data,'tree_data_type',tree_data_type ,'flow_uuid' ,c.flow_uuid,'page_element_disabled',c.page_element_disabled,'page_name',c.page_name,'from_param_key',c.from_param_key) as current_page, ");
        sql.append(" ( SELECT table_name FROM product_sys_datamodel_table where uuid =(select table_uuid FROM product_sys_functions b WHERE a.function_uuid = b.uuid and data_type=1) ) table_name ");
        sql.append(" FROM ");
        sql.append(" product_sys_function_buttons a ");
        sql.append(" JOIN product_sys_link b ON b.line_from = a.uuid ");
        sql.append(" JOIN product_sys_mvc_page c ON c.uuid = b.line_to ");
        sql.append(" WHERE a.function_uuid=? and a.uuid=? ");
        FieldSetEntity currentButton = getBaseDao().getFieldSetEntityBySQL(sql.toString(), new Object[]{functionUuid, buttonUuid}, false);
        if (currentButton != null && !StringUtils.isEmpty(currentButton.getString("params"))) {
            String params = currentButton.getString("params");
            boolean valid = JSON.isValid(params);
            if (valid) {
                JSONObject json = JSON.parseObject(params);
                Iterator var12 = json.entrySet().iterator();
                while (var12.hasNext()) {
                    Map.Entry<String, Object> one = (Map.Entry<String, Object>) var12.next();
                    Object value = one.getValue();
                    if (value != null) {
                        String val = value.toString();
                        one.setValue(SystemParamReplace.replaceParams(val, null));
                    }
                }

                currentButton.setValue("params", json.toJSONString());
            }
        }
        if (currentButton != null && currentButton.getObject("current_page") != null) {
            currentButton.setValue("current_page", JSON.parseObject(currentButton.getString("current_page")));
        }
        Map<String, Object> map = new HashMap<>();
        map.put("currentButton", BaseUtil.fieldSetEntityToJson(currentButton));
        DataTableEntity buttonByPage = systemButtonsService.findButtonByPage(fse);
        return BaseUtil.success(buttonByPage, map);
    }


}