
package org.tio.sitexxx.server.controller.wx;

import cn.hutool.core.io.FileUtil;
import org.tio.http.common.HttpRequest;
import org.tio.http.common.UploadFile;
import org.tio.http.server.annotation.RequestPath;
import org.tio.jfinal.kit.Ret;
import org.tio.sitexxx.service.model.main.File;
import org.tio.sitexxx.service.service.chat.SysService;
import org.tio.sitexxx.service.utils.RetUtils;
import org.tio.sitexxx.service.vo.Const;
import org.tio.sitexxx.service.vo.RequestExt;
import org.tio.sitexxx.server.utils.WebUtils;
import org.tio.utils.hutool.StrUtil;
import org.tio.utils.resp.Resp;

/**
 * 系统相关
 * @author xufei
 * 2020年1月6日 下午7:41:13
 */
@RequestPath(value = "/sys")
public class SysController {
	/**
	 * 版本更新
	 * @param request
	 * @param version
	 * @return
	 * @throws Exception
	 * @author xufei
	 * 2020年9月18日 上午11:11:58
	 */
	@RequestPath(value = "/version")
	public Resp version(HttpRequest request, String version) throws Exception {
		RequestExt requestExt = WebUtils.getRequestExt(request);
		short deviceType = requestExt.getDeviceType();
		if (requestExt.isFromAppAndroid()) {
			deviceType = 1;
		} else if (requestExt.isFromAppIos()) {
			deviceType = 2;
		}
		if (StrUtil.isBlank(version)) {
			version = requestExt.getAppVersion();
		}
		Ret ret = SysService.me.checkVersion(deviceType, version);
		if (ret.isFail()) {
			return Resp.fail().msg(RetUtils.getRetMsg(ret));
		}
		return Resp.ok(RetUtils.getOkData(ret));
	}
	
	/**
	 * 举报投诉
	 * @param request
	 * @param groupid
	 * @param touid
	 * @param mid
	 * @param reason
	 * @return
	 * @throws Exception
	 * @author xufei
	 * 2021年1月27日 下午3:01:25
	 */
	@RequestPath(value = "/report")
	public Resp report(HttpRequest request, Long groupid, Integer touid, Long mid, String reason) {
		if (groupid == null && touid == null && mid == null) {
			return RetUtils.getInvalidResp();
		}
		Integer userId = WebUtils.currUserId(request);
		Ret ret = SysService.me.report(userId, touid, groupid, mid, reason);
		if (ret.isFail()) {
			return Resp.fail().msg(RetUtils.getRetMsg(ret));
		}
		return Resp.ok(RetUtils.OPER_RIGHT);
	}

	/**
	 * @param request
	 * @param reason
	 * @return
	 * @throws Exception
	 * @author xufei
	 * 2021年4月8日 上午10:31:40
	 */
	@RequestPath(value = "/advise")
	public Resp advise(HttpRequest request, String reason) {
		if (StrUtil.isBlank(reason)) {
			return RetUtils.getInvalidResp();
		}
		Integer userId = WebUtils.currUserId(request);
		Ret ret = SysService.me.report(userId, null, null, null, reason);
		if (ret.isFail()) {
			return Resp.fail().msg(RetUtils.getRetMsg(ret));
		}
		return Resp.ok(RetUtils.OPER_RIGHT);
	}

	/**
	 * 错误日志
	 * @param request
	 * @param uploadFile
	 * @return
	 * @author xufei
	 * 2020年11月2日 上午11:33:42
	 */
	@RequestPath(value = "/errlog")
	public Resp errlog(HttpRequest request, UploadFile uploadFile) {
//		if (uploadFile == null) {
//			return Resp.fail("上传信息为空");
//		}
//		User curr = WebUtils.currUser(request);
//		Integer uid;
//		if (curr != null) {
//			uid = curr.getId();
//		} else {
//			uid = -777;
//		}
//		String sessionid = request.getHttpSession().getId();
//		innerUploadFile(uid, uploadFile, sessionid);
		return Resp.ok();
	}

	@RequestPath(value = "/uploadFile")
	public Resp uploadFile(UploadFile uploadFile) {
//		if (uploadFile == null) {
//			return Resp.fail("上传信息为空");
//		}
//		byte[] bs = uploadFile.getData();
//		String filename = uploadFile.getName();
//		String ext = FileUtil.extName(filename);
//		String resRootDir = Const.RES_ROOT;
//		String path = "/" + Const.UPLOAD_DIR.APP_LOG_ERR + "/" + DateUtil.format(new Date(), "yyyyMMddHH") + "/" + 222;
//		java.io.File dir = new java.io.File(resRootDir, path);
//		FileUtil.mkdir(dir);
//		String url = path + "/" + UUID.randomUUID() +"."+ext;
//		java.io.File file = new java.io.File(resRootDir, url);
//		FileUtil.writeBytes(bs, file);
//		File dbFile = new File();
//		dbFile.setExt(ext);
//		dbFile.setFilename(file.getName());
//		dbFile.setSize((long) bs.length);
//		dbFile.setUrl(url);
//		dbFile.save();
//		return Resp.ok(dbFile);
		return Resp.ok(new File());
	}

	@RequestPath(value = "/uploadSign")
	public Resp uploadSign(HttpRequest request, UploadFile uploadFile) {
		if (uploadFile == null) {
			return Resp.fail("上传信息为空");
		}
		Integer userId = WebUtils.currUserId(request);
		byte[] bs = uploadFile.getData();
		String filename = uploadFile.getName();
		String ext = FileUtil.extName(filename);
		String resRootDir = Const.RES_ROOT;
		String path = "/sign";
		String url = path + "/" + userId + "."+"jpeg";
		java.io.File file = new java.io.File(resRootDir, url);
		FileUtil.writeBytes(bs, file);
		File dbFile = new File();
		dbFile.setExt(ext);
		dbFile.setFilename(file.getName());
		dbFile.setSize((long) bs.length);
		dbFile.setUrl(url);
		dbFile.save();
		return Resp.ok(dbFile);
	}

	/**
	 * @param bizid
	 * @param chatmode
	 * @return
	 * @throws Exception
	 * @author xufei
	 * 2021年3月2日 上午10:16:20
	 */
	@RequestPath(value = "/screenshot")
	public Resp screenshot(Long bizid, Short chatmode) {
		if (bizid == null && chatmode == null) {
			return RetUtils.getInvalidResp();
		}
		return Resp.ok(RetUtils.OPER_RIGHT);
	}

}
