package com.goafanti.business.controller;

import java.util.Base64;
import java.util.Date;

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

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.goafanti.business.service.JtBusinessService;
import com.goafanti.business.service.JtBusinessServiceWithCategoryMapper;
import com.goafanti.business.service.JtTagService;
import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.CertifyApiController;
import com.goafanti.common.enums.AttachmentType;
import com.goafanti.common.enums.ProjectAuditStatus;
import com.goafanti.common.model.JtBusinessCategory;
import com.goafanti.common.model.JtBusinessProject;
import com.goafanti.common.model.JtTag;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.core.shiro.token.TokenManager;

@RestController
@RequestMapping(value = "/api/user/jtBusiness")
public class UserJtBusinessController extends CertifyApiController{
	
	@Resource 
	private JtBusinessService jtBusinessService;
	
	@Resource JtBusinessServiceWithCategoryMapper JtBusinessServiceWithCategoryMapper;
	@Resource
	private JtTagService	jtTagService;
	/*
	 * 
	 * 新增项目
	 * 
	 * */
	@RequestMapping(value="/project/apply",method=RequestMethod.POST)
	private Result insertJtBusinessProject(JtBusinessProject jtBusinessProject) {
		Result result=new Result();
		disposeProjectParam(result, jtBusinessProject);
		if(result.getError().size()<=0) {
			result.setData(jtBusinessService.insertProject(jtBusinessProject));
		}
		return result;
		
	}
	private void disposeProjectParam(Result result,JtBusinessProject jtBusinessProject ) {
		if(jtBusinessProject.getName() == null) {
			result.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "名称不可为空", "名称"));
			return ;
		}
		if(jtBusinessProject.getCategoryId() == null) {
			result.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "品类"));
			return ;
		}
		//todo  判断品类是否存在
		JtBusinessCategory jtBusinessCategory=jtBusinessService.getCategoryById(jtBusinessProject.getCategoryId());
		if(jtBusinessCategory==null ||jtBusinessCategory.getLayer()!=2) {
			result.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "二级品类未找到"));
			return ;
		}
		
		if(jtBusinessProject.getProvince()==null)
		{
			result.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "省份"));
			return ;
		}
//		if(jtBusinessProject.getCity()==null)
//		{
//			result.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "城市"));
//			return ;
//		}
//		if(jtBusinessProject.getPrice()==null)
//		{
//			result.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "价格"));
//			return ;
//		}
		return ;
	}
	
	/*
	 * 项目详情
	 * 
	 * */
	@RequestMapping(value="/project/detail",method=RequestMethod.GET)
	private Result getProjectDetail(String id) {
		Result result=new Result();
		if(id == null) {
			result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"","id不能为空,"));
			return result;
		}
		result.setData(jtBusinessService.getBusinessProjectDetail(id));
		return result;
	}
	
	/*
	 * 删除项目
	 * */
	@RequestMapping(value="/project/delete",method=RequestMethod.GET)
	private Result deleteProjecById(String id) {
		Result result=new Result();
		if(id == null) {
			result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"","id不能为空,"));
			return result;
		}
		result.setData(jtBusinessService.deleteProjectById(id));
		return result;
	}
	
	@RequestMapping(value="/project/update",method=RequestMethod.POST)
	private Result updateProject(JtBusinessProject jtBusinessProject) {
		Result result=new Result();
		jtBusinessProject.setCreateTime(null);
		jtBusinessProject.setNumber(null);
		disposeProjectParam(result, jtBusinessProject);
		if(jtBusinessProject.getPrice()==null) {
			result.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "价格不可为空，"));
		}
		//判断各项数据长度
		if(StringUtils.isNotBlank(jtBusinessProject.getName()) && jtBusinessProject.getName().length() > 16){
			result.getError().add(buildError("16", "项目名称字数不能超过16"));
		}
		if(StringUtils.isNotBlank(jtBusinessProject.getAdvertisement()) && jtBusinessProject.getAdvertisement().length() > 128){
			result.getError().add(buildError("128", "项目营销说明字数不能超过128"));
		}
		if(StringUtils.isNotBlank(jtBusinessProject.getValue()) && jtBusinessProject.getValue().length() > 1024){
			result.getError().add(buildError("1024", "业务项目价值及作用字数不能超过1024"));
		}
		if(StringUtils.isNotBlank(jtBusinessProject.getApplyConditions()) && jtBusinessProject.getApplyConditions().length() > 512){
			result.getError().add(buildError("512", "项目客户条件字数不能超过512"));
		}
		if(result.getError().size()>0) {
			return result;
		}
		jtBusinessService.updateProject(jtBusinessProject);
		return result;
	}
	

	
	@RequestMapping(value = "/project/uploadPicture", method = RequestMethod.POST)
	public Result uploadProjectPicture(HttpServletRequest req, String sign) {
		Result res = new Result();
		AttachmentType attachmentType = AttachmentType.getField(sign);

		if (attachmentType == AttachmentType.JT_PROJECT_PICTURE
				) {
			res.setData(handleFiles(res, "/jtProjects/", false, req, sign, ""));
		} else {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "图片"));
		}
		return res;
	}
	@RequestMapping(value = "/project/uploadPicture64", method = RequestMethod.POST)
	public Result uploadProjectPicture64(String picturebase, String filename,String sign) {
		Result res = new Result();
 		byte[] bs=Base64.getDecoder().decode(picturebase);
		AttachmentType attachmentType = AttachmentType.getField(sign);
		if (attachmentType == AttachmentType.JT_PROJECT_PICTURE) {
			//res.setData(handleFiles(res, "/jtProjects/", false, req, sign, ""));
			res.setData(handleBaseFiles(res, "/jtProjects/", false,bs, sign, TokenManager.getUserId(),filename));
		} else {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "图片"));
		}
		return res;
	}
	
	/*
	 * 查询品类
	 * 
	 * */
	@RequestMapping(value="/category/list",method=RequestMethod.GET)
	private Result getCategoryList(JtBusinessCategory jtBusinessCategory,Integer pageNo,Integer pageSize) {
		Result result=new Result();
		 result.setData(JtBusinessServiceWithCategoryMapper.getCategoryListByCondition(jtBusinessCategory,pageNo,pageSize));
		 return result;
	}

	/**
	 * 发布项目
	 * @param id
	 * @param auditStatus
	 * @return
	 */
	@RequestMapping(value = "/project/publish",method=RequestMethod.GET)
	public Result publishProject(String id,Integer auditStatus){
		Result res = new Result();
	
		JtBusinessProject currentP=jtBusinessService.getBusinessProjectDetail(id);
		Integer audit=currentP.getAuditStatus();
//		if(audit != ProjectAuditStatus.CREATE.getCode()  && audit !=ProjectAuditStatus.REVOKE.getCode()&& audit!=ProjectAuditStatus.UNAUDITED.getCode()){
//			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "当前状态无法发布"));
//			return res;
//		}
		JtBusinessProject jtBusinessProject = new JtBusinessProject();
		jtBusinessProject.setId(id);
		jtBusinessProject.setAuditStatus(ProjectAuditStatus.SUBMIT.getCode());
		jtBusinessProject.setReleaseDate(new Date());
		jtBusinessService.updateProject(jtBusinessProject);
		return res;
	}
	
	/**
	 * 撤销发布
	 * @param id
	 * @param auditStatus
	 * @return
	 */
	@RequestMapping(value = "/project/offShelf",method=RequestMethod.GET)
	public Result offShelf(String id,Integer auditStatus){
		Result res = new Result();
		if(auditStatus != ProjectAuditStatus.AUDITED.getCode()){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "当前状态无法撤销发布"));
			return res;
		}
		JtBusinessProject jtBusinessProject = new JtBusinessProject();
		jtBusinessProject.setId(id);
		jtBusinessProject.setAuditStatus(ProjectAuditStatus.REVOKE.getCode());
		jtBusinessService.updateJtCollectSearch(id);
		jtBusinessService.updateByPrimaryKeySelective(jtBusinessProject);
		return res;
	}
	
	/**
	 * 刷新发布时间
	 * @param id
	 * @param auditStatus
	 * @return
	 */
	@RequestMapping(value = "/project/refreshPublish",method=RequestMethod.GET)
	public Result refreshPublish(String id,Integer auditStatus){
		Result res = new Result();
		if(auditStatus != ProjectAuditStatus.AUDITED.getCode()){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "当前状态无法刷新发布"));
		}
		JtBusinessProject jtBusinessProject = new JtBusinessProject();
		jtBusinessProject.setId(id);
		jtBusinessProject.setReleaseDate(new Date());
		jtBusinessService.updateProject(jtBusinessProject);
		return res;
	}
	/*
	 * 标签列表
	 * */
	@RequestMapping(value="/tag/list",method=RequestMethod.GET)
	private Result detailJtTag(JtTag a,Integer pageNo,Integer pageSize) {
		Result result=new Result();
		result.data(jtTagService.selectListJtTag(a,pageNo,pageSize));
		return result;
	}
	
	
	
	
}
