package com.goafanti.admin.controller;

import java.io.IOException;
import java.util.Base64;
import java.util.concurrent.ExecutionException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
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.bind.annotation.RestController;

import com.goafanti.admin.bo.KnowledgeBaseBo;
import com.goafanti.admin.service.CommunityService;
import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.BaseApiController;
import com.goafanti.common.enums.AttachmentType;
import com.goafanti.common.enums.KnowledgeBaseFields;
import com.goafanti.common.enums.VideoFields;
import com.goafanti.common.model.JtKnowledgeBase;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.core.mybatis.JDBCIdGenerator;
import com.goafanti.core.shiro.token.TokenManager;


@RestController
@RequestMapping(value = "/api/admin/community")
public class AdminCommunityApiController extends BaseApiController {
	
	@Autowired
	private JDBCIdGenerator	idGenerator;
	
	@Resource
	private CommunityService communityService;
	
	//查看所有产品专栏信息
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public Result clist(KnowledgeBaseBo knowledgeBase , Integer pageSize, Integer pageNo){
		Result res = new Result();
		//查看更多
		res.setData(communityService.getJtKnowledgeBaseList(knowledgeBase, pageNo, pageSize,1));
		return res;
	}
	
	// 查看单个信息
	@RequestMapping(value = "/getCommunintyById", method = RequestMethod.GET)
	@ResponseBody
	public Result getCommunintyById(String id) {
		Result result = new Result();
		JtKnowledgeBase o = new JtKnowledgeBase();
		o.setId(id);
		result.setData(communityService.getCommunintyById(o));
		return result;
	}

	// 新增产品信息
	@RequestMapping(value = "/insertCommunity", method = RequestMethod.POST)
	@ResponseBody
	public Result insertCommunity(@Valid JtKnowledgeBase knowledgeBase, BindingResult bindingResult)
			throws IOException, InterruptedException, ExecutionException {
		// 数据限制
		Result result = new Result();
		if (bindingResult.hasErrors()) {
			result.getError().add(
					buildErrorByMsg(bindingResult.getFieldError()
							.getDefaultMessage(), KnowledgeBaseFields 
							.getFieldDesc(bindingResult.getFieldError()
									.getField())));
			return result;
		}
		if (StringUtils.isBlank(knowledgeBase.getTitle())) {
			result.getError().add(buildError("视频名称不能是空", "视频名称不能是空"));
			return result;
		}
		// 名称重名限制
		 if(communityService.nameCheck(knowledgeBase.getTitle().trim()) > 0){
			 result.getError().add(
			 buildError(ErrorConstants.PARAM_ERROR, "", "视频名称重复不能新增"));
			 return result;
		 }
		 result.setData(communityService.insertCommunity(knowledgeBase));
		 return result;
	}

	// 修改知识社区
	@RequestMapping(value = "/updateCommunity", method = RequestMethod.POST)
	@ResponseBody
	public Result updateCommunity(@Valid JtKnowledgeBase knowledgeBase, BindingResult bindingResult)
			throws IOException, InterruptedException, ExecutionException {
		Result result = new Result();
		if (bindingResult.hasErrors()) {
			result.getError().add(
					buildErrorByMsg(bindingResult.getFieldError()
							.getDefaultMessage(), VideoFields
							.getFieldDesc(bindingResult.getFieldError()
									.getField())));
			return result;
		}
		// 名称重名限制
		if(null != knowledgeBase.getTitle() && "".equals(knowledgeBase.getTitle().trim())){
			result.getError().add(
					buildError( "视频名称不能是空", "视频名称不能是空"));
			return result;
		}
		//修改
		result.setData(communityService.updateCommunity(knowledgeBase));
		return result;
	}
		
	//图片上传接口
	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	@ResponseBody
	public Result uploadPicture(HttpServletRequest req, String sign) {
		Result res = new Result();
		AttachmentType attachmentType = AttachmentType.getField(sign);
		if (attachmentType == AttachmentType.KNOWLEDGE_BASE) {
			String picturebase = req.getParameter("picturebase");
			String filename = req.getParameter("filename");
			byte[] bs = Base64.getDecoder().decode(picturebase);
			filename = System.nanoTime()
					+ filename.substring(filename.indexOf("."));
			res.setData(handleBaseFiles(res, "/knowledge_base/", false, bs, sign,
					TokenManager.getUserId(), filename));

		} else {
			res.getError().add(
					buildError(ErrorConstants.PARAM_ERROR, "", "文件标示"));
		}
		return res;
	}
		
}
