package cn.pluss.platform.controller.merchantMenber;

import cn.jiguang.common.utils.StringUtils;
import cn.pluss.platform.controller.BaseNoModelController;
import cn.pluss.platform.entitiy.ShiroUser;
import cn.pluss.platform.entity.*;
import cn.pluss.platform.merchant.MerchantBaseInfoService;
import cn.pluss.platform.merchantMenber.MerchantMenberService;
import cn.pluss.platform.merchantMenberComsume.MerchantMenberComsumeService;
import cn.pluss.platform.merchantMenberRecharge.MerchantMenberRechargeService;
import cn.pluss.platform.merchantStore.MerchantStoreService;
import cn.pluss.platform.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("merchantMenber")
public class MerchantMenberController extends BaseNoModelController {

	private Integer PAGE_SIZE = 12;

	@Resource
	private HttpServletRequest request;
	
	@Autowired
	private MerchantMenberService merchantMenberService;

	@Autowired
	private MerchantMenberRechargeService merchantMenberRechargeService;

	@Autowired
	private MerchantMenberComsumeService merchantMenberComsumeService;
	
	@Autowired
	private MerchantBaseInfoService merchantBaseInfoService;
	
	@Autowired
	private MerchantStoreService merchantStoreService;

	/**
	 * 引导页
	 * 
	 * @return
	 */
	@GetMapping("/index")
	public String index() {
		return "merchantMenber/merchantMenberList";
	}
	/**
	 * 引导页
	 * 
	 * @return
	 */
	@GetMapping("/market")
	public String market() {
		return "merchantMenber/memberMarket";
	}
	/**
	 * 分页查询
	 */
	@RequestMapping(value = "/queryMerchantMenberByPage", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> queryMerchantMenberByPage(@RequestParam(defaultValue = "1") Integer currPage,MerchantMenber merchantMenber) {
		Map<String, Object> map = new HashMap<String, Object>(16);
		Map<String, Object> result = new HashMap<String, Object>(16);
		map.put("pageSize", PAGE_SIZE);
		map.put("offset", (currPage - 1) * PAGE_SIZE);
		// 是否是管理员
		Boolean bol = verifyUserRole();
		if(StringUtil.isNotEmpty(merchantMenber.getMemberName())) {
			map.put("nickName", merchantMenber.getMemberName());
		}
		if(StringUtil.isNotEmpty(merchantMenber.getPhone())) {
			map.put("phone", merchantMenber.getPhone());
		}
		ShiroUser shiroUser = queryCurrentShiroUser();
		// 如果不是管理员通过当前用户的merchantCode进行筛选数据
		if (!bol) {
			map.put("merchantCode", shiroUser.getMerchantCode());
			//通过进行权限控制
			if(StringUtil.isNotEmpty(shiroUser.getMerchantCode())) {
				result.put("merchant", "merchant");
			}
		}else {
			//通过admin是不是管理员进行操作权限控制
			result.put("admin", "admin");
		}
		if(StringUtil.isNotEmpty(merchantMenber.getId())) {
			map.put("id",merchantMenber.getId());
		}
		if(StringUtil.isNotEmpty(merchantMenber.getUserId())) {
			map.put("userId",merchantMenber.getUserId());
		}
		if(StringUtil.isNotEmpty(merchantMenber.getMemberName())) {
			map.put("memberName",merchantMenber.getMemberName());
		}
		if(StringUtils.isNotEmpty(merchantMenber.getAlias())) {
			map.put("alias",merchantMenber.getAlias());
		}
		List<MerchantMenber> merchantMenberList = null ;
		Integer count=0;
		//管理员查看的数据
		if (bol){
			merchantMenberList = merchantMenberService.manageQueryMerchantMenberPage(map);
			count = merchantMenberService.manageQueryMerchantMenberPageCount(map);
		}else {
			merchantMenberList = merchantMenberService.manageQueryMerchantNoMenberPage(map);
			count = merchantMenberService.manageQueryMerchantNoMenberPageCount(map);
		}
		result.put("merchantMenberList", merchantMenberList);
		result.put("count", count);
		result.put("pageCount", StringUtil.getPageCount(count, PAGE_SIZE));
		result.put("currPage", currPage);
		return result;
	}

	/**
	 * 查询详情
	 */
	@RequestMapping(value = "/queryMerchantMenberDetails", method = RequestMethod.GET)
	public String queryMerchantMenberDetails(HttpServletRequest request, Integer id, Integer pageIndex) {

		if (id != null && id != 0) {
			MerchantMenber merchantMenber = new MerchantMenber();
			merchantMenber.setId(id);
			merchantMenber = merchantMenberService.queryMerchantMenber(merchantMenber);
			request.setAttribute("merchantMenber", merchantMenber);
			request.setAttribute("pageIndex", pageIndex);
		}
		return "merchantMenber/merchantMenberDetails";
	}
	//其它用户查看详情
	@RequestMapping(value="/otherMerchantMenberDetails",method = RequestMethod.GET)
	public String otherMerchantMenberDetails(Integer id,HttpServletRequest request,Integer pageIndex) {
		if (id != null && id != 0) {
			MerchantMenber merchantMenber = new MerchantMenber();
			merchantMenber.setId(id);
			merchantMenber = merchantMenberService.queryMerchantMenber(merchantMenber);
			request.setAttribute("merchantMenber", merchantMenber);
			request.setAttribute("pageIndex", pageIndex);
		}
		return "merchantMenber/otherMerchantMenberDetails";
	}

	/**
	 * 查询详情
	 */
	@RequestMapping(value = "/queryMerchantMenberBaseInfo", method = RequestMethod.GET)
	public String queryMerchantMenberBaseInfo(HttpServletRequest request, Integer id, Integer pageIndex) {
		MerchantMenber merchantMenber = new MerchantMenber();
		if (id != null && id != 0) {
			merchantMenber.setId(id);
			merchantMenber = merchantMenberService.queryMerchantMenber(merchantMenber);
			request.setAttribute("merchantMenber", merchantMenber);
			request.setAttribute("pageIndex", pageIndex);
		}
		return "merchantMenber/merchantMenberBaseInfo";
	}

	/**
	 * 查询详情
	 */
	@RequestMapping(value = "/queryMerchantMenberExtendsInfo", method = RequestMethod.GET)
	public String queryMerchantMenberExtendsInfo() {
		return "merchantMenber/merchantMenberExtendsInfo";
	}

	/**
	 * 查询详情
	 */
	@RequestMapping(value = "/queryMerchantMenberFunctionsInfo", method = RequestMethod.GET)
	public String queryMerchantMenberFunctionsInfo() {

		return "merchantMenber/merchantMenberFunctionsInfo";
	}

	/**
	 * 保存对象
	 * 
	 * @throws IOException
	 */
	@RequestMapping(value = "/saveMerchantMenber", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> saveMerchantMenber(MerchantMenber merchantMenber) {
		//通过merchantCode查商户信息
    	MerchantBaseInfo merchantBaseInfo=new MerchantBaseInfo();
    	merchantBaseInfo.setMerchantCode(merchantMenber.getMerchantCode());
		List<MerchantBaseInfo> merchantBaseInfoList = merchantBaseInfoService.queryMerchantBaseInfoList(merchantBaseInfo);
		if(merchantBaseInfoList!=null&&merchantBaseInfoList.size()>0) {
			merchantMenber.setMerchantName(merchantBaseInfoList.get(0).getMerchantName());
		}
		//通过StoreId查门店信息
		MerchantStore merchantStore=new MerchantStore();
		merchantStore.setStoreId(merchantMenber.getStoreId());
		List<MerchantStore> merchantStoreList = merchantStoreService.queryMerchantStoreList(merchantStore);
		if(merchantStoreList!=null&&merchantStoreList.size()>0) {
			merchantMenber.setStoreName(merchantStoreList.get(0).getStoreName());
		}
		if (merchantMenber.getId() != null && merchantMenber.getId() != 0) {
			merchantMenberService.updateMerchantMenber(merchantMenber);
		} else {
			//生成会员编码
			merchantMenber.setMemberCode(StringUtil.getBillno());
			if(StringUtil.isEmpty(merchantMenber.getProfitMoney())) {
				merchantMenber.setProfitMoney(0d);
			}
			if(StringUtil.isEmpty(merchantMenber.getComsumeMoney())) {
				merchantMenber.setComsumeMoney(0d);
			}
			merchantMenberService.saveMerchantMenber(merchantMenber);
		}
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("resultCode", "1");
		result.put("resultMessage", "保存成功!");
		return result;
	}

	/**
	 * 修改对象
	 * 
	 * @throws IOException
	 */
	@RequestMapping(value = "/updateMerchantMenber", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updateMerchantMenber(Integer id) {

		MerchantMenber merchantMenber = new MerchantMenber();
		merchantMenber.setId(id);
		merchantMenberService.updateMerchantMenber(merchantMenber);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("resultCode", "1");
		result.put("resultMessage", "保存成功!");
		return result;
	}

	/**
	 * 删除对象
	 * 
	 * @throws IOException
	 */
	@RequestMapping(value = "/deleteMerchantMenber", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> deleteMerchantMenber(Integer id) {
		MerchantMenber merchantMenber = new MerchantMenber();
		merchantMenber.setId(id);
		merchantMenberService.deleteMerchantMenber(merchantMenber);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("resultCode", "1");
		result.put("resultMessage", "保存成功!");
		return result;
	}

	/**
	 * 到充值
	 * 
	 * @return
	 */
	@RequestMapping(value = "/showRechargeDetails", method = RequestMethod.GET)
	public String showRechargeDetails() {
		String memberCode = request.getParameter("memberCode");
		request.setAttribute("memberCode", memberCode);
		return "merchantMenber/showRechargeDetails";
	}

	@RequestMapping(value = "/rechargeDetails", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> rechargeDetails(@RequestParam(defaultValue = "1") Integer currPage,
			MerchantMenberRecharge menberRecharge) {
		Boolean bol = verifyUserRole();
		ShiroUser shiroUser = queryCurrentShiroUser();
		Map<String, Object> map = new HashMap<String, Object>(16);
		Map<String, Object> result = new HashMap<String, Object>(16);
		String memberCode = request.getParameter("memberCode");
		map.put("pageSize", PAGE_SIZE);
		map.put("offset", (currPage - 1) * PAGE_SIZE);
		map.put("memberCode", memberCode);
		if (StringUtil.isNotEmpty(menberRecharge.getNickName())) {
			map.put("nickName", menberRecharge.getNickName());
		}
		if (StringUtil.isNotEmpty(menberRecharge.getPhone())) {
			map.put("phone", menberRecharge.getPhone());
		}
		//如果不是管理员
		if (!bol) {
			if (StringUtil.isNotEmpty(shiroUser.getMerchantCode())) {
				map.put("merchantCode", shiroUser.getMerchantCode());
			}
		}
		if(StringUtil.isNotEmpty(menberRecharge.getId())) {
			map.put("id",menberRecharge.getId());
		}
		if(StringUtil.isNotEmpty(menberRecharge.getMerchantName())) {
			map.put("merchantName",menberRecharge.getMerchantName() );
		}
		if(StringUtil.isNotEmpty(menberRecharge.getOrderNumber())) {
			map.put("orderNumber",menberRecharge.getOrderNumber());
		}
		if(StringUtil.isNotEmpty(menberRecharge.getAlias())) {
			map.put("alias", menberRecharge.getAlias());
		}
		List<MerchantMenberRecharge> merchantMenberRechargeList = merchantMenberRechargeService
				.queryMerchantMenberRechargePage(map);
		Integer count = merchantMenberRechargeService.queryMerchantMenberRechargePageCount(map);
		result.put("merchantMenberRechargeList", merchantMenberRechargeList);
		result.put("count", count);
		result.put("pageCount", StringUtil.getPageCount(count, PAGE_SIZE));
		result.put("currPage", currPage);
		return result;
	}

	/**
	 * 到消费
	 * 
	 * @return
	 */
	@RequestMapping(value = "/showConsumptionDetails", method = RequestMethod.GET)
	public String showConsumptionDetails() {
		String memberCode = request.getParameter("memberCode");
		request.setAttribute("memberCode", memberCode);
		return "merchantMenber/showConsumptionDetails";
	}

	@RequestMapping(value = "/consumptionDetails", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> consumptionDetails(@RequestParam(defaultValue = "1") Integer currPage,
			MerchantMenberComsume menberComsume) {
		Boolean bol = verifyUserRole();
		ShiroUser shiroUser = queryCurrentShiroUser();
		Map<String, Object> map = new HashMap<String, Object>(16);
		Map<String, Object> result = new HashMap<String, Object>(16);
		String memberCode = request.getParameter("memberCode");
		map.put("pageSize", PAGE_SIZE);
		map.put("offset", (currPage - 1) * PAGE_SIZE);
		map.put("memberCode", memberCode);
		if (StringUtil.isNotEmpty(menberComsume.getNickName())) {
			map.put("nickName", menberComsume.getNickName());
		}
		if (StringUtil.isNotEmpty(menberComsume.getPhone())) {
			map.put("phone", menberComsume.getPhone());
		}
		//如果不是管理员
		if (!bol) {
			if (StringUtil.isNotEmpty(shiroUser.getMerchantCode())) {
				map.put("merchantCode", shiroUser.getMerchantCode());
			}
		}
		if(StringUtil.isNotEmpty(menberComsume.getOrderNumber())) {
			map.put("orderNumber",menberComsume.getOrderNumber());
		}
		if(StringUtil.isNotEmpty(menberComsume.getMerchantName())) {
			map.put("merchantName",menberComsume.getMerchantName());
		}
		if(StringUtil.isNotEmpty(menberComsume.getAlias())) {
			map.put("alias",menberComsume.getAlias());
		}
		List<MerchantMenberComsume> merchantMenberComsumeList = merchantMenberComsumeService
				.queryMerchantMenberComsumePage(map);
		Integer count = merchantMenberComsumeService.queryMerchantMenberComsumePageCount(map);
		result.put("merchantMenberComsumeList", merchantMenberComsumeList);
		result.put("count", count);
		result.put("pageCount", StringUtil.getPageCount(count, PAGE_SIZE));
		result.put("currPage", currPage);
		return result;
	}
	
	  /**
     * 是否是管理员
     */
    public void getRole() {
    	Boolean bol = verifyUserRole();
    	if(bol) {
			request.setAttribute("admin", "admin");
		}
    }
		    /**
		     *不是管理员 获取的权限  是商户
		*/
		public void getPermission() {
		ShiroUser user = queryCurrentShiroUser();
			if(StringUtil.isNotEmpty(user.getMerchantCode())) {
				request.setAttribute("merchant", "merchant");
			}
		}
    /**
     * 获取所有商户信息
     */
    public void getMerchant() {
    	List<MerchantBaseInfo> merchantBaseInfoList = merchantBaseInfoService.queryMerchantBaseInfoList(null);
    	request.setAttribute("merchantBaseInfoList", merchantBaseInfoList);
    }
    /**
     * 根据选择的商户code获取下面的门店
     */
    @RequestMapping(value="/getStoreByMerchantCode",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getStoreByMerchantCode(MerchantStore merchantStore) {
    	Map<String, Object> result = new HashMap<String, Object>(16);
		List<MerchantStore> merchantStoreList = merchantStoreService.queryMerchantStoreList(merchantStore);
		result.put("merchantStoreList", merchantStoreList);
		return result;
    }

}
