package com.uas.account.controller;

import java.io.IOException;

import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.uas.account.core.Page;
import com.uas.account.entity.Asset;
import com.uas.account.service.AppService;
import com.uas.account.service.AssetService;
import com.uas.account.web.BaseController;
import com.uas.dfs.service.FileClient;

/**
 * 资源
 * 
 * @author yingp
 *
 */
@Controller
public class AssetManagerController extends BaseController {

	@Autowired
	private AssetService assetService;
	@Autowired
	private AppService appService;
	@Autowired
	private FileClient fileClient;

	/**
	 * page
	 * 
	 * @return
	 */
	@RequestMapping(value = "/console/asset", method = RequestMethod.GET)
	public ModelAndView getPage(ModelMap model) {
		// 列出全部app以供筛选
		return new ModelAndView("console/asset", success(appService.findAllView()));
	}

	/**
	 * 资源新增、修改
	 * 
	 * @param asset
	 * @return
	 */
	@RequestMapping(value = "/api/asset", method = RequestMethod.POST)
	@ResponseBody
	public ModelMap save(Asset asset) {
		if (asset.getAppId() == null)
			return error("参数错误");
		return success(assetService.save(asset));
	}

	/**
	 * 资源查找
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/api/asset/{id}", method = RequestMethod.GET)
	@ResponseBody
	public ModelMap findOne(@PathVariable("id") Long id) {
		return success(assetService.findOne(id));
	}

	/**
	 * 资源删除
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/api/asset/{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public ModelMap delete(@PathVariable("id") Long id) {
		Asset asset = assetService.findOne(id);
		if (asset != null)
			assetService.delete(asset);
		return success();
	}

	/**
	 * 按应用查找资源
	 * 
	 * @param appId
	 * @return
	 */
	@RequestMapping(value = "/api/asset/list", method = RequestMethod.GET)
	@ResponseBody
	public Page<Asset> findAll(String appId, int pageNumber, int pageSize) {
		return assetService.findAll(appId, pageNumber, pageSize);
	}

	/**
	 * 资源背景图保存
	 * 
	 * @return
	 * @throws IOException
	 * @throws FileUploadException
	 */
	@RequestMapping(value = "/api/util/upload", method = RequestMethod.POST)
	@ResponseBody
	public ModelMap uploadImage(@RequestBody MultipartFile file) throws IOException {
		String url = fileClient.uploadImage(file.getBytes(), file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()), null);
		return success(url);
	}

}
