package com.hzya.frame.sysnew.application.api.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzya.frame.sysnew.application.api.entity.SysApplicationApiEntity;
import com.hzya.frame.sysnew.application.api.dao.ISysApplicationApiDao;
import com.hzya.frame.sysnew.application.api.service.ISysApplicationApiService;
import com.hzya.frame.sysnew.application.plugin.entity.SysApplicationPluginEntity;
import com.hzya.frame.sysnew.messageTemplate.entity.SysMessageTemplateEntity;
import com.hzya.frame.web.entity.BaseResult;
import com.hzya.frame.web.entity.JsonResultEntity;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.Resource;
import com.hzya.frame.basedao.service.impl.BaseService;

import java.util.ArrayList;
import java.util.List;

/**
 * 应用api(SysApplicationApi)表服务实现类
 *
 * @author makejava
 * @since 2023-09-22 16:23:35
 */
@Service(value = "sysApplicationApiService")
public class SysApplicationApiServiceImpl extends BaseService<SysApplicationApiEntity, String> implements ISysApplicationApiService {
     
    private ISysApplicationApiDao sysApplicationApiDao;
    
    @Autowired
	public void setSysApplicationApiDao(ISysApplicationApiDao dao) {
		this.sysApplicationApiDao = dao;
		this.dao = dao;
	}


	/**
	 * @param jsonObject
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 * @Author lvleigang
	 * @Description 查询应用api列表
	 * @Date 11:52 上午 2024/3/27
	 **/
	@Override
	public JsonResultEntity queryEntity(JSONObject jsonObject) {
		SysApplicationApiEntity entity = getData("jsonStr", jsonObject, SysApplicationApiEntity.class);
		if (entity == null) {
			entity = new SysApplicationApiEntity();
		}
		List<SysApplicationApiEntity> list = sysApplicationApiDao.queryByLike(entity);
		return BaseResult.getSuccessMessageEntity("查询数据成功", list);
	}

	@Override
	public JsonResultEntity queryMultiAppPage(JSONObject jsonObject){
		SysApplicationApiEntity entity = getData("jsonStr", jsonObject, SysApplicationApiEntity.class);
		//判断分页
		if (entity == null || entity.getPageNum() == null || entity.getPageSize() == null) {
			return BaseResult.getFailureMessageEntity("分页查询参数不存在");
		}
		int pageNum = entity.getPageNum();
		int pageSize = entity.getPageSize();
		//查询时不分页
		entity.setPageNum(null);
		entity.setPageSize(null);
		String appIds = entity.getAppId();
		if(appIds == null || "".equals(appIds.trim())){
			return BaseResult.getFailureMessageEntity("应用ID不能为空");
		}
		String[] appIdList = appIds.split(",");
		List<SysApplicationApiEntity> resultList = new ArrayList<>();
		for (int i = 0; i < appIdList.length; i++) {
			entity.setAppId(appIdList[i]);
			List<SysApplicationApiEntity> list = sysApplicationApiDao.queryBase(entity);
			resultList.addAll(list);
		}
        //手动分页
		int total = resultList.size();
		int fromIndex = (pageNum - 1) * pageSize;
		int toIndex = Math.min(fromIndex + pageSize, total);
		List<SysApplicationApiEntity> pageList = resultList.subList(fromIndex, toIndex);

		PageInfo<SysApplicationApiEntity> pageInfo = new PageInfo<>(pageList);
		pageInfo.setTotal(total);
		pageInfo.setPages((total + pageSize - 1) / pageSize);

		return BaseResult.getSuccessMessageEntity("查询数据成功", pageInfo);
	}

	/**
	 * 模糊查询，联查sys_app
	 *
	 * @param entity
	 * @return
	 */
	@Override
	public List<SysApplicationApiEntity> queryLike(SysApplicationApiEntity entity) {
		List<SysApplicationApiEntity> list = sysApplicationApiDao.queryList(entity, "com.hzya.frame.sysnew.application.api.dao.impl.SysApplicationApiDaoImpl.entity_list_like_join_sysApp");
		return list;
	}
}
