CarCompanyAndTouristGuideService.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. using Models;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Linq;
  7. using System.Text;
  8. namespace DAL
  9. {
  10. public class CarCompanyAndTouristGuideService
  11. {
  12. /// <summary>
  13. /// 查询所有
  14. /// </summary>
  15. /// <param name="sql">sql语句</param>
  16. /// <param name="param">可变参数数组</param>
  17. /// <returns>返回集合</returns>
  18. List<CarCompanyAndTouristGuide> excuteSql(string sql, params SqlParameter[] param)
  19. {
  20. return ServiceBase<CarCompanyAndTouristGuide>.excuteSql(new CarCompanyAndTouristGuide(), "CarCompanyAndTouristGuide", sql, CommandType.Text, param);
  21. }
  22. /// <summary>
  23. /// 获取单个对象
  24. /// </summary>
  25. /// <param name="sql">sql语句</param>
  26. /// <param name="param">可变参数数组</param>
  27. /// <returns>返回空或者单个对象</returns>
  28. CarCompanyAndTouristGuide excuteType(string sql, params SqlParameter[] param)
  29. {
  30. //查询结果放入对象集合
  31. List<CarCompanyAndTouristGuide> cList = excuteSql(sql, param);
  32. //判断集合是否为空
  33. if (cList == null || cList.Count == 0)
  34. //返回null
  35. return null;
  36. //返回单个对象
  37. return cList[0];
  38. }
  39. public int CountByCGId(int CGId)
  40. {
  41. string sql = "select COUNT(*) from dbo.CarCompanyAndTouristGuide where CGId=@CGId";
  42. SqlParameter[] parmet = new SqlParameter[] {
  43. new SqlParameter("@CGId",CGId)
  44. };
  45. int obj = Convert.ToInt32(SqlHelper.ExecuteScalar(sql, CommandType.Text, parmet));
  46. if (obj > 0)
  47. {
  48. return obj;
  49. }
  50. else
  51. {
  52. return 0;
  53. }
  54. }
  55. public List<CarCompanyAndTouristGuide> CarCompanyAndTouristGuideList(int CGId)
  56. {
  57. string sql = "select * from CarCompanyAndTouristGuide where CGId =@CGId and isdel=0 order by id desc";
  58. SqlParameter[] parameter = new SqlParameter[]{
  59. new SqlParameter("@CGId",CGId)
  60. };
  61. return excuteSql(sql, parameter);
  62. }
  63. public CarCompanyAndTouristGuide CarCompanyAndTouristGuide(int CGId)
  64. {
  65. string sql = "select * from CarCompanyAndTouristGuide where CGId =@CGId and isdel=0 order by id desc";
  66. SqlParameter[] parameter = new SqlParameter[]{
  67. new SqlParameter("@CGId",CGId)
  68. };
  69. return excuteType(sql, parameter);
  70. }
  71. public CarCompanyAndTouristGuide CarCompanyAndTouristGuideById(int id)
  72. {
  73. string sql = "select * from CarCompanyAndTouristGuide where id =@id and isdel=0 order by id desc";
  74. SqlParameter[] parameter = new SqlParameter[]{
  75. new SqlParameter("@id",id)
  76. };
  77. return excuteType(sql, parameter);
  78. }
  79. public CarCompanyAndTouristGuide GetCCATG(int CGId)
  80. {
  81. string sql = "select * from CarCompanyAndTouristGuide where CGId =@CGId and isdel=0 order by id desc";
  82. SqlParameter[] parameter = new SqlParameter[]{
  83. new SqlParameter("@CGId",CGId)
  84. };
  85. return excuteType(sql, parameter);
  86. //return excuteType(sql, parameter);
  87. }
  88. /// <summary>
  89. /// 新增
  90. /// 20210828 贾文滔
  91. /// </summary>
  92. /// <param name="CarCompanyAndTouristGuide"></param>
  93. /// <returns></returns>
  94. public bool Add(CarCompanyAndTouristGuide ctggd)
  95. {
  96. string sql = "insert into CarCompanyAndTouristGuide values(@CGId,@CarType,@Price,@Unit,@CompanyOrGuide,@Operators,@OperatorDate,@Remark,@Currency,@IsDel)";
  97. SqlParameter[] parameter = new SqlParameter[]{
  98. new SqlParameter("@CGId",ctggd.CGId),
  99. new SqlParameter("@CarType",ctggd.CarType),
  100. new SqlParameter("@Price",ctggd.Price),
  101. new SqlParameter("@Unit",ctggd.Unit),
  102. new SqlParameter("@CompanyOrGuide",ctggd.CompanyOrGuide),
  103. new SqlParameter("@Operators",ctggd.Operators),
  104. new SqlParameter("@OperatorDate",ctggd.OperatorDate),
  105. new SqlParameter("@Remark",ctggd.Remark),
  106. new SqlParameter("@Currency",ctggd.Currency),
  107. new SqlParameter("@IsDel",ctggd.IsDel)
  108. };
  109. if (SqlHelper.ExecuteNonQuery(sql, CommandType.Text, parameter) > 0)
  110. return true;
  111. return false;
  112. }
  113. /// <summary>
  114. /// 批量添加方法
  115. /// </summary>
  116. /// <param name="list"></param>
  117. /// <returns></returns>
  118. public bool AddCarCompanyAndTouristGuide(List<CarCompanyAndTouristGuide> list)
  119. {
  120. SqlCommand cmd = SqlHelper.createCon().CreateCommand();
  121. cmd.Connection.Open();
  122. // SqlTransaction trans = cmd.Connection.BeginTransaction();
  123. try
  124. {
  125. foreach (CarCompanyAndTouristGuide ctg in list)
  126. {
  127. if (ctg.CarType != null)
  128. {
  129. cmd.CommandText = "insert into CarCompanyAndTouristGuide values(" + ctg.CGId + ",'" + ctg.CarType + "'," + ctg.Price + ",'" + ctg.Unit + "'," + ctg.CompanyOrGuide + "," + ctg.Operators + ",'" + ctg.OperatorDate + "','" + ctg.Remark + ",'" + ctg.Currency + "'," + ctg.IsDel + "')";
  130. }
  131. else
  132. {
  133. cmd.CommandText = "insert into CarCompanyAndTouristGuide values(" + ctg.CGId + ",' '," + ctg.Price + ",'" + ctg.Unit + "'," + ctg.CompanyOrGuide + "," + ctg.Operators + ",'" + ctg.OperatorDate + "','" + ctg.Remark + ",'" + ctg.Currency + "'," + ctg.IsDel + "')";
  134. }
  135. cmd.ExecuteNonQuery();
  136. }
  137. //trans.Commit();
  138. cmd.Connection.Close();
  139. return true;
  140. }
  141. catch (Exception ex)
  142. {
  143. string error = ex.Message.ToString();
  144. // trans.Rollback();
  145. cmd.Connection.Close();
  146. return false;
  147. }
  148. }
  149. /// <summary>
  150. /// 新增
  151. /// 20210828 贾文滔
  152. /// </summary>
  153. /// <param name="CarCompanyAndTouristGuide"></param>
  154. /// <returns></returns>
  155. public bool Edit(CarCompanyAndTouristGuide ctggd)
  156. {
  157. string sql = "update CarCompanyAndTouristGuide set CGId=@CGId,CarType=@CarType,Price=@Price,Unit=@Unit," +
  158. "CompanyOrGuide=@CompanyOrGuide,Operators=@Operators,OperatorDate=@OperatorDate,Remark=@Remark" +
  159. "Currency=@Currency,IsDel=@IsDel where Id = @Id";
  160. SqlParameter[] parameter = new SqlParameter[]{
  161. new SqlParameter("@CGId",ctggd.CGId),
  162. new SqlParameter("@CarType",ctggd.CarType),
  163. new SqlParameter("@Price",ctggd.Price),
  164. new SqlParameter("@Unit",ctggd.Unit),
  165. new SqlParameter("@CompanyOrGuide",ctggd.CompanyOrGuide),
  166. new SqlParameter("@Operators",ctggd.Operators),
  167. new SqlParameter("@OperatorDate",ctggd.OperatorDate),
  168. new SqlParameter("@Remark",ctggd.Remark),
  169. new SqlParameter("@Currency",ctggd.Currency),
  170. new SqlParameter("@IsDel",ctggd.IsDel)
  171. };
  172. if (SqlHelper.ExecuteNonQuery(sql, CommandType.Text, parameter) > 0)
  173. return true;
  174. return false;
  175. }
  176. /// <summary>
  177. /// 批量更新方法
  178. /// </summary>
  179. /// <param name="list"></param>
  180. /// <returns></returns>
  181. public bool UpdateCarCompanyAndTouristGuide(List<CarCompanyAndTouristGuide> list)
  182. {
  183. SqlCommand cmd = SqlHelper.createCon().CreateCommand();
  184. cmd.Connection.Open();
  185. // SqlTransaction trans = cmd.Connection.BeginTransaction();
  186. try
  187. {
  188. foreach (CarCompanyAndTouristGuide ctg in list)
  189. {
  190. if (ctg.CarType != null)
  191. {
  192. cmd.CommandText = "update CarCompanyAndTouristGuide set CarType= '" + ctg.CarType + "', Price = " + ctg.Price + ", Unit = '" + ctg.Unit + "',CompanyOrGuide = " + ctg.CompanyOrGuide + ",Operators = " + ctg.Operators + ",OperatorDate = '" + ctg.OperatorDate + "',Remark = '" + ctg.Remark + "',Currency = '" + ctg.Currency + "' where id = " + ctg.Id + "";
  193. }
  194. else
  195. {
  196. cmd.CommandText = "update CarCompanyAndTouristGuide set CarType= '" + ctg.CarType + "', Price = " + ctg.Price + ", Unit = '" + ctg.Unit + "',CompanyOrGuide = " + ctg.CompanyOrGuide + ",Operators = " + ctg.Operators + ",OperatorDate = '" + ctg.OperatorDate + "',Remark = '" + ctg.Remark + "',Currency = '" + ctg.Currency + "' where Cgid = " + ctg.Id + "";
  197. }
  198. cmd.ExecuteNonQuery();
  199. }
  200. //trans.Commit();
  201. cmd.Connection.Close();
  202. return true;
  203. }
  204. catch
  205. {
  206. // trans.Rollback();
  207. cmd.Connection.Close();
  208. return false;
  209. }
  210. }
  211. /// <summary>
  212. /// 批量更新方法
  213. /// </summary>
  214. /// <param name="list"></param>
  215. /// <returns></returns>
  216. public bool UpdateCarCompanyAndTouristGuide2(List<CarCompanyAndTouristGuide> list)
  217. {
  218. SqlCommand cmd = SqlHelper.createCon().CreateCommand();
  219. cmd.Connection.Open();
  220. // SqlTransaction trans = cmd.Connection.BeginTransaction();
  221. try
  222. {
  223. foreach (CarCompanyAndTouristGuide ctg in list)
  224. {
  225. if (ctg.CarType != null)
  226. {
  227. cmd.CommandText = "update CarCompanyAndTouristGuide set CarType= '" + ctg.CarType + "', Price = " + ctg.Price + ", Unit = '" + ctg.Unit + "',CompanyOrGuide = " + ctg.CompanyOrGuide + ",Operators = " + ctg.Operators + ",OperatorDate = '" + ctg.OperatorDate + "',Remark = '" + ctg.Remark + "',Currency = '" + ctg.Currency + "' where cgid = " + ctg.Id + "";
  228. }
  229. else
  230. {
  231. cmd.CommandText = "update CarCompanyAndTouristGuide set CarType= '" + ctg.CarType + "', Price = " + ctg.Price + ", Unit = '" + ctg.Unit + "',CompanyOrGuide = " + ctg.CompanyOrGuide + ",Operators = " + ctg.Operators + ",OperatorDate = '" + ctg.OperatorDate + "',Remark = '" + ctg.Remark + "',Currency = '" + ctg.Currency + "' where cgid = " + ctg.Id + "";
  232. }
  233. cmd.ExecuteNonQuery();
  234. }
  235. //trans.Commit();
  236. cmd.Connection.Close();
  237. return true;
  238. }
  239. catch
  240. {
  241. // trans.Rollback();
  242. cmd.Connection.Close();
  243. return false;
  244. }
  245. }
  246. }
  247. }