package cn.api.atv.app;

import cn.common.annotation.Log;
import cn.common.enums.BizOpType;
import cn.common.enums.ResultCode;
import cn.common.page.response.Result;
import cn.facade.abs.IService;
import cn.service.atv.IAtvService;
import cn.util.core.IsKit;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**地铁广播系统专用请求*/
@RestController
@RequestMapping("/app")
public class ImageCtrl extends IService
{
	@Resource
	private IAtvService atvService;
	//app上传文件保存目录
	private final String appDirName = "app";
	/**
	 * 预览图片&下载文件
	 * 请求地址：url
	 */
	//url：http://localhost:8080/common/static/{user/20190119/e1fe9925bc315c60addea1b98eb1cb1349547719_1547866868179.jpg}
	@GetMapping(value = "/sys/common/static/**")
	@Log(title = "预览图片&下载文件-fileView", businessType = BizOpType.UPDATEFILE)
	public void download(HttpServletRequest request, HttpServletResponse response)
	{
		System.out.println("download-0:" );
		downloadFileService.download(appDirName,request,response);
	}


	@GetMapping(value = "/sys/common/removeFile")
	@Log(title = "APP-删除上传的文件-mLogin", businessType = BizOpType.OTHER)
	public Result<?> removeFile(@RequestParam(name="fileName") String fileName) {
		int num = fileService.deleteFile(appDirName,fileName);
		if(num > 0) {
			return Result.ok();
		}
		else {
			return Result.warnMsg(ResultCode.FILE_DELETE.getMsg());
		}
	}

	/**文件上传统一方法*/
	@PostMapping(value = "/sys/common/upload")
	@Log(title = "APP-文件上传统一方法-fileUpload", businessType = BizOpType.UPDATEFILE)
	public Result<?> fileUpload(HttpServletRequest request)
	{
		System.out.println("w-1:");
		String savePath = uploadFileService.uploadFile(request,appDirName);
		System.out.println("w-2:" +savePath);
		if(IsKit.isNotEmpty(savePath)){
			return Result.okMsg(savePath);
		}else {
			return Result.warnMsg("上传失败！");
		}
	}

	//目前没有用
	@GetMapping("/sys/common/getStreamFile/{fileName}")
	@Log(title = "文件操作-返回在线浏览所需流数据-getStreamFile", businessType = BizOpType.EXPORT)
	public void getStreamFile(HttpServletRequest request, HttpServletResponse response, @PathVariable String fileName)
	{
		downloadFileService.download(appDirName,request,response);
		//获取目录
		//String filePath = fileService.getFileFullPath(appDirName);
		//System.out.println("getFileTest-0:" +(filePath + fileName));
		//downloadFileService.download_stream(filePath,fileName,response);
	}

}
