public int AddCarPicture(CarPictureModel model) { const string sql = @"INSERT INTO car_picture (innerid, carid, typeid, path, sort, createdtime) VALUES (@innerid, @carid, @typeid, @path, @sort, @createdtime);"; try { Helper.Execute(sql, model); return 1; } catch (Exception ex) { return 0; } }
/// <summary> /// 单次添加图片 /// </summary> /// <param name="model"></param> /// <returns></returns> public int AddCarPictureEx(CarPictureModel model) { const string sqlSCarPic = "select innerid, carid, typeid, path, sort, createdtime from car_picture where carid=@carid order by sort desc;";//查询车辆图片 const string sqlIPic = @"insert into car_picture (innerid, carid, typeid, path, sort, createdtime) values (@innerid, @carid, @typeid, @path, @sort, @createdtime);"; const string sqlUCover = @"update car_info set pic_url=@pic_url where innerid=@carid;"; using (var conn = Helper.GetConnection()) { //获取车辆图片 var picedList = conn.Query<CarPictureModel>(sqlSCarPic, new { carid = model.Carid }).ToList(); var number = picedList.Count + 1; if (number > 9) { //图片数量控制在>=3 and <=9 return 402; } var tran = conn.BeginTransaction(); try { if (picedList.Count == 0) { model.Sort = 1; } else { model.Sort = picedList[0].Sort + 1; } conn.Execute(sqlIPic, model, tran); //插入图片 //表示添加张图片 if (picedList.Count == 0) { conn.Execute(sqlUCover, new { carid = model.Carid, pic_url = model.Path }, tran); } tran.Commit(); return 1; } catch (Exception ex) { tran.Rollback(); LoggerFactories.CreateLogger().Write("单次添加图片异常:" + ex.Message, TraceEventType.Warning); return 0; } } }