Пример #1
0
        /// <summary>
        /// 组合protobuf消息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="job"></param>
        /// <param name="ids"></param>
        /// <returns></returns>
        private SmartWeb.ProtoBuffer.Models.CrewInfo GetCrewInfo(int id, string name, string job, List <string> ids)
        {
            ProtoBuffer.Models.CrewInfo emp = new ProtoBuffer.Models.CrewInfo()
            {
                job  = job,
                name = name,
                uid  = id.ToString()
            };
            emp.pictures = new List <string>();
            List <CrewPictureViewModel> vmPicList = new List <CrewPictureViewModel>();

            if (id > 0)
            {
                var crew = crewVMList.FirstOrDefault(c => c.Id == id);
                vmPicList = crew.crewPictureViewModels;
            }
            foreach (var item in ids)
            {
                if (picBytes.Where(c => c.Key == item).Any())
                {
                    byte[] value = picBytes.Where(c => c.Key == item).FirstOrDefault().Value;
                    string pic   = Convert.ToBase64String(value);
                    emp.pictures.Add(pic);
                }
                else if (vmPicList.Where(c => c.Id == item).Any())
                {
                    var pic = vmPicList.FirstOrDefault(c => c.Id == item).Picture;
                    emp.pictures.Add(pic);
                }
            }
            return(emp);
        }
Пример #2
0
        /// <summary>
        /// 保存(新增/修改)
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="job"></param>
        /// <param name="picIds">图片ID</param>
        /// <returns></returns>
        public IActionResult Save(int id, string name, string job, string picIds)
        {
            try
            {
                if (base.user.ShipId == "")
                {
                    return(new JsonResult(new { code = 1, msg = "船不存在,无法添加数据" }));
                }
                int           code   = 1;
                string        errMsg = "";
                List <string> ids    = new List <string>();
                if (picIds != null)
                {
                    ids = picIds.Split(',').ToList();
                }
                if (base.user.IsLandHome)
                {
                    #region 陆地端添加/修改船员
                    //xmq的组件ID
                    string XMQComId = base.user.ShipId;
                    string tokenstr = HttpContext.Session.GetString("comtoken");
                    string identity = ManagerHelp.GetLandToId(tokenstr);
                    if (identity == "")
                    {
                        return(new JsonResult(new { code = 1, msg = "当前船舶已失联,请重新连接" }));
                    }
                    string algoComId = ManagerHelp.GetLandToId(tokenstr, ComponentType.AI, ManagerHelp.FaceName);
                    if (string.IsNullOrEmpty(algoComId))
                    {
                        return(new JsonResult(new { code = 1, msg = "人脸组件未启动" }));
                    }
                    ProtoBuffer.Models.CrewInfo emp = GetCrewInfo(id, name, job, ids);
                    if (id > 0)
                    {
                        assembly.SendCrewUpdate(emp, XMQComId + ":" + identity);
                    }
                    else
                    {
                        assembly.SendCrewAdd(emp, XMQComId + ":" + identity);
                    }
                    code = GetResult();
                    if (code == 2)
                    {
                        errMsg = "船员名称不能重复";
                    }
                    if (code == 400)
                    {
                        errMsg = "网络请求超时。。。";
                    }
                    else if (code != 0)
                    {
                        errMsg = "船员信息保存失败";
                    }
                    //清除已经上传了的图片
                    foreach (var item in ids)
                    {
                        picBytes.Remove(item);
                    }
                    return(new JsonResult(new { code = code, msg = code == 2 ? "船员名称不能重复" : "数据保存失败" }));

                    #endregion
                }
                else
                {
                    Crew employee = new Crew();
                    if (!CheckData(id, name, ref employee, ref errMsg))
                    {
                        return(new JsonResult(new { code = 1, msg = errMsg }));
                    }
                    string identity = ManagerHelp.GetShipToId(ComponentType.AI, ManagerHelp.FaceName);
                    if (identity == "")
                    {
                        return(new JsonResult(new { code = 1, msg = "人脸组件未启动" }));
                    }
                    //增加或修改
                    AddOrUpdate(id, name, job, ids, ref employee);
                    //发送netmq消息
                    var           dbPic = _context.CrewPicture.Where(c => c.CrewId == employee.Id).ToList();
                    List <string> bytes = new List <string>();
                    foreach (var item in dbPic)
                    {
                        bytes.Add(Convert.ToBase64String(item.Picture));
                    }
                    ProtoBuffer.Models.CrewInfo crewInfo = new ProtoBuffer.Models.CrewInfo()
                    {
                        job      = employee.Job,
                        name     = employee.Name,
                        uid      = employee.Id.ToString(),
                        pictures = bytes
                    };
                    //if (id > 0) { assembly.SendCrewUpdate(crewInfo, identity); }
                    //else { assembly.SendCrewAdd(crewInfo, identity); }
                    assembly.SendCrewAdd(crewInfo, identity);
                    code = GetResult();
                    if (code == 400)
                    {
                        errMsg = "网络请求超时。。。";
                    }
                    else if (code != 0)
                    {
                        errMsg = "船员信息保存失败";
                    }
                }
                return(new JsonResult(new { code = code, msg = errMsg }));
            }
            catch (Exception ex)
            {
                return(new JsonResult(new { code = 1, msg = "保存失败!" + ex.Message }));
            }
        }