package com.blueland.render.rest;


import java.util.HashMap;
import java.util.Map;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blueland.core.util.UserUtils;
import com.blueland.model.TPfApplication;
import com.blueland.model.TPfParameter;
import com.blueland.result.R;
import com.blueland.service.IApplicationService;
import com.blueland.service.IParameterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.blueland.bo.ApplicationEntity;
import com.blueland.bo.DictEntity;
import com.blueland.bo.ParameterEntity;

import io.swagger.annotations.Api;

import static com.blueland.constant.ResponseConstant.*;


@Api(value = "parameter")
@RestController
@RequestMapping(value = "/parameter")
public class ParameterRest {
	@Autowired
	private IParameterService iParameterService;
	@Autowired
	private IApplicationService appService;
	
	
	/**
	 * 	根据应用ID获取参数列表（不提供单独查参数的功能）*/
	@ResponseBody
	@RequestMapping(value="/page", method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	public Map<String, Object> parameterListPage(@RequestParam Map<String,Object> queryParams){
		String appId = UserUtils.getTenantId();
		int curPageSize = 10;
		int curPage = 1;
		Map < String , Object > result = new HashMap< String , Object>(); 
		Page<ParameterEntity> page = null;
		if(queryParams.containsKey("curPageSize")){
			curPageSize =Integer.parseInt(queryParams.get("curPageSize").toString());
		}
		if(queryParams.containsKey("curPage")){
			curPage =Integer.parseInt(queryParams.get("curPage").toString());
		}
		queryParams.put("appId", appId);
		TPfApplication appEntity = appService.getById(appId);
			if(appEntity==null){
				result.put("resultCode", RESULT_CODE_ERROR_CODE);
				result.put("resultMsg", "应用ID错误");
			}else{
				page = iParameterService.parameterListPage(queryParams,curPageSize,curPage);
				result.put("resultCode", RESULT_CODE_SUCCESS_CODE);
				result.put("pageNum", page.getCurrent());
				result.put("pageSize", page.getSize());
				result.put("total", page.getTotal());
				result.put("pages", page.getPages());
				result.put("resultMsg", "处理成功");
				result.put("result", page.getRecords());
			}

		return result;
	}
	
	/** 
	 * 搜索对象 
	 **/
	@RequestMapping(value = "",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<Page<ParameterEntity>> list(ParameterEntity parameterEntity) {
		Page<ParameterEntity> page = this.iParameterService.findPage(parameterEntity);
		return R.ok(page);
	}

	@ResponseBody
	@RequestMapping(value="/list", method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	public Page<ParameterEntity> listPage(@RequestParam Map<String,Object> queryParams){
		int curPageSize = 10;
		int curPage = 1;
		if(queryParams.containsKey("curPageSize")){
			curPageSize =Integer.parseInt(queryParams.get("curPageSize").toString());
		}
		if(queryParams.containsKey("curPage")){
			curPage =Integer.parseInt(queryParams.get("curPage").toString());
		}
		Page<ParameterEntity> page = iParameterService.findPageList(queryParams,curPage,curPageSize);
		return page;
	}
	
	/** 
	 * 查看对象
	 **/
	@RequestMapping(value = "/{pid}",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<TPfParameter> view(@PathVariable String pid)  {
		TPfParameter parameterEntity  = iParameterService.getById(pid);
		return R.ok(parameterEntity);
	}


	/** 
	 * 查看对象（支持多条件查询）
	 **/
	@RequestMapping(value = "get",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<TPfParameter> get(ParameterEntity model)  {
		TPfParameter parameterEntity  = iParameterService.get(model);
		return R.ok(parameterEntity);
	}
}



