Esempio n. 1
0
        public JsonResult StationEdit()
        {
            var stationJson = Request["station"];
            var station     = JsonHelper.Deserialize <BaseStation>(stationJson);

            var updateType = station.Id > 0 ? DataUpdateType.Update : DataUpdateType.Insert;

            // 更新车站
            var stationUpdateRes = false;
            var stationBll       = new BaseStationBll();

            if (station.Id > 0)
            {
                stationUpdateRes = stationBll.Update(station);
            }
            else
            {
                var condition = string.Format("StationName='{0}' AND IsDelete=0", station.StationName);
                if (stationBll.Exists(condition))
                {
                    return(Json(ErrorModel.ExistSameItem));
                }

                var insertedStation = stationBll.Insert(station);
                stationUpdateRes = insertedStation.Id > 0;
            }

            // 更新文件
            if (stationUpdateRes)
            {
                // 写入更新记录
                DataUpdateLog.SingleUpdate(typeof(BaseStation).Name, station.Id, updateType);

                var filesJson = Request["files"];
                var files     = JsonHelper.Deserialize <StationFiles[]>(filesJson);
                if (!files.Any())
                {
                    return(Json(ErrorModel.OperateSuccess));
                }

                files.ForEach(f => { f.StationId = station.Id; });

                var filesToInsert = files.Where(f => f.Id == 0);
                var filesBll      = new StationFilesBll();

                var maxId = (int)filesBll.GetMaxId();

                filesBll.BulkInsert(filesToInsert);

                // 写入更新记录
                DataUpdateLog.BulkUpdate(typeof(StationFiles).Name, maxId);

                return(Json(ErrorModel.OperateSuccess));
            }

            return(Json(ErrorModel.OperateFailed));
        }
Esempio n. 2
0
        /// <summary>
        /// 将指定集合中的车站信息写入数据库,并确保数据库中不会存在同名的车站
        /// 对于已经存在的车站则查询数据库,将新值存入返回的集合中
        /// 对于新的车站则插入数据库,将插入后的新值存入返回的集合中
        /// 最终返回的集合中所有的车站均带有唯一主键
        /// </summary>
        /// <param name="stations">待写稿的车站信息集合</param>
        /// <returns><see cref="List{BaseStation}"/>对象</returns>
        private static List <BaseStation> WriteStations(List <BaseStation> stations)
        {
            var stationBll  = new BaseStationBll();
            var newStations = new List <BaseStation>();

            stations.ForEach(s =>
            {
                var temp = _stationCaches.Find(item => item.StationName == s.StationName);
                if (temp != null)
                {
                    newStations.Add(temp);
                }
                else
                {
                    stationBll.Insert(s);
                    DataUpdateLog.SingleUpdate(nameof(BaseStation), s.Id, DataUpdateType.Insert);

                    newStations.Add(s);
                    _stationCaches.Add(s);
                }
            });

            return(newStations);
        }