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.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.uas.account.core.Page;
import com.uas.account.entity.PartnershipRecord;
import com.uas.account.entity.User;
import com.uas.account.entity.UserSpace;
import com.uas.account.service.PartnershipService;
import com.uas.account.service.UserSpaceService;
import com.uas.account.support.SystemSession;
import com.uas.account.web.BaseController;

/**
 * 合作关系操作记录的controller
 * 
 * @author hejq
 * @time 创建时间：2017年1月11日
 */
@Controller
@RequestMapping("/partnership")
public class PartnershipController extends BaseController {

	@Autowired
	private PartnershipService partnershipService;
	@Autowired
	private UserSpaceService userSpaceService;

	/**
	 * 发送企业请求
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/sendRequest")
	@ResponseBody
	public ModelMap SendRequest(Long id) {
		partnershipService.sendRequestById(id);
		return success();
	}

	/**
	 * 查找发出的请求，待审批
	 * 
	 * @param name
	 * @param businessCode
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value = "/sendRequest/list", params = "status=UNAUDIT", method = RequestMethod.GET)
	@ResponseBody
	public Page<PartnershipRecord> findAllSendUNAUDIT(String name, String businessCode, String custUserName,
			int pageNumber, int pageSize) {
		return partnershipService.findAllSendUnAudit(name, businessCode, custUserName, pageNumber, pageSize);
	}

	/**
	 * 查找发出的请求，已通过
	 * 
	 * @param name
	 * @param businessCode
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value = "/sendRequest/list", params = "status=ENABLED", method = RequestMethod.GET)
	@ResponseBody
	public Page<PartnershipRecord> findAllSendEnabled(String name, String businessCode, String custUserName,
			int pageNumber,
			int pageSize) {
		return partnershipService.findAllSendEnable(name, businessCode, custUserName, pageNumber, pageSize);
	}

	/**
	 * 查找发出的请求，未通过
	 * 
	 * @param name
	 * @param businessCode
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value = "/sendRequest/list", params = "status=UNAPPLY", method = RequestMethod.GET)
	@ResponseBody
	public Page<PartnershipRecord> findAllSendUNAPPLY(String name, String businessCode, String custUserName,
			int pageNumber, int pageSize) {
		return partnershipService.findAllSendUnApply(name, businessCode, custUserName, pageNumber, pageSize);
	}

	/**
	 * 通过id查找发出请求的详情
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/sendRequest/detail/id/{id}", method = RequestMethod.GET)
	@ResponseBody
	public ModelMap findSendDetailById(@PathVariable Long id) {
		return success(partnershipService.findSendRequestById(id));
	}

	/**
	 * 查找收到的请求，待审批
	 * 
	 * @param name
	 * @param businessCode
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value = "/receivedRequest/list", params = "status=UNAUDIT", method = RequestMethod.GET)
	@ResponseBody
	public Page<PartnershipRecord> findAllReceivedUNAUDIT(String name, String businessCode, String custUserName,
			int pageNumber, int pageSize) {
		return partnershipService.findAllReceivedUnAudit(name, businessCode, custUserName, pageNumber, pageSize);
	}

	/**
	 * 查找收到的请求，已通过
	 * 
	 * @param name
	 * @param businessCode
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value = "/receivedRequest/list", params = "status=ENABLED", method = RequestMethod.GET)
	@ResponseBody
	public Page<PartnershipRecord> findAllReceivedEnabled(String name, String businessCode, String custUserName,
			int pageNumber, int pageSize) {
		return partnershipService.findAllReceivedEnable(name, businessCode, custUserName, pageNumber, pageSize);
	}

	/**
	 * 查找收到的请求，未通过
	 * 
	 * @param name
	 * @param businessCode
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value = "/receivedRequest/list", params = "status=UNAPPLY", method = RequestMethod.GET)
	@ResponseBody
	public Page<PartnershipRecord> findAllReceivedUNAPPLY(String name, String businessCode, String custUserName,
			int pageNumber, int pageSize) {
		return partnershipService.findAllReceivedUnApply(name, businessCode, custUserName, pageNumber, pageSize);
	}

	/**
	 * 通过id查找收到请求的详情
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/receivedRequest/detail/id/{id}", method = RequestMethod.GET)
	@ResponseBody
	public ModelMap findReceivedDetailById(@PathVariable Long id) {
		return success(partnershipService.findReceivedRequestById(id));
	}

	/**
	 * 审核通过
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/receivedRequest/audit", params = "enable=1")
	@ResponseBody
	public ModelMap enableDetail(Long id) {
		partnershipService.enable(id);
		return success();
	}

	/**
	 * 审批未通过
	 * 
	 * @param reason
	 * @param businessCode
	 * @return
	 */
	@RequestMapping(value = "/receivedRequest/audit", params = "enable=0")
	@ResponseBody
	public ModelMap disableDetail(String reason, Long id) {
		partnershipService.disable(id, reason);
		return success();
	}
	
	/**
	 * 再次申请
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/sendRequest/resend")
	@ResponseBody
	public ModelMap resendRequest(Long id) {
		partnershipService.resendRequestById(id);
		return success();
	}

	/**
	 * 发出的请求统计
	 * 
	 * @return
	 */
	@RequestMapping(value = "/sendRequest/count", method = RequestMethod.GET)
	@ResponseBody
	public ModelMap sendRequestCount() {
		return success(partnershipService.getsendRequestCount());
	}

	/**
	 * 收到的请求统计
	 * 
	 * @return
	 */
	@RequestMapping(value = "/receivedRequest/count", method = RequestMethod.GET)
	@ResponseBody
	public ModelMap receivedRequestCount() {
		return success(partnershipService.getreceivedRequestCount());
	}

	/**
	 * 查询当前登录人信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getCurrentUserInfo", method = RequestMethod.GET)
	@ResponseBody
	public ModelMap getCurrentUserInfo() {
		User user = SystemSession.getUser();
		ModelMap map = new ModelMap();
		if (user == null) {
			return error("暂未检测到当前用户信息，请通过商务平台或ERP系统进行登录");
		} else {
			List<UserSpace> space = userSpaceService.findByUID(user.getSpaceUID());
			map.put("username", user.getName());
			map.put("enName", space.get(0).getName());
			map.put("enBusCode", space.get(0).getUid());
		}

		return success(map);
	}
}
