package org.cs.mgr.setting.ctl;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.cs.constants.Constants;
import org.cs.core.ctl.BaseCtl;
import org.cs.mgr.setting.model.AppVersion;
import org.cs.mgr.setting.service.IAppVersionService;
import org.cs.util.FastDFSClient;
import org.cs.util.FileUtil;
import org.cs.util.Pager;
import org.cs.util.PropUtil;
import org.cs.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;

/**
 * @Description APP版本维护
 * @author Sss
 * @version 1.0.0
 */
@Controller
@RequestMapping("/version")
public class VersionCtrl extends BaseCtl {

	private static Logger log = Logger.getLogger(VersionCtrl.class);
	
	@Autowired
	private IAppVersionService appVersionService;
	
	@RequestMapping("list")
	public String list(ModelMap mm){
		return "mgr/setting/version/list";	
	}
	
	@RequestMapping(value="list2", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject list2(ModelMap mm){
		JSONObject json = new JSONObject();
		Pager pager = new Pager(1);
		pager = appVersionService.getVersionList(pager);
		json.put("rows", pager.getResults());
		json.put("total", pager.getTotal());
		return json;
	}
	
	
	@RequestMapping("transfer")
	public String transfer(String id, String action, ModelMap mm){
		AppVersion version = null;
		if(StringUtil.isNotBlank(id)){
			try {
				version = this.appVersionService.findById(id);
				mm.put("v", version);
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
		}
		return "mgr/setting/version/" + action;
	}
	
	@RequestMapping(value="edit", method = RequestMethod.POST)
	@ResponseBody
	public String edit(AppVersion version,@RequestParam(required = false) MultipartFile app, ModelMap mm){
		try {
			if(version != null ){
				AppVersion version2 = this.appVersionService.findById(version.getId());
				version2.setVersion(version.getVersion());
				version2.setCompel(version.getCompel());
				version2.setModifyDate(new Date());
				version2.setRemark(version.getRemark());
				if(app != null && !app.isEmpty()){
					/*String path = PropUtil.getString("file.path")+Constants.APK_PATH;
					FileUtil.makeDir(path);
					//String fileName = Constants.APP_NAME+"_v_"+version.getVersion()+".apk";
					String fileName = Constants.APP_NAME+".apk";
					File file =  new File(path, fileName);
					FileUtil.inputStreamToFile(app.getInputStream(), file);
					//String fileName1 = FastDFSClient.uploadFileStreanm((FileInputStream)app.getInputStream(), fileName);
					version2.setUrl(PropUtil.getString("app.file.path")+fileName);*/

					String fileName = Constants.APP_NAME+"_v_"+version.getVersion()+".apk";
					/*FileUtil.inputStreamToFile(app.getInputStream(), file);*/
					String fileName1 = FastDFSClient.uploadFileStreanm((FileInputStream)app.getInputStream(), fileName);
					version2.setUrl(PropUtil.getString("file_http")+fileName1);

				}
				this.appVersionService.update(version2);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return AJAX_SUCCESS;
	}
	
	@RequestMapping(value="getAndroidUrl", method = RequestMethod.POST)
	@ResponseBody
	public String getAndroidUrl(){
		Map<String, Object> map = this.appVersionService.getVersionBy("android");
		map.put("URL", map.get("URL").toString());
		return JSONObject.toJSONString(map, features);
	}

	@RequestMapping(value="getAndroid")
	public String getAndroid(ModelMap mm){
		Map<String, Object> map = this.appVersionService.getVersionBy("android");
		mm.put("url", map.get("URL").toString());
		return "download";
	}
}
