package com.iamberry.wechat.handles.official;

import com.iamberry.wechat.core.entity.ResultMsg;
import com.iamberry.wechat.core.entity.official.Prospectus;
import com.iamberry.wechat.core.entity.thanksgiving.ThanksGiving;
import com.iamberry.wechat.face.official.ProspectusService;
import com.iamberry.wechat.face.thanksgiving.ThanksGivingService;
import com.iamberry.wechat.utils.ExcelUtil;
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 javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 */
@Controller
public class ProspectusHandler {

	@Autowired
	ProspectusService prospectusService;
	@Autowired
	private ThanksGivingService thanksGivingService;

	/**
	 *
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/official/getProspectus")
	public ResultMsg getCartNumByCartOpenId(HttpServletRequest request,String prospectusCode) throws Exception {
		ResultMsg remsg=new ResultMsg();
		Prospectus prospectus  = prospectusService.getProspectusByCode(prospectusCode);
		if(prospectus == null){
			return new ResultMsg(true, ResultMsg.SUCCESS, "未查询到授权码!",null);
		}
		//prospectusTime
		if(prospectus.getProspectusTime().before(new Date())){
			return new ResultMsg(true, ResultMsg.SUCCESS, "授权码已过期!",null);
		};
		if(prospectus.getProspectusStatus() == 2){
			return new ResultMsg(true, ResultMsg.SUCCESS, "授权码已禁用!",null);
		};
		if(prospectus.getProspectusStatus() == 1){
			remsg.setMessage("授权码可用");
		}
		remsg.setData(prospectus);
		return remsg;
	}


	/**
	 *
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
//	@RequestMapping("/export_excel")
	public ResultMsg exportExcel(HttpServletRequest request) throws Exception {
		String filePath = "E:\\java-work\\iamberry-watero\\watero-wechat-web\\src\\test\\java\\excel.xls";
		ResultMsg remsg=new ResultMsg();
		List<String> phoneList = new ArrayList<String>();
		List<String> nameList = new ArrayList<String>();
		List<String> landlineList = new ArrayList<String>();
		try {
			phoneList = ExcelUtil.readCell(filePath, "手机号码");
		} catch (Exception e) {
			e.printStackTrace();
		}

		for (int i=0;i<phoneList.size();i++) {
			String phone = phoneList.get(i);
			phone = phone.replaceAll("\r|\n|\t", "");
			try{
				String[] phones = phone.split("@");
				String name = phones[0];
				phone = phones[1];
				if (phone.length() == 11) {
					ThanksGiving thanksGiving = thanksGivingService.getThanksGivingByPhone(phone);
					if(thanksGiving == null){
						ThanksGiving tg = new ThanksGiving();
						tg.setThanksGivingPhone(phone);
						tg.setThanksGivingStatus(1);
						tg.setThanksGivingName(name);
						Integer flag = thanksGivingService.save(tg);
						if(flag < 1){
							landlineList.add(phone);
						}
					}
				} else {
					landlineList.add(phone);
				}
			}catch (Exception e){
				e.printStackTrace();
				landlineList.add(phone);
			}
		}

		System.out.println("========kaishi============");
		//格式不对的手机号码
		for (String landline : landlineList) {
			System.out.println(landline);
		}
		System.out.println("========结束============");
		return remsg;
	}

}
