package com.mandi.fendan.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mandi.springmvc.logs.OpLogs;
import com.mandi.system.persist.ModuleEnum;
import org.apache.commons.lang3.StringUtils;
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.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.mandi.common.JSONobj;
import com.mandi.common.Jacksonmethod;
import com.mandi.common.RequestParam;
import com.mandi.common.SessionMethod;
import com.mandi.common.Wxhtmethod;
import com.mandi.dao.common.ObjectResult;
import com.mandi.dao.common.PageRequest;
import com.mandi.dao.common.PageResult;
import com.mandi.fendan.mapper.BusinessMapper;
import com.mandi.fendan.mapper.Fd_BsheetMapper;
import com.mandi.fendan.service.IErpService;
import com.mandi.fendan.service.Ifd_bsheetService;
import com.mandi.fendan.util.Contants;
import com.mandi.fendan.util.persist.WaitXSTDMX;
import com.mandi.system.persist.Login;

@Controller("fd_bsheettaskcon")
@RequestMapping(value="/task",method={RequestMethod.POST})
public class Fd_bsheetTaskCon {
	
	@Resource
	private IErpService erpser;
	@Resource
	private Fd_BsheetMapper bsper;
	@Resource
	private Ifd_bsheetService fdbser;
	@Resource
	private BusinessMapper bsiper;
	
	/**
	 * 获取已分单列表
	 * @param r
	 * @param re
	 * @return
	 */
	@RequestMapping(value="/getList.htm",method={RequestMethod.POST})
	@ResponseBody
	public String getXSTDList(HttpServletRequest r,HttpServletResponse re){
		String vehicleId=RequestParam.getSqlString(r, "searchword");//车牌号
		String companyNo=RequestParam.getSqlString(r, "companyNo");//物流公司编号
		int page=RequestParam.getInt(r, "page"); 
		int pagesize=RequestParam.getInt(r, "pagesize");
		int pages=RequestParam.getInt(r, "pages");
		String cqV=RequestParam.getSqlString(r, "cqV");//厂区
		PageRequest pr=new PageRequest(page, pagesize, null);
		if(pages>0)
			pr.setNeedPages(true);
		else
			pr.setNeedPages(false);
		Login lg=SessionMethod.getlogin(r.getSession());
		PageResult<Map<String, Object>> prr=erpser.getFdlist(lg,companyNo,vehicleId, pr,cqV);
		String str=Jacksonmethod.tojson(prr, false);
		SessionMethod.writeresp(re, str);
		return null;
	}
	
	/**
	 * 换车
	 * @param r
	 * @param re
	 * @return
	 */
	@RequestMapping(value="/changeVehicle.htm",method={RequestMethod.POST})
	@ResponseBody
	@OpLogs(name="换车",module = ModuleEnum.业务模块)
	public String changeVehicle(HttpServletRequest r,HttpServletResponse re){
		String bsNo=RequestParam.getSqlString(r, "bussinessNo");
		String vehicleInfo=RequestParam.getString(r, "vehicle");
		//vehicle: {vehicleId, driver, phone, vehicleweight, loadweight, feature, note}
		Login lg=SessionMethod.getlogin(r.getSession());
		ObjectResult<Boolean> orr=new ObjectResult<Boolean>();
		try {
			orr=erpser.changeVehicle(lg, bsNo, vehicleInfo);
		} catch (Exception e) {
			e.printStackTrace();
			orr.setCode(1);
			orr.setErrmsg(StringUtils.isEmpty(e.getMessage())?"换车程序错误!":e.getMessage());
		}
		String str=Jacksonmethod.tojson(orr, false);
		SessionMethod.writeresp(re, str);;
		return null;
	}
	/**
	 * 取消派车
	 * @param r
	 * @param re
	 * @return
	 */
	@RequestMapping(value="/delItem.htm",method={RequestMethod.POST})
	@ResponseBody
	@OpLogs(name="取消派车",module = ModuleEnum.业务模块)
	public String delXSFD(HttpServletRequest r,HttpServletResponse re){
		String bsNo=RequestParam.getSqlString(r, "bussinessNo");
		Login lg=SessionMethod.getlogin(r.getSession());
		ObjectResult<Boolean> orr=new ObjectResult<Boolean>();
		try {
			orr=erpser.delFdsheet(lg, bsNo);
		} catch (Exception e) {
			e.printStackTrace();
			orr.setCode(1);
			orr.setErrmsg(StringUtils.isEmpty(e.getMessage())?"取消派车程序错误!":e.getMessage());
		}
		String str=Jacksonmethod.tojson(orr, false);
		SessionMethod.writeresp(re, str);;
		return null;
	}
	
	
	
	/**
	 * 获取分单物资
	 * @param r
	 * @param re
	 * @return
	 */
	@RequestMapping(value="/getVehicleDetails.htm",method={RequestMethod.POST})
	@ResponseBody
	public String getVehicleDetails(HttpServletRequest r,HttpServletResponse re){
		String bsNo=RequestParam.getSqlString(r, "keyword");
		Login lg=SessionMethod.getlogin(r.getSession());
		PageResult<WaitXSTDMX> pr=erpser.getVehicleDetails(lg, bsNo);
		String str=Jacksonmethod.tojson(pr, false);
		SessionMethod.writeresp(re, str);;
		return null;
	}
	
	/**
	 * 发验证码
	 * @param r
	 * @param re
	 * @return
	 */
	@RequestMapping(value="/send.htm",method={RequestMethod.POST})
	@ResponseBody
	@Deprecated
	public String send(HttpServletRequest r,HttpServletResponse re){
		ObjectResult<String> orr=new ObjectResult<String>();
		String bussinessNo=RequestParam.getSqlString(r, "item");
		Map<String, Object> mp=new HashMap<String, Object>();
		mp.put("bussinessNo", bussinessNo);
		String str=Jacksonmethod.tojson(mp, false);
		JsonObject result=Wxhtmethod.postJsonStr(Contants.NCfdhx_url,str);
		if(result==null||JSONobj.getint(result, "code")!=0){
			orr.setCode(1);
			orr.setErrmsg(StringUtils.isEmpty(JSONobj.getstring(result, "errmsg"))?"邀请码生成出错,请重新操作!":JSONobj.getstring(result, "errmsg"));
		}else{
			orr.setCode(0);
			orr.setItem(JSONobj.getstring(result, "item"));
		}
		str=Jacksonmethod.tojson(orr, false);
		SessionMethod.writeresp(re, str);
		return null;
	}
	/**
	 * 重发验证码
	 * @param r
	 * @param re
	 * @return
	 */
	@RequestMapping(value="/resend.htm",method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public String resend(HttpServletRequest r,HttpServletResponse re){
		String bussinessNo=RequestParam.getSqlString(r, "item");
		Login lg=SessionMethod.getlogin(r.getSession());
		ObjectResult<String> orr=new ObjectResult<String>();
		try {
			orr = fdbser.sendMsg(lg,bussinessNo);
		} catch (Exception e) {
			e.printStackTrace();
			orr.setCode(1);
			orr.setErrmsg(StringUtils.isEmpty(e.getMessage())?"短信邀请码发送失败!":e.getMessage());
		}
		String str=Jacksonmethod.tojson(orr, false);
		SessionMethod.writeresp(re, str);
		return null;
	}
	
	/**
	 * 通过车牌号打印装车通知单
	 * @param r
	 * @param re
	 * @return
	 */
	@RequestMapping(value="/Printfdxq.htm",method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public String Printfdxq(HttpServletRequest r,HttpServletResponse re){
		String bsNo =RequestParam.getSqlString(r, "bsNo");
		Login lg=SessionMethod.getlogin(r.getSession());
		JsonObject jo=new JsonObject();
		Gson g=new Gson(); 
		String ipadd=r.getLocalAddr();
		if(lg==null){
			jo.addProperty("code", 1);
			jo.addProperty("errmsg", "请登录账号!");
			String str=g.toJson(jo);
			SessionMethod.writeresp(re, str);
			return null;
		}
		ObjectResult<String> orr=erpser.tzPrint(lg, bsNo,ipadd);
		if(orr.getCode()!=0){
			String str=Jacksonmethod.tojson(orr, false);
			SessionMethod.writeresp(re, str);
			return null;
		}else{
			try {
				re.sendRedirect(orr.getItem());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	/**
	 * 通过卡号打印装车通知单
	 * @param r
	 * @param re
	 * @return
	 */
	@RequestMapping(value="/printbycard.htm",method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public String printbyCard(HttpServletRequest r,HttpServletResponse re){
		String cardno =RequestParam.getSqlString(r, "cardno");
		String cqNo =RequestParam.getSqlString(r, "cqNo");
		String ipadd=r.getLocalAddr();
		ObjectResult<String> orr=erpser.tzPrint(cardno,ipadd,cqNo);
		if(orr.getCode()!=0){
			String str=Jacksonmethod.tojson(orr, false);
			SessionMethod.writeresp(re, str);
			return null;
		}else{
			try {
				re.sendRedirect(orr.getItem());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	/**
	 * 打印提单全部
	 * @param r
	 * @param re
	 * @return
	 */
	@RequestMapping(value="/printtdall.htm",method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public String printtdall(HttpServletRequest r,HttpServletResponse re){
		String tdNo =RequestParam.getSqlString(r, "tdNo");
		Login lg=SessionMethod.getlogin(r.getSession());
		String ipadd=r.getLocalAddr();
		ObjectResult<String> orr=new ObjectResult<String>();
		if(lg==null){
			orr.setCode(1);
			orr.setErrmsg("请登录账号!");
			String str=Jacksonmethod.tojson(orr, false);
			SessionMethod.writeresp(re, str);
			return null;
		}
		if(StringUtils.isEmpty(tdNo)){
			orr.setCode(1);
			orr.setErrmsg("提单编号为空!");
			String str=Jacksonmethod.tojson(orr, false);
			SessionMethod.writeresp(re, str);
			return null;
		}
		String dyAdd=this.bsiper.getUsedbyAdd();
		if(StringUtils.isBlank(dyAdd)){
			orr.setCode(1);
			orr.setErrmsg("系统未设置打印地址!");
			String str=Jacksonmethod.tojson(orr, false);
			SessionMethod.writeresp(re, str);
			return null;
		}
		orr.setCode(0);
		orr.setItem("http://"+dyAdd+Contants.GJTDALLPrintAdd+"&tdNo="+tdNo);
//		System.err.println(":::::::::::http://"+dyAdd+Contants.GJTDALLPrintAdd+"&tdNo="+tdNo);
		if(!StringUtils.isEmpty(orr.getItem())){
			try {
				re.sendRedirect(orr.getItem());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}else{
			orr.setCode(1);
			orr.setErrmsg("未找到跳转地址!");
			String str=Jacksonmethod.tojson(orr, false);
			SessionMethod.writeresp(re, str);
		}
		return null;
	}
	
	/**
	 * 通过批次号单条删除
	 * @param r
	 * @param re
	 * @return
	 */
	@RequestMapping(value="/delbypch.htm",method={RequestMethod.POST})
	@ResponseBody
	@OpLogs(name="批次号取消配车", module = ModuleEnum.业务模块)
	public String delbypc(HttpServletRequest r,HttpServletResponse re){
		String tdno =RequestParam.getSqlString(r, "tdno");//提单编号
		String pch =RequestParam.getSqlString(r, "pch");//批次号
		String cph =RequestParam.getSqlString(r, "cph");//车牌号
		String bsno =RequestParam.getSqlString(r, "bsno");//车牌号
		Login lg=SessionMethod.getlogin(r.getSession());
		JsonObject jo=new JsonObject();
		Gson g=new Gson(); 
		if(lg==null){
			jo.addProperty("code", 1);
			jo.addProperty("errmsg", "请登录账号!");
			String str=g.toJson(jo);
			SessionMethod.writeresp(re, str);
			return null;
		}
		ObjectResult<Boolean> orr=new ObjectResult<Boolean>();
		try {
			orr = erpser.delbyPCH(lg, tdno, pch,cph,bsno);
		} catch (Exception e) {
			e.printStackTrace();
			orr.setCode(1);
			orr.setErrmsg(StringUtils.isEmpty(e.getMessage())?"单条删除出错!":e.getMessage());
		}
		String str=Jacksonmethod.tojson(orr, false);
		SessionMethod.writeresp(re, str);
		return null;
	}
	

}