package com.hzya.frame.sys.apireflection.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.hzya.frame.basedao.service.impl.BaseService;
import com.hzya.frame.sys.apireflection.dao.IApiReflectionDao;
import com.hzya.frame.sys.apireflection.dao.IApiReflectionDetailDao;
import com.hzya.frame.sys.apireflection.entity.ApiReflectionDTO;
import com.hzya.frame.sys.apireflection.entity.ApiReflectionDetailEntity;
import com.hzya.frame.sys.apireflection.entity.ApiReflectionEntity;
import com.hzya.frame.sys.apireflection.service.IApiReflectionService;
import com.hzya.frame.uuid.UUIDLong;
import com.hzya.frame.web.entity.BaseResult;
import com.hzya.frame.web.entity.JsonResultEntity;
import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: 接口映射配置  service
 * @tableName: sys_api_reflection
 * @entityName: ApiReflectionEntity
 * @author: gjh
 * @history: 1.0
 */
@Service(value = "apiReflectionService")
public class ApiReflectionServiceImpl extends BaseService<ApiReflectionEntity, String> implements IApiReflectionService {

    protected IApiReflectionDao apireflectionDao;
    @Autowired
    private IApiReflectionDetailDao apiReflectionDetailDao;
    @Autowired
    @Resource
    private SqlSession sqlSession;

    @Autowired
    public void setApiReflectionDao(IApiReflectionDao dao) {
        this.apireflectionDao = dao;
        this.dao = dao;
    }
    /**
     * 查询所有
     * @param jsonObject
     * @return
     */
	/*@Override
	public JsonResultEntity queryList(JSONObject jsonObject) {
		ApiReflectionEntity entity = JSON.parseObject(jsonObject.toJSONString(),ApiReflectionEntity.class);
		List<ApiReflectionEntity> apiReflectionEntityList = apireflectionDao.queryList(entity);
		return BaseResult.getSuccessMessageEntity(apiReflectionEntityList);
	}*/

    /**
     * 根据id查询单个
     *
     * @param jsonObject
     * @return
     */
    @Override
    public JsonResultEntity queryOneById(JSONObject jsonObject) {
        ApiReflectionEntity apiReflectionEntity = JSONObject.parseObject(jsonObject.getString("jsonStr")).toJavaObject(ApiReflectionEntity.class);
        if (null == apiReflectionEntity.getId() || "".equals(apiReflectionEntity.getId())) {
            return BaseResult.getFailureMessageEntity("单个查看得时候请携带id");
        }
        ApiReflectionEntity apiReflectionEntityOne = apireflectionDao.queryOneById(apiReflectionEntity.getId());
        ApiReflectionDetailEntity apiReflectionDetailEntity = new ApiReflectionDetailEntity();
        apiReflectionDetailEntity.setMainid(apiReflectionEntity.getId());
        List<ApiReflectionDetailEntity> apiReflectionDetailEntityListBase = apiReflectionDetailDao.query(apiReflectionDetailEntity);
        JSONObject jsonObjectResult = new JSONObject();
        jsonObjectResult.put("main", apiReflectionEntityOne);
        List<ApiReflectionDetailEntity> list = new ArrayList<>();
        for (ApiReflectionDetailEntity reflectionDetailEntity : apiReflectionDetailEntityListBase) {
            list.add(reflectionDetailEntity);
        }
        jsonObjectResult.put("details", list);
        return BaseResult.getSuccessMessageEntity(jsonObjectResult);
    }

    /**
     * 保存
     *
     * @param jsonObject
     * @return
     */
    @Override
    public JsonResultEntity saveApiReflection(JSONObject jsonObject) {
        //ApiReflectionEntity apiReflectionEntity = JSONObject.parseObject(jsonObject.getString("jsonStr")).toJavaObject(ApiReflectionEntity.class);
        ApiReflectionEntity apiReflectionEntity = getData("jsonStr", jsonObject, ApiReflectionEntity.class);
        if (null == apiReflectionEntity.getRef_code() || "".equals(apiReflectionEntity.getRef_code())) {
            return BaseResult.getFailureMessageEntity("请输入场景编码");
        }
        if (null == apiReflectionEntity.getRef_name() || "".equals(apiReflectionEntity.getRef_name())) {
            return BaseResult.getFailureMessageEntity("请输入场景名称");
        }
        if (null == apiReflectionEntity.getRef_type() || "".equals(apiReflectionEntity.getRef_type())) {
            return BaseResult.getFailureMessageEntity("请输入场景分类");
        }
        if (null == apiReflectionEntity.getSource_api_app() || "".equals(apiReflectionEntity.getSource_api_app())) {
            return BaseResult.getFailureMessageEntity("请选择源应用");
        }
        if (null == apiReflectionEntity.getTarget_api_app() || "".equals(apiReflectionEntity.getTarget_api_app())) {
            return BaseResult.getFailureMessageEntity("请选择目标应用");
        }
        if (null == apiReflectionEntity.getDescription() || "".equals(apiReflectionEntity.getDescription())) {
            return BaseResult.getFailureMessageEntity("请输入场景描述");
        }
        //设置接口映射配置id
        long uuid = UUIDLong.longUUID();
        apiReflectionEntity.setId(String.valueOf(uuid));
        String jsonStr = jsonObject.getString("jsonStr");
        JSONObject jsonObjectStr = JSONObject.parseObject(jsonStr);
        //判断是否是插件，如果是插件，明细数据不保存，如果不是插件，明细数据保存 1是2否
        String plug = jsonObjectStr.getString("plug");
        //不是插件 2
        String isNotPlug = "2";
        //是插件 1
        String isPlug = "1";
        if (isNotPlug.equals(plug)) {
            //获取接口映射配置明细数据
            JSONArray jsonArray = jsonObjectStr.getJSONArray("details");
            for (Object object : jsonArray) {
                String jsonString = JSON.toJSONString(object);
                JSONObject jsonObjectDeatils = JSONObject.parseObject(jsonString);
                ApiReflectionDetailEntity apiReflectionDetailEntity = jsonObjectDeatils.toJavaObject(ApiReflectionDetailEntity.class);
                //设置接口映射配置明细id
                long uuidDetails = UUIDLong.longUUID();
                apiReflectionDetailEntity.setId(String.valueOf(uuidDetails));
                apiReflectionDetailEntity.setMainid(String.valueOf(uuid));
                apiReflectionDetailDao.save(apiReflectionDetailEntity);
            }
        }
        apiReflectionEntity = apireflectionDao.saveApiReflection(apiReflectionEntity);
        return BaseResult.getSuccessMessageEntity("新增成功", apiReflectionEntity.getId());
    }

    /**
     * 修改
     *
     * @param jsonObject
     * @return
     */
    @Override
    public JsonResultEntity updateApiReflection(JSONObject jsonObject) {
        //ApiReflectionEntity apiReflectionEntity = JSONObject.parseObject(jsonObject.getString("jsonStr")).toJavaObject(ApiReflectionEntity.class);
        ApiReflectionEntity apiReflectionEntity = getData("jsonStr", jsonObject, ApiReflectionEntity.class);
        if (null == apiReflectionEntity.getId() || "".equals(apiReflectionEntity.getId())) {
            return BaseResult.getFailureMessageEntity("修改时候请携带id");
        }
        //修改时，先将明细表数据删除，然后再新增
        ApiReflectionDetailEntity apiReflectionDetailEntity = new ApiReflectionDetailEntity();
        apiReflectionDetailEntity.setMainid(apiReflectionEntity.getId());
        apiReflectionDetailDao.update("ApiReflectionDetailEntity_update_details", apiReflectionDetailEntity);
        //解析明细数据
        String jsonStr = jsonObject.getString("jsonStr");
        JSONObject jsonObjectStr = JSONObject.parseObject(jsonStr);
        JSONArray jsonArray = jsonObjectStr.getJSONArray("details");
        //判断是否是插件，如果是插件，明细数据不保存，如果不是插件，明细数据保存 1是2否
        String plug = jsonObjectStr.getString("plug");
        //不是插件 2
        String isNotPlug = "2";
        //是插件 1
        String isPlug = "1";
        if (isNotPlug.equals(plug)) {
            for (Object object : jsonArray) {
                String jsonString = JSON.toJSONString(object);
                JSONObject jsonObjectDeatils = JSONObject.parseObject(jsonString);
                ApiReflectionDetailEntity apiReflectionDetailEntitySave = jsonObjectDeatils.toJavaObject(ApiReflectionDetailEntity.class);
                //设置接口映射配置明细id
                long uuidDetails = UUIDLong.longUUID();
                //保存接口映射配置明细数据
                apiReflectionDetailEntitySave.setId(String.valueOf(uuidDetails));
                apiReflectionDetailEntitySave.setMainid(String.valueOf(apiReflectionEntity.getId()));
                apiReflectionDetailDao.save(apiReflectionDetailEntitySave);
            }
        }
        apireflectionDao.updateApiReflection(apiReflectionEntity);
        return BaseResult.getSuccessMessageEntity("修改成功", apiReflectionEntity.getId());
    }

    /**
     * 删除  将sts修改为N
     *
     * @param
     * @return
     */
    @Override
    public JsonResultEntity deleteApiReflection(JSONObject jsonObject) {
        ApiReflectionEntity apiReflectionEntity = JSONObject.parseObject(jsonObject.getString("jsonStr")).toJavaObject(ApiReflectionEntity.class);
        if (null == apiReflectionEntity.getId() || "".equals(apiReflectionEntity.getId())) {
            return BaseResult.getFailureMessageEntity("删除时候id不能为null");
        }
        apireflectionDao.deleteApiReflection(apiReflectionEntity.getId());
        ApiReflectionDetailEntity apiReflectionDetailEntity = new ApiReflectionDetailEntity();
        apiReflectionDetailEntity.setMainid(apiReflectionEntity.getId());
        apiReflectionDetailDao.update("ApiReflectionDetailEntity_logicDelete_Multi_Condition", apiReflectionDetailEntity);
        return BaseResult.getSuccessMessageEntity("删除成功", apiReflectionEntity.getId());
    }

    @Override
    public ApiReflectionDTO queryApiRefById(String ref_id) {
        ApiReflectionDTO apiReflectionDTO = new ApiReflectionDTO();
        apiReflectionDTO.setRef_id(ref_id);
        List<ApiReflectionDTO> list = sqlSession.selectList("com.hzya.frame.sys.apireflection.dao.impl.ApiReflectionDaoImpl.ApiReflectionEntity_list_view", apiReflectionDTO);
        return CollectionUtils.isNotEmpty(list) ? list.get(0) : null;
    }

}
