package org.tio.sitexxx.server.controller;

import org.tio.sitexxx.server.utils.WebUtils;
import org.tio.http.common.HttpRequest;
import org.tio.http.server.annotation.RequestPath;
import org.tio.sitexxx.service.model.main.UserBackgroup;
import org.tio.sitexxx.service.service.ConfService;
import org.tio.utils.resp.Resp;

@RequestPath(value = "/backGroup")
public class UserBackGroupController {
	/**
	 * 修改聊天背景
	 * @param request
	 * @return
	 */
	@RequestPath(value = "/updateUrl")
	public Resp updateUrl(HttpRequest request,String url) {
		Integer userId = WebUtils.currUserId(request);
		UserBackgroup group = UserBackgroup.dao.findById(userId);
		UserBackgroup backgroup = new UserBackgroup();
		backgroup.setUrl(url);
		backgroup.setId(userId);
		if(group == null) {
			backgroup.save();	
		}else {
			backgroup.update();			
		}
		return Resp.ok();
	}

	/**
	 * 修改聊天背景
	 * @param request
	 * @return
	 */
	@RequestPath(value = "/getUrl")
	public Resp getUrl(HttpRequest request) {
		Integer userId = WebUtils.currUserId(request);
		UserBackgroup group = UserBackgroup.dao.findById(userId);
		if(group == null) {
			String string = ConfService.getString("defaultBack", "/backgroup/backGroup.png");
			UserBackgroup userBackgroup = new  UserBackgroup();
			userBackgroup.setUrl(string);
			return Resp.ok(userBackgroup);
		}
		return Resp.ok(group);
	}
}
