package com.sino.rrs.action;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sino.publicclass.base.BaseAction;
import com.sino.publicclass.base.CException;
import com.sino.publicclass.database.ConnectionPool;
import com.sino.rrs.business.WeixinSignatureThread;
import com.sino.rrs.entity.RcNewAreaPO;
import com.sino.rrs.entity.SendInformation;
import com.sino.rrs.oper.RcNewAreaOper;
import com.sino.rrs.oper.RcRescueCompanyOper;
import com.sino.rrs.oper.RcRescueManagerOper;
import com.sino.weixin.oauth.util.Constants;
import com.sino.weixin.oauth.util.HttpsUtil;

public class QueryAreaSupplierAction extends BaseAction{
	private SendInformation sendPo=new SendInformation();
	//查看某个区域经理管理的省份
	public void getProvinceByLoginName(){
		JSONObject jsonObject=new JSONObject();
		Connection conn=null;
		String loginName=getParameter("loginName");
		jsonObject.put("loginName", loginName);
		jsonObject.put("action", 18);
		
		JSONObject jsonList=JSON.parseObject(HttpsUtil.doPost(Constants.url, jsonObject,"UTF-8"));
		String managerArea = (String) jsonList.get("managerArea"); 
		if(null!=managerArea){
			try {
				conn=ConnectionPool.getConnection();
				RcNewAreaOper oper=new RcNewAreaOper(conn);
				List<RcNewAreaPO> provinceList = oper.queryPrivince(managerArea.toString());
				jsonObject.put("provinceList", provinceList);
				response.setContentType("text/html;charset=UTF-8");
				response.getWriter().print(jsonObject.toJSONString());
				setNext(false);
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	//查询省下的市
	public void getAllCityByParentid(){
		JSONObject jsonObject=new JSONObject();
		int areaid=Integer.parseInt(getParameter("areaid"));
		Connection conn=null;
		try {
			conn=ConnectionPool.getConnection();
			RcNewAreaOper oper=new RcNewAreaOper(conn);
			List<RcNewAreaPO> provinceCityList = oper.queryPrivinceCity(areaid);
			jsonObject.put("provinceCityList", provinceCityList);
			response.setContentType("text/html;charset=UTF-8");
			response.getWriter().print(jsonObject.toJSONString());
			setNext(false);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	//查询大区经理下管理的所有服务商并发送模板消息
	public void getAllSuppliersByLoginName(){
		JSONObject jsonObject=new JSONObject();
		JSONObject json=new JSONObject();
		boolean sendFlag=false;
		String loginName=getParameter("loginName");
		String title=getParameter("title");
		String content=getParameter("content");
		
		jsonObject.put("loginName", loginName);
		jsonObject.put("action", 21);
		
		List<String> rescueCodes =new ArrayList<String>();
		JSONObject jsonList=JSON.parseObject(HttpsUtil.doPost(Constants.url, jsonObject,"UTF-8"));
		List<Map<String,String>>provinceSupplierList = (List<Map<String, String>>) jsonList.get("provinceSupplierList");
		if(null!=provinceSupplierList&&provinceSupplierList.size()>0){
			for(Map<String,String> supplierMap:provinceSupplierList){
				String rescueCode=supplierMap.get("code");
				rescueCodes.add(rescueCode);
			}
		}else{
			rescueCodes.add(null);
		}
		Connection conn=null;
		try {
			conn=ConnectionPool.getConnection();
			RcNewAreaOper oper=new RcNewAreaOper(conn);
			
			List<Map<String,String>> suppliers = oper.queryPrivinceCitySupplier(rescueCodes.toString());
			//发送模板消息
			for(Map<String,String> supplier:suppliers){
				int result=sendPo.sendInformation(title, content, supplier.get("open_id"));
				if(result == 40001){
					WeixinSignatureThread signature = new WeixinSignatureThread();
					signature.bulidApiAccessToken();
				    result = sendPo.sendInformation(title, content, supplier.get("open_id"));
				}
				if(result==0){
					sendFlag = true;
				}
			}
			json.put("code", sendFlag);
			response.setContentType("text/html;charset=UTF-8");
			response.getWriter().print(json.toJSONString());
			setNext(false);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	//查询区域经理下-->市--》的供应商
	public void getSuppliersByParentid(){
		JSONObject jsonObject=new JSONObject();
		JSONObject json=new JSONObject();
		String areaCode=getParameter("city_area_code");  //市编码
		String loginName=getParameter("loginName");
		
		jsonObject.put("loginName", loginName);
		jsonObject.put("areaCode", areaCode);
		jsonObject.put("action", 19);
		
		List<String> rescueCodes =new ArrayList<String>();
		JSONObject jsonList=JSON.parseObject(HttpsUtil.doPost(Constants.url, jsonObject,"UTF-8"));
		List<Map<String,String>>supplierList = (List<Map<String, String>>) jsonList.get("supplierList");
		if(null!=supplierList&&supplierList.size()>0){
			for(Map<String,String> supplierMap:supplierList){
				String rescueCode=supplierMap.get("code");
				rescueCodes.add(rescueCode);
			}
		}else{
			rescueCodes.add(null);
		}
		Connection conn=null;
		try {
			conn=ConnectionPool.getConnection();
			RcRescueCompanyOper oper=new RcRescueCompanyOper(conn);
			List<Map<String,String>> suppliers=oper.queryPrivinceCitySupplierName(rescueCodes.toString());
			json.put("suppliers", suppliers);
			response.setContentType("text/html;charset=UTF-8");
			response.getWriter().print(json.toJSONString());
			setNext(false);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	//查询供应商对应的大区经理并发送模板消息
	public void getManagerByRescueCode(){
		JSONObject jsonObject=new JSONObject();
		JSONObject json=new JSONObject();
		Connection conn=null;
		boolean sendFlag=false;
		String rescueCode=getParameter("rescueCode");
		String title=getParameter("title");
		String content=getParameter("content");
		
		jsonObject.put("rescueCode", rescueCode);
		jsonObject.put("action", 20);
		
		JSONObject jsonList=JSON.parseObject(HttpsUtil.doPost(Constants.url, jsonObject,"UTF-8"));
		String managerLoginName = (String) jsonList.get("managerLoginName"); 
		if(null!=managerLoginName){
			try {
				conn=ConnectionPool.getConnection();
				RcRescueManagerOper managerOper=new RcRescueManagerOper(conn);
				List<String> managerList = managerOper.queryManager(managerLoginName);
				if(null!=managerList&&managerList.size()>0){
					for(int i=0;i<managerList.size();i++){
						//发送模板消息
						System.out.println(managerList.get(i));
						int result=sendPo.sendInformation(title, content, managerList.get(i));
						if(result == 40001){
							WeixinSignatureThread signature = new WeixinSignatureThread();
							signature.bulidApiAccessToken();
						    result = sendPo.sendInformation(title, content, managerList.get(i));
						}
						if(result==0){
							sendFlag = true;
						}
					}
				}
				json.put("code", sendFlag);
				response.setContentType("text/html;charset=UTF-8");
				response.getWriter().print(json.toJSONString());
				setNext(false);
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public void sendInformation() throws IOException{
		JSONObject json=new JSONObject();
		boolean sendFlag=false;
		List<String> openIdList=new ArrayList<String>();
		
		String title=getParameter("title");
		String content=getParameter("content");
		String rescueCodes=getParameter("rescueCodes").toString();
		
		Connection conn=null;
		try {
			conn=ConnectionPool.getConnection();
			RcNewAreaOper oper=new RcNewAreaOper(conn);
			List<Map<String,String>> suppliers = oper.queryPrivinceCitySupplier(rescueCodes);
			if(null!=suppliers&&suppliers.size()>0){
				for(Map<String,String> supplierId:suppliers){
					String openid=supplierId.get("open_id");
					openIdList.add(openid);
				}
			}
			System.out.println("openIdList==="+openIdList);
			for(int j=0;j<openIdList.size();j++){
				int result=sendPo.sendInformation(title, content, openIdList.get(j));
				if(result == 40001){
					WeixinSignatureThread signature = new WeixinSignatureThread();
					signature.bulidApiAccessToken();
				    result = sendPo.sendInformation(title, content, openIdList.get(j));
				}
				if(result==0){
					sendFlag = true;
				}
			}
			json.put("code", sendFlag);
			response.setContentType("text/html;charset=UTF-8");
			response.getWriter().print(json.toJSONString());
			setNext(false);
		} catch (CException e) {
			e.printStackTrace();
		}finally{
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args) throws IOException {
		/*Date time=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
		String a=sdf.format(time);
	//	String aw=(Math.random()*9+1)*1000;
		 Random random = new Random();
	     int num = random.nextInt(900);
	     num = num + 1000;
		System.out.println(a+"-"+num);*/
		QueryAreaSupplierAction a=new QueryAreaSupplierAction();
		a.sendInformation();
	}
}
