package com.goafanti.business.controller;

import com.goafanti.business.service.BusinessProjectService;
import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.CertifyApiController;
import com.goafanti.common.enums.AttachmentType;
import com.goafanti.common.model.BusinessProject;
import com.goafanti.common.model.BusinessProjectModel;
import com.goafanti.common.utils.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
@RequestMapping("/api/admin/ProjectSize")
public class AdminBusinessProjectController extends CertifyApiController{

	@Resource
	BusinessProjectService businessprojectService;

	/**
	 * 新增项目
	 */
	@RequestMapping(value = "/addProject" , method = RequestMethod.POST)
	public Result addProject(String bname,String cid,String country,String province,String city,String district,String boutique) {
		Result res=new Result();
		if (StringUtils.isBlank(bname)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "项目名称名称为空", "项目名称"));
			return res;
		}
		if (StringUtils.isBlank(boutique)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "是否精品为空", "是否精品"));
			return res;
		}
		if (StringUtils.isBlank(cid)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "所属品类为空", "所属品类"));
			return res;
		}
		if (businessprojectService.getBnamecount(bname)>0) {
			res.getError().add(buildError(ErrorConstants.PARAM_BEING_ERROR, "项目名称已存在", "项目名称"));
			return res;
		}
		if(Integer.parseInt(country)==0){
			if (StringUtils.isBlank(province)) {
				res.getError().add(buildError( "至少指定省", "至少指定省"));
				return res;
			}
		}
		res.setData(businessprojectService.insert(bname, cid, country,province,city,district,boutique));
		return res;
	}

	/**
	 * 项目搜索
	 */
	@RequestMapping(value = "/listProject" , method = RequestMethod.POST)
	public Result listProject(String bname,String cid,String country,String province,String city,
			String district,String activityFlag,String status,Integer type,Integer restrictStatus,
							  Integer pageNo, Integer pageSize ){
		Result res=new Result();
		if (country==AFTConstants.USER_TYPE_PERSONAL) {
			if (StringUtils.isBlank(province)) {
				res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "所属省份为空", "所属省份"));
				return res;
			}
		}
		 res.setData(businessprojectService.listProject( bname, cid, country,province, city, district ,activityFlag, status, type,restrictStatus,pageNo,pageSize ));
		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.BUSINESS_PROJECT_MAX_PICTURE
				|| attachmentType == AttachmentType.BUSINESS_PROJECT_MIN_PICTURE
				) {
			res.setData(handleFiles(res, "/Project/", false, req, sign, "project"));
		} else {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件"));
		}
		return res;
	}
	/**
	 * 获取项目详情
	 */
	@RequestMapping(value = "/orgProject", method = RequestMethod.GET)
	public Result orgProject(String id){
		Result res=new Result();
		res.setData(businessprojectService.orgProjects(id));
		return res;
	}
	/**
	 * 删除项目
	 */
	@RequestMapping(value = "/deleteProject", method = RequestMethod.GET)
	public Result deleteProject(String id){
		Result res=new Result();
		res.setData(businessprojectService.deleteProject(id));
		return res;
	}

	/**
	 * 停用项目
	 */
	@RequestMapping(value = "/stopProject", method = RequestMethod.GET)
	public Result stopProject(String id){
		Result res=new Result();
		res.setData(businessprojectService.updateStopProject(id));
		return res;
	}
	/**
	 * 获取业务品类全路径
	 */
	@RequestMapping(value = "/getAllCname", method = RequestMethod.GET)
	public Result getAllCname(Integer flag){
		Result res=new Result();
		res.setData(businessprojectService.getAllCnames(flag));
		return res;
	}

	/**
	 * 编辑基本保存
	 */
	@RequestMapping(value = "/updateProject", method = RequestMethod.POST)
	public Result updateProject(BusinessProject s){
		Result res=new Result();
		if (StringUtils.isBlank(s.getBname())) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "项目名称名称为空", "项目名称"));
			return res;
		}

		if (StringUtils.isBlank(s.getCid())) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "所属品类为空", "所属品类"));
			return res;
		}
		if (businessprojectService.judgeBeing(s)) {
			res.getError().add(buildError(ErrorConstants.PARAM_BEING_ERROR, "项目名称已存在", "项目名称"));
			return res;
		}
		if(Integer.valueOf(s.getCountry())==0){
			if (StringUtils.isBlank(s.getProvince())) {
				res.getError().add(buildError( "至少指定省", "至少指定省"));
				return res;
			}
		}
		res.setData(businessprojectService.updateProject(s));
		return res;
	}
	/**
	 * 新建规格
	 */
	@RequestMapping(value = "/addProjectSize", method = RequestMethod.POST)
	public Result addProjectSize(BusinessProjectModel ps){
		Result res=new Result();
		if (businessprojectService.WhetherRepeat(ps)) {
				res.getError().add(buildError(ErrorConstants.PARAM_BEING_ERROR, "规格名称已存在", "规格名称"));
				return res;
			}
		if (businessprojectService.judgeStatus(ps)) {
			res.getError().add(buildError( "项目已停用", "项目已停用"));
			return res;
		}

		res.setData(businessprojectService.addProjectSize(ps));
		return res;
	}

	/**
	 * 编辑保存规格
	 */
	@RequestMapping(value = "/orgProjectSize", method = RequestMethod.POST)
	public Result orgProjectSize(BusinessProjectModel ps){
		Result res=new Result();
		res.setData(businessprojectService.updateOrgProjectSize(ps));
		return res;
	}
	/**
	 * 规格列表
	 */
	@RequestMapping(value = "/listProjectSize", method = RequestMethod.GET)
	public Result listProjectSize(String pid,Integer pNo,Integer Psize){
		Result res=new Result();
		if (StringUtils.isBlank(pid)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "项目名称名称为空", "项目"));
			return res;
		}
		res.setData(businessprojectService.listProjectSize(pid,pNo, Psize));
		return res;
	}

	/**
	 * 获取规格详情
	 */
	@RequestMapping(value = "/getProjectSize", method = RequestMethod.GET)
	public Result getProjectSize(String  id){
		Result res=new Result();
		res.setData(businessprojectService.getProjectSize(id));
		return res;
	}
	/**
	 * 删除规格
	 */
	@RequestMapping(value = "/deleteProjectSize", method = RequestMethod.GET)
	public Result deleteProjectSize(String  id){
		Result res=new Result();
		res.setData(businessprojectService.deleteProjectSize(id));
		return res;
	}
	/**
	 * 停用规格
	 */
	@RequestMapping(value = "/stopProjectSize", method = RequestMethod.GET)
	public Result stopProjectSize(String  id){
		Result res=new Result();
		res.setData(businessprojectService.updateSotpProjectSize(id));
		return res;
	}


}
