using Sugar.Enties;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;

public class maxbillcodeManager : DbContext<maxbillcode>
{
    public maxbillcodeManager()
    {

    }
    public maxbillcodeManager(SqlSugarClient _db)
    {
        Db = _db;
    }
    //当前类已经继承了 DbContext增、删、查、改的方法

    //这里面写的代码不会给覆盖,如果要重新生成请删除 maxbillcodeManager.cs
    #region 获取最大单号
    /// <summary>
    /// 获取最大单号
    /// </summary>
    /// <param name="orgCode">销售机构</param>
    /// <param name="category">业务分类</param>
    /// <param name="sgn">标识</param>
    /// <param name="flag">递增标识</param>
    /// <param name="sqlTran">事务</param>
    /// <returns>最大单号</returns>
    public string GetMaxBillCode(string category, string sgn, bool flag)
    {
        string cmdText = "";
        if (flag == true)
        {
            cmdText = @"DECLARE @BillCode VARCHAR(20)
                              
                            DECLARE @Year varchar(2)  
                            DECLARE @MyTableVar table(NewMaxCode varchar(30) NOT NULL)  
                              
                            SELECT @Year=Right(DatePart(YY,GetDate()),2)  
                                IF EXISTS(SELECT MaxCode FROM dbo.MaxBillCode   
                                        WHERE  SGN=@Sign AND AccountYear=@Year)  
                                    BEGIN   UPDATE MaxBillCode SET MaxCode=MaxCode+1   
                                            WHERE  SGN=@Sign AND AccountYear=@Year
                                            SELECT @BillCode=ltrim(SGN+AccountYear+CONVERT(VARCHAR,MaxCode)) FROM MaxBillCode  
                                             WHERE  SGN=@Sign AND AccountYear=@Year
                                    END  
                                    ELSE 
                                    BEGIN   DECLARE @EventCode VARCHAR(10) 
                                            INSERT INTO MaxBillCode( EventCode, SGN, Category, AccountYear, MaxCode)
                                            VALUES('10',@Sign,@Category,@Year,1000000)
                                            SELECT @BillCode=ltrim(@Sign+@Year+'1000000')
                                    END  
                              
                            SELECT @BillCode";

        }
        else
        {
            cmdText = @"DECLARE @BillCode VARCHAR(20)
                              
                            DECLARE @Year varchar(2)  
                            DECLARE @MyTableVar table(NewMaxCode varchar(30) NOT NULL) 
                            SELECT @Year=Right(DatePart(YY,GetDate()),2)  
                            BEGIN   IF EXISTS(SELECT MaxCode FROM dbo.MaxBillCode   
                                        WHERE  SGN=@Sign AND AccountYear=@Year)  
                                    BEGIN   SELECT @BillCode=ltrim(SGN+AccountYear+CONVERT(VARCHAR,MaxCode+1))  
                                            FROM dbo.MaxBillCode WHERE  SGN=@Sign  AND AccountYear=@Year 
                                    END  
                                    ELSE 
                                    BEGIN   DECLARE @EventCode VARCHAR(10) 
                                            SELECT @BillCode=ltrim(@Sign+@Year+'1000000')
                                    END  
                            END  
                            SELECT @BillCode";


        }


        //SqlParameter[] cmdParams = {
        //        new SqlParameter("@Category", SqlDbType.VarChar, 2),
        //        new SqlParameter("@Sign", SqlDbType.VarChar, 2),
        //    };
        //cmdParams[0].Value = category;
        //cmdParams[1].Value = sgn;

        var cmdParams = new List<SugarParameter>()
        {
            new SugarParameter("@Category",category),
            new SugarParameter("@Sign",sgn)
        };

        string billCode = "";
        billCode = Db.Ado.GetString(cmdText, cmdParams);
        return billCode;
    }
    #endregion



}