using Dapper; using dccdc.Models; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; namespace dccdc.DAL { public class ckdDal { public List GetAllList(string id) { using (IDbConnection conn = CommHelper.GetSqlConnection()) { string sql = "select * from ckd where 1=1"; if (!string.IsNullOrEmpty(id)) { sql += " and id=@id"; } return conn.Query(sql, new { @id = id }).ToList(); } } public List GetAllList(string id, string zt) { using (IDbConnection conn = CommHelper.GetSqlConnection()) { string sql = "select * from ckd where 1=1"; if (!string.IsNullOrEmpty(id)) { sql += " and id=@id"; } if (!string.IsNullOrEmpty(zt)) { sql += " and zt=@zt"; } return conn.Query(sql, new { @id = id, @zt = zt }).ToList(); } } public object save(ckdModel model) { string sql = ""; if (model.id == 0) { sql = @"INSERT INTO [dbo].[ckd] ([ckdh] ,[cksj] ,[ckrid] ,[ckr] ,[ksid] ,[ksmc] ,[ckje] ,[lyr]) VALUES (@ckdh ,@cksj ,@ckrid ,@ckr ,@ksid ,@ksmc ,@ckje ,@lyr)"; } else { sql = @"UPDATE [dbo].[ckd] SET [ckdh] = @ckdh ,[cksj] = @cksj ,[ckrid] = @ckrid ,[ckr] = @ckr ,[ksid] = @ksid ,[ksmc] = @ksmc ,[ckje] = @ckje ,[lyr] = @lyr WHERE id=@id"; } using (IDbConnection conn = CommHelper.GetSqlConnection()) { try { int result = conn.Execute(sql, model); if (result > 0) return new { State = 1, Message = "保存成功!" }; else return new { State = 0, Message = "保存失败!" }; } catch (Exception ex) { return new { State = 0, Message = ex.Message }; } } } //public bool opSave(string id, string ksid, string ksmc,string ckje, string lyr, string items, int ckrid, string ckr) //{ public List getCkdByCkdh(string ckdh) { string sql = "select * from ckd where ckdh in (@ckdh)"; using (IDbConnection conn = CommHelper.GetSqlConnection()) { return conn.Query(sql, new { ckdh = ckdh }).ToList(); } } public object getCudMxByCkdId(string ckdid) { string sql = "select a.*,b.ggxh,b.jldw dw,(a.sl*a.dj) hj from ckdmx a join wz b on a.wzid=b.id where ckdid in (@ckdid)"; using (IDbConnection conn = CommHelper.GetSqlConnection()) { return conn.Query(sql, new { ckdid = ckdid }).ToList(); } } public object opSave(string id, string ksid, string ksmc, string lyr, string items, int ckrid, string ckr,string ckje) { string errmsg = ""; string ckdh = getckkdh(out errmsg); if (errmsg != "") return new { State = 0, Message = "false" }; ckdModel model = new ckdModel(); model.ckdh = ckdh; model.ckrid = ckrid; model.ckr = ckr; model.cksj = DateTime.Now; model.ksid = Convert.ToInt32(ksid); model.ksmc = ksmc; model.ckje = Convert.ToDecimal(ckje); model.lyr = lyr; string sql = @"INSERT INTO [dbo].[ckd] ([ckdh],[cksj],[ckrid],[ckr],[ksid],[ksmc],[ckje],[lyr]) VALUES (@ckdh,@cksj,@ckrid,@ckr,@ksid,@ksmc,@ckje,@lyr)select SCOPE_IDENTITY()"; int ckdid = 0; using (IDbConnection conn = CommHelper.GetSqlConnection()) { try { ckdid = conn.Query(sql, model).FirstOrDefault(); } catch (Exception ex) { return new { State = 0, Message = "false" }; } } List models = new List(); ckdmxModel m; foreach (string item in items.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) { string[] itemss = item.Split('|'); if (itemss.Length == 5) { m = new ckdmxModel(); m.ckdid = ckdid; m.wzid = Convert.ToInt32(itemss[0]); m.wzmc = itemss[1]; m.dj = Convert.ToDecimal(itemss[2]); m.sl = Convert.ToDecimal(itemss[3]); m.sign = Convert.ToInt32(itemss[4]); m.czsj = DateTime.Now; models.Add(m); } } string sql2 = @"INSERT INTO [dbo].[ckdmx] ([ckdid],[wzid],[wzmc],[sl],[dj],[czsj],[sign]) VALUES (@ckdid,@wzid,@wzmc,@sl,@dj,@czsj,@sign)"; using (IDbConnection conn = CommHelper.GetSqlConnection()) { try { int result = conn.Execute(sql2, models); if (result > 0) { //退货 ck(models, -1); return new { State = 1, Message = ckdh }; } else return new { State = 0, Message = "false" }; } catch (Exception ex) { return new { State = 0, Message = "false" }; } } } private void ck(List models, int multiplier) { List kcModels = new List(); kcModel kcModel; foreach (ckdmxModel model in models) { kcModel = new kcModel(); kcModel.wzid = model.wzid; kcModel.wzmc = model.wzmc; kcModel.sl = model.sl * multiplier; //负数 kcModel.sign = model.sign; kcModels.Add(kcModel); } new kcDal().saveList(kcModels); } //获取申请单号 private string getckkdh(out string errmsg) { errmsg = ""; string result = ""; string start = DateTime.Now.ToString("yyyyMM") + "0001"; string end = DateTime.Now.ToString("yyyyMM") + "9999"; using (IDbConnection conn = CommHelper.GetSqlConnection()) { string sql = "select top 1 ckdh from ckd where ckdh >= '" + start + "' and ckdh<= '" + end + "' order by id desc"; try { var result2 = conn.ExecuteScalar(sql); if (result2 == null) result = start; else { result = (Convert.ToInt32(result2) + 1).ToString(); if (result == end) errmsg = "单号不足!"; } } catch (Exception ex) { errmsg = ex.Message; } } return result; } public object delete(string id) { string sql = "delete from ckd where id=@id"; string sql2 = "delete from ckdmx where ckdid=@id"; using (IDbConnection conn = CommHelper.GetSqlConnection()) { try { //删除之前获取明细 var kclist = new ckdmxDal().GetListByParent(id); int result = conn.Execute(sql, new { id = id }); result = conn.Execute(sql2, new { id = id }); if (result > 0) { //退货 ck(kclist, 1); return new { State = 1, Message = "操作成功!" }; } else return new { State = 0, Message = "操作失败!" }; } catch (Exception ex) { return new { State = 0, Message = ex.Message }; } } } public int getCount(string key) { string sql = "select count(1) from dbo.ckd where 1=1"; if (!string.IsNullOrEmpty(key)) { sql += " and ksid = @key"; } using (IDbConnection conn = CommHelper.GetSqlConnection()) { return conn.ExecuteScalar(sql, new { key = key }); } } public List getPage(int page, int pagesize, string key) { string sql = "select *,row_number() over(order by id desc) as rownum from ckd where 1=1"; if (!string.IsNullOrEmpty(key)) { sql += " and ksid = @key"; } sql = "select * from (" + sql + ") t where t.rownum>(" + page + "-1)*" + pagesize + " and rownum<=" + page + "*" + pagesize; using (IDbConnection conn = CommHelper.GetSqlConnection()) { return conn.Query(sql, new { key = key }).ToList(); } } } }