package com.uas.account.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.uas.account.core.Page;
import com.uas.account.entity.InvitationRecord;
import com.uas.account.entity.PartnershipRecord;
import com.uas.account.entity.RequsetStatus;
import com.uas.account.entity.UserSpaceDetail;
import com.uas.account.service.PartnershipService;
import com.uas.account.service.UserSpaceService;

/**
 * 为平台和ERP用户提供公共接口查询当前企业的合作伙伴
 * 
 * @author hejq
 * @time 创建时间：2017年1月17日
 */
@Controller
@RequestMapping(value = "/api/partners")
public class GetAllPartnersInfoController {

	@Autowired
	private PartnershipService partnershipService;

	@Autowired
	private UserSpaceService userSpaceService;

	/**
	 * 查询发出的申请
	 * 
	 * @param businessCode
	 * @param statusCode
	 * @param keyword
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(params = "_operate=getAllRequest")
	@ResponseBody
	public Page<PartnershipRecord> getAllRequest(String businessCode, Integer statusCode, String keyword,
			int pageNumber, int pageSize) {
		return partnershipService.findBySpaceUidAndStatusCode(businessCode, statusCode, keyword, pageNumber, pageSize);
	}

	/**
	 * 过滤掉已存在UU，查询发出的申请（UAS接口）
	 *
	 * @param businessCode
	 * @param statusCode
	 * @param keyword
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(params = "_operate=getAllRequestFilterByPartnerUUs")
	@ResponseBody
	public Page<PartnershipRecord> getAllRequestFilterByPartnerUUs(String businessCode, Integer statusCode, String keyword, int pageNumber, int pageSize, String partnerUUs) {
		List<Long> UUs = JSON.parseArray(partnerUUs, Long.class);
		if (!CollectionUtils.isEmpty(UUs)) { // 存在需要过滤的UU时
			return partnershipService.findBySpaceUidAndStatusCodeFilterByPartnerUUs(businessCode, statusCode, keyword, UUs, pageNumber, pageSize);
		}
		return partnershipService.findBySpaceUidAndStatusCode(businessCode, statusCode, keyword, pageNumber, pageSize);
	}

	/**
	 * 查询所有企业列表
	 */
	@RequestMapping(params = "_operate=getUserSpaces")
	@ResponseBody
	public Page<UserSpaceDetail> getUserSpaces(String keyword, int pageNumber, int pageSize) {
		return userSpaceService.findByKeyword(keyword, pageNumber, pageSize);
	}

	/**
	 * 从各应用新增一条合作关系
	 * 
	 * @param record
	 * @return
	 */
	@RequestMapping(params = "_operate=addPartner", method = RequestMethod.POST)
	@ResponseBody
	public ModelMap addPartner(PartnershipRecord record) {
		return partnershipService.addPartner(record);
	}

	/**
	 * b2b因为单据产生新增一条合作关系
	 *
	 * @param record
	 * @return
	 */
	@RequestMapping(params = "_operate=synchronizePartner", method = RequestMethod.POST)
	@ResponseBody
	public ModelMap synchronizePartner(PartnershipRecord record) {
		return partnershipService.synchronizePartner(record);
	}

	/**
	 * 拒绝申请
	 * 
	 * @param id
	 * @param reason
	 * @param vendUserTel
	 * @param appId
	 * @return
	 */
	@RequestMapping(params = "_operate=rejectRequest")
	@ResponseBody
	public ModelMap rejectRequest(Long id, String reason, String vendUserTel, String appId) {
		return partnershipService.rejectRequest(id, reason, vendUserTel, appId);
	}

	/**
	 * 通过申请
	 * 
	 * @param id
	 * @param vendUserTel
	 * @param appId
	 * @return
	 */
	@RequestMapping(params = "_operate=acceptRequest")
	@ResponseBody
	public ModelMap acceptRequest(Long id, String vendUserTel, String appId) {
		return partnershipService.acceptRequest(id, vendUserTel, appId);
	}

	/**
	 * 通过营业执照号获取申请状态
	 * 
	 * @param custUid
	 * @param vendUid
	 * @return
	 */
	@RequestMapping(params = "_operate=getRequestStatus")
	@ResponseBody
	public RequsetStatus getRequestStatus(String custUid, String vendUid) {
		return partnershipService.getRequestByCustUidAndVendUid(custUid, vendUid);
	}

	/**
	 * 通过id查询企业信息（方便平台搜索调用）
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "_operate=findAll")
	@ResponseBody
	public List<UserSpaceDetail> findAll(String ids) {
		return userSpaceService.findAll(ids);
	}

	/**
	 * 获取收到的待处理的请求的数量
	 * 
	 * @param businessCode
	 * @return
	 */
	@RequestMapping(params = "_operate=getRequestTodo")
	@ResponseBody
	public ModelMap getRequestTodo(String businessCode) {
		return partnershipService.getRequestTodo(businessCode);
	}
	
	/**
	 * 邀请合作伙伴记录
	 * 
	 * @param formStore
	 */
	@RequestMapping(params = "_operate=invitation", method = RequestMethod.POST)
	@ResponseBody
	public void invite(String jsonStr) {
		InvitationRecord record = JSONObject.parseObject(jsonStr, InvitationRecord.class);
		partnershipService.invite(record);
	}
}
