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.BusinessProjectService;
import com.goafanti.business.service.BusinessVarietiesService;
import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.ErrorConstants;

import com.goafanti.common.controller.CertifyApiController;
import com.goafanti.common.dao.BusinessVarietiesMapper;
import com.goafanti.common.enums.AttachmentType;
import com.goafanti.common.utils.StringUtils;
@RestController
@RequestMapping("/api/admin/Varieties")
public class AdminVarietiesApiController extends CertifyApiController{
	@Resource
	private BusinessVarietiesService businessVarietiesService;
	@Resource
	BusinessProjectService businessprojectService;
	@Resource
	BusinessVarietiesMapper	businessVarietiesMapper;
	
	/**
	 * 新增品类
	 */
	@RequestMapping(value = "/addVarieties" , method = RequestMethod.GET)
	public Result addVarieties(String cname,String superId) {
		Result res=new Result();
		if (StringUtils.isBlank(cname)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "品类名称为空", "品类名称"));
			return res;
		}
		if (StringUtils.isBlank(superId)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "上级品类为空", "上级品类"));
			return res;
		}
		if (0<(businessVarietiesService.isBeing(cname))) {
			res.getError().add(buildError(ErrorConstants.PARAM_BEING_ERROR, "品类名称已存在", "品类名称"));
			return res;
		}
		if (2<businessVarietiesService.getLayer(superId)) {
			res.getError().add(buildError( "品类层级不能大于3", "品类层级不能大于3")); 
			return res;
		}
		if (businessVarietiesService.getCountSon(superId)>99) {
			res.getError().add(buildError( "品类下级最多存在99个", "品类下级最多存在99个")); 
			return res;
		}
		res.setData(businessVarietiesService.insert(cname,superId));
		return res;
		
	}
	/**
	 * 搜索品类
	 */
	@RequestMapping(value="/vtList" ,method=RequestMethod.POST)
	public Result vtList(String cname,String superId,Integer layer,
			String status,Integer pageNo, Integer pageSize) {
		Result res = new Result();
        res.setData(businessVarietiesService.listVarieties(cname, superId, layer,
        		status, pageNo, pageSize));
		return res;
	}
	/**
	 * 获取父类信息
	 * 
	 */
	@RequestMapping(value = "/getSuperList" , method = RequestMethod.GET)
	public Result getSuperList() {
		Result res=new Result();
		res.setData(businessVarietiesService.getSuperList());
		return res;
		
	}
	/**
	 * 获取品类详情
	 */
	@RequestMapping(value="/detailVarieties" ,method=RequestMethod.GET)
	public Result detailVarieties(String  id) {
		Result res = new Result();
		res.setData(businessVarietiesService.editVarieties(id));
		return res;
	}
	/**
	 * 保存编辑信息
	 */
	@RequestMapping(value = "/editVarieties", method = RequestMethod.POST)
	public Result editVarieties (String id,String cname,String superId,String  status,String varietiesLogo) {
		Result res = new Result();
		if (StringUtils.isBlank(cname)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "品类名称为空", "品类名称"));
			return res;
		}
		if (StringUtils.isBlank(superId)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "上级用户为空", "上级用户"));
			return res;
		}
		if (1<(businessVarietiesService.isBeing(cname))) {
			res.getError().add(buildError(ErrorConstants.PARAM_BEING_ERROR, "品类名称已存在", "品类名称"));
			return res;
		}
		if (businessVarietiesService.ifLayer(superId,id)) {
			res.getError().add(buildError( "品类层级不能超过三级", "品类层级不能超过三级"));
			return res;
		}
		if (businessVarietiesService.getCountSon(superId)>99) {
			res.getError().add(buildError( "品类下级最多存在99个", "品类下级最多存在99个")); 
			return res;
		}
		if (businessVarietiesService.getCountSon(superId)>99) {
			res.getError().add(buildError( "品类下级最多存在99个", "品类下级最多存在99个")); 
			return res;
		}
		res.setData(businessVarietiesService.updateVarietoes(id, cname, superId, status, varietiesLogo));
		return res; 
	}
	/**
	 * 品类删除
	 */
	@RequestMapping(value = "/deleteState", method = RequestMethod.GET)
	public Result deleteState(String id){
		Result res=new Result();
		if (businessVarietiesService.getCountSon(id)>0) {
			res.getError().add(buildError("品类下存在子品类", "品类存在子品类不允许删除"));
			return res;
		}
		if (businessprojectService.checkProject(id)) {
			res.getError().add(buildError("品类下存在项目", "品类存在项目不允许删除"));
			return res;
		}
		res.setData(businessVarietiesService.deleteState(id));
		return res;
	}
	/**
	 * 品类停用
	 */
	@RequestMapping(value = "/updateStatus", method = RequestMethod.GET)
	public Result updateStatus(String id){
		Result res=new Result();
		res.setData(businessVarietiesService.updateStatus(id));
		return res;
	}
	/**
	 * 图片上传
	 */
	@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.VARIETIES_PICTURE
				) {
			res.setData(handleFiles(res, "/varieties/", false, req, sign, ""));
		} else {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "图片"));
		}*/
		return res;
	}
	/**
	 * 品类显示位置更换
	 */
	@RequestMapping(value = "/locationChange", method = RequestMethod.GET)
	public Result locationChange(String id,Integer sort,Integer flag){
		Result res=new Result();
		if (1==sort&&flag==1) {
			res.getError().add(buildError("上移失败", "上移失败"));
			return res;
		}
		if (sort>=Integer.valueOf(businessVarietiesMapper.getSortMax())&&flag==0) {
			res.getError().add(buildError("下移失败", "下移失败"));
			return res;
		}
		res.setData(businessVarietiesService.updateLocation(id,sort,flag));
		return res;
	}

}
