package com.goafanti.member.controller;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.JSON;
import com.goafanti.common.bo.Error;
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.model.MemberOrder;
import com.goafanti.common.utils.DateUtils;
import com.goafanti.common.utils.LoggerUtils;
import com.goafanti.member.bo.MemberGrade;
import com.goafanti.member.bo.MemberGradeBO;
import com.goafanti.member.service.MemberGradeService;
import com.goafanti.member.service.MemberOrderService;

/**
 * 会员等级管理 后台
 * 
 * @author Administrator
 *
 */
@RestController
@RequestMapping(value = "/api/admin/member")
public class AdminMemberController extends BaseApiController {
	@Autowired
	private MemberGradeService	memberGradeService;
	@Autowired
	private MemberOrderService	memberOrderService;

	/**
	 * 查询全部信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/selectMemberGrade", method = RequestMethod.GET)
	public Result selectMemberGrade() {
		List<MemberGrade> list = memberGradeService.selectMemberGrade();
		List<MemberGradeBO> result = new ArrayList<>();
		for (MemberGrade mg : list) {
			result.add(new MemberGradeBO(mg.getId(), mg.getGradeName(), JSON.parseObject(mg.getBusiness()),
					JSON.parseObject(mg.getCost()), mg.getMemberRank()));
		}
		return res().data(result);
	}

	/**
	 * 删除会员等级
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/delGrade", method = RequestMethod.POST)
	public Result delGrade(int id) {
		return res().data(memberGradeService.delGrade(id));
	}

	/**
	 * 新增会员等级名称
	 * 
	 * @param name
	 * @return
	 */
	@RequestMapping(value = "/addGradeName", method = RequestMethod.POST)
	public Result addGradeName(MemberGrade me) {
		if (memberGradeService.selectRank(me) >= 1) {
			Error e = new Error();
			e.setMessage("该会员等级已存在");
			return res().error(e);
		}
		memberGradeService.addGradeName(me);
		return res().data(me.getId());
	}

	/**
	 * 增加业务及费用 与更改
	 * 
	 * @param me
	 * @return
	 */
	@RequestMapping(value = "/addBusinessAndCost", method = RequestMethod.POST)
	public Result addBusinessAndCost(MemberGrade me) {
		return res().data(memberGradeService.addBusinessAndCost(me));
	}

	/**
	 * 后台会员订单列表单
	 * 
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value = "/selectMemberOrderBa", method = RequestMethod.GET)
	public Result selectMemberOrderBa(String pageNo, String pageSize, Long id, String mobile, String nickname,
			Integer orderState, String startTime, String endTime) {
		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", id);
		map.put("mobile", mobile);
		map.put("nickname", nickname);
		map.put("orderState", orderState);
		map.put("startTimes", startTime);
		if (endTime != null) {
			try {
				map.put("endTimes", DateUtils.addDays(DateUtils.parseDate(endTime, AFTConstants.YYYYMMDD), 1));
			} catch (ParseException e) {
				LoggerUtils.error(this.getClass(), "ffff", e);
			}
		}
		List<MemberOrder> list = memberGradeService.selectMemberOrderBa(map);
		int countWhere = memberGradeService.countMemberOrderWhere(map);
		Map<String, Object> maps = new HashMap<>();
		maps.put("list", list);
		maps.put("countWhere", countWhere);
		maps.put("pNo", pNo);
		return res().data(maps);
	}

	/**
	 * 后台会员更改
	 * 
	 * @param id
	 * @param memberRank
	 * @param validMonth
	 * @param paymentValue
	 * @param orderState
	 * @param paymentTime
	 * @param paymentType
	 * @return
	 */
	@RequestMapping(value = "/updateMemberOrder", method = RequestMethod.POST)
	public Result updateMemberOrder(Long id, String memberRank, String validMonth, String paymentValue,
			String orderState, String paymentTime, String paymentType, String userId, String paymentId) {
		MemberOrder mo = memberOrderService.getById(id);
		Assert.notNull(mo, "找不到订单");
		mo.setOrderState(OrderState.getStatus(orderState).getCode());
		try {
			Integer month = Integer.parseInt(validMonth);
			mo.setValidMonth(month);
			mo.setMenberType(Integer.parseInt(memberRank));
			if (OrderState.isPayed(OrderState.getStatus(mo.getOrderState()))) {
				Assert.hasText(paymentId, "已支付订单信息不完整");
				Assert.hasText(paymentType, "已支付订单信息不完整");
				Assert.hasText(paymentTime, "已支付订单信息不完整");
				mo.setPaymentId(paymentId);
				mo.setPaymentType(paymentType);
				mo.setPaymentValue(Double.parseDouble(paymentValue));
				Date payTime = DateUtils.parseDate(paymentTime, AFTConstants.YYYYMMDDHHMMSS);
				Date validTime = DateUtils.addMonths(payTime, month);
				mo.setValidTime(validTime);
				mo.setPaymentTime(payTime);
			}
		} catch (ParseException e) {
			return res().error(buildError("", "参数错误"));
		}
		return res().data(memberOrderService.update(mo));
	}

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

}
