/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/yogee/zhebuy">JeeSite</a> All rights reserved.
 */
package com.yogee.zhebuy.modules.ebuy.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.yogee.zhebuy.common.utils.DateUtils;
import com.yogee.zhebuy.common.weixin.web.TemplateMsg;
import com.yogee.zhebuy.modules.ebuy.entity.EbuyBusiness;
import com.yogee.zhebuy.modules.ebuy.entity.EbuyProCategory;
import com.yogee.zhebuy.modules.ebuy.service.EbuyBusinessService;
import com.yogee.zhebuy.modules.ebuy.service.EbuyLinkShopServicerService;
import com.yogee.zhebuy.modules.ebuy.service.EbuyProCategoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.yogee.zhebuy.common.config.Global;
import com.yogee.zhebuy.common.persistence.Page;
import com.yogee.zhebuy.common.web.BaseController;
import com.yogee.zhebuy.common.utils.StringUtils;
import com.yogee.zhebuy.modules.ebuy.entity.EbuyServicerCoupon;
import com.yogee.zhebuy.modules.ebuy.service.EbuyServicerCouponService;

import java.util.List;

/**
 * 优惠券Controller
 * @author sunye
 * @version 2017-08-14
 */
@Controller
@RequestMapping(value = "${adminPath}/ebuy/ebuyServicerCoupon")
public class EbuyServicerCouponController extends BaseController {

	@Autowired
	private EbuyServicerCouponService ebuyServicerCouponService;
	@Autowired
	private EbuyBusinessService ebuyBusinessService;
	@Autowired
	private EbuyProCategoryService ebuyProCategoryService;
	@Autowired
	private EbuyLinkShopServicerService ebuyLinkShopServicerService;

	@ModelAttribute
	public EbuyServicerCoupon get(@RequestParam(required=false) String id) {
		EbuyServicerCoupon entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = ebuyServicerCouponService.get(id);
		}
		if (entity == null){
			entity = new EbuyServicerCoupon();
		}
		return entity;
	}
	
	@RequiresPermissions("ebuy:ebuyServicerCoupon:view")
	@RequestMapping(value = {"list", ""})
	public String list(EbuyServicerCoupon ebuyServicerCoupon, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<EbuyServicerCoupon> page = ebuyServicerCouponService.findPage(new Page<EbuyServicerCoupon>(request, response), ebuyServicerCoupon);
		List<EbuyProCategory> ebuyProCategories =  ebuyProCategoryService.findList(new EbuyProCategory());
		model.addAttribute("ebuyProCategories", ebuyProCategories);
		model.addAttribute("page", page);
		return "modules/ebuy/ebuyServicerCouponList";
	}

	@RequiresPermissions("ebuy:ebuyServicerCoupon:view")
	@RequestMapping(value = "form")
	public String form(EbuyServicerCoupon ebuyServicerCoupon, Model model) {
		List<EbuyBusiness> ebuyBusinessList =  ebuyBusinessService.findTypeOne("1");
		List<EbuyProCategory> categoryList = ebuyProCategoryService.getCategory("1","0");
//		List<EbuyProCategory> ebuyProCategories =  ebuyProCategoryService.findList(new EbuyProCategory());
		model.addAttribute("ebuyServicerCoupon", ebuyServicerCoupon);
		model.addAttribute("ebuyProCategories", categoryList);
		model.addAttribute("ebuyBusinessList", ebuyBusinessList);
		return "modules/ebuy/ebuyServicerCouponForm";
	}

	@RequiresPermissions("ebuy:ebuyServicerCoupon:edit")
	@RequestMapping(value = "save")
	public String save(EbuyServicerCoupon ebuyServicerCoupon, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, ebuyServicerCoupon)){
			return form(ebuyServicerCoupon, model);
		}
		if(ebuyServicerCoupon.getType().equals("0")){//平台发放
			ebuyServicerCoupon.setServicerId(null);
		}else{//服务商发放
			ebuyServicerCoupon.setCityId(null);
		}
		if(Integer.parseInt(ebuyServicerCoupon.getReceiveCount())<Integer.parseInt(ebuyServicerCoupon.getRemainCount())){
			addMessage(redirectAttributes, "保存失败，剩余数量不能大于发放数量");
			return "redirect:"+Global.getAdminPath()+"/ebuy/ebuyServicerCoupon/?repage";
		}
		ebuyServicerCouponService.save(ebuyServicerCoupon);
		addMessage(redirectAttributes, "保存优惠券成功");
		//推送能领取优惠券商家
		String serviceId = ebuyServicerCoupon.getServicerId().toString();
		List<String> businessIds = ebuyLinkShopServicerService.findBusinessByServiceId(serviceId);
		for (String businessId : businessIds){
			EbuyBusiness business = ebuyBusinessService.get(businessId);
			if (StringUtils.isNotEmpty(business.getWxOpenid())){
				TemplateMsg.pushNewCoupon(business.getWxOpenid(), "您有一个待领取的优惠券", "嫩菜优惠活动", "嫩菜商家用户", DateUtils.formatDate(ebuyServicerCoupon.getDateStart(), "yyyy-MM-dd HH:mm:ss")+"至"+DateUtils.formatDate(ebuyServicerCoupon.getDateStop()), "请您尽快领取，以免过期", Global.getConfig("domain.web") + "/#/indexCoupon");
			}
		}

		return "redirect:"+Global.getAdminPath()+"/ebuy/ebuyServicerCoupon/?repage";
	}
	
	@RequiresPermissions("ebuy:ebuyServicerCoupon:edit")
	@RequestMapping(value = "delete")
	public String delete(EbuyServicerCoupon ebuyServicerCoupon, RedirectAttributes redirectAttributes) {
		ebuyServicerCouponService.delete(ebuyServicerCoupon);
		addMessage(redirectAttributes, "删除优惠券成功");
		return "redirect:"+Global.getAdminPath()+"/ebuy/ebuyServicerCoupon/?repage";
	}

}