package com.goafanti.business.controller;


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;

@RestController
@RequestMapping(value = "/api/admin/jtBusiness")
public class AdminJtBusinessController extends CertifyApiController{
	
	@Resource 
	private JtBusinessService jtBusinessService;
	@Resource 
	private JtTagService	jtTagService;
	
	@Resource JtBusinessServiceWithCategoryMapper JtBusinessServiceWithCategoryMapper;
	

	
	/*
	 * 删除品类
	 * */
	@RequestMapping(value="/category/delete",method=RequestMethod.POST)
	private Result deleteCetegoryById(String id) {
		Result result=new Result();
		if(id==null) {result.getError().add(buildError(ErrorConstants.STATUS_ERROR,"","id"));
		return result;} 
		int rc=jtBusinessService.deleteCategoryById(id);
		result.setData(rc);
		if(rc==-1) {
			result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"","请先删除子项目及子分类,"));
		}
		return result;
		
	}

	
	/*
	 * 品类详情
	 * */
	@RequestMapping(value="/category/detail",method=RequestMethod.GET)
	private Result getCategoryDetailById(String id)
	{
		Result result=new Result();
		if(id==null) {result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"","id"));
		return result;}
		
		result.setData(jtBusinessService.getCategoryById(id));
		return result;
	}
	/*
	 * 新增品類
	 * */
	@RequestMapping(value="/category/apply",method=RequestMethod.POST)
	private Result insertCategory(JtBusinessCategory jtBusinessCategory) {
		Result result=new Result();
		disposeCateParam(result, jtBusinessCategory);
		if(result.getError().size()>0)return result;
		int rc=jtBusinessService.insertCategory(jtBusinessCategory);
		if(rc==-1)result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"","每层品类不可超过99个,"));
		if(rc==-2)result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"","父类Id与层数需要对应,"));
		if(rc==-3)result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"","该品类已经存在,"));
		return result;
	}
	
	/*
	 * 修改
	 * */
	@RequestMapping(value="/category/update",method=RequestMethod.POST)
	private Result updateCategory(JtBusinessCategory jtBusinessCategory, Integer nextModule) {
		Result result=new Result();
		int re = jtBusinessService.updateCategory(jtBusinessCategory);
		result.setData(re);
		return result;
	}
	
	/*
	 * 查询品类
	 * */
	@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;
	}
	private void disposeCateParam(Result result,JtBusinessCategory jtBusinessCategory ) {
		if(jtBusinessCategory.getName() == null) {
			result.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "名称不可为空", "名称"));
			return ;
		}else if(jtBusinessCategory.getName().length()>16){
			result.getError().add(buildError("品类名称长度不能超过16位", "品类名称长度不能超过16位"));
			return ;
		}
		if(jtBusinessCategory.getLayer() == null) {
			result.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "层级不可为空", "层级"));
			return ;
		}
		if(jtBusinessCategory.getLayer()==2 && jtBusinessCategory.getSuperId()==null)
		{
			result.getError().add(buildError(ErrorConstants.PARAM_ERROR, "父级品类不可为空", "父级品类"));
			return ;
		}
		if(jtBusinessCategory.getLayer()>3 || jtBusinessCategory.getLayer()<1)
		{
			result.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "层级只能为一级或二级")); 
			return ;
		}
	
		
		return ;
	}
	
	/*
	 * 新增项目
	 * */
	@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.setId(null);
		jtBusinessProject.setNumber(null);
		disposeProjectParam(result, jtBusinessProject);
		if(result.getError().size()>0) {
			return result;
		}
		if(jtBusinessProject.getPrice()==null) {
			result.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "价格不可为空，"));
		}
		jtBusinessService.updateProject(jtBusinessProject);
		return result;
	}
	
	@RequestMapping(value = "/uploadPicture", method = RequestMethod.POST)
	public Result uploadPicture(HttpServletRequest req, String sign) {
		Result res = new Result();
		AttachmentType attachmentType = AttachmentType.getField(sign);

		if (attachmentType == AttachmentType.JT_BUSINESS_PICTURE
				) {
			res.setData(handleFiles(res, "/jtVarieties/", false, req, sign, ""));
		} else {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "图片"));
		}
		return res;
	}
	
	@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/audit" ,method=RequestMethod.POST)
    private Result auditAchievemnt(String id,Integer auditResult,String auditInfo) {
    	Result result=new Result();
    	if(id == null || auditResult == null || auditResult.intValue()!=0 && auditResult.intValue()!=1)
    	{
    		result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"",""));return result;
    	}
    	JtBusinessProject jtBusinessProject = jtBusinessService.getBusinessProjectDetail(id);
    	if(jtBusinessProject==null) {
    		result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"未找到服务","服务Id"));return result;
    	}
    	/*if(jtBusinessProject.getAuditStatus()!= ProjectAuditStatus.SUBMIT.getCode()) {
    		result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"未找到服务","当前状态不可审核"));return result;
    	}*/
    	if(auditResult.intValue() == 0){
    		jtBusinessProject.setAuditStatus(ProjectAuditStatus.AUDITED.getCode());
    		jtBusinessService.addJtCollectSearch(jtBusinessProject);
    	} 
    	if(auditResult.intValue() == 1) jtBusinessProject.setAuditStatus(ProjectAuditStatus.UNAUDITED.getCode());
    	jtBusinessProject.setAuditInfo(auditInfo);
    	jtBusinessService.updateProject(jtBusinessProject);
    	return result; 
	}
	/**
	 * 撤销项目
	 * @param jtBusinessProject
	 * @return
	 */
	@RequestMapping(value="/project/annul" ,method=RequestMethod.POST)
    private Result projectAnnul(JtBusinessProject jtBusinessProject) {
    	Result result=new Result();
    	if (StringUtils.isBlank(jtBusinessProject.getId())) {
    		result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"","撤销"));
			return result;
		}
    	jtBusinessProject.setAuditStatus(3);
    	jtBusinessService.updateJtCollectSearch(jtBusinessProject.getId());
    	result.setData(jtBusinessService.updateProject(jtBusinessProject));
    	return result; 
	}
	/**
	 * 项目是否热门
	 * @param jtBusinessProject
	 * @return
	 */
	@RequestMapping(value="/project/ifHot" ,method=RequestMethod.POST)
    private Result projectIfHot(JtBusinessProject jtBusinessProject) {
		Result result=new Result();
    	if (StringUtils.isBlank(jtBusinessProject.getId())) {
    		result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"","撤销"));
			return result;
		}
    	if (null==jtBusinessProject.getIsHot()) {
    		result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"","撤销"));
			return result;
		}
		if (jtBusinessProject.getIsHot()==0) {
			jtBusinessProject.setIsHot(1);
		}else {
			jtBusinessProject.setIsHot(0);
		}
		result.setData(jtBusinessService.updateProject(jtBusinessProject));
		return result;
	}
	/*
	 * 新增标签
	 * */
	@RequestMapping(value="/tag/apply",method=RequestMethod.POST)
	private Result insertJtTag(JtTag a) {
		Result result=new Result();
		if(StringUtils.isBlank(a.getName())) {
    		result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"标签名称","标签名称"));return result;
    	}
		result.data(jtTagService.addTag(a));
		return result;
		
	}
	/*
	 * 删除标签
	 * */
	@RequestMapping(value="/tag/delete",method=RequestMethod.POST)
	private Result deleteJtTag(JtTag a) {
		Result result=new Result();
		if(StringUtils.isBlank(a.getId())) {
    		result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"标签ID","标签ID"));return result;
    	}
		result.data(jtTagService.deleteJtTag(a.getId()));
		return result;
		
	}
	
	/*
	 * 修改标签
	 * */
	@RequestMapping(value="/tag/update",method=RequestMethod.POST)
	private Result updateJtTag(JtTag a) {
		Result result=new Result();
		if(StringUtils.isBlank(a.getId())) {
    		result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"标签ID","标签ID"));return result;
    	}
		if(StringUtils.isBlank(a.getName())) {
    		result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"标签名称","标签名称"));return result;
    	}
		result.data(jtTagService.updateJtTag(a));
		return result;
		
	}
	/*
	 * 标签内容
	 * */
	@RequestMapping(value="/tag/detail",method=RequestMethod.GET)
	private Result detailJtTag(JtTag a) {
		Result result=new Result();
		if(StringUtils.isBlank(a.getId())) {
    		result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"标签ID","标签ID"));return result;
    	}
		result.data(jtTagService.detailJtTag(a));
		return result;
	}
	/*
	 * 标签列表
	 * */
	@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;
	}
	/*
	 * 上移品类
	 * */
	@RequestMapping(value="/project/moveUp",method=RequestMethod.POST)
	private Result updateProjectMoveUp(String id) {
		Result result=new Result();
		if(StringUtils.isBlank(id)) {
    		result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"品类ID","品类ID"));return result;
    	}
		int x=jtBusinessService.updateProjectMoveUp(id);
		if(x==-1) {
    		result.getError().add(buildError("已到顶部无法上移","已到顶部无法上移"));return result;
    	}
		result.data(x);
		return result;
	}
	/*
	 * 品类置顶
	 * */
	@RequestMapping(value="/project/setTop",method=RequestMethod.POST)
	private Result updateProjectSetTop(String id) {
		Result result=new Result();
		if(StringUtils.isBlank(id)) {
    		result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"品类ID","品类ID"));return result;
    	}
		int x=jtBusinessService.updateProjectSetTop(id);
		if(x==-1) {
    		result.getError().add(buildError("已到顶部无法上移","已到顶部无法上移"));return result;
    	}
		result.data(x);
		return result;
	}
	
	
	
	
}
