package com.blueland.sys.power.web;


import java.util.Arrays;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blueland.annotation.Log;
import com.blueland.core.util.UserUtils;
import com.blueland.model.TPfParameter;
import com.blueland.result.R;
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.RequestBody;
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.DictEntity;
import com.blueland.bo.ParameterEntity;

import io.swagger.annotations.Api;

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


@Api(value = "parameter")
@RestController
@RequestMapping(value = "/parameter")
public class ParameterRest {
	@Autowired
	private IParameterService parameterService;
	
	/**
	 * 查询对象
	 * @param parameterEntity
	 * @return
	 */
	@RequestMapping(value = "/getObj",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<TPfParameter> getObj(ParameterEntity parameterEntity) {
		TPfParameter obj = parameterService.get(parameterEntity);
		return R.ok(obj);
	}
	
	
	/** 
	 * 搜索对象 
	 **/
	@RequestMapping(value = "",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<Page<ParameterEntity>> list(ParameterEntity parameterEntity) {
		Page<ParameterEntity> page = this.parameterService.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 = parameterService.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  = parameterService.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  = parameterService.get(model);
		return R.ok(parameterEntity);
	}
	
	
	/** 
	 * 新增对象
	 **/
	@Log(busiType = "新增高级查询对象", opType = SAVE, type = "web")
	@RequestMapping(value = "save",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<ParameterEntity> save(@RequestBody ParameterEntity model,HttpServletRequest request)  {
		TPfParameter pfParameter = BeanUtil.copyProperties(model,TPfParameter.class);
		pfParameter.setAppId(UserUtils.getTenantId());
		if(model.getPid()==null) {
			ParameterEntity obj = new ParameterEntity();
			obj.setAppId(UserUtils.getTenantId());
			obj.setPkey(model.getPkey());
			ParameterEntity parameterEntity  = parameterService.getObj(obj);
			if(parameterEntity!=null){
				throw new RuntimeException("参数编码已存在，请重新输入");
			}
			parameterService.save(pfParameter);
		}else {
			parameterService.updateById(pfParameter);
		}
		return R.ok(model);
	}
	
	
	/**
	 * 更新对象
	 * @return 
	 **/
	@Log(busiType = "更新高级查询对象", opType = UPDATE, type = "web")
	@RequestMapping(value = "update",method = RequestMethod.PUT,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<ParameterEntity> update(@RequestBody ParameterEntity model,HttpServletRequest request)  {
		TPfParameter pfParameter = BeanUtil.copyProperties(model,TPfParameter.class);
		parameterService.updateById(pfParameter);
		return R.ok(model);
	}
	
	/**
	 * 删除对象
	 * @return 
	 **/
	@Log(busiType = "删除高级查询对象", opType = DELETE, type = "web")
	@RequestMapping(value = "delete/{pid}",method = RequestMethod.DELETE,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<Boolean> delete(@PathVariable String pid,HttpServletRequest request) {
		parameterService.removeById(pid);
		return R.ok();
	}
	
	/**
	 *  批量删除对象
	 * @return 
	 **/
	@Log(busiType = "批量删除高级查询对象", opType = DELETE, type = "web")
	@RequestMapping(value = "deleteBatch",method = RequestMethod.DELETE,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<Boolean> deleteBatch(String[] ids,HttpServletRequest request) {
		if (ObjectUtil.isEmpty(ids)) {
			throw new RuntimeException("id不能为空");
		}
		parameterService.removeByIds(Arrays.asList(ids));
		return R.ok();
	}
	
}



