package com.goafanti.member.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.controller.BaseApiController;
import com.goafanti.common.enums.OrderState;
import com.goafanti.common.enums.UserLevel;
import com.goafanti.common.model.MemberOrder;
import com.goafanti.common.model.User;
import com.goafanti.common.model.UserIdentity;
import com.goafanti.core.mybatis.JDBCIdGenerator;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.member.bo.BusinessType;
import com.goafanti.member.service.MemberGradeService;
import com.goafanti.user.service.UserIdentityService;
import com.goafanti.user.service.UserService;

/**
 * 会员 前台
 * 
 * @author Administrator
 *
 */
@RestController
@RequestMapping(value = "/api/user/member")
public class UserMemberController extends BaseApiController {
	@Autowired
	private MemberGradeService	memberGradeService;
	@Autowired
	private UserService			userService;
	@Autowired
	private UserIdentityService	userIdentityService;
	@Autowired
	private JDBCIdGenerator		jDBCIdGenerator;

	/**
	 * 前台会员订单
	 * 
	 * @return
	 */
	@RequestMapping(value = "/selectMemberOrder", method = RequestMethod.GET)
	public Result selectMemberOrder(String pageNo, String pageSize) {
		Integer pNo = getPageNo(pageNo);
		Integer pSize = getPageSize(pageSize);
		Integer pNos = (pNo - 1) * pSize;
		Map<String, Object> map = new HashMap<>();
		map.put("pNos", pNos);
		map.put("pSize", pSize);
		map.put("id", TokenManager.getUserId());
		List<MemberOrder> list = memberGradeService.selectMemberOrder(map);
		int count = memberGradeService.countMemberOrder(TokenManager.getUserId());
		Map<String, Object> maps = new HashMap<>();
		maps.put("list", list);
		maps.put("count", count);
		maps.put("pNo", pNo);
		return res().data(maps);
	}

	/**
	 * 前台生成订单
	 * 
	 * @param menberType
	 * @param price
	 * @param validMonth
	 * @return
	 */
	@RequestMapping(value = "/addBasicsMemberOrder", method = RequestMethod.POST)
	public Result addBasicsMemberOrder(Integer menberType, String price, Integer validMonth) {
		Assert.notNull(TokenManager.getUserToken(), "必须用户登录");
		User u = userService.selectByPrimaryKey(TokenManager.getUserId());
		Assert.isTrue(u.getLvl() == 1, "必须为实名认证用户，已经是会员的无需重复申请");
		MemberOrder mo = new MemberOrder();
		mo.setId(jDBCIdGenerator.generateId());
		mo.setMenberType(menberType);
		mo.setPrice(Double.valueOf(price));
		mo.setValidMonth(validMonth);
		mo.setOrderState(OrderState.UNPAYED.getCode());
		mo.setOrderTime(new Date());
		mo.setUserId(u.getId());
		return res().data(memberGradeService.addBasicsMemberOrder(mo));
	}

	/**
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/delMemberOrder", method = RequestMethod.POST)
	public Result delMemberOrder(Long id) {
		return res().data(memberGradeService.delMemberOrder(id));
	}

	/**
	 * 获取专家联系手机
	 */
	@RequestMapping(value = "/contact", method = RequestMethod.GET)
	public Result contact(String uid) {
		User u = userService.selectByPrimaryKey(TokenManager.getUserId());
		Assert.isTrue(u != null && u.getLvl() > UserLevel.CERTIFIED.getCode(), "必须是登录实名用户");
		Assert.hasText(uid, "找不到专家");
		JSONObject business = memberGradeService.selectMemberBusiness(u.getLvl());
		Assert.isTrue(String.valueOf(AFTConstants.YES).equals(business.get(BusinessType.CHA_KAN_LIAN_XI.getKey())), "该会员不能查看专家联系方式");
		UserIdentity ui = userIdentityService.selectUserIdentityByUserId(uid);
		if (ui != null && StringUtils.isNotBlank(ui.getContactMobile())) {
			return res().data(ui.getContactMobile());
		}
		User expert = userService.selectByPrimaryKey(uid);
		Assert.notNull(expert, "找不到专家");
		return res().data(expert.getMobile());
	}
}
