package com.iamberry.wechat.handles.flow;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

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.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.iamberry.wechat.core.entity.banner.MessageDTO;
import com.iamberry.wechat.core.entity.drp.PlaceRoleInfo;
import com.iamberry.wechat.core.entity.flow.TemplateFlowInfo;
import com.iamberry.wechat.core.entity.flow.TemplateInfo;
import com.iamberry.wechat.core.entity.product.ProductType;
import com.iamberry.wechat.face.drp.DrpRoleInfoService;
import com.iamberry.wechat.face.flow.TemplateFlowInfoService;
import com.iamberry.wechat.face.flow.TemplateInfoService;
import com.iamberry.wechat.face.porduct.ProductTypeService;

@Controller
@RequestMapping("/admin/templateFlow")
public class TemplateFlowInfoHandler {

	@Autowired
	private TemplateInfoService templateInfoService;
	
	@Autowired
	private TemplateFlowInfoService templateFlowInfoService;
	
	@Autowired
	private DrpRoleInfoService drpRoleInfoService;

	@Autowired
	private ProductTypeService productTypeService;
	/**
	 * 跳转到添加节点的页面
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/addTemplateFlow")
	public ModelAndView addTemplateFlow(HttpServletRequest request) throws Exception {
		ModelAndView mv = new ModelAndView("admin/flow/addTemplateFlowInfo");
		// 模板ID
		String templateId = request.getParameter("templateId");
		TemplateInfo templateInfo = new TemplateInfo();
		List<PlaceRoleInfo> roleList = drpRoleInfoService.getDrpRoleListByState();
		if (null != templateId && !"".equals(templateId)) {
			StringBuffer templateName = new StringBuffer();
			templateInfo.setTemplateId(Integer.parseInt(templateId));
			templateInfo.setState(1);
			templateInfo = templateInfoService.getTemplateInfoById(templateInfo);
			if (templateInfo == null) {
				mv = new ModelAndView("admin/msg/msg");
				mv.addObject("msgObj", new MessageDTO(false, "对不起，操作有误", "对不起，您刚才的操作流程有误，请按照系统流程操作！"));
				return mv;
			}
			for (PlaceRoleInfo placeRoleInfo : roleList) {
				templateName.append(placeRoleInfo.getRoleId() + ":" + placeRoleInfo.getRoleName() + "+");
			}
			templateInfo.setTemplateName(templateName.toString());
		}
		mv.addObject("templateInfo", templateInfo);
		
		// 只显示当前不存在的
		List<ProductType> types = productTypeService.getAll();
		List<Integer> integers = templateFlowInfoService.getTypeId(Integer.parseInt(templateId));
		if (integers != null && integers.size() != 0) {
			for (Integer integer : integers) {
				check(integer, types);
			}
		}
		mv.addObject("typeList", types);
		return mv;
	}
	
	/**
	 * 判断types中是否存在typeId,如果存在，删除types中对应数据
	 * @param typeId
	 * @param addList
	 * @param types
	 */
	public void check(int typeId, List<ProductType> types) {
		for (ProductType productType : types) {
			if (typeId == productType.getTypeId().intValue()) {
				types.remove(productType);
				return;
			}
		}
	}
	
	@RequestMapping("/addTemplateFlowInfo")
	public ModelAndView addTemplateFlowInfo(HttpServletRequest request) throws Exception {
		ModelAndView mv = new ModelAndView("admin/msg/msg");
		List<TemplateFlowInfo> flowList = getTemplateFlowInfo(request);
		Integer num = templateFlowInfoService.addTemplateFlowList(flowList);
		if (num > 0) {
			mv.addObject("msgObj", new MessageDTO(true, "恭喜您,操作成功", "返利节点已添加成功,刷新即可!"));
		} else {
			mv.addObject("msgObj", new MessageDTO(false, "对不起，操作有误", "对不起，您刚才的操作流程有误，请按规定填写数据！"));
		}
		return mv;
	}
	
	//封装返利金额添加入返利节点表中
	public List<TemplateFlowInfo> getTemplateFlowInfo(HttpServletRequest request) throws Exception{
		// 产品类型
		String[] types = request.getParameterValues("productType");
		if (types == null || types.length == 0) {
			return null;
		}
		
		// 待插入的数据
		List<TemplateFlowInfo> flowList = new ArrayList<TemplateFlowInfo>();

		// 返利层级
		int num = 0;
		String count = request.getParameter("count");
		String templateId = request.getParameter("templateId");
		if (null == templateId || "".equals(templateId)) {
			return null;
		}
		if (null != count && !"".equals(count)) {
			num = Integer.parseInt(count);
		}
		
		// 所有的角色
		List<PlaceRoleInfo> roleList = drpRoleInfoService.getDrpRoleListByState();
		int flowType = 0;
		for (String string : types) {
			for (int i = 0;i < num;i++) {
				String templateName = request.getParameter("templateName"+i+1);
				for (PlaceRoleInfo placeRoleInfo : roleList) {
					if (templateName.equals(placeRoleInfo.getRoleId().toString())) {
						flowType = placeRoleInfo.getRoleType();
					}
				}
				for (int j = 2;j <= 4;j++) {
					if (num + 2 - i - j > 0) {
						String flow = request.getParameter("flow"+i+j);
						TemplateFlowInfo flowInfo = new TemplateFlowInfo();
						flowInfo.setTemplateId(Integer.parseInt(templateId));
						//页面金额是纵坐标为2开始的，所以在计算层级的时候要加2
						flowInfo.setFlowLevel(num + 2 - i - j);
						flowInfo.setFlowSkipStep(1);
						flowInfo.setFlowMoney(Integer.parseInt(flow)*100);
						flowInfo.setFlowRoleId(Integer.parseInt(templateName));
						flowInfo.setFlowType(flowType);
						flowInfo.setFlowState(1);
						flowInfo.setFlowNo(i + "," + j);
						flowList.add(flowInfo);
						flowInfo.setFlowProductType(Integer.parseInt(string));
					}
				}
			}
		}
		return flowList;
	}
	
	/**
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/updateTemplateFlow")
	public ModelAndView updateTemplateFlow(HttpServletRequest request) throws Exception {
		ModelAndView mv = new ModelAndView("admin/flow/updateTemplateFlowInfo");
		String templateId = request.getParameter("templateId");
		if (null == templateId || "".equals(templateId)) {
			return null;
		}
		mv.addObject("templateId", templateId);
		// 只显示当前不存在的
		List<ProductType> types = productTypeService.getAll();
		List<Integer> integers = templateFlowInfoService.getTypeId(Integer.parseInt(templateId));
		List<ProductType> list = new ArrayList<ProductType>();
		// 只显示当前存在的
		for (Integer integer : integers) {
			list.add(checkE(integer, types));
		}
		mv.addObject("types", list);
		return mv;
	}
	
	/**
	 * 判断当前typeId是否存在，如果存在返回
	 * @param typeId
	 * @param types
	 */
	public ProductType checkE(int typeId, List<ProductType> types) {
		for (ProductType productType : types) {
			if (productType.getTypeId().intValue() == typeId) {
				types.remove(productType);
				return productType;
			}
		}
		return null;
	}
	
	@ResponseBody
	@RequestMapping("/getFlowInfo")
	public Map<String,Object> getFlowInfo(HttpServletRequest request) {
		String templateId = request.getParameter("templateId");
		if (null == templateId || "".equals(templateId)) {
			return null;
		}
		List<TemplateFlowInfo> flowList = templateFlowInfoService.getFlowInfoListByTemplateId(Integer.parseInt(templateId));
		if (null == flowList || flowList.size() <= 0) {
			return null;
		}
		StringBuilder templateName = new StringBuilder();
		List<PlaceRoleInfo> roleList = drpRoleInfoService.getDrpRoleListByState();
		//获取所有角色信息，并将其拼接为字符串
		for (PlaceRoleInfo placeRoleInfo : roleList) {
			templateName.append(placeRoleInfo.getRoleId() + ":" + placeRoleInfo.getRoleName() + "+");
		}
		Map<String,Object> flowInfoMap = new HashMap<String,Object>();
		flowInfoMap.put("templateName", templateName.toString());
		
		// 查询
		TemplateInfo templateInfo = new TemplateInfo();
		templateInfo.setTemplateId(Integer.parseInt(templateId));
		templateInfo = templateInfoService.getTemplateInfoById(templateInfo);
		flowInfoMap.put("count", templateInfo.getCount());

		// 根据产品类型分段
		List<Integer> integers = templateFlowInfoService.getTypeId(Integer.parseInt(templateId));
		for (Integer integer : integers) {
			Map<String, Object> flowNotType = new HashMap<String, Object>();
			//将查询出的返利节点所有的返利金额存放在map中
			for (TemplateFlowInfo templateFlowInfo : flowList) {
				if (templateFlowInfo.getFlowProductType().intValue() == integer.intValue()) {
					String flowNo = templateFlowInfo.getFlowNo();
					if (null != flowNo && !"".equals(flowNo)) {
						String i = flowNo.split(",")[0];
						String j = flowNo.split(",")[1];
						flowNotType.put("templateName"+i+1, templateFlowInfo.getFlowRoleId());
						flowNotType.put("flow"+i+j, templateFlowInfo.getFlowMoney());
					}
				}
			}
			flowInfoMap.put(integer.toString(), flowNotType);
		}
		return flowInfoMap;
	}
	
	@RequestMapping("/updateTemplateFlowInfo")
	public ModelAndView updateTemplateFlowInfo(HttpServletRequest request) throws Exception {
		ModelAndView mv = new ModelAndView("admin/msg/msg");
		List<TemplateFlowInfo> flowList = getTemplateFlowInfo(request);
		Integer num = templateFlowInfoService.updateTemplateFlowInfo(flowList);
		if (num > 0) {
			mv.addObject("msgObj", new MessageDTO(true, "恭喜您,操作成功", "返利节点已修改成功,刷新即可!"));
		} else {
			mv.addObject("msgObj", new MessageDTO(false, "对不起，操作有误", "对不起，您刚才的操作流程有误，请按规定填写数据！"));
		}
		return mv;
	}
}
