
package com.jeelowcode.module.api;

import cn.hutool.json.JSONUtil;
import com.jeelowcode.core.framework.controller.BaseController;
import com.jeelowcode.core.framework.entity.ReportEntity;
import com.jeelowcode.core.framework.service.IFormService;
import com.jeelowcode.core.framework.service.IReportService;
import com.jeelowcode.framework.utils.enums.AuthTypeEnum;
import com.jeelowcode.framework.utils.model.ResultDataModel;
import com.jeelowcode.framework.utils.tool.CollectionUtil;
import com.jeelowcode.framework.utils.utils.FuncBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 低代码 API 接口
 */
@Service
public class JeeLowCodeApiImpl extends BaseController implements JeeLowCodeApi {

    @Autowired
    private IFormService formService;

    @Autowired
    private IReportService reportService;


    //获取表单设置的授权类型
    @Override
    public String getDbformAuthType(Long dbFormid) {
        AuthTypeEnum authType = formService.getAuthType(dbFormid);
        return authType.getType();

    }

    //获取表单设置的授权类型
    @Override
    public String getReportAuthType(String reportCode) {
        AuthTypeEnum authType = reportService.getAuthType(reportCode);
        return authType.getType();

    }

    @Override
    public void syncData(String reportCode, Map<String, Object> req, ResultDataModel pages) {
        //1.根据reportCode 获取报表数据
        ReportEntity reportEntity = reportService.getReportEntityByCode(reportCode);
        String tableConfig = reportEntity.getTableConfig();
        if (FuncBase.isEmpty(tableConfig)) {
            return;
        }
        List<String> dataConfigList = FuncBase.toStrList(tableConfig);
        if(!dataConfigList.isEmpty()&&dataConfigList.contains("sync")){
            //报表设置同步才同步
            //构建同步主体
            String syncTableName="lc_report_data_sync";
            String syncDetailTableName="lc_report_data_sync_detail";

            Long dbFormId = formService.getDbFormIdByTableName(syncTableName);
            Long detailFormId = formService.getDbFormIdByTableName(syncDetailTableName);
            HashMap<String, Object> newRecord = new HashMap<>();

            newRecord.put("reportCode", reportEntity.getReportCode());
            newRecord.put("reportParam", JSONUtil.toJsonStr(req));
            newRecord.put("isSync", 0);
            if(CollectionUtil.isNotEmpty(pages.getRecords()))
            {
                newRecord.put("resultCount", (long) pages.getRecords().size());
                List<HashMap<String, Object>> detail = new ArrayList<>();

                for (Map<String, Object> map : pages.getRecords()) {
                    HashMap<String, Object> newDetail = new HashMap<>();
                    newDetail.put("isSync", 0);
                    newDetail.put("resultData", JSONUtil.toJsonStr(map));
                    newDetail.put("sort", pages.getRecords().indexOf(map));
                    detail.add(newDetail);
                }
                newRecord.put(syncDetailTableName, detail);
            }else
            {
                newRecord.put("resultCount",0);
            }

            super.addJsonData(dbFormId, JSONUtil.parseObj(newRecord));
        }
    }

}
