package com.platform.modules.plm.controller;

import java.util.Map;

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

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.PageUtils;
import com.platform.common.utils.R;
import com.platform.modules.plm.service.PlmMaterialService;
import com.platform.modules.sys.controller.AbstractController;


/**
 * 资源信息表
 * 
 * @author weiyan
 * @email 
 * @date 2018-08-06 19:34:17
 */
@RestController
@RequestMapping("/plm/material")
public class PlmMaterialController extends AbstractController{
	
	@Autowired
	private PlmMaterialService plmMaterialService;
	
	/**
	 * 上传列表列表
	 */
	@RequestMapping("/upload/list")
	@RequiresPermissions("plm:material:upload:list")
	public R uploadList(@RequestParam Map<String, Object> params){
		params.put("isDelete", 0);
		params.put("materialSources", new Integer[]{1});
		PageUtils page =  plmMaterialService.queryPageByParams(params);
		return R.ok().put("page", page);
	}
	/**
	 * 下载列表
	 * @param params
	 * @return
	 */
	@RequestMapping("/download/list")
	@RequiresPermissions("plm:material:download:list")
	public R downloadList(@RequestParam Map<String, Object> params){
		params.put("isDelete", 0);
		if(params.containsValue("materialSources") || StringUtils.isBlank(params.get("materialSources").toString())){
			params.put("materialSources", new Integer[]{2,3,4});
		}else{
			Integer materialSources = Integer.valueOf(params.get("materialSources").toString());
			params.put("materialSources", new Integer[]{materialSources});
		}
		PageUtils page =  plmMaterialService.queryPageByParams(params);
		return R.ok().put("page", page);
	}
	
	/**
	 * 回收站列表
	 * @param params
	 * @return
	 */
	@RequestMapping("/cycle/list")
	@RequiresPermissions("plm:material:cycle:list")
	public R recycleList(@RequestParam Map<String, Object> params){
		params.put("isDelete", 1);
		PageUtils page =  plmMaterialService.queryRecyclePageMap(params);
		return R.ok().put("page", page);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info")
	@RequiresPermissions("plm:material:info")
	public R info(@RequestParam("id") Long id){
		if(id==null){
			return R.error("id不能为空!");
		}
		return R.ok().put("info", plmMaterialService.findById(id));
	}
	
	/**
	 * 保存
	 */
	@SysLog("本地资源上传")
	@PostMapping("/upload")
	@RequiresPermissions("plm:material:upload")
	public R save(@RequestParam("materialTitle") String materialTitle,@RequestParam("materialDescription") String materialDescription,
			@RequestParam("materialType") Integer materialType,HttpServletResponse response,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("资源文件不能为空!");
		}
		plmMaterialService.save(materialFile, coverFile,materialTitle,materialDescription,materialType,getUserId());
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@SysLog("本地资源修改")
	@PostMapping("/update")
	@RequiresPermissions("plm:material:update")
	public R save(@RequestParam("id") Long id,@RequestParam("materialTitle") String materialTitle,@RequestParam("materialDescription") String materialDescription,
			@RequestParam("materialType") Integer materialType,HttpServletResponse response,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();
		        }
			}
		}
		plmMaterialService.update(id,materialFile, coverFile, materialTitle,materialDescription,materialType,getUserId());
		return R.ok();
	}
	
	
		
	/**
	 * 删除
	 */
	@SysLog("资源删除")
	@PostMapping("/delete")
	@RequiresPermissions("plm:material:delete")
	public R delete(@RequestBody Long[] ids){
		plmMaterialService.deleteBatch(ids,getUserId());
		return R.ok();
	}
	
	/**
	 * 回收站还原
	 */
	@SysLog("回收站还原")
	@RequestMapping("/recycle")
	@RequiresPermissions("plm:material:recycle")
	public R recycle(@RequestBody Long[] ids){
		plmMaterialService.recycleBatch(ids,getUserId());
		return R.ok();
	}
	
	/**
	 * 回收站删除
	 * @param params
	 * @return
	 */
	@SysLog("回收站删除")
	@RequestMapping("/cycle/delete")
	@RequiresPermissions("plm:material:cycle:delete")
	public R recycleDelete(@RequestBody Long[] ids){
		plmMaterialService.recycleDelete(ids,getUserId());
		return R.ok();
	}
}
