using DSWeb.Areas.CommMng.DAL; using DSWeb.Areas.MvcShipping.Comm; using DSWeb.Areas.MvcShipping.DB; using DSWeb.Areas.MvcShipping.Helper; using DSWeb.Areas.MvcShipping.Job; using DSWeb.Areas.MvcShipping.Models.MsOpSeaeRunBill; using DSWeb.MvcShipping.Comm.Cookie; using DSWeb.MvcShipping.DAL.MsOpSeaeRunBill; using log4net; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Configuration; using System.Data.SqlClient; using System.Dynamic; using System.Linq; using System.Runtime.Caching; using System.Web; using System.Web.Mvc; namespace DSWeb.MvcShipping.Controllers { public class MsOpSeaeRunBillController : Controller { private ILog logger = LogManager.GetLogger("MsOpSeaeRunBillController"); private RunBillContext runBillContext = new RunBillContext(); //新建 public ActionResult Create(string strBSNO) { ViewData["StrBSNO"] = strBSNO; string BSNO = strBSNO.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).First(); var opseae = runBillContext.OpSeaes.First(s => s.BSNO == BSNO); var info = runBillContext.InfoClients.SingleOrDefault(i => i.SHORTNAME == opseae.CUSTOMERNAME); OpSeaeRunBill runBill = new OpSeaeRunBill(); runBill.CUSTOMERNAME = opseae.CUSTOMERNAME; runBill.CARRIER = opseae.CARRIER; runBill.FORWARDER = opseae.FORWARDER; runBill.ADDR = info.ADDR; runBill.STATUS = OpSeaeRunBillStatus.Create.ToString(); ViewData["Bill"] = JsonConvert.SerializeObject(runBill.AsEditViewModel()); return View("edit"); } //编辑 public ActionResult Edit(string gid) { ViewData["GID"] = gid; var bsnoList = runBillContext.OpSeaeRunBillItems.Where(b => b.ParentId == gid).Select(b => b.BSNO).ToList(); ViewData["StrBSNO"] = string.Join(",", bsnoList); ViewData["Bill"] = JsonConvert.SerializeObject(runBillContext.OpSeaeRunBills.First(b => b.GID == gid).AsEditViewModel()); return View(); } //获取客户的联系人、电话、地址等信息 public JsonResult GetCustomerInfo(string shortName) { var info = runBillContext.InfoClients.SingleOrDefault(i => i.SHORTNAME == shortName); if (info != null) { return Json(info); } return null; } //根据bsno字符串,获取opseae列表信息 public JsonResult GetOpseadList(string strBSNO) { string[] arrBSNO = strBSNO.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); var list = runBillContext.OpSeaes.Where(s => arrBSNO.Contains(s.BSNO)).ToList(); return Json(new { Success = true, Message = "", data = list, totalCount = list.Count }); } //保存 public JsonResult Save(string data, string strBSNO) { var viewModel = JsonConvert.DeserializeObject(data); OpSeaeRunBill saveModel = null; if (!string.IsNullOrEmpty(viewModel.GID)) { saveModel = runBillContext.OpSeaeRunBills.First(b => b.GID == viewModel.GID); viewModel.AsModel(saveModel); runBillContext.OpSeaeRunBillItems.RemoveRange(runBillContext.OpSeaeRunBillItems.Where(i => i.ParentId == viewModel.GID)); } else { saveModel = viewModel.AsModel(); saveModel.GID = Guid.NewGuid().ToString(); saveModel.CREATETIME = DateTime.Now; saveModel.CREATEUSER = Convert.ToString(Session["USERID"]); saveModel.CREATEUSERNAME = Convert.ToString(Session["SHOWNAME"]); saveModel.BillNO = PubSysDAL.GetBillNo("0700"); if (string.IsNullOrWhiteSpace(saveModel.STATUS)) { saveModel.STATUS = OpSeaeRunBillStatus.Create.ToString(); } runBillContext.OpSeaeRunBills.Add(saveModel); } //判断时间区间 if (saveModel.RUNTIME.HasValue) { int hour = saveModel.RUNTIME.Value.Hour; if (hour <= 12) { saveModel.TimeSpan = "上午"; } else { saveModel.TimeSpan = "下午"; } } string[] arrBSNO = strBSNO.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); foreach (var bsno in arrBSNO) { OpSeaeRunBillItem item = new OpSeaeRunBillItem(); item.GID = Guid.NewGuid().ToString(); item.BSNO = bsno; item.ParentId = saveModel.GID; runBillContext.OpSeaeRunBillItems.Add(item); } runBillContext.SaveChanges(); //推送 if (saveModel.STATUS == OpSeaeRunBillStatus.Dispatch.ToString()) { var user = runBillContext.Users.FirstOrDefault(u => u.GID == saveModel.USERID); if (!string.IsNullOrWhiteSpace(user.WeChatAccount)) { WorkWeixinSendHelper.SendNotifyToUser(user.WeChatAccount, $"/MvcShipping/MsOpseaeRunBill/WxUserRedirect?userId={user.WeChatAccount}"); } } return Json(new { Success = true, Message = "保存成功", Data = saveModel.AsEditViewModel() }); } //删除 public JsonResult Delete(string data) { var viewModel = JsonConvert.DeserializeObject(data); OpSeaeRunBill delModel = null; delModel = runBillContext.OpSeaeRunBills.First(b => b.GID == viewModel.GID); if (delModel.STATUS != OpSeaeRunBillStatus.Create.ToString()) { return Json(new { Success = false, Message = "只能删除“创建”状态的跑单信息", Data = "" }); } viewModel.AsModel(delModel); runBillContext.OpSeaeRunBillItems.RemoveRange(runBillContext.OpSeaeRunBillItems.Where(i => i.ParentId == viewModel.GID)); runBillContext.OpSeaeRunBills.RemoveRange(runBillContext.OpSeaeRunBills.Where(i => i.GID == viewModel.GID)); runBillContext.SaveChanges(); return Json(new { Success = true, Message = "删除成功", Data = "" }); } //列表页 public ActionResult Index() { return View(); } //获取跑单列表数据 public JsonResult GetDataList(string condition, int start, int limit, int pageSize, string sort = null) { limit = start + pageSize; var sessionInfo = new { USERID = Convert.ToString(Session["USERID"]), SHOWNAME = Convert.ToString(Session["SHOWNAME"]), COMPANYID = Convert.ToString(Session["COMPANYID"]) }; var count = runBillContext.Database.SqlQuery(MsOpSeaeRunBillDAL.GetDataCountSql(condition, Convert.ToString(Session["USERID"]), Convert.ToString(Session["SHOWNAME"]), Convert.ToString(Session["COMPANYID"]))).First(); var list = runBillContext.Database.SqlQuery(MsOpSeaeRunBillDAL.GetDataListSql(condition, Convert.ToString(Session["USERID"]), Convert.ToString(Session["SHOWNAME"]), Convert.ToString(Session["COMPANYID"]), sort, start, limit)); return Json(new { Success = true, Message = "查询成功", totalCount = count, data = list.AsListViewModelList() }, JsonRequestBehavior.AllowGet); } //确认接单 public JsonResult AcceptBill(string strGID) { string[] arrGID = strGID.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); var uid = Convert.ToString(Session["USERID"]); var staDispatch = OpSeaeRunBillStatus.Dispatch.ToString(); var list = runBillContext.OpSeaeRunBills.Where(s => s.STATUS == staDispatch && arrGID.Contains(s.GID)).ToList(); list.ForEach(b => { b.ACCEPTTIME = DateTime.Now; b.STATUS = OpSeaeRunBillStatus.Accept.ToString(); }); runBillContext.SaveChanges(); return Json(new { Success = true, Message = "操作成功" }); } //转单 public JsonResult ChangeDispatch(string strGID, string userid, string username) { string[] arrGID = strGID.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); var uid = Convert.ToString(Session["USERID"]); var staComplete = OpSeaeRunBillStatus.Complete.ToString(); var list = runBillContext.OpSeaeRunBills.Where(s => s.STATUS != staComplete && arrGID.Contains(s.GID)).ToList(); var user = runBillContext.Users.FirstOrDefault(u => u.GID == userid); if (list.Count == 0) { return Json(new { Success = false, Message = "没有可供操作的已选数据" }); } list.ForEach(b => { b.USERID = userid; b.USERNAME = username; b.TRANSFERTIME = DateTime.Now; b.TRANSFERUSER = Convert.ToString(Session["USERID"]); }); runBillContext.SaveChanges(); logger.Debug("跑单转单,准备推送通知"); if (!string.IsNullOrWhiteSpace(user.WeChatAccount)) { WorkWeixinSendHelper.SendNotifyToUser(user.WeChatAccount, $"/MvcShipping/MsOpseaeRunBill/WxUserRedirect?userId={user.WeChatAccount}"); logger.Debug("推送通知:" + user.WeChatAccount); } if (list.Count == 1) { return Json(new { Success = true, Message = "操作成功", Data = list[0].AsEditViewModel() }); } else { return Json(new { Success = true, Message = "操作成功" }); } } //转单 public JsonResult CancelChangeDispatch(string strGID) { string[] arrGID = strGID.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); var uid = Convert.ToString(Session["USERID"]); var staComplete = OpSeaeRunBillStatus.Complete.ToString(); var list = runBillContext.OpSeaeRunBills.Where(s => s.STATUS != staComplete && arrGID.Contains(s.GID)).ToList(); if (list.Count == 0) { return Json(new { Success = false, Message = "没有可供操作的已选数据" }); } list.ForEach(b => { b.USERID = b.TRANSFERUSER; var user = runBillContext.Users.FirstOrDefault(u => u.GID == b.TRANSFERUSER); b.USERNAME = user.SHOWNAME; b.TRANSFERTIME = DateTime.Now; b.TRANSFERUSER = Convert.ToString(Session["USERID"]); }); runBillContext.SaveChanges(); if (list.Count == 1) { return Json(new { Success = true, Message = "操作成功", Data = list[0].AsEditViewModel() }); } else { return Json(new { Success = true, Message = "操作成功" }); } } //派单 public JsonResult DispatchBill(string strGID) { string[] arrGID = strGID.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); var uid = Convert.ToString(Session["USERID"]); var staComplete = OpSeaeRunBillStatus.Complete.ToString(); var list = runBillContext.OpSeaeRunBills.Where(s => s.STATUS != staComplete && arrGID.Contains(s.GID)).ToList(); if (list.Count == 0) { return Json(new { Success = false, Message = "没有可供操作的已选数据" }); } list.ForEach(b => { b.STATUS = OpSeaeRunBillStatus.Dispatch.ToString(); }); runBillContext.SaveChanges(); //推送消息 logger.Debug("跑单派单,准备推送通知"); string[] arrUser = list.Select(b => "'" + b.USERID + "'").Distinct().ToArray(); string strUserIds = string.Join(",", arrUser); var users = runBillContext.Database.SqlQuery($"select * from vw_user where userid in ({strUserIds})").ToList(); logger.Debug($"推送用户数量:{users.Count}"); foreach (var u in users) { logger.Debug($"推送给用户:{u.WeChatAccount}"); if (!string.IsNullOrWhiteSpace(u.WeChatAccount)) { WorkWeixinSendHelper.SendNotifyToUser(u.WeChatAccount, $"/MvcShipping/MsOpseaeRunBill/WxUserRedirect?userId={u.WeChatAccount}"); } } if (list.Count == 1) { return Json(new { Success = true, Message = "操作成功", Data = list[0].AsEditViewModel() }); } else { return Json(new { Success = true, Message = "操作成功" }); } } //跑单完成 public JsonResult CompleteBill(string strGID) { string[] arrGID = strGID.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); var uid = Convert.ToString(Session["USERID"]); var staAccept = OpSeaeRunBillStatus.Accept.ToString(); var list = runBillContext.OpSeaeRunBills.Where(s => s.STATUS == staAccept && arrGID.Contains(s.GID)).ToList(); list.ForEach(b => { b.COMPLETETIME = DateTime.Now; b.STATUS = OpSeaeRunBillStatus.Complete.ToString(); }); runBillContext.SaveChanges(); return Json(new { Success = true, Message = "操作成功" }); } public ActionResult TokenShow() { var c = MemoryCache.Default.Contains(WechatWorkTokenJob.WorkWeixinAccessTokenStoreKey); if (c) { return Content($"{c}, {MemoryCache.Default[WechatWorkTokenJob.WorkWeixinAccessTokenStoreKey].ToString()}"); } else { return Content($"{c}"); } } #region 企业号 //跳转 public ActionResult WxRedirect(string code, string state) { if (!MemoryCache.Default.Contains(WechatWorkTokenJob.WorkWeixinAccessTokenStoreKey)) { return Content("token失效,请联系管理员"); } string token = MemoryCache.Default[WechatWorkTokenJob.WorkWeixinAccessTokenStoreKey].ToString(); string resp = WebRequestHelper.DoGet($"https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token={token}&code={code}"); var jsonObj = JsonConvert.DeserializeAnonymousType(resp, new { errcode = 0, errmsg = "", UserId = "" }); if (jsonObj.errcode == 0) { return WxUserRedirect(jsonObj.UserId); } else { return Content("获取用户信息失败,请重试"); } } public ActionResult WxUserRedirect(string userId) { var user = runBillContext.Database.SqlQuery("select * from vw_user where wechataccount=@uid", new SqlParameter("uid", userId)).FirstOrDefault(); if (user != null) { //获取用户信息,存储到session Session["USERID"] = user.userid; Session["SHOWNAME"] = user.SHOWNAME; Session["COMPANYID"] = user.COMPANYID; return RedirectToAction("WxRunBillList"); } else { return Content($"未找到用户:{userId},请联系管理员"); } } //跑单列表 public ActionResult WxRunBillList() { return View(); } //跑单详情 public ActionResult WxRunBillDetail(string gid) { ViewData["gid"] = gid; return View(); } public ActionResult WxRunBillDetailData(string gid) { var bill = runBillContext.OpSeaeRunBills.FirstOrDefault(b => b.GID == gid); if (bill != null) { var query = from a in runBillContext.OpSeaeRunBillItems join s in runBillContext.OpSeaes on a.BSNO equals s.BSNO where a.ParentId == gid select s; return Json(new { Success = true, Bill = bill.AsListViewModel(), Items = query.ToList(), IsDispatch = bill.STATUS == OpSeaeRunBillStatus.Dispatch.ToString(), IsAccept = bill.STATUS == OpSeaeRunBillStatus.Accept.ToString(), IsTransfer = bill.STATUS != OpSeaeRunBillStatus.Complete.ToString() }); } else { return Json(new { Success = false }); } } public ActionResult WxRunBillTransfer(string gid) { ViewData["gid"] = gid; return View(); } #endregion } }