public JsonResult Save(Wjbq model) { ReturnModel returnModel; if (model.ID.Equals(0)) { returnModel = wjbqService.Insert(model); return(Json(returnModel, JsonRequestBehavior.AllowGet)); } returnModel = wjbqService.Update(model); return(Json(returnModel, JsonRequestBehavior.AllowGet)); }
/// <summary> /// 修改 /// </summary> /// <returns></returns> public ReturnModel Update(Wjbq model) { try { if (string.IsNullOrWhiteSpace(model.BQMC)) { return new ReturnModel { Code = 201, Msg = "标签名称不能为空" }; } StringBuilder sbSql = new StringBuilder(); using (IDbConnection conn = new DapperConnection().DbConnection) { //判断标签存在 sbSql.Append("SELECT * FROM WJ_BQ WHERE Id=@Id AND SFQY=1"); Wjbq existModel = conn.QueryFirstOrDefault<Wjbq>(sbSql.ToString(), new { Id = model.ID }); if (existModel == null) { return new ReturnModel { Code = 201, Msg = "标签不存在" }; } //判断重名 sbSql.Clear(); sbSql.Append("SELECT * FROM WJ_BQ WHERE ID<>@ID AND BQMC=@BQMC and SFQY=1"); Wjbq sameName = conn.QueryFirstOrDefault<Wjbq>(sbSql.ToString(), model); if (sameName != null) { return new ReturnModel { Code = 201, Msg = "存在相同名称的标签" }; } //保存更新 sbSql.Clear(); sbSql.Append("UPDATE WJ_BQ SET BQMC = @BQMC, BQXQ = @BQXQ WHERE ID = @ID"); int result = conn.Execute(sbSql.ToString(), model); if (result.Equals(1)) { return new ReturnModel { Code = 200, Msg = "保存成功" }; } return new ReturnModel { Code = 201, Msg = "保存失败" }; } } catch (Exception e) { Log.WriteLog(e.Message); return new ReturnModel { Code = 201, Msg = "保存失败" }; } }
public JsonResult AddOrEdit(int id = 0) { try { Wjbq bq = new Wjbq(); if (id > 0) { bq = wjbqService.GetEntity(id); } return(Json(new ReturnModel { Code = 200, Msg = "Success", Result = bq })); } catch (Exception e) { return(Json(new ReturnModel { Code = 201, Msg = "获取标签失败" })); } }
/// <summary> /// 插入 /// </summary> /// <param name="model"></param> /// <returns></returns> public ReturnModel Insert(Wjbq model) { try { //验证参数 if (string.IsNullOrWhiteSpace(model.BQMC)) { return new ReturnModel { Code = 201, Msg = "标签名称不能为空" }; } //保存 StringBuilder sbSql = new StringBuilder(); using (IDbConnection conn = new DapperConnection().DbConnection) { //判断重名 sbSql.Append("SELECT * FROM WJ_BQ WHERE bqmc=@bqmc and sfqy=1"); Wjbq sameName = conn.QueryFirstOrDefault<Wjbq>(sbSql.ToString(), model); if (sameName != null) { return new ReturnModel { Code = 201, Msg = "存在相同名称的标签" }; } sbSql.Clear(); model.SFQY = 1; sbSql.Append("INSERT INTO WJ_BQ(BQMC, BQXQ, SFQY) VALUES (@BQMC, @BQXQ, @SFQY)"); int result = conn.Execute(sbSql.ToString(), model); if (result.Equals(1)) { return new ReturnModel { Code = 200, Msg = "保存成功" }; } return new ReturnModel { Code = 201, Msg = "保存失败" }; } } catch (Exception e) { Log.WriteLog(e.Message); return new ReturnModel { Code = 201, Msg = "保存失败" }; } }
/// <summary> /// 修改 /// </summary> /// <returns></returns> public ReturnModel Update(Wjtc model) { try { if (string.IsNullOrWhiteSpace(model.TCID)) { return(new ReturnModel { Code = 201, Msg = "套餐不能为空" }); } StringBuilder sbSql = new StringBuilder(); using (IDbConnection conn = new DapperConnection().DbConnection) { var transaction = conn.BeginTransaction(); try { //判断存在 sbSql.Append("SELECT * FROM WJ_TC WHERE Id=@Id AND SFQY=1"); Wjbq existModel = conn.QueryFirstOrDefault <Wjbq>(sbSql.ToString(), new { Id = model.ID }, transaction); if (existModel == null) { return(new ReturnModel { Code = 201, Msg = "标签不存在" }); } //判断重名 sbSql.Clear(); sbSql.Append("SELECT * FROM WJ_TC WHERE ID<>@ID AND TCID=@TCID and SFQY=1"); Wjbq sameName = conn.QueryFirstOrDefault <Wjbq>(sbSql.ToString(), model, transaction); if (sameName != null) { return(new ReturnModel { Code = 201, Msg = "存在相同名称的套餐" }); } //保存更新 sbSql.Clear(); model.SFQY = 1; sbSql.Append("UPDATE WJ_TC SET TCID = @TCID, SYRQ = @SYRQ, TCZY = @TCZY, SFQY = @SFQY WHERE ID = @ID;"); sbSql.Append("DELETE FROM WJ_TCBQ WHERE GLID = @ID;"); conn.Execute(sbSql.ToString(), model, transaction); if (!string.IsNullOrWhiteSpace(model.BQIDS)) { //标签列表 List <string> lstBq = model.BQIDS.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList(); //深度列表 List <string> lstSd = model.BQSDS.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList(); foreach (var item in lstBq) { var sd = lstSd[lstBq.IndexOf(item)]; sbSql.Clear(); sbSql.Append("INSERT INTO WJ_TCBQ(GLID, BQID, JCSD) VALUES (@GLID, @BQID, @JCSD)"); conn.Execute(sbSql.ToString(), new { GLID = model.ID, BQID = item, JCSD = sd }, transaction); } } transaction.Commit(); return(new ReturnModel { Code = 200, Msg = "保存成功" }); } catch (Exception e) { transaction.Rollback(); Log.WriteLog(e.Message); return(new ReturnModel { Code = 201, Msg = "保存失败" }); } } } catch (Exception e) { Log.WriteLog(e.Message); return(new ReturnModel { Code = 201, Msg = "保存失败" }); } }
/// <summary> /// 插入 /// </summary> /// <param name="model"></param> /// <returns></returns> public ReturnModel Insert(Wjtc model) { try { //验证参数 if (string.IsNullOrWhiteSpace(model.TCID)) { return(new ReturnModel { Code = 201, Msg = "套餐不能为空" }); } //保存 StringBuilder sbSql = new StringBuilder(); using (IDbConnection conn = new DapperConnection().DbConnection) { var transaction = conn.BeginTransaction(); try { //判断重名 sbSql.Append("SELECT * FROM WJ_TC WHERE TCID=@TCID and SFQY=1"); Wjbq sameName = conn.QueryFirstOrDefault <Wjbq>(sbSql.ToString(), model, transaction); if (sameName != null) { return(new ReturnModel { Code = 201, Msg = "存在相同名称的套餐" }); } sbSql.Clear(); model.SFQY = 1; sbSql.Append("INSERT INTO WJ_TC(TCID, SYRQ, TCZY, SFQY) VALUES (@TCID, @SYRQ, @TCZY, @SFQY);select @@IDENTITY"); int id = Convert.ToInt32(conn.ExecuteScalar(sbSql.ToString(), model, transaction)); if (!string.IsNullOrWhiteSpace(model.BQIDS)) { //标签列表 List <string> lstBq = model.BQIDS.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList(); //深度列表 List <string> lstSd = model.BQSDS.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList(); foreach (var item in lstBq) { var sd = lstSd[lstBq.IndexOf(item)]; sbSql.Clear(); sbSql.Append("INSERT INTO WJ_TCBQ(GLID, BQID, JCSD) VALUES (@GLID, @BQID, @JCSD)"); conn.Execute(sbSql.ToString(), new { GLID = id, BQID = item, JCSD = sd }, transaction); } } transaction.Commit(); return(new ReturnModel { Code = 200, Msg = "保存成功" }); } catch (Exception e) { transaction.Rollback(); Log.WriteLog(e.Message); return(new ReturnModel { Code = 201, Msg = "保存失败" }); } } } catch (Exception e) { Log.WriteLog(e.Message); return(new ReturnModel { Code = 201, Msg = "保存失败" }); } }