package com.zcloud.controller.system;

import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.PageData;
import com.zcloud.service.system.PhotoService;
import com.zcloud.util.Const;
import com.zcloud.util.DateUtil;
import com.zcloud.util.Jurisdiction;
import com.zcloud.util.Smb;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * 说明：头像编辑处理类
 * 作者：luoxiaobao
 * 官网：www.qdkjchina.com
 */
@Controller
@RequestMapping("/photo")
public class PhotoController extends BaseController {

	@Autowired
	private PhotoService photoService;
	@Autowired
	private Smb smb;
	/**保存
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/save")
	@ResponseBody
	public Object save() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		pd.put("USERNAME", Jurisdiction.getUsername());	//用户名
		String type = pd.getString("type");				//类型，1：带原图的。2不带原图
		String strphotos = pd.getString("strphotos");	//图片路径拼接
		String[] arrayStr = strphotos.split(",fh,");
		if("1".equals(type)){
			String tu0 = arrayStr[0].split("angle=")[0];
			tu0 = tu0.substring(0, tu0.length()-1);
			pd.put("PHOTO0", tu0);			//原图
			pd.put("PHOTO1", arrayStr[1]);	//头像1
			pd.put("PHOTO2", arrayStr[2]);	//头像2
			pd.put("PHOTO3", arrayStr[3]);	//头像3
		}else{
			pd.put("PHOTO0", "");			//原图
			pd.put("PHOTO1", arrayStr[0]);	//头像1
			pd.put("PHOTO2", arrayStr[1]);	//头像2
			pd.put("PHOTO3", arrayStr[2]);	//头像3
		}
		PageData ypd = photoService.findById(pd);
		if(null == ypd){			//没有数据就新增，否则就修改
			pd.put("USERPHOTO_ID", this.get32UUID());		//主键
			photoService.save(pd);
		}else{
			photoService.edit(pd);
		}
		map.put("userPhoto",pd.getString("PHOTO2"));
		map.put("result", errInfo);
		return map;
	}

	/**保存
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/saveNew")
	@ResponseBody
	public Object saveNew(@RequestParam(value="FFILE",required=false) MultipartFile file) throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		pd.put("USERNAME", Jurisdiction.getUsername());	//用户名

		if (null != file && !file.isEmpty()) {
			String  ffile = DateUtil.getDays();
    		String fileName = this.get32UUID()+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
		 	smb.sshSftp(file, fileName,Const.FILEPATHPHOTO + Jurisdiction.getCORPINFO_ID() + "/" + ffile);
			pd.put("PHOTO1", Const.FILEPATHPHOTO + Jurisdiction.getCORPINFO_ID() + "/" + ffile + "/" + fileName);
			pd.put("PHOTO2", Const.FILEPATHPHOTO + Jurisdiction.getCORPINFO_ID() + "/" + ffile + "/" + fileName);
			pd.put("PHOTO3", Const.FILEPATHPHOTO + Jurisdiction.getCORPINFO_ID() + "/" + ffile + "/" + fileName);
		}
		PageData ypd = photoService.findById(pd);
		if(null == ypd){			//没有数据就新增，否则就修改
			pd.put("USERPHOTO_ID", this.get32UUID());		//主键
			photoService.save(pd);
		}else{
			photoService.edit(pd);
		}
		map.put("userPhoto",pd.getString("PHOTO2"));
		map.put("result", errInfo);
		return map;
	}

	/**保存用户头像(手机端)
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/saveUserPhoto")
	@ResponseBody
	public Object saveUserPhoto() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		String USERNAME = Jurisdiction.getUsername();//用户名
		String userphoto = "uploadFiles/photo/"+USERNAME+"_photo.jpg";
		pd.put("USERNAME", USERNAME);
		pd.put("PHOTO0", "");			//原图
		pd.put("PHOTO1", userphoto);	//头像1
		pd.put("PHOTO2", userphoto);	//头像2
		pd.put("PHOTO3", userphoto);	//头像3
		map.put("userPhoto",pd.getString("PHOTO2"));
		PageData ypd = photoService.findById(pd);
		if(null == ypd){				//没有数据就新增，否则就修改
			pd.put("USERPHOTO_ID", this.get32UUID());		//主键
			photoService.save(pd);
		}else{
			photoService.edit(pd);
		}
		map.put("result", errInfo);				//返回结果
		return map;
	}

}
