package com.project.zcustom.tools;


import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.project.common.enums.EmployeeTypeEnum;
import com.project.common.utils.StringUtils;
import com.project.common.utils.http.HttpUtils;
import com.project.zcustom.domain.plat.PlatApiRecord;
import com.project.zcustom.service.access.IPlatAccessService;
import com.project.zcustom.service.service.basics.IPlatBasicsService;
import com.project.zcustom.service.service.employee.IPlatEmployeeHealthService;
import com.project.zcustom.service.service.employee.IPlatEmployeeTypeService;
import com.project.zcustom.service.plat.IPlatApiRecordService;
import com.project.zcustom.service.service.post.IPlatPostStationService;
import com.project.zcustom.service.service.property.IPlatPropertyService;
import com.project.zcustom.service.service.smart.IPlatGreenSmartOtherService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;


/**
 *  后勤服务保障中心
 *
 * @author change
 * @date 2024-10-21
 */
@Service
public class GuaranteeService {

    @Resource
    IPlatEmployeeTypeService platEmployeeTypeService;
    @Resource
    IPlatEmployeeHealthService platEmployeeHealthService;
    @Resource
    IPlatBasicsService platBasicsService;
    @Resource
    IPlatPostStationService postStationService;
    @Resource
    IPlatPropertyService platPropertyService;
    @Resource
    IPlatGreenSmartOtherService iPlatGreenSmartOtherService;
    @Resource
    IPlatAccessService iPlatAccessService;
    @Resource
    IPlatApiRecordService iPlatApiRecordService;


    /**
     * 获取服务保障中心的数据
     */
    public void getServiceData(String appOrg, String appName) {
        String url = ApiUrlConfig.getServiceUrl();
        JSONObject params = new JSONObject();
        params.put("appOrg", appOrg);
        String result = HttpUtils.sendPost(url, params.toJSONString());

        // 添加接口请求记录
        PlatApiRecord platApiRecord = new PlatApiRecord();
        platApiRecord.setApiUrl(url);
        platApiRecord.setApiResult(result);
        platApiRecord.setAppOrg(appOrg);
        iPlatApiRecordService.save(platApiRecord);

        if (StringUtils.isNotBlank(result)) {
            Map<String, Object> resultMap = JSONObject.parseObject(result, Map.class);
            if (Objects.equals(resultMap.get("code").toString(), "200") && resultMap.containsKey("entity")) {
                JSONArray data = JSONArray.parseArray(resultMap.get("entity").toString());
                for (Object o : data) {
                    Map<String, Object> temp = JSONObject.parseObject(o.toString(), Map.class);
                    if (temp.containsKey("dataType")) {
                        if (temp.containsKey("dataResult")) {
                            Map<String, Object> dataResult = JSONObject.parseObject(temp.get("dataResult").toString(), Map.class);
                            if (Objects.equals(temp.get("dataType").toString(), EmployeeTypeEnum.EMPLOYEE_TYPE.getCode())) {
                                platEmployeeTypeService.addPlatEmployeeType(appOrg, dataResult);
                            } else if (Objects.equals(temp.get("dataType").toString(), EmployeeTypeEnum.EMPLOYEE_HEALTH.getCode())) {
                                platEmployeeHealthService.addPlatEmployeeHealth(appOrg, dataResult);
                            } else if (Objects.equals(temp.get("dataType").toString(), EmployeeTypeEnum.GREEN_SMART.getCode())) {
                                iPlatGreenSmartOtherService.addPlatGreenSmartOther(appOrg, dataResult);
                            } else if (Objects.equals(temp.get("dataType").toString(), EmployeeTypeEnum.POST_STATION.getCode())) {
                                postStationService.addPlatPostStation(appOrg, dataResult);
                            } else if (Objects.equals(temp.get("dataType").toString(), EmployeeTypeEnum.PROPERTY.getCode())) {
                                platPropertyService.addPlatProperty(appOrg, dataResult);
                            } else if (Objects.equals(temp.get("dataType").toString(), EmployeeTypeEnum.BASICS.getCode())) {
                                platBasicsService.addPlatBasics(appOrg, appName, dataResult);
                            } else if (Objects.equals(temp.get("dataType").toString(), EmployeeTypeEnum.ACCESS.getCode())) {
                                iPlatAccessService.addPlatAccess(appOrg, dataResult);
                            }
                        }
                    }
                }
            }
        }
    }

}
