笔记20150522-生成更新重复单据编号的update语句

简介: public string GenereteUpdateSql(int type = 0,int localdb=0) { StringBuilder sb = new StringBuil...
        public string GenereteUpdateSql(int type = 0,int localdb=0)
        {
            StringBuilder sb = new StringBuilder();
            StringBuilder sbToday = new StringBuilder();
            sbToday.Append("<font color='red'>");
            if (localdb == 1) DbHelper.Conn = "Data Source=192.168.9.222;Initial Catalog=test;Persist Security Info=True;User ID=dev;Password=dev;";
            else DbHelper.Conn = "Data Source=****;Initial Catalog=test;Persist Security Info=True;User ID=test;Password=test";
            DbHelper db = new DbHelper();
            string sql = "SELECT  MAX(ReceiptNo) ReceiptNo FROM dbo.FinanceReceipts  f  GROUP BY f.ReceiptNo  HAVING COUNT(f.ReceiptNo)>1";//WHERE SUBSTRING(f.ReceiptNo,3,2)!='LS' ,,COUNT(ReceiptNo) repeatcount,MAX(ObjectType) ObjectType,MAX(CreatedDate) CreatedDate,MAX(CASE WHEN  IsSummary=1 THEN 1 ELSE 0 END ) isSummary,
            DataTable dtReceiptNo = db.ExecuteDataTable(sql);
            Dictionary<string, int> DateTypeSeriaNo = new Dictionary<string, int>();
            if (dtReceiptNo != null && dtReceiptNo.Rows.Count > 0)
            {
                foreach (DataRow drReceiptNo in dtReceiptNo.Rows)
                {
                    string ReceiptNoQuery = drReceiptNo["ReceiptNo"].ToString();
                    DataTable dtData = db.ExecuteDataTable(string.Format("select ReceiptId,ReceiptNo,ObjectType,CreatedDate,CASE WHEN  ReceiptStatus=0 THEN 0 ELSE 1 END IsCommit  FROM dbo.FinanceReceipts WHERE ReceiptNo='{0}' ", ReceiptNoQuery));
                    if (dtData != null && dtData.Rows.Count > 1)//同一编号大于2个
                    {

                        for (int i = 0; i < dtData.Rows.Count - 1; i++)//更新前n-1个
                        {
                            DataRow drData = dtData.Rows[i];
                            int ReceiptId = Convert.ToInt32(drData["ReceiptId"]);
                            int ObjectType = Convert.ToInt32(drData["ObjectType"]);
                            string CreatedDate = drData["CreatedDate"].ToString();

                            int maxSerial = 0;
                            int IsCommit = Convert.ToInt32(drData["IsCommit"]);
                            string DateTypeKey = string.Format("{0}{1}{2}", CreatedDate, ObjectType, IsCommit);
                            if (DateTypeSeriaNo.ContainsKey(DateTypeKey))
                            {
                                maxSerial = DateTypeSeriaNo[DateTypeKey];
                            }

                            {
                                string maxSerialSql = "";
                                if (IsCommit == 0) maxSerialSql = string.Format(" SELECT MAX(SerialNumber) FROM dbo.FinanceReceipts WHERE  ObjectType={0} AND ReceiptStatus=0 AND CreatedDate='{1}' ", ObjectType, CreatedDate);
                                else maxSerialSql = string.Format(" SELECT MAX(SerialNumber) FROM dbo.FinanceReceipts WHERE  ObjectType={0} AND ReceiptStatus!=0 AND CreatedDate='{1}' ", ObjectType, CreatedDate);
                                object retobj = db.ExecuteScalar(maxSerialSql);
                                int dbMaxSerail = 0;
                                if (retobj != DBNull.Value) dbMaxSerail = Convert.ToInt32(retobj);
                                maxSerial=(maxSerial>dbMaxSerail?maxSerial:dbMaxSerail);
                                DateTypeSeriaNo[DateTypeKey] = maxSerial;//存入dict
                            }
                            string ReceiptNo = string.Empty;
                            switch (ObjectType)
                            {
                                case 1: // 应收
                                    ReceiptNo = "YS";
                                    break;

                                case 2: // 收款
                                    ReceiptNo = "SK";
                                    break;

                                case 3: // 应付
                                    ReceiptNo = "YF";
                                    break;

                                case 4: // 付款
                                    ReceiptNo = "FK";
                                    break;

                                default:
                                    throw new InvalidOperationException("未知票据类型,不能生成单据编号");
                            }

                            // 未提交
                            if (IsCommit == 0 || ReceiptNoQuery.Substring(2, 2) == "LS")
                            {
                                ReceiptNo += "LS";
                            }
                            ReceiptNo += CreatedDate;
                            maxSerial++;
                            DateTypeSeriaNo[DateTypeKey] = maxSerial;
                            ReceiptNo += string.Format("{0:D6}", maxSerial);
                            string CreatedDateToday=DateTime.Now.ToString("yyyyMMdd");
                            if (CreatedDate == CreatedDateToday)
                            {
                                sbToday.AppendFormat("UPDATE dbo.FinanceReceipts SET SerialNumber={0},ReceiptNo='{1}' WHERE ReceiptId={2};<br/>", maxSerial, ReceiptNo, ReceiptId);
                                sbToday.AppendFormat("INSERT INTO dbo.FinanceBillLog(ReceiptId,[Action] ,Remark ,CreatedById ,CreatedByName ,CreatedDate) VALUES  ({0},N'修改重复付款单编号' ,N'从 {1} 改为 {2}' ,0 ,N'sql' ,'2015-05-22 15:30:00');<br/>", ReceiptId, ReceiptNoQuery, ReceiptNo);
                            }
                            else
                            {
                                sb.AppendFormat("UPDATE dbo.FinanceReceipts SET SerialNumber={0},ReceiptNo='{1}' WHERE ReceiptId={2};<br/>", maxSerial, ReceiptNo, ReceiptId);
                                sb.AppendFormat("INSERT INTO dbo.FinanceBillLog(ReceiptId,[Action] ,Remark ,CreatedById ,CreatedByName ,CreatedDate) VALUES  ({0},N'修改重复付款单编号' ,N'从 {1} 改为 {2}' ,0 ,N'sql' ,'2015-05-22 15:30:00');<br/>", ReceiptId, ReceiptNoQuery, ReceiptNo);
                            }
                        }
                    }
                }
            }
            sbToday.Append("</font>");
            if (type == 1) return sb.ToString();
            if (type == 2) return sbToday.ToString();
            return sb.ToString() + "<br/><br/><br/>" + sbToday.ToString();
        }
 
 


相关文章
|
4月前
|
关系型数据库 MySQL 数据库
mysql,归零,无法自动排序,删除id,表单的数据没有从零开始出现怎样解决?删除数据仍然从删除的地方该怎样解决?表单的数据没有从2开始,而是从之前的删除的序号开始自增。
mysql,归零,无法自动排序,删除id,表单的数据没有从零开始出现怎样解决?删除数据仍然从删除的地方该怎样解决?表单的数据没有从2开始,而是从之前的删除的序号开始自增。
update 批量更新某条数据时取最新、最大的一笔更新
update 批量更新某条数据时取最新、最大的一笔更新
92 0
|
6月前
|
JavaScript
记录一些报错记录
记录一些报错记录
59 0
|
6月前
|
SQL 存储 Java
MyBatis【付诸实践 02】 mapper文件未编译+statementType使用+返回结果字段顺序不一致+获取自增ID+一个update标签批量更新记录
MyBatis【付诸实践 02】 mapper文件未编译+statementType使用+返回结果字段顺序不一致+获取自增ID+一个update标签批量更新记录
72 0
|
SQL 关系型数据库 MySQL
操作delete或者update语句,加个limit或者循环分批次删除
操作delete或者update语句,加个limit或者循环分批次删除
删除一段时间内的记录,关键在于删除时筛选条件确定删除范围
删除一段时间内的记录,关键在于删除时筛选条件确定删除范围
95 0
|
SQL 关系型数据库 MySQL
十一、操作delete或者update语句,加个limit或者循环分批次删除
十一、操作delete或者update语句,加个limit或者循环分批次删除
315 0
|
SQL 关系型数据库 MySQL
MySql数据库Update批量更新与批量更新多条记录的不同值实现方法
MySql数据库Update批量更新与批量更新多条记录的不同值实现方法
2120 0
|
Java 数据库 缓存
JPA更新后查询结果还是更新前的
此时数据库数据状态如下 执行如下流程代码 执行update语句,更新状态字段 语句执行完毕,然而看看数据库 hibernate执行更新需要较长时间,因此需要等待,否则无法获得更新后字段 JPA更新一条记录,数据库查看更新成功,但是紧接着查询,发现还是更新前的结果,怀疑缓存问题但是网上查了下没解决成功。
2111 0