package cn.pluss.platform.controller.activityActivate;

import cn.pluss.platform.activityActivate.ActivityActivateService;
import cn.pluss.platform.controller.BaseNoModelController;
import cn.pluss.platform.entitiy.ShiroUser;
import cn.pluss.platform.entity.ActivityActivate;
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;

/**
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("activityActivate")
public class ActivityActivateController extends BaseNoModelController {

	private Integer PAGE_SIZE = 12;

	@Autowired
	private HttpServletRequest request;

	@Autowired
	private ActivityActivateService activityActivateService;

	@Autowired
	private MerchantBaseInfoService merchantBaseInfoService;

	@Autowired
	private MerchantStoreService merchantStoreService;

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

	/**
	 * 分页查询
	 */
	@RequestMapping(value = "/queryActivityActivateByPage", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> queryActivityActivateByPage(@RequestParam(defaultValue = "1") Integer currPage,
			String name) {
		Map<String, Object> map = new HashMap<>(16);
		Map<String, Object> result = new HashMap<>(16);
		Boolean bol = verifyUserRole();
		// 是否是管理员进行数据筛选和权限标记
		if (bol) {
			result.put("admin", "admin");
		} else {
			ShiroUser shiroUser = queryCurrentShiroUser();
			if (StringUtil.isNotEmpty(shiroUser.getMerchantCode())) {
				result.put("merchant", "merchant");
				map.put("merchantCode", shiroUser.getMerchantCode());
			} else {
				map.put("merchantCode", StringUtil.genRandomNum(33));
			}
		}
		map.put("pageSize", PAGE_SIZE);
		map.put("offset", (currPage - 1) * PAGE_SIZE);
		map.put("name", name);
		List<ActivityActivate> activityActivateList = null;
		Integer count = 0 ;
		if (bol){
			activityActivateList = activityActivateService.queryActivityActivatePage(map);
			count = activityActivateService.queryActivityActivatePageCount(map);
		}else{
			activityActivateList = activityActivateService.queryNoActivityActivatePage(map);
			count = activityActivateService.queryNoActivityActivatePageCount(map);
		}
		result.put("activityActivateList", activityActivateList);
		result.put("count", count);
		result.put("pageCount", StringUtil.getPageCount(count, PAGE_SIZE));
		result.put("currPage", currPage);
		return result;
	}

	/**
	 * 查询详情
	 */
	@RequestMapping(value = "/queryActivityActivateDetails", method = RequestMethod.GET)
	public String queryActivityActivateDetails(HttpServletRequest request, Integer id, Integer pageIndex) {
		if (id != null && id != 0) {
			ActivityActivate activityActivate = new ActivityActivate();
			activityActivate.setId(id);
			activityActivate = activityActivateService.queryActivityActivate(activityActivate);
			request.setAttribute("activityActivate", activityActivate);
			request.setAttribute("pageIndex", pageIndex);
		}
		return "activityActivate/activityActivateDetails";
	}

	/**
	 * 其他用户查询详情
	 */
	@RequestMapping(value = "/otherActivityActivateDetails", method = RequestMethod.GET)
	public String otherActivityActivateDetails(HttpServletRequest request, Integer id, Integer pageIndex) {
		if (id != null && id != 0) {
			ActivityActivate activityActivate = new ActivityActivate();
			activityActivate.setId(id);
			activityActivate = activityActivateService.queryActivityActivate(activityActivate);
			request.setAttribute("activityActivate", activityActivate);
			request.setAttribute("pageIndex", pageIndex);
		}
		return "activityActivate/otherActivityActivateDetails";
	}

	/**
	 * 查询详情
	 */
	@RequestMapping(value = "/queryActivityActivateBaseInfo", method = RequestMethod.GET)
	public String queryActivityActivateBaseInfo(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.queryMerchantNoStoreList(merchantStore);
				request.setAttribute("merchantStoreList", merchantStoreList);
			}
		}
		if (id != null && id != 0) {
			ActivityActivate activityActivate = new ActivityActivate();
			activityActivate.setId(id);
			activityActivate = activityActivateService.queryActivityActivate(activityActivate);
			request.setAttribute("activityActivate", activityActivate);
			request.setAttribute("pageIndex", pageIndex);
			// 如果是管理员
			if (bol) {
				MerchantStore merchantStore = new MerchantStore();
				merchantStore.setMerchantCode(activityActivate.getMerchantCode());
				List<MerchantStore> merchantStoreList = merchantStoreService.queryMerchantStoreList(merchantStore);
				request.setAttribute("merchantStoreList", merchantStoreList);
			}
		}
		getRole();
		getPermission();
		getMerchant();
		return "activityActivate/activityActivateBaseInfo";
	}

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

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

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

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

		ActivityActivate activityActivate = new ActivityActivate();
		activityActivate.setId(id);
		activityActivateService.updateActivityActivate(activityActivate);
		Map<String, Object> result = new HashMap<String, Object>(16);
		result.put("resultCode", "1");
		result.put("resultMessage", "保存成功!");
		return result;
	}

	/**
	 * 删除对象
	 * 
	 * @throws IOException
	 */
	@RequestMapping(value = "/deleteActivityActivate", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> deleteActivityActivate(Integer id) {
		ActivityActivate activityActivate = new ActivityActivate();
		activityActivate.setId(id);
		activityActivateService.deleteActivityActivate(activityActivate);
		Map<String, Object> result = new HashMap<String, Object>(16);
		result.put("resultCode", "1");
		result.put("resultMessage", "保存成功!");
		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);
	}

}
