using System; using System.Data; using System.Collections.Generic; using System.Text; using DSWeb.MvcShipping.Models.MsOpCtnStatus; using Microsoft.Practices.EnterpriseLibrary.Data; using DSWeb.EntityDA; using HcUtility.Comm; using System.Web; using System.Text.RegularExpressions; using System.Data.Common; using DSWeb.TruckMng.Helper; using DSWeb.Areas.CommMng.DAL; using DSWeb.MvcShipping.Models.MsOpCtnRepair; using System.Data.SqlClient; using DSWeb.DataAccess; namespace DSWeb.Areas.MvcShipping.DAL { public class MsOpCtnRepairDAL { static public List GetDataList ( string strCondition, int start, int limit, out int total, string sort = null ) { var strSql = new StringBuilder(); strSql.Append(@"SELECT * FROM Op_CtnRepair WHERE DelFlag = 0 "); if (!string.IsNullOrEmpty(strCondition)) { strSql.Append(" and " + strCondition); } if (!string.IsNullOrEmpty(sort)) { var so = JsonConvert.Deserialize>(sort); string orderbyStr = " order by "; foreach (var s in so) { orderbyStr += s.property + " " + s.direction + ","; } orderbyStr = orderbyStr.Substring(0, orderbyStr.Length - 1); strSql.Append(orderbyStr); } else { strSql.Append(" order by UpdateTime desc "); } int totalCount = 0; StringBuilder sb = PagerHelper.PageSQL(strSql.ToString(), start, limit, out totalCount); total = totalCount; return SetData(sb); } private static List SetData ( StringBuilder strSql ) { var headList = new List(); Database db = DatabaseFactory.CreateDatabase(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString())) { while (reader.Read()) { MsOpCtnRepair data = new MsOpCtnRepair(); #region Set DB data to Object data.GID = reader["GID"].ToString(); data.XiangZhu = reader["XiangZhu"].ToString(); data.ChangZhan = reader["ChangZhan"].ToString(); data.ChangZhanSub = reader["ChangZhanSub"].ToString(); data.XiuXiangDanHao = reader["XiuXiangDanHao"].ToString(); data.JinChangRiQi = DateTime.Parse(reader["JinChangRiQi"].ToString()).ToString("yyyy-MM-dd"); data.XiuXiangRiQi = DateTime.Parse(reader["XiuXiangRiQi"].ToString()).ToString("yyyy-MM-dd"); data.ZaoXiangRiQi = reader["ZaoXiangRiQi"].ToString(); data.XiangHao = reader["XiangHao"].ToString(); data.ChuanMing = reader["ChuanMing"].ToString(); data.MBLNO = reader["MBLNO"].ToString(); data.XiangXing = reader["XiangXing"].ToString(); data.GongShiFei = reader["GongShiFei"].ToString(); data.ShuiLv = reader["ShuiLv"].ToString(); data.CeLiangDanWei = reader["CeLiangDanWei"].ToString(); data.BiBie = reader["BiBie"].ToString(); data.IsLock = reader["IsLock"].ToString(); data.IsSendMail = reader["IsSendMail"].ToString(); #endregion headList.Add(data); } reader.Close(); } return headList; } public static bool DeleteData (string xiuxiangdanhao) { string sql = "update Op_CtnRepair set DelFlag = 1 where XiuXiangDanHao in ( " + xiuxiangdanhao + ")"; Database db = DatabaseFactory.CreateDatabase(); string sqldet = "update Op_CtnRepairDetails set DelFlag = 1 where XiuXiangDanHao in ( " + xiuxiangdanhao + ")"; bool rst = db.ExecuteNonQuery(CommandType.Text, sql) > 0; db.ExecuteNonQuery(CommandType.Text, sql); return rst; } public static bool Save ( MsOpCtnRepair mainData,List detailsList,out string RepairID) { #region 保存主表 bool rst = false; Database db = DatabaseFactory.CreateDatabase(); if (checkHasRepairData(mainData.GID, mainData.XiuXiangDanHao)) { //update StringBuilder sbMainUpd = new StringBuilder(); sbMainUpd.Append("update Op_CtnRepair set "); sbMainUpd.Append("XiangZhu = '" + mainData.XiangZhu + "',"); sbMainUpd.Append("ChangZhan = '" + mainData.ChangZhan + "',"); sbMainUpd.Append("ChangZhanSub = '" + mainData.ChangZhanSub + "',"); sbMainUpd.Append("JinChangRiQi = '" + mainData.JinChangRiQi + "',"); sbMainUpd.Append("XiuXiangRiQi = '" + mainData.XiuXiangRiQi + "',"); sbMainUpd.Append("ZaoXiangRiQi = '" + mainData.ZaoXiangRiQi + "',"); sbMainUpd.Append("XiangHao = '" + mainData.XiangHao + "',"); sbMainUpd.Append("ChuanMing = '" + mainData.ChuanMing + "',"); sbMainUpd.Append("MBLNO = '" + mainData.MBLNO + "',"); sbMainUpd.Append("XiangXing = '" + mainData.XiangXing + "',"); string GongShiFei = mainData.GongShiFei == null ? "0" : (mainData.GongShiFei == "" ? "0" : mainData.GongShiFei); sbMainUpd.Append("GongShiFei = " + GongShiFei + ","); sbMainUpd.Append("BiBie = '" + mainData.BiBie + "',"); string ShuiLv = mainData.ShuiLv == null ? "0" : (mainData.ShuiLv == "" ? "0" : mainData.ShuiLv); sbMainUpd.Append("ShuiLv = " + ShuiLv + ","); sbMainUpd.Append("CeLiangDanWei = '" + mainData.CeLiangDanWei + "',"); sbMainUpd.Append("DelFlag = 0 "); sbMainUpd.Append("where xiuxiangdanhao = '"+mainData.XiuXiangDanHao+"'"); rst = db.ExecuteNonQuery(CommandType.Text, sbMainUpd.ToString()) > 0; } else { //insert mainData.GID = Guid.NewGuid().ToString(); StringBuilder sbMainIns = new StringBuilder(); sbMainIns.Append("insert into Op_CtnRepair"); sbMainIns.Append(@"(GID , XiangZhu , ChangZhan , ChangZhanSub , XiuXiangDanHao , JinChangRiQi , XiuXiangRiQi , ZaoXiangRiQi , XiangHao , ChuanMing , MBLNO , XiangXing , GongShiFei , BiBie , ShuiLv , CeLiangDanWei , IsLock , IsSendMail) "); sbMainIns.Append(" values "); sbMainIns.Append("("); sbMainIns.Append("'" + mainData.GID + "',"); sbMainIns.Append("'" + mainData.XiangZhu + "',"); sbMainIns.Append("'" + mainData.ChangZhan + "',"); sbMainIns.Append("'" + mainData.ChangZhanSub + "',"); sbMainIns.Append("'" + mainData.XiuXiangDanHao + "',"); sbMainIns.Append("'" + mainData.JinChangRiQi + "',"); sbMainIns.Append("'" + mainData.XiuXiangRiQi + "',"); sbMainIns.Append("'" + mainData.ZaoXiangRiQi + "',"); sbMainIns.Append("'" + mainData.XiangHao + "',"); sbMainIns.Append("'" + mainData.ChuanMing + "',"); sbMainIns.Append("'" + mainData.MBLNO + "',"); sbMainIns.Append("'" + mainData.XiangXing + "',"); string GongShiFei = mainData.GongShiFei == null ? "0" : (mainData.GongShiFei == "" ? "0" : mainData.GongShiFei); sbMainIns.Append("" + GongShiFei + ","); sbMainIns.Append("'" + mainData.BiBie + "',"); string ShuiLv = mainData.ShuiLv == null ? "0" : (mainData.ShuiLv==""?"0":mainData.ShuiLv); sbMainIns.Append("" + ShuiLv + ","); sbMainIns.Append("'" + mainData.CeLiangDanWei + "',"); sbMainIns.Append("0,"); sbMainIns.Append("0"); sbMainIns.Append(")"); rst = db.ExecuteNonQuery(CommandType.Text, sbMainIns.ToString()) > 0; } #endregion #region 保存明细表 if (detailsList!=null) { string delStr = "delete from Op_CtnRepairDetails where RepairID = '"+mainData.GID+"' "; db.ExecuteNonQuery(CommandType.Text,delStr); StringBuilder insValues = new StringBuilder(); foreach (var item in detailsList) { insValues.Append("("); insValues.Append("'" + item.GID + "',"); insValues.Append("'" + mainData.GID + "',"); insValues.Append("'" + mainData.XiuXiangDanHao + "',"); insValues.Append("'" + item.XuHao + "',"); insValues.Append("'" + item.WeiXiuMiaoShu + "',"); insValues.Append("'" + item.ShuLiang + "',"); insValues.Append("'" + item.ZeRenRen + "',"); insValues.Append("'" + item.WeiXiuGongShi + "',"); insValues.Append("'" + item.GongShiFei + "',"); insValues.Append("'" + item.CaiLiaoFei + "',"); insValues.Append("'" + item.IsRepair + "',"); insValues.Append("'" + item.IsShouFei + "'"); insValues.Append("),"); } string valuesStr = insValues.ToString().Substring(0,insValues.ToString().Length-1); StringBuilder detInsSb = new StringBuilder(); detInsSb.Append("insert into Op_CtnRepairDetails "); detInsSb.Append(@"(GID , RepairID , XiuXiangDanHao , XuHao , WeiXiuMiaoShu , ShuLiang , ZeRenRen , WeiXiuGongShi , GongShiFei , CaiLiaoFei , IsRepair , IsShouFei) "); detInsSb.Append("values"); detInsSb.Append(valuesStr); db.ExecuteNonQuery(CommandType.Text,detInsSb.ToString()); } #endregion RepairID = mainData.GID; return rst; } static public List GetDetailsList ( string strCondition, int start, int limit, out int total, string sort = null ) { var strSql = new StringBuilder(); strSql.Append(@"SELECT * FROM Op_CtnRepairDetails WHERE DelFlag = 0 "); if (!string.IsNullOrEmpty(strCondition)) { strSql.Append(" and " + strCondition); } if (!string.IsNullOrEmpty(sort)) { var so = JsonConvert.Deserialize>(sort); string orderbyStr = " order by "; foreach (var s in so) { orderbyStr += s.property + " " + s.direction + ","; } orderbyStr = orderbyStr.Substring(0, orderbyStr.Length - 1); strSql.Append(orderbyStr); } else { strSql.Append(" order by XuHao "); } int totalCount = 0; StringBuilder sb = PagerHelper.PageSQL(strSql.ToString(), start, limit, out totalCount); total = totalCount; return SetDetailsData(sb); } private static List SetDetailsData ( StringBuilder strSql ) { var headList = new List(); Database db = DatabaseFactory.CreateDatabase(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString())) { while (reader.Read()) { MsOpCtnRepairDetails data = new MsOpCtnRepairDetails(); #region Set DB data to Object data.GID = reader["GID"].ToString(); data.RepairID = reader["RepairID"].ToString(); data.XiuXiangDanHao = reader["XiuXiangDanHao"].ToString(); data.XuHao = reader["XuHao"].ToString(); data.XiuXiangDanHao = reader["XiuXiangDanHao"].ToString(); data.WeiXiuMiaoShu = reader["WeiXiuMiaoShu"].ToString(); data.ShuLiang = reader["ShuLiang"].ToString(); data.ZeRenRen = reader["ZeRenRen"].ToString(); data.WeiXiuGongShi = reader["WeiXiuGongShi"].ToString(); data.GongShiFei = reader["GongShiFei"].ToString(); data.CaiLiaoFei = reader["CaiLiaoFei"].ToString(); data.IsRepair = reader["IsRepair"].ToString(); data.IsShouFei = reader["IsShouFei"].ToString(); data.DelFlag = reader["DelFlag"].ToString(); #endregion headList.Add(data); } reader.Close(); } return headList; } public static bool LockAndCreateCost ( string RepairID, string IsLock,string userid) { //锁定 string sql = "update Op_CtnRepair set IsLock = '"+IsLock+"' where GID = '"+RepairID+"'"; Database db = DatabaseFactory.CreateDatabase(); int rst = db.ExecuteNonQuery(CommandType.Text, sql); #region 费用入库 //删除旧数据 string delSql = "delete from ch_fee where bsno = '"+RepairID+"'"; db.ExecuteNonQuery(CommandType.Text, delSql); //计算费用 float yingshou = 0; float yingfu = 0; string biBie = ""; string custName = feeCalculate(RepairID, out yingshou, out yingfu, out biBie); //插入新数据 if (yingshou>0) { //应收 StringBuilder sb = new StringBuilder(); sb.Append(@"insert into ch_fee (GID,BSNO,FEETYPE,FEENAME,REMARK,CUSTOMERTYPE,CUSTOMERNAME"); sb.Append(@",UNIT,UNITPRICE,QUANTITY,AMOUNT,CURRENCY,EXCHANGERATE,FEESTATUS,NOTAXAMOUNT,TAXUNITPRICE,WMSOUTBSNO,ENTEROPERATOR)"); sb.Append(@"VALUES("); sb.Append("'" + Guid.NewGuid() + "',");//GID sb.Append("'" + RepairID + "',");//BSNO sb.Append("1" + ",");//FEETYPE sb.Append("'" + "修箱费" + "',");//FEENAME sb.Append("'',");//REMARK sb.Append("'',");//CUSTOMERTYPE sb.Append("'" + custName + "',");//CUSTOMERNAME sb.Append("'',");//UNIT sb.Append(yingshou + ",");//UNITPRICE sb.Append("1" + ",");//QUANTITY sb.Append(yingshou + ",");//AMOUNT sb.Append("'" + biBie + "',");//CURRENCY sb.Append("1" + ",");//EXCHANGERATE sb.Append("1" + ",");//FEESTATUS sb.Append(yingshou + ",");//NOTAXAMOUNT sb.Append(yingshou + ",");//TAXUNITPRICE sb.Append("'" + RepairID + "',");//WMSOUTBSNO sb.Append("'" + userid + "'");//ENTEROPERATOR sb.Append(")"); db.ExecuteNonQuery(CommandType.Text, sb.ToString()); } if (yingfu>0) { //应付 StringBuilder sb = new StringBuilder(); sb.Append(@"insert into ch_fee (GID,BSNO,FEETYPE,FEENAME,REMARK,CUSTOMERTYPE,CUSTOMERNAME"); sb.Append(@",UNIT,UNITPRICE,QUANTITY,AMOUNT,CURRENCY,EXCHANGERATE,FEESTATUS,NOTAXAMOUNT,TAXUNITPRICE,WMSOUTBSNO,ENTEROPERATOR)"); sb.Append(@"VALUES("); sb.Append("'" + Guid.NewGuid() + "',");//GID sb.Append("'" + RepairID + "',");//BSNO sb.Append("2" + ",");//FEETYPE sb.Append("'" + "修箱费" + "',");//FEENAME sb.Append("'',");//REMARK sb.Append("'',");//CUSTOMERTYPE sb.Append("'" + custName + "',");//CUSTOMERNAME sb.Append("'',");//UNIT sb.Append(yingfu + ",");//UNITPRICE sb.Append("1" + ",");//QUANTITY sb.Append(yingfu + ",");//AMOUNT sb.Append("'" + biBie + "',");//CURRENCY sb.Append("1" + ",");//EXCHANGERATE sb.Append("1" + ",");//FEESTATUS sb.Append(yingfu + ",");//NOTAXAMOUNT sb.Append(yingfu + ",");//TAXUNITPRICE sb.Append("'" + RepairID + "',");//WMSOUTBSNO sb.Append("'" + userid + "'");//ENTEROPERATOR sb.Append(")"); db.ExecuteNonQuery(CommandType.Text, sb.ToString()); } #endregion return rst > 0; } private static bool checkHasRepairData ( string gid ,string xiuxiangdanhao) { if (gid==""||gid==null||gid=="*") { return false; } string sql = "select count(*) from Op_CtnRepair where gid = '" + gid + "' or xiuxiangdanhao ='" + xiuxiangdanhao + "'"; Database db = DatabaseFactory.CreateDatabase(); int rst = Convert.ToInt32(db.ExecuteScalar(CommandType.Text,sql)); return rst > 0; } /// /// /// /// /// /// /// CustomName private static string feeCalculate (string RepairID,out float yingshouFee,out float yingfuFee,out string biBie) { int total = 0; var data = GetDataList("GID='" + RepairID + "'", 0, 1, out total); MsOpCtnRepair mainData = null; if (data!=null&&data.Count>0) { mainData = data[0]; } float ShuiLv = float.Parse(mainData.ShuiLv==""?"0":mainData.ShuiLv); List detailsList = GetDetailsList("RepairID='" + RepairID + "'", 0, 999, out total); float yingShou = 0; float yingFu = 0; foreach (var item in detailsList) { if (item.ZeRenRen == "O") { if (item.IsRepair == "1") { yingFu += float.Parse(item.GongShiFei == "" ? "0" : item.GongShiFei) + float.Parse(item.CaiLiaoFei == "" ? "0" : item.CaiLiaoFei); } else { //不计入费用 } } else { if (item.IsRepair=="1" && item.IsShouFei=="0") { yingFu += float.Parse(item.GongShiFei == "" ? "0" : item.GongShiFei) + float.Parse(item.CaiLiaoFei == "" ? "0" : item.CaiLiaoFei); } else if (item.IsRepair=="1" && item.IsShouFei=="1") { //不计入费用 } else if (item.IsRepair=="0" && item.IsShouFei=="0") { //不计入费用 } else if (item.IsRepair=="0" && item.IsShouFei=="1") { yingShou += float.Parse(item.GongShiFei == "" ? "0" : item.GongShiFei) + float.Parse(item.CaiLiaoFei == "" ? "0" : item.CaiLiaoFei); } } } yingshouFee = yingShou; yingfuFee = yingFu; biBie = mainData.BiBie; return mainData.ChangZhan; } #region 导入Excel public static DBResult ImportCtnRepairData ( DataTable table, string userid,out string repairId ) { var result = new DBResult(); if (table == null) throw new ArgumentNullException("table"); MsOpCtnRepair main = new MsOpCtnRepair(); List details = new List(); //主表数据 for (int i = 0; i < table.Rows.Count; i++) { DataRow r = table.Rows[i]; if (i == 0) { main.XiangZhu = r[1].ToString(); } else if (i == 1) { main.ChangZhanSub = r[1].ToString(); } else if (i == 2) { main.XiuXiangDanHao = r[1].ToString(); } else if (i == 3) { string date = DateTime.Parse(r[1].ToString()).ToString("yyyy-MM-dd"); main.JinChangRiQi = date; } else if (i == 4) { string date = DateTime.Parse(r[1].ToString()).ToString("yyyy-MM-dd"); main.XiuXiangRiQi = date; } else if (i == 5) { main.ZaoXiangRiQi = r[1].ToString(); } else if (i == 6) { main.XiangHao = r[1].ToString(); } else if (i == 7) { main.ChuanMing = r[1].ToString(); } else if (i == 8) { main.MBLNO= r[1].ToString(); } else if (i == 9) { main.XiangXing = r[1].ToString(); } else if (i == 10) { main.GongShiFei = r[1].ToString(); } else if (i == 11) { main.BiBie = r[1].ToString(); } else if (i == 12) { main.CeLiangDanWei = r[1].ToString(); } //明细 else if (i>=15) { if (!IsInt(r[0].ToString())) { break; } MsOpCtnRepairDetails d = new MsOpCtnRepairDetails(); d.GID = Guid.NewGuid().ToString(); d.XuHao = r[0].ToString(); d.WeiXiuMiaoShu = r[1].ToString(); d.ShuLiang = r[2].ToString(); d.ZeRenRen = r[3].ToString(); d.WeiXiuGongShi = r[4].ToString(); d.GongShiFei = r[5].ToString(); d.CaiLiaoFei = r[6].ToString(); details.Add(d); } } main.GID = GetRepairIdWithXiuXiangDanHao(main.XiuXiangDanHao); string RepairID = ""; bool rst= Save(main, details, out RepairID); repairId = RepairID; result.Success = rst; result.Message = rst? "导入成功!":"导入失败!"; return result; } public static bool IsInt ( string value ) { if (value==null||value.Trim()=="") { return false; } return Regex.IsMatch(value, @"^[+-]?\d*$"); } private static string GetRepairIdWithXiuXiangDanHao ( string xiuxiangdanhao ) { string sql = "select top 1 gid from Op_CtnRepair where xiuxiangdanhao = '"+xiuxiangdanhao+"'"; Database db = DatabaseFactory.CreateDatabase(); var gid = db.ExecuteScalar(CommandType.Text,sql); if (gid==null) { return ""; } else { return gid.ToString(); } } #endregion public static bool UnLock ( string RepairIDs,string userid,out string msg) { msg = ""; if (!CheckLockAndUnlockPower(0,userid)) { msg = "当前用户没有解锁权限!"; return false; } string sql = "update Op_CtnRepair set IsLock = 0 where GID in ("+RepairIDs+")"; Database db = DatabaseFactory.CreateDatabase(); bool rst = db.ExecuteNonQuery(CommandType.Text, sql) > 0; msg=rst?"操作成功":"操作失败"; return rst; } public static bool ChangeMailStatus ( string RepairIDs ) { string sql = "update Op_CtnRepair set IsSendMail = 1 where GID in (" + RepairIDs + ")"; Database db = DatabaseFactory.CreateDatabase(); return db.ExecuteNonQuery(CommandType.Text,sql)>0; } public static bool CheckLockAndUnlockPower ( int type, string userid ) { //解锁 if (type==0) { string sql = "select count(*) from user_action where USERID = '" + userid + "' AND ACTIONID = '66661245-C3A8-46D4-9FE4-746A805244B5'"; Database db = DatabaseFactory.CreateDatabase(); int rst = Convert.ToInt32(db.ExecuteScalar(CommandType.Text,sql)); return rst > 0; } //锁定 else if (type==1) { string sql = "select count(*) from user_action where USERID = '" + userid + "' AND ACTIONID = '77761145-C3A8-46D4-9FE4-746A805244B5'"; Database db = DatabaseFactory.CreateDatabase(); int rst = Convert.ToInt32(db.ExecuteScalar(CommandType.Text, sql)); return rst > 0; } else { return false; } } } }