package com.platform.modules.plm.controller;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.platform.common.annotation.SysLog;
import com.platform.common.utils.R;
import com.platform.modules.plm.entity.PlmVideoNewsEntity;
import com.platform.modules.plm.service.PlmVideoNewsService;
import com.platform.modules.sys.controller.AbstractController;


/**
 * 视频发布信息表
 * 
 * @author weiyan
 * @email 
 * @date 2018-08-06 19:48:51
 */
@RestController
@RequestMapping("/plm/videonews")
public class PlmVideoNewsController  extends AbstractController {
	@Autowired
	private PlmVideoNewsService plmVideoNewsService;
	
	/**
	 * 列表
	 */ 
	@RequestMapping("/list")
	@RequiresPermissions("plm:videonews:list")
	public R list(@RequestParam Map<String, Object> params){
		Long userId = getUserId();
		if(userId!=null){
			params.put("userId", getUserId());
		}
		if(params.get("status")==null || (params.get("status")!=null && StringUtils.isBlank(params.get("status").toString()))){
			params.put("status", new Integer[]{-1,0,1,2});
		}else{
			Integer status = Integer.valueOf(params.get("status").toString());
			params.put("status", new Integer[]{status});
		}
		params.put("isDelete", 0);
		return R.ok().put("page",plmVideoNewsService.queryPage(params));
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info")
	@RequiresPermissions("plm:videonews:info")
	public R info(@RequestParam("id") Long id){
		if(id==null){
			return R.error("id不能为空");
		}
		return R.ok().put("info", plmVideoNewsService.queryById(id));
	}
	
	/**
	 * 本地上传
	 */
	@PostMapping("/upload")
	@SysLog("视频信息上传")
	@RequiresPermissions("plm:videonews:upload")
	public R upload(@RequestParam("title") String title,@RequestParam("description") String description,HttpServletRequest request){
		//转换成多部分request
		MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;

		Map<String, MultipartFile> files = multiRequest.getFileMap();
		MultipartFile     materialFile = null;
		MultipartFile coverFile = null;
		if(files!=null && files.size()>0){
			for(Map.Entry<String, MultipartFile> entry : files.entrySet()) {
		        if(entry.getKey()=="materialFile" || entry.getKey().equals("materialFile")){
		        	materialFile = entry.getValue();
		        }else if(entry.getKey()=="coverFile" || entry.getKey().equals("coverFile")){
		        	coverFile = entry.getValue();
		        }
			}
		}
		if(materialFile==null){
			return R.error("资源文件不能为空!");
		}
		if(StringUtils.isBlank(title)){
			return R.error("标题不能为空");
		}
		plmVideoNewsService.upload(materialFile, coverFile, title, description,getUserId());
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@PostMapping("/update")
	@SysLog("视频信息修改")
	@RequiresPermissions("plm:videonews:update")
	public R update(@RequestParam("id") Long id,@RequestParam("title") String title,@RequestParam("description") String description,HttpServletRequest request){
		//转换成多部分request
		MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;

		Map<String, MultipartFile> files = multiRequest.getFileMap();
		MultipartFile     materialFile = null;
		MultipartFile coverFile = null;
		if(files!=null && files.size()>0){
			for(Map.Entry<String, MultipartFile> entry : files.entrySet()) {
		        if(entry.getKey()=="materialFile" || entry.getKey().equals("materialFile")){
		        	materialFile = entry.getValue();
		        }else if(entry.getKey()=="coverFile" || entry.getKey().equals("coverFile")){
		        	coverFile = entry.getValue();
		        }
			}
		}
		if(StringUtils.isBlank(title)){
			return R.error("标题不能为空");
		}
		if(id==null){
			return R.error("id不能为空");
		}
		plmVideoNewsService.update(id,materialFile, coverFile, title, description,getUserId());
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@SysLog("视频信息删除")
	@RequiresPermissions("plm:videonews:delete")
	public R delete(@RequestBody Long[] ids){
		plmVideoNewsService.deleteBatch(ids,getUserId());
		return R.ok();
	}
	
	/**
	 * 送审
	 */
	@PostMapping("/audit")
	@SysLog("视频信息送审")
	@RequiresPermissions("plm:videonews:audit")
	public R audit(@RequestBody Long[] ids){
		if(ids==null || ids.length==0){
			return R.error("id不能为空");
		}
		for(Long id : ids){
			if(id==null){
				continue;
			}
			PlmVideoNewsEntity plmVideoNews = new PlmVideoNewsEntity();
			plmVideoNews.setId(id);
			plmVideoNews.setAuditDate(new Date());
			plmVideoNews.setAuditUser(getUserId());
			plmVideoNews.setStatus(3);
			plmVideoNewsService.updateById(plmVideoNews);
		}
		return R.ok();
	}
	
	/**
	 * 审核列表
	 */
	@RequestMapping("/audit/list")
	@RequiresPermissions("plm:videonews:audit:list")
	public R auditList(@RequestParam Map<String, Object> params){
//		params.put("userId", getUserId());
		params.put("status", new Integer[]{3});
		params.put("isDelete", 0);
//		Map<String, Object> map =  plmVideoNewsService.queryPageMap(params);
		return R.ok().put("page",plmVideoNewsService.queryPage(params));
	}
	
	/**
	 * 审核通过
	 */
	@SysLog("视频信息审核通过")
	@PostMapping("/audit/pass")
	@RequiresPermissions("plm:videonews:audit:pass")
	public R auditPass(@RequestParam("id") Long id,@RequestParam("auditOpinion") String auditOpinion){
		if(id==null){
			return R.error("id不能为空");
		}
		PlmVideoNewsEntity plmVideoNews = new PlmVideoNewsEntity();
		plmVideoNews.setId(id);
		plmVideoNews.setAuditDate(new Date());
		plmVideoNews.setAuditUser(getUserId());
		plmVideoNews.setStatus(4);
		plmVideoNews.setAuditOpinion(auditOpinion);
		plmVideoNewsService.updateById(plmVideoNews);
		return R.ok();
	}
	
	/**
	 * 审核通过
	 */
	@SysLog("视频信息审核驳回")
	@PostMapping("/audit/reject")
	@RequiresPermissions("plm:videonews:audit:reject")
	public R auditReject(@RequestParam("id") Long id,@RequestParam("auditOpinion") String auditOpinion){
		if(id==null){
			return R.error("id不能为空");
		}
		PlmVideoNewsEntity plmVideoNews = new PlmVideoNewsEntity();
		plmVideoNews.setId(id);
		plmVideoNews.setAuditDate(new Date());
		plmVideoNews.setAuditUser(getUserId());
		plmVideoNews.setStatus(2);
		plmVideoNews.setAuditOpinion(auditOpinion);
		plmVideoNewsService.updateById(plmVideoNews);
		return R.ok();
	}
	/**
	 * 成品列表
	 */
	@RequestMapping("/finished/list")
	@RequiresPermissions("plm:videonews:finished:list")
	public R finishedList(@RequestParam Map<String, Object> params){
//		params.put("userId", getUserId());
		params.put("status", new Integer[]{4});
		params.put("isDelete", 0);
//		Map<String, Object> map =  plmVideoNewsService.queryPageMap(params);
		return R.ok().put("page",plmVideoNewsService.queryPage(params));
	}
	/**
	 * 回收站列表
	 */
	@RequestMapping("/cycle/list")
	@RequiresPermissions("plm:videonews:cycle:list")
	public R cycleList(@RequestParam Map<String, Object> params){
		params.put("isDelete", 1);
		return R.ok().put("page",plmVideoNewsService.queryPage(params));
	}
	/**
	 * 还原
	 */
	@RequestMapping("/recycle")
	@SysLog("视频信息回收站还原")
	@RequiresPermissions("plm:videonews:recycle")
	public R recycle(@RequestBody Long[] ids){
		plmVideoNewsService.recycleBatch(ids,getUserId());
		return R.ok();
	}
	
	/**
	 * 回收站删除
	 * @param params
	 * @return
	 */
	@RequestMapping("/cycle/delete")
	@SysLog("视频信息回收站删除")
	@RequiresPermissions("plm:videonews:cycle:delete")
	public R recycleDelete(@RequestBody Long[] ids){
		plmVideoNewsService.recycleDelete(ids,getUserId());
		return R.ok();
	}
}
