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 pddDal { public List GetAllList(string id) { using (IDbConnection conn = CommHelper.GetSqlConnection()) { string sql = "select * from pdd 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 pdd 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(pddModel model) { string sql = ""; if (model.id == 0) { sql = @"INSERT INTO [dbo].[pdd] ([pddh] ,[pdsj] ,[pdrid] ,[pdr] ,[ksid] ,[ksmc] ,[pdje] ,[bz]) VALUES (@pddh ,@pdsj ,@pdrid ,@pdr ,@ksid ,@ksmc ,@pdje ,@bz)"; } else { sql = @"UPDATE [dbo].[pdd] SET [pddh] = @pddh ,[pdsj] = @pdsj ,[pdrid] = @pdrid ,[pdr] = @pdr ,[ksid] = @ksid ,[ksmc] = @ksmc ,[pdje] = @pdje ,[bz] = @bz 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 object opSave(string id, string ksid, string ksmc, string bz, string items, int pdrid, string pdr,string pdje) { string errmsg = ""; string pddh = getpdkdh(out errmsg); if (errmsg != "") return new { State = 0, Message = "false" }; pddModel model = new pddModel(); model.pddh = pddh; model.pdrid = pdrid; model.pdr = pdr; model.pdsj = DateTime.Now; model.ksid = Convert.ToInt32(ksid); model.ksmc = ksmc; model.bz = bz; model.pdje = decimal.Parse( pdje); string sql = @"INSERT INTO [dbo].[pdd] ([pddh],[pdsj],[pdrid],[pdr],[ksid],[ksmc],[bz],pdje) VALUES (@pddh,@pdsj,@pdrid,@pdr,@ksid ,@ksmc,@bz,@pdje)select SCOPE_IDENTITY()"; int pddid = 0; using (IDbConnection conn = CommHelper.GetSqlConnection()) { try { pddid = conn.Query(sql, model).FirstOrDefault(); } catch (Exception ex) { return new { State = 0, Message = "false" }; } } List models = new List(); pddmxModel m; foreach (string item in items.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) { string[] itemss = item.Split('|'); if (itemss.Length == 6) { m = new pddmxModel(); m.pddid = pddid; m.wzid = Convert.ToInt32(itemss[0]); m.wzmc = itemss[1]; m.dj = Convert.ToDecimal(itemss[2]); m.sl = Convert.ToInt32(itemss[3]); m.oldsl = Convert.ToInt32(itemss[4]); m.sign = Convert.ToInt32(itemss[5]); m.czsj = DateTime.Now; models.Add(m); } } string sql2 = @"INSERT INTO [dbo].[pddmx] ([pddid],[wzid],[wzmc],[sl],[dj],[oldsl],[czsj],[sign]) VALUES (@pddid,@wzid,@wzmc,@sl,@dj,@oldsl,@czsj,@sign)"; using (IDbConnection conn = CommHelper.GetSqlConnection()) { try { int result = conn.Execute(sql2, models); if (result > 0) { //退货 pd(models, 1); return new { State = 1, Message = pddh }; } else return new { State = 0, Message = "false" }; } catch (Exception ex) { return new { State = 0, Message = "false" }; } } } public List getPddByPddh(string ppdh) { string sql = "select * from pdd where pddh in (@ppdh)"; using (IDbConnection conn = CommHelper.GetSqlConnection()) { return conn.Query(sql, new { ppdh = ppdh }).ToList(); } } public object getPddMxByPddId(string pddid) { string sql = "select a.*,b.ggxh,b.jldw dw,(a.sl*a.dj) hj from pddmx a join wz b on a.wzid=b.id where a.pddid in (@pddid)"; using (IDbConnection conn = CommHelper.GetSqlConnection()) { return conn.Query(sql, new { pddid = pddid }).ToList(); } } private void pd(List models, int multiplier) { List kcModels = new List(); kcModel kcModel; foreach (pddmxModel 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 getpdkdh(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 pddh from pdd where pddh >= '" + start + "' and pddh<= '" + 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 pdd where id=@id"; string sql2 = "delete from pddmx where pddid=@id"; using (IDbConnection conn = CommHelper.GetSqlConnection()) { try { //删除之前获取明细 var kclist = new pddmxDal().GetListByParent(id); int result = conn.Execute(sql, new { id = id }); result = conn.Execute(sql2, new { id = id }); if (result > 0) { //退货 pd(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.pdd where 1=1"; if (!string.IsNullOrEmpty(key)) { sql += " and pdr like @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 pdd where 1=1"; if (!string.IsNullOrEmpty(key)) { sql += " and pdr like @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(); } } } }