package cn.pluss.platform.controller.activityRecommend;

import cn.pluss.platform.activityRecommend.ActivityRecommendService;
import cn.pluss.platform.controller.BaseNoModelController;
import cn.pluss.platform.entitiy.ShiroUser;
import cn.pluss.platform.entity.ActivityRecommend;
import cn.pluss.platform.entity.MerchantBaseInfo;
import cn.pluss.platform.entity.MerchantStore;
import cn.pluss.platform.merchant.MerchantBaseInfoService;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

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

@Controller
@RequestMapping("activityRecommend")
public class ActivityRecommendController extends BaseNoModelController {

	private Integer PAGE_SIZE = 12;

	@Autowired
	private HttpServletRequest request;

	@Autowired
	private MerchantBaseInfoService merchantBaseInfoService;

	@Autowired
	private MerchantStoreService merchantStoreService;

	@Autowired
	private ActivityRecommendService activityRecommendService;

	/**
	 * 引导页
	 * 
	 * @return
	 */
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String index() {
		return "activityRecommend/activityRecommendList";
	}

	/**
	 * 分页查询
	 */
	@RequestMapping(value = "/queryActivityRecommendByPage", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> queryActivityRecommendByPage(@RequestParam(defaultValue = "1") Integer currPage,
			ActivityRecommend activityRecommend) {
		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 (!bol) {
			ShiroUser shiroUser = queryCurrentShiroUser();
			if(StringUtil.isNotEmpty(shiroUser.getMerchantCode())) {
				map.put("merchantCode", shiroUser.getMerchantCode());
			}else {
				map.put("merchantCode", StringUtil.genRandomNum(33));
			}
		}
		if (StringUtil.isNotEmpty(activityRecommend.getName())) {
			map.put("name", activityRecommend.getName());
		}
		List<ActivityRecommend> activityRecommendList = activityRecommendService.queryActivityRecommendPage(map);
		Integer count = activityRecommendService.queryActivityRecommendPageCount(map);
		result.put("activityRecommendList", activityRecommendList);
		result.put("count", count);
		result.put("pageCount", StringUtil.getPageCount(count, PAGE_SIZE));
		result.put("currPage", currPage);
		return result;
	}

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

		if (id != null && id != 0) {
			ActivityRecommend activityRecommend = new ActivityRecommend();
			activityRecommend.setId(id);
			activityRecommend = activityRecommendService.queryActivityRecommend(activityRecommend);
			request.setAttribute("activityRecommend", activityRecommend);
			request.setAttribute("pageIndex", pageIndex);
		}
		return "activityRecommend/activityRecommendDetails";
	}

	/**
	 * 查询详情
	 */
	@RequestMapping(value = "/queryActivityRecommendBaseInfo", method = RequestMethod.GET)
	public String queryActivityRecommendBaseInfo(HttpServletRequest request, Integer id, Integer pageIndex) {
		// 是否是管理员
		Boolean bol = verifyUserRole();
		if (!bol) {
			ShiroUser user = queryCurrentShiroUser();
			if (StringUtil.isNotEmpty(user.getMerchantCode())) {
				MerchantStore merchantStore = new MerchantStore();
				merchantStore.setMerchantCode(user.getMerchantCode());
				List<MerchantStore> merchantStoreList = merchantStoreService.queryMerchantStoreList(merchantStore);
				request.setAttribute("merchantStoreList", merchantStoreList);
			}
		}

		if (id != null && id != 0) {
			ActivityRecommend activityRecommend = new ActivityRecommend();
			activityRecommend.setId(id);
			activityRecommend = activityRecommendService.queryActivityRecommend(activityRecommend);
			request.setAttribute("activityRecommend", activityRecommend);
			request.setAttribute("pageIndex", pageIndex);
			if (bol) {
				MerchantStore merchantStore = new MerchantStore();
				merchantStore.setMerchantCode(activityRecommend.getMerchantCode());
				List<MerchantStore> merchantStoreList = merchantStoreService.queryMerchantStoreList(merchantStore);
				request.setAttribute("merchantStoreList", merchantStoreList);
			}
		}
		getMerchant();
		return "activityRecommend/activityRecommendBaseInfo";
	}

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

		return "activityRecommend/activityRecommendExtendsInfo";
	}

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

		return "activityRecommend/activityRecommendFunctionsInfo";
	}

	/**
	 * 保存对象
	 * 
	 * @throws IOException
	 */
	@RequestMapping(value = "/saveActivityRecommend", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> saveActivityRecommend(ActivityRecommend activityRecommend) {
		Map<String, Object> result = new HashMap<String, Object>(16);
		// 通过merchantCode查商户信息
		MerchantBaseInfo merchantBaseInfo = new MerchantBaseInfo();
		merchantBaseInfo.setMerchantCode(activityRecommend.getMerchantCode());
		if (StringUtil.isNotEmpty(activityRecommend.getMerchantCode())) {
			List<MerchantBaseInfo> merchantBaseInfoList = merchantBaseInfoService
					.queryMerchantBaseInfoList(merchantBaseInfo);
			if (merchantBaseInfoList != null && merchantBaseInfoList.size() > 0) {
				activityRecommend.setMerchantName(merchantBaseInfoList.get(0).getMerchantName());
			}
		}
		// 通过StoreId查门店信息
		MerchantStore merchantStore = new MerchantStore();
		merchantStore.setStoreId(activityRecommend.getStoreId());
		if (StringUtil.isNotEmpty(activityRecommend.getStoreId())) {
			List<MerchantStore> merchantStoreList = merchantStoreService.queryMerchantStoreList(merchantStore);
			if (merchantStoreList != null && merchantStoreList.size() > 0) {
				activityRecommend.setStoreName(merchantStoreList.get(0).getStoreName());
			}
		}
		if (activityRecommend.getId() != null && activityRecommend.getId() != 0) {
			activityRecommendService.updateActivityRecommend(activityRecommend);
		} else {
			ShiroUser shiroUser = queryCurrentShiroUser();
			if (StringUtil.isNotEmpty(shiroUser)) {
				activityRecommend.setUserId(shiroUser.getId().intValue());
			}
			activityRecommendService.saveActivityRecommend(activityRecommend);
		}
		result.put("resultCode", "1");
		result.put("resultMessage", "保存成功!");
		return result;
	}

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

		ActivityRecommend activityRecommend = new ActivityRecommend();
		activityRecommend.setId(id);
		activityRecommendService.updateActivityRecommend(activityRecommend);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("resultCode", "1");
		result.put("resultMessage", "保存成功!");
		return result;
	}

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

	/**
	 * 获取所有商户信息
	 */
	public void getMerchant() {
		List<MerchantBaseInfo> merchantBaseInfoList = merchantBaseInfoService.queryMerchantBaseInfoList(null);
		request.setAttribute("merchantBaseInfoList", merchantBaseInfoList);
	}

}
