package com.ifish.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.ifish.dto.JsonResult;
import com.ifish.dto.PageingDto;
import com.ifish.entity.ShopsInfo;
import com.ifish.enums.ResultEnum;
import com.ifish.exception.IfishException;
import com.ifish.service.ShopsService;
import com.ifish.util.IfishUtil;
import com.ifish.validator.PageParam;
import com.ifish.validator.ShopsUserInfoParam;

/**
 * 
 * @author ggw
 *
 */
@RestController
public class ShopsAction {
	
	@Autowired
	private ShopsService shopsService;
	
	/**
	 * 保存商户信息
	 * @param shopsInfo
	 * @param file1
	 * @param file2
	 * @param file3
	 * @return
	 */
	@RequestMapping(value="/shops/v3/saveShopsInfo.do",method=RequestMethod.POST)
	public Object saveShopsInfo(ShopsInfo shopsInfo,MultipartFile file1,MultipartFile file2,MultipartFile file3) {
		if(file1.getSize()>3145728 || file2.getSize()>3145728 || file3.getSize()>3145728){
			return IfishUtil.returnJson(ResultEnum.warn206.getKey(), "");
		}
		else{
			return this.shopsService.saveShopsInfo(shopsInfo,file1,file2,file3);
		}
	}
	
	/**
	 * 审核失败后修改商户信息
	 * @param shopsInfo
	 * @param file1
	 * @param file2
	 * @param file3
	 * @return
	 */
	@RequestMapping(value="/shops/v3/updateShopsInfo.do",method=RequestMethod.POST)
	public Object updateShopsInfo(ShopsInfo shopsInfo,MultipartFile file1,MultipartFile file2,MultipartFile file3) {
		if(file1!=null && !file1.isEmpty() && file1.getSize()>3145728){
			return IfishUtil.returnJson(ResultEnum.warn206.getKey(), "");
		}
		if(file2!=null && !file2.isEmpty() && file2.getSize()>3145728){
			return IfishUtil.returnJson(ResultEnum.warn206.getKey(), "");
		}
		if(file3!=null && !file3.isEmpty() && file3.getSize()>3145728){
			return IfishUtil.returnJson(ResultEnum.warn206.getKey(), "");
		}
		else{
			return this.shopsService.updateShopsInfo(shopsInfo,file1,file2,file3);
		}
	}

	/**
	 * 审核成功后修改商户信息
	 * @param shopsInfo
	 * @param file4
	 * @return
	 */
	@RequestMapping(value="/shops/v3/updateBaseShopsInfo.do",method=RequestMethod.POST)
	public Object updateBaseShopsInfo(ShopsInfo shopsInfo,MultipartFile file4) {
		if(file4!=null && !file4.isEmpty() && file4.getSize()>3145728){
			return IfishUtil.returnJson(ResultEnum.warn206.getKey(), "");
		}
		else{
			return this.shopsService.updateBaseShopsInfo(shopsInfo,file4);
		}
	}
	
	/**
	 * 查询审核状态
	 * @param shopsId
	 * @return
	 */
	@RequestMapping(value="/shops/v3/getShopsStatus.do",method=RequestMethod.POST)
	public Object getShopsStatus(Integer shopsId) {
		return this.shopsService.getShopsStatus(shopsId);
	}
	
	/**
	 * 看护商家列表
	 * @param firstResult
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value="/shops/v3/getShopsInfo.do",method=RequestMethod.POST)
	public Object pushListInf(Integer userId,Integer firstResult,Integer pageSize) {
		return this.shopsService.getShopsInfoByPage(userId,firstResult, pageSize);
	}
	
	/**
	 * 选择看护商家
	 * @param shopsUserId
	 * @param userId
	 * @return
	 */
	@RequestMapping(value="/shops/v3/choiceShops.do",method=RequestMethod.POST)
	public Object choiceShops(@RequestParam Integer shopsUserId,@RequestParam Integer userId) {
		return this.shopsService.choiceShops(shopsUserId,userId);
	}
	
	/**
	 * 解除看护关系
	 * @param shopsUserId
	 * @param userId
	 * @return
	 */
	@RequestMapping(value="/shops/v3/removeLook.do",method=RequestMethod.POST)
	public Object removeLook(Integer userId) {
		return this.shopsService.removeLook(userId);
	}
	
	/**
	 * 获取商家会员信息
	 * @param userId
	 * @param shopsId
	 * @return
	 */
	@RequestMapping(value="/shopsUserInfo/{userId}/{shopsId}",method=RequestMethod.GET)
	public JsonResult<?> getShopsUserInfoById(@PathVariable("userId")Integer userId,@PathVariable("shopsId")Integer shopsId){
		return shopsService.getShopsUserInfoById(userId, shopsId);
	}
	
	/**
	 * 更新商家会员信息
	 * @param userId
	 * @param shopsId
	 * @param result
	 * @param info
	 * @return
	 */
	@RequestMapping(value="/shopsUserInfo/{userId}/{shopsId}",method=RequestMethod.POST)
	public JsonResult<?> updateShopsUserInfo(@PathVariable("userId")Integer userId,
											 @PathVariable("shopsId")Integer shopsId,
											 @Validated ShopsUserInfoParam param,
											 BindingResult result){
		//参数校验
		if(result.hasErrors()){
			throw new IfishException(ResultEnum.error401);
		}
		return shopsService.updateShopsUserInfo(userId,shopsId,param);
	}
	
	/**
	 * 分页获取商家会员信息
	 * @param pageParam
	 * @param result
	 * @return
	 */
	@RequestMapping(value="/shopsUserInfo",method=RequestMethod.GET)
	public PageingDto<?> getShopsUserInfoById(@RequestParam("shopsId")Integer shopsId,
											  @Validated PageParam pageParam,
											  BindingResult result){
		//参数校验
		if(result.hasErrors()){
			throw new IfishException(ResultEnum.error401);
		}
		return shopsService.getShopsUserInfoByPage(shopsId,pageParam);
	}
	
	/**
	 * 成为商家会员
	 * @param userId
	 * @param shopsId
	 * @return
	 */
	@RequestMapping(value="/becomingShopsUser/{userId}/{shopsId}",method=RequestMethod.POST)
	public JsonResult<?> becomeShopsUser(@PathVariable("userId")Integer userId,@PathVariable("shopsId")Integer shopsId){
		return shopsService.becomeShopsUser(userId, shopsId);
	}
	
}