天涯一飘絮

导航

 
有这样一张报表,如图:

要求用GridView显示,所以查询的结果要么是一个临时表或者是一个查询视图,想到sql2005中加入CLR的支持,有关CLR的操作请看体验:用C#写存储过程(VS.NET 2005) 或者在sqlserver2005中部署C#编写的自定义函数 ,实现的类如下

   1using System;
   2using
 System.Collections.Generic;
   3using
 System.Data;
   4using
 System.Data.SqlClient;
   5using
 System.Data.SqlTypes;
   6using
 Microsoft.SqlServer.Server;
   7using
 System.Collections;
   8

   9public partial class
 WYTableFunction
  10
{
  11      
#region Fun_FactIncome_CLR
  12    //这个特性定义了一个sql表值函数,此函数返回的表的定义为:String nvarchar(200)
  13    //
并且指定了填充这个表的行的方法是FillRow 方法
  14    //注意这个方法返回的一定是一个IEnumerable类型的,并且为公开,静态,这个方法的入参就是sql函数的入参

  15    [SqlFunction(DataAccess = DataAccessKind.Read, TableDefinition = @"tid    int ,unitname nvarchar(100),itemname nvarchar(100),
  16
                jan    decimal(18,2),feb    decimal(18,2),    mar    decimal(18,2),    apr    decimal(18,2),
  17
                may    decimal(18,2),jun    decimal(18,2),    jul    decimal(18,2),    aug    decimal(18,2),
  18
                sep    decimal(18,2),oct    decimal(18,2),    nov    decimal(18,2),    dec    decimal(18,2),
  19                total decimal(18,2),    flag int ", FillRowMethodName = "FillRow3"
)]
  20    public static IEnumerable Fun_FactIncome_CLR(int iYear, string CompanyID, int
 type)
  21    
{
  22        List<DataRow> rowList = new List<DataRow>
();
  23        DataTable dt = new
 DataTable();
  24        
#region 表结构
  25        DataColumn col = new DataColumn("tid"typeof(int));
  26
        dt.Columns.Add(col);
  27        dt.Columns.Add("unitname"typeof(string
));
  28        dt.Columns.Add("itemname"typeof(string
));
  29

  30        AddColumns(ref dt, new string[] "jan""feb""mar""apr""may""jun""jul""aug""sep""oct""nov""dec""total" }
);
  31

  32        col = new DataColumn("flag"typeof(int
));
  33        col.DefaultValue = 0
;
  34
        dt.Columns.Add(col);
  35

  36        col = new DataColumn("myTemp"typeof(decimal
));
  37        col.Expression = "jan+feb+mar+apr+may+jun+jul+aug+sep+oct+nov+dec"
;
  38
        dt.Columns.Add(col);
  39        #endregion

  40        if (type == 0)//总部查询
  41        {
  42            string CompanyList =
 GetCompanyList(CompanyID);
  43            string UnitName = ""
;
  44            SearchCompany(iYear, CompanyID, rowList, ref
 dt, CompanyList, UnitName);
  45        }

  46        else if (type == 1)//区域查询
  47        {
  48            string CompanyList =
 GetCompanyList(CompanyID);
  49            string UnitName = ""
;
  50            SearchRegion(iYear, CompanyID, rowList, ref
 dt, CompanyList, UnitName);
  51        }

  52        else if (type == 2)//所有楼盘
  53        {
  54            string CompanyList =
 GetCompanyList(CompanyID);
  55            string UnitName = ""
;
  56            SearchBuilding(iYear, CompanyID, rowList, ref
 dt, CompanyList, UnitName);
  57        }

  58        else if (type == 3)//自定义楼盘查询
  59        {
  60            string CompanyList =
 GetUnitList(CompanyID);
  61            string UnitName = ""
;
  62            dt =
 SearchBuildingDef(iYear, rowList, dt, CompanyList);
  63        }

  64        else if (type == 4)//自定义区域查询
  65        {
  66            string CompanyList =
 GetUnitList(CompanyID);
  67            string UnitName = ""
;
  68            SearchRegionDef(iYear, CompanyID, rowList, ref
 dt, CompanyList, UnitName);
  69        }

  70        return rowList as IEnumerable;
  71        //返回一个string 数组,这个数组符合IEnumerable接口,当然你也可以返回hashtable等类型。

  72
  73    }

  74
  75    //自定义楼盘查询

  76    private static DataTable SearchBuildingDef(int iYear, List<DataRow> rowList, DataTable dt, string CompanyList)
  77    
{
  78        
#region 数据查询
  79
  80        string Err = null
;
  81

  82        //1、服务费收入(本年度)[包括应收部分和优惠]

  83        string sql = @"select sf.UnitName,se.[Name] as itemname,month(fp.gatherdate) as [Month],sum(far.gainmoney) as TotalMoney
  84
                                    from finance_preceipt fp 
  85
                                    join finance_arecei_receiptlist far on fp.receiptid = far.receiptid
  86
                                    join finance_areceivable fa on far.areceivableid = fa.areceivableid
  87
                                    join Sys_Exestype  as se on se.ExecTypeID=fa.itemtype
  88
                                    join system_framework as sf on sf.unitid=fp.companyid
  89
                                    where year(fp.gatherdate)= {0} and year(fa.enddate) = {0} and  fa.itemtype = 7 
  90
                                    and fp.companyid in ({1})
  91
                                    group by sf.unitcode,sf.UnitName,se.[Name],month(fp.gatherdate)
  92
                        union all
  93
                                    select  sf.UnitName,se.[Name] as itemname,Month(fp.gatherdate) as [Month],sum(far.GainPbMoney) as TotalMoney                        
  94
                                    from Pb_Genledger fp 
  95
                                    join Pb_Datail far on fp.GenledgerID = far.GenledgerID
  96
                                    join finance_areceivable fa on far.areceivableid = fa.areceivableid
  97
                                    join Sys_Exestype  as se on se.ExecTypeID=fa.itemtype
  98
                                    join system_framework as sf on sf.unitid=fp.companyid
  99
                                    where year(fp.gatherdate)= {0} and year(fa.enddate) = {0} and  fa.itemtype = 7 
 100
                                    and fp.companyid in ({1})
 101                                    group by sf.unitcode,sf.UnitName,se.[Name],Month(fp.gatherdate);"
;
 102        //2、服务费收入(往年)[包括应收部分和优惠]

 103        sql += @"select sf.UnitName,se.[Name] as itemname,month(fp.gatherdate) as [Month],sum(far.gainmoney) as TotalMoney
 104
                                    from finance_preceipt fp 
 105
                                    join finance_arecei_receiptlist far on fp.receiptid = far.receiptid
 106
                                    join finance_areceivable fa on far.areceivableid = fa.areceivableid
 107
                                    join Sys_Exestype  as se on se.ExecTypeID=fa.itemtype
 108
                                    join system_framework as sf on sf.unitid=fp.companyid
 109
                                    where year(fp.gatherdate)= {0} and year(fa.enddate) < {0} and  fa.itemtype = 7 
 110
                                    and fp.companyid in ({1})
 111
                                    group by sf.unitcode,sf.UnitName,se.[Name],month(fp.gatherdate)
 112
                        union all
 113
                                    select  sf.UnitName,se.[Name] as itemname,Month(fp.gatherdate) as [Month],sum(far.GainPbMoney) as TotalMoney                        
 114
                                    from Pb_Genledger fp 
 115
                                    join Pb_Datail far on fp.GenledgerID = far.GenledgerID
 116
                                    join finance_areceivable fa on far.areceivableid = fa.areceivableid
 117
                                    join Sys_Exestype  as se on se.ExecTypeID=fa.itemtype
 118
                                    join system_framework as sf on sf.unitid=fp.companyid
 119
                                    where year(fp.gatherdate)= {0} and year(fa.enddate) < {0} and  fa.itemtype = 7 
 120
                                    and fp.companyid in ({1})
 121                                    group by sf.unitcode,sf.UnitName,se.[Name],Month(fp.gatherdate);"
;
 122        //3、服务费收入(预收)

 123        sql += @"    select sf.UnitName,se.[Name] as itemname,month(fi.gatherdate) as [Month],sum(fi.incomeMoney) as TotalMoney    
 124
                                from finance_rmoney fr 
 125
                                join finance_income fi on fr.rmoneyid = fi.rmoneyid
 126
                                join finance_chargeitemset as fc on fc.itemid=fr.itemid
 127
                                join Sys_Exestype  as se on se.ExecTypeID=fc.ExesTypeID 
 128
                                join system_framework as sf on sf.unitid=fr.companyid
 129
                                where fi.incomemoney > 0 and year(fi.gatherdate) = {0} and fc.ExesTypeID=7
 130
                                and fr.companyid in ({1})    
 131                                group by sf.unitcode,sf.UnitName,se.[Name],month(fi.gatherdate);"
;
 132

 133        //4、计划收入

 134        /*sql += @"select  '所有区域' as UnitName,    sum(isnull(janmoney,0))as jan,sum(isnull(febmoney,0))as feb,sum(isnull(marmoney,0))as mar,
 135
                        sum(isnull(aprmoney,0))as apr,sum(isnull(maymoney,0))as may,sum(isnull(junmoney,0))as jun,
 136
                        sum(isnull(julmoney,0))as jul,sum(isnull(augmoney,0))as aug,sum(isnull(septmoney,0))as sep,
 137
                        sum(isnull(octMoney,0))as oct,sum(isnull(NovMoney,0))as nov,sum(isnull(DecMoney,0))as dec
 138                    from plan_income where planyear = {0} and companyID in ({1});";*/

 139        sql += @"select '所有楼盘',sum(p.jan) as jan ,sum(p.feb) as feb,sum(p.mar) as mar,sum(p.apr) as apr,sum(p.may) as may,
 140
                        sum(p.jun) as jun, sum(p.jul) as jul,sum(p.aug) as aug,sum(p.sep) as sep, sum(p.oct) as oct,sum(p.nov) as nov, sum(p.[dec]) as [dec]
 141
                        from (
 142
                        select sf.unitcode,sf.UnitName, sum(isnull(janmoney,0))as jan,sum(isnull(febmoney,0))as feb,sum(isnull(marmoney,0))as mar,
 143
                        sum(isnull(aprmoney,0))as apr,sum(isnull(maymoney,0))as may,sum(isnull(junmoney,0))as jun,
 144
                        sum(isnull(julmoney,0))as jul,sum(isnull(augmoney,0))as aug,sum(isnull(septmoney,0))as sep,
 145
                        sum(isnull(octMoney,0))as oct,sum(isnull(NovMoney,0))as nov,sum(isnull(DecMoney,0))as [dec]
 146
                        from plan_income 
 147
                        join system_framework as sf on sf.unitid=plan_income.companyid
 148
                        where planyear = {0} and companyID in ({1})
 149
                        group by sf.unitcode,sf.UnitName
 150                        ) as p;"
;
 151        //5、其他服务收入(除服务费以外)                                                

 152        sql += @"select sf.unitname,se.[Name] as itemname,month(fp.gatherdate) as [Month],sum(far.gainmoney) as TotalMoney                        
 153
                                from finance_preceipt fp join finance_arecei_receiptlist far on fp.receiptid = far.receiptid
 154
                                join finance_areceivable fa on far.areceivableid = fa.areceivableid
 155
                                left join finance_chargeitemset ft on ft.itemid=fa.itemid
 156
                                left join Sys_ExesGenre on Sys_ExesGenre.ExesGenreID=ft.ExesGenreID
 157
                                join Sys_Exestype  as se on se.ExecTypeID=fa.itemtype
 158
                                left join system_framework as sf on sf.unitid=fa.companyid 
 159
                                where year(fp.gatherdate)= {0} and  fa.itemtype <> 7 and Sys_ExesGenre.ExesGenreID=1
 160
                                    and fp.companyid in ({1})
 161                                group by sf.unitcode,sf.unitname, se.[Name] ,month(fp.gatherdate) ;"
;
 162        //6、空置房(本年)        

 163        sql += @"select sf.unitcode,sf.unitname,se.[Name] as itemname,month(fp.gatherdate) as [Month],sum(far.gainmoney) as TotalMoney                        
 164
                                from finance_preceipt fp 
 165
                                join finance_arecei_receiptlist far on fp.receiptid = far.receiptid
 166
                                join finance_areceivable fa on far.areceivableid = fa.areceivableid
 167
                                join Sys_Exestype as se on se.ExecTypeID=fa.itemtype
 168
                                left join system_framework as sf on sf.unitid=fa.companyid 
 169
                                where year(fp.gatherdate)= {0} and year(fa.enddate) = {0} and fa.UnitOrindividualID is not null and isnull(fa.FavourableSign,0) !=1
 170
                                and fa.itemtype=7 and fp.companyid in ({1}) 
 171                                group by sf.unitcode,sf.unitname,se.[Name],month(fp.gatherdate) ;"
;
 172        //7、空置房(往年)        

 173        sql += @"select sf.unitname,se.[Name] as itemname,month(fp.gatherdate) as [Month],sum(far.gainmoney) as TotalMoney                        
 174
                                from finance_preceipt fp 
 175
                                join finance_arecei_receiptlist far on fp.receiptid = far.receiptid
 176
                                join finance_areceivable fa on far.areceivableid = fa.areceivableid
 177
                                join Sys_Exestype as se on se.ExecTypeID=fa.itemtype
 178
                                left join system_framework as sf on sf.unitid=fa.companyid 
 179
                                where year(fp.gatherdate)= {0} and year(fa.enddate) < {0} and fa.UnitOrindividualID is not null and isnull(fa.FavourableSign,0) !=1
 180
                                and fa.itemtype=7 and fp.companyid in ({1}) 
 181                                group by sf.unitcode,sf.unitname,se.[Name],month(fp.gatherdate) ;"
;
 182        //8、空置房(预收)    

 183        sql += @"select sf.unitcode,sf.unitname,se.[Name] as itemname,month(fp.gatherdate) as [Month],sum(far.gainmoney) as TotalMoney                        
 184
                            from finance_preceipt fp 
 185
                            join finance_arecei_receiptlist far on fp.receiptid = far.receiptid
 186
                            join finance_areceivable fa on far.areceivableid = fa.areceivableid
 187
                            join Sys_Exestype as se on se.ExecTypeID=fa.itemtype
 188
                            left join system_framework as sf on sf.unitid=fa.companyid 
 189
                            where fa.GainMoney>0 and year(fp.gatherdate)= {0} and year(fa.enddate) > {0} and fa.UnitOrindividualID is not null and isnull(fa.FavourableSign,0) !=1
 190
                            and fa.itemtype=7 and fp.companyid in ({1}) 
 191                            group by sf.unitcode,sf.unitname,se.[Name],month(fp.gatherdate) ;"
;
 192        //UnitName = sqlHead;

 193        sql = string.Format(sql, iYear, CompanyList);
 194

 195        // throw new Exception();

 196        DataSet ds = AmbitsWY_CLR.Globe.ExecSqlForDataSet(sql, out Err);
 197

 198

 199        if (Err != null
)
 200        
{
 201            throw new Exception("错误01:" + Err + "\r\n语句:\r\n" +
 sql);
 202

 203        }

 204
 205        //
col = new DataColumn("myTemp", typeof(decimal));
 206        //
col.Expression = "jan+feb+mar+apr+may+jun+jul+aug+sep+oct+nov+dec";
 207        //dt.Columns.Add(col);

 208        #endregion

 209
 210        
#region 服务费收入
 211        //本年度
 212        DataRow row = dt.NewRow();
 213        row["UnitName"= "服务费收入(本年度)"
;
 214
        dt.Rows.Add(row);
 215        InsertData(ref dt, ds.Tables[0], 1
);
 216        AddSum(ref dt, """小计"11"flag = 1 "
);
 217

 218        //往年度

 219        row = dt.NewRow();
 220        row["UnitName"= "服务费收入(往年度)"
;
 221
        dt.Rows.Add(row);
 222        InsertData(ref dt, ds.Tables[1], 2
);
 223        AddSum(ref dt, """小计"12"flag = 2 "
);
 224

 225        //预收款

 226        row = dt.NewRow();
 227        row["UnitName"= "服务费收入(预收款)"
;
 228
        dt.Rows.Add(row);
 229        InsertData(ref dt, ds.Tables[2], 3
);
 230        AddSum(ref dt, """小计"13"flag = 3 "
);
 231

 232        //合计

 233        AddSum(ref dt, "所有楼盘服务费(④=①+②+③)""合计"7"Flag in (11,12,13)");
 234

 235

 236        //本年收入差异率

 237        CalculatePercent(ref dt, ds.Tables[3]);
 238

 239        #endregion

 240
 241        
#region 其他服务收入
 242        row = dt.NewRow();
 243        row["UnitName"= "其他服务收入"
;
 244
        dt.Rows.Add(row);
 245        InsertData(ref dt, ds.Tables[4], 8
);
 246        AddSum(ref dt, """小计"18"flag = 8 "
);
 247        AddSum(ref dt, "所有楼盘总收入(⑥=④+⑤)""共计"18"flag in (7,8) "
);
 248        #endregion

 249
 250        
#region 空置房
 251        //本年度
 252        row = dt.NewRow();
 253        row["UnitName"= "空置房(本年度)"
;
 254
        dt.Rows.Add(row);
 255        InsertData(ref dt, ds.Tables[5], 4
);
 256        AddSum(ref dt, """小计"4"flag = 4 "
);
 257

 258        //往年度

 259        row = dt.NewRow();
 260        row["UnitName"= "空置房(往年度)"
;
 261
        dt.Rows.Add(row);
 262        InsertData(ref dt, ds.Tables[6], 5
);
 263        AddSum(ref dt, """小计"15"flag = 5 "
);
 264

 265        //预收款

 266        row = dt.NewRow();
 267        row["UnitName"= "空置房(预收款)"
;
 268
        dt.Rows.Add(row);
 269        InsertData(ref dt, ds.Tables[7], 6
);
 270        AddSum(ref dt, """小计"16"flag = 6 "
);
 271
        dt.AcceptChanges();
 272        for (int i = 0; i < dt.Rows.Count; i++
)
 273        
{
 274            dt.Rows[i]["tid"= i + 1
;
 275            DataRow row2 =
 dt.Rows[i];
 276            if (AmbitsWY_CLR.Globe.ConvertToInt(row2["Flag"]) > 0
)
 277                row2["Total"= row2["myTemp"
];
 278            else

 279                row2["Total"= DBNull.Value;//空值
 280            rowList.Add(dt.Rows[i]);
 281        }

 282
 283        dt.Columns.Remove("myTemp"
);
 284

 285        #endregion

 286        return dt;
 287    }

 288    //自定义区域查询
 289    private static void SearchRegionDef(int iYear, string CompanyID, List<DataRow> rowList, ref DataTable dt, string CompanyList, string UnitName)
 290    
{
 291        
#region 数据查询
 292        string Err = null;
 293        //1、服务费收入(本年度)[包括应收部分和优惠]

 294        string sql = @"select  (select unitname from system_framework where unitcode=b.punitcode) as unitname ,
 295
                                    a.itemname,a.[month],a.[totalmoney]
 296
                                    from (select sf.unitcode,sf.UnitName,se.[Name] as itemname,month(fp.gatherdate) as [Month],sum(far.gainmoney) as TotalMoney
 297
                                    from finance_preceipt fp 
 298
                                    join finance_arecei_receiptlist far on fp.receiptid = far.receiptid
 299
                                    join finance_areceivable fa on far.areceivableid = fa.areceivableid
 300
                                    join Sys_Exestype  as se on se.ExecTypeID=fa.itemtype
 301
                                    join system_framework as sf on sf.unitid=fp.companyid
 302
                                    where year(fp.gatherdate)= {0} and year(fa.enddate) = {0} and  fa.itemtype = 7 
 303
                                    and fp.companyid in ({1})
 304
                                    group by sf.unitcode,sf.UnitName,se.[Name],month(fp.gatherdate)
 305
                                    ) as a join system_framework as b on a.unitcode=b.unitcode
 306

 307
                        union all
 308
                                    select (select unitname from system_framework where unitcode=b.punitcode) as unitname ,
 309
                                    a.itemname,a.[month],a.[totalmoney]
 310
                                    from (select  sf.unitcode,sf.UnitName,se.[Name] as itemname,Month(fp.gatherdate) as [Month],sum(far.GainPbMoney) as TotalMoney                        
 311
                                    from Pb_Genledger fp 
 312
                                    join Pb_Datail far on fp.GenledgerID = far.GenledgerID
 313
                                    join finance_areceivable fa on far.areceivableid = fa.areceivableid
 314
                                    join Sys_Exestype  as se on se.ExecTypeID=fa.itemtype
 315
                                    join system_framework as sf on sf.unitid=fp.companyid
 316
                                    where year(fp.gatherdate)= {0} and year(fa.enddate) = {0} and  fa.itemtype = 7 
 317
                                    and fp.companyid in ({1})
 318
                                    group by sf.unitcode,sf.UnitName,se.[Name],Month(fp.gatherdate)
 319                                    ) as a join system_framework as b on a.unitcode=b.unitcode;"
;
 320        //2、服务费收入(往年)[包括应收部分和优惠]

 321        sql += @"select (select unitname from system_framework where unitcode=b.punitcode) as unitname ,
 322
                                    a.itemname,a.[month],a.[totalmoney]
 323
                                    from (select sf.unitcode,sf.UnitName,se.[Name] as itemname,month(fp.gatherdate) as [Month],sum(far.gainmoney) as TotalMoney
 324
                                    from finance_preceipt fp 
 325
                                    join finance_arecei_receiptlist far on fp.receiptid = far.receiptid
 326
                                    join finance_areceivable fa on far.areceivableid = fa.areceivableid
 327
                                    join Sys_Exestype  as se on se.ExecTypeID=fa.itemtype
 328
                                    join system_framework as sf on sf.unitid=fp.companyid
 329
                                    where year(fp.gatherdate)= {0} and year(fa.enddate) < {0} and  fa.itemtype = 7 
 330
                                    and fp.companyid in ({1})
 331
                                    group by sf.unitcode,sf.UnitName,se.[Name],month(fp.gatherdate)
 332
                                    ) as a join system_framework as b on a.unitcode=b.unitcode
 333

 334
                        union all
 335
                                    select  (select unitname from system_framework where unitcode=b.punitcode) as unitname ,
 336
                                    a.itemname,a.[month],a.[totalmoney]
 337
                                    from (select  sf.unitcode,sf.UnitName,se.[Name] as itemname,Month(fp.gatherdate) as [Month],sum(far.GainPbMoney) as TotalMoney                        
 338
                                    from Pb_Genledger fp 
 339
                                    join Pb_Datail far on fp.GenledgerID = far.GenledgerID
 340
                                    join finance_areceivable fa on far.areceivableid = fa.areceivableid
 341
                                    join Sys_Exestype  as se on se.ExecTypeID=fa.itemtype
 342
                                    join system_framework as sf on sf.unitid=fp.companyid
 343
                                    where year(fp.gatherdate)= {0} and year(fa.enddate) < {0} and  fa.itemtype = 7 
 344
                                    and fp.companyid in ({1})
 345
                                    group by sf.unitcode,sf.UnitName,se.[Name],Month(fp.gatherdate)
 346                                    ) as a join system_framework as b on a.unitcode=b.unitcode;"
;
 347        //3、服务费收入(预收)

 348        sql += @"    select  (select unitname from system_framework where unitcode=b.punitcode) as unitname ,
 349
                                a.itemname,a.[month],a.[totalmoney]
 350
                                from (
 351
                                select sf.unitcode,sf.UnitName,se.[Name] as itemname,month(fi.gatherdate) as [Month],sum(fi.incomeMoney) as TotalMoney    
 352
                                from finance_rmoney fr 
 353
                                join finance_income fi on fr.rmoneyid = fi.rmoneyid
 354
                                join finance_chargeitemset as fc on fc.itemid=fr.itemid
 355
                                join Sys_Exestype  as se on se.ExecTypeID=fc.ExesTypeID 
 356
                                join system_framework as sf on sf.unitid=fr.companyid
 357
                                where fi.incomemoney > 0 and year(fi.gatherdate) = {0} and fc.ExesTypeID=7
 358
                                and fr.companyid in ({1})    
 359
                                group by sf.unitcode,sf.UnitName,se.[Name],month(fi.gatherdate)
 360                                ) as a join system_framework as b on a.unitcode=b.unitcode;"
;
 361

 362        //4、计划收入

 363        /*sql += @"select  '所有区域' as UnitName,    sum(isnull(janmoney,0))as jan,sum(isnull(febmoney,0))as feb,sum(isnull(marmoney,0))as mar,
 364
                        sum(isnull(aprmoney,0))as apr,sum(isnull(maymoney,0))as may,sum(isnull(junmoney,0))as jun,
 365
                        sum(isnull(julmoney,0))as jul,sum(isnull(augmoney,0))as aug,sum(isnull(septmoney,0))as sep,
 366
                        sum(isnull(octMoney,0))as oct,sum(isnull(NovMoney,0))as nov,sum(isnull(DecMoney,0))as dec
 367                    from plan_income where planyear = {0} and companyID in ({1});";*/

 368        sql += @"select '所有区域',sum(p.jan) as jan ,sum(p.feb) as feb,sum(p.mar) as mar,sum(p.apr) as apr,sum(p.may) as may,
 369
                                sum(p.jun) as jun, sum(p.jul) as jul,sum(p.aug) as aug,sum(p.sep) as sep, sum(p.oct) as oct,sum(p.nov) as nov, sum(p.[dec]) as [dec]
 370
                                from (
 371
                                select b.punitcode as unitcode,(select unitname from system_framework where unitcode=b.punitcode) as unitname ,
 372
                                a.jan,a.feb,a.mar,a.apr,a.may,a.jun,a.jul,a.aug,a.sep,a.oct,a.nov,a.[dec]
 373
                                from (
 374
                                select sf.unitcode,sf.UnitName, sum(isnull(janmoney,0))as jan,sum(isnull(febmoney,0))as feb,sum(isnull(marmoney,0))as mar,
 375
                                sum(isnull(aprmoney,0))as apr,sum(isnull(maymoney,0))as may,sum(isnull(junmoney,0))as jun,
 376
                                sum(isnull(julmoney,0))as jul,sum(isnull(augmoney,0))as aug,sum(isnull(septmoney,0))as sep,
 377
                                sum(isnull(octMoney,0))as oct,sum(isnull(NovMoney,0))as nov,sum(isnull(DecMoney,0))as [dec]
 378
                                from plan_income 
 379
                                join system_framework as sf on sf.unitid=plan_income.companyid
 380
                                where planyear = {0} and companyID in ({1})
 381
                                group by sf.unitcode,sf.UnitName
 382
                                ) as a join system_framework as b on a.unitcode=b.unitcode
 383                                ) as p;"
;
 384        //5、其他服务收入(除服务费以外)                                                

 385        sql += @"select (select unitname from system_framework where unitcode=b.punitcode) as unitname ,
 386
                                a.itemname,a.[month],a.[totalmoney]
 387
                                from (
 388
                                select sf.unitcode,sf.unitname,se.[Name] as itemname,month(fp.gatherdate) as [Month],sum(far.gainmoney) as TotalMoney                        
 389
                                from finance_preceipt fp join finance_arecei_receiptlist far on fp.receiptid = far.receiptid
 390
                                join finance_areceivable fa on far.areceivableid = fa.areceivableid
 391
                                left join finance_chargeitemset ft on ft.itemid=fa.itemid
 392
                                left join Sys_ExesGenre on Sys_ExesGenre.ExesGenreID=ft.ExesGenreID
 393
                                join Sys_Exestype  as se on se.ExecTypeID=fa.itemtype
 394
                                left join system_framework as sf on sf.unitid=fa.companyid 
 395
                                where year(fp.gatherdate)= {0} and  fa.itemtype <> 7 and Sys_ExesGenre.ExesGenreID=1
 396
                                    and fp.companyid in ({1})
 397
                                group by sf.unitcode,sf.unitname, se.[Name] ,month(fp.gatherdate)
 398                                )as a join system_framework as b on a.unitcode=b.unitcode ;"
;
 399        //6、空置房(本年)        

 400        sql += @"select (select unitname from system_framework where unitcode=b.punitcode) as unitname ,
 401
                                a.itemname,a.[month],a.[totalmoney]
 402
                                from (
 403
                                select sf.unitcode,sf.unitname,se.[Name] as itemname,month(fp.gatherdate) as [Month],sum(far.gainmoney) as TotalMoney                        
 404
                                from finance_preceipt fp 
 405
                                join finance_arecei_receiptlist far on fp.receiptid = far.receiptid
 406
                                join finance_areceivable fa on far.areceivableid = fa.areceivableid
 407
                                join Sys_Exestype as se on se.ExecTypeID=fa.itemtype
 408
                                left join system_framework as sf on sf.unitid=fa.companyid 
 409
                                where year(fp.gatherdate)= {0} and year(fa.enddate) = {0} and fa.UnitOrindividualID is not null and isnull(fa.FavourableSign,0) !=1
 410
                                and fa.itemtype=7 and fp.companyid in ({1}) 
 411
                                group by sf.unitcode,sf.unitname,se.[Name],month(fp.gatherdate)
 412                                )as a join system_framework as b on a.unitcode=b.unitcode ;"
;
 413        //7、空置房(往年)        

 414        sql += @"select (select unitname from system_framework where unitcode=b.punitcode) as unitname ,
 415
                                a.itemname,a.[month],a.[totalmoney]
 416
                                from (
 417
                                select sf.unitcode,sf.unitname,se.[Name] as itemname,month(fp.gatherdate) as [Month],sum(far.gainmoney) as TotalMoney                        
 418
                                from finance_preceipt fp 
 419
                                join finance_arecei_receiptlist far on fp.receiptid = far.receiptid
 420
                                join finance_areceivable fa on far.areceivableid = fa.areceivableid
 421
                                join Sys_Exestype as se on se.ExecTypeID=fa.itemtype
 422
                                left join system_framework as sf on sf.unitid=fa.companyid 
 423
                                where year(fp.gatherdate)= {0} and year(fa.enddate) < {0} and fa.UnitOrindividualID is not null and isnull(fa.FavourableSign,0) !=1
 424
                                and fa.itemtype=7 and fp.companyid in ({1}) 
 425
                                group by sf.unitcode,sf.unitname,se.[Name],month(fp.gatherdate)
 426                                )as a join system_framework as b on a.unitcode=b.unitcode ;"
;
 427        //8、空置房(预收)    

 428        sql += @"select (select unitname from system_framework where unitcode=b.punitcode) as unitname ,
 429
                            a.itemname,a.[month],a.[totalmoney]
 430
                            from (
 431
                            select sf.unitcode,sf.unitname,se.[Name] as itemname,month(fp.gatherdate) as [Month],sum(far.gainmoney) as TotalMoney                        
 432
                            from finance_preceipt fp 
 433
                            join finance_arecei_receiptlist far on fp.receiptid = far.receiptid
 434
                            join finance_areceivable fa on far.areceivableid = fa.areceivableid
 435
                            join Sys_Exestype as se on se.ExecTypeID=fa.itemtype
 436
                            left join system_framework as sf on sf.unitid=fa.companyid 
 437
                            where fa.GainMoney>0 and year(fp.gatherdate)= {0} and year(fa.enddate) > {0} and fa.UnitOrindividualID is not null and isnull(fa.FavourableSign,0) !=1
 438
                            and fa.itemtype=7 and fp.companyid in ({1}) 
 439
                            group by sf.unitcode,sf.unitname,se.[Name],month(fp.gatherdate)
 440                            )as a join system_framework as b on a.unitcode=b.unitcode ;"
;
 441        //UnitName = sqlHead;

 442        sql = string.Format(sql, iYear, CompanyList);
 443

 444

 445        DataSet ds = AmbitsWY_CLR.Globe.ExecSqlForDataSet(sql, out
 Err);
 446

 447

 448        if (Err != null
)
 449        
{
 450            throw new Exception("错误01:" + Err + "\r\n语句:\r\n" +
 sql);
 451

 452        }

 453
 454        //
col = new DataColumn("myTemp", typeof(decimal));
 455        //
col.Expression = "jan+feb+mar+apr+may+jun+jul+aug+sep+oct+nov+dec";
 456        //dt.Columns.Add(col);

 457        #endregion

 458
 459        
服务费收入
 489
 490        
其他服务收入
 498
 499        
空置房
 535    }

 536    //总部查询
 537    private static void SearchCompany(int iYear, string CompanyID, List<DataRow> rowList, ref DataTable dt, string CompanyList, string UnitName)
 538    
{
 539        
数据查询
 649
 650        
服务费收入
 680
 681        
其他服务收入
 689
 690        
空置房
 726    }

 727    //楼盘查询
 728    private static void SearchBuilding(int iYear, string CompanyID, List<DataRow> rowList, ref DataTable dt, string CompanyList, string UnitName)
 729    
{
 730        string Err = null
;
 731        
数据查询
 859
 860        
服务费收入
 890
 891        
其他服务收入
 899
 900        
空置房
 936    }

 937    //区域查询
 938    private static void SearchRegion(int iYear, string CompanyID, List<DataRow> rowList, ref DataTable dt, string CompanyList, string UnitName)
 939    
{
 940        
数据查询
1107
1108        
服务费收入
1138
1139        
其他服务收入
1147
1148        
空置房
1184    }

1185    private static void AddColumns(ref DataTable dt, string[] colArray)
1186    
{
1187        for (int i = 0; i < colArray.Length; i++
)
1188        
{
1189            DataColumn col = new DataColumn(colArray[i], typeof(decimal
));
1190            col.DefaultValue = 0
;
1191
            dt.Columns.Add(col);
1192        }

1193    }

1194    /// <summary>
1195    /// 计算百分比
1196    /// </summary>

1197    /// <param name="dt"></param>
1198    /// <param name="dtData"></param>

1199    private static void CalculatePercent(ref DataTable dt, DataTable dtData)
1200    
{
1201        string[] sa = new string[] "jan""feb""mar""apr""may""jun""jul""aug""sep""oct""nov""dec" }
;
1202        decimal dTotal = 0, dTotal2 = 0
;
1203        dt.DefaultView.RowFilter = "Flag=7"
;
1204        DataRow row = dt.DefaultView[0
].Row;
1205        dt.DefaultView.RowFilter = ""
;
1206

1207        DataRow NewRow =
 dt.NewRow();
1208        NewRow["UnitName"= "--"
;
1209        NewRow["ItemName"= "本年收入差异率"
;
1210        NewRow["flag"= 20
;
1211
        dt.Rows.Add(NewRow);
1212

1213        foreach (string s in
 sa)
1214        
{
1215            decimal d = AmbitsWY_CLR.Globe.ConvertToDecimal(dtData.Rows[0
][s]);
1216            if (d != 0)//注意:如果除数为0会出错,所以这里有判断。

1217                NewRow[s] = AmbitsWY_CLR.Globe.ConvertToDecimal(row[s]) / d * 100;
1218            else

1219                NewRow[s] = -1;
1220            dTotal +=
 d;
1221            dTotal2 +=
 AmbitsWY_CLR.Globe.ConvertToDecimal(row[s]);
1222

1223        }

1224        if (dTotal != 0)//注意:如果除数为0会出错,所以这里有判断。
1225            NewRow["Total"= dTotal2 / dTotal * 100;
1226        else

1227            NewRow["Total"= -1;
1228

1229    }

1230    /// <summary>
1231    /// 将数据插入目标表。
1232    /// </summary>

1233    /// <param name="dt"></param>
1234    /// <param name="dtData"></param>
1235    /// <param name="Flag"></param>

1236    private static void InsertData(ref DataTable dt, DataTable dtData, int Flag)
1237    
{
1238        string[] sa = new string[] "jan""feb""mar""apr""may""jun""jul""aug""sep""oct""nov""dec" }
;
1239        while (dtData.Rows.Count > 0
)
1240        
{
1241            
////每次都以第一条记录作为月汇总的ItemName,因为使用完后会删除掉。
1242            //
string sName = Convert.ToString(dtData.Rows[0]["ItemName"]);
1243            //
dtData.DefaultView.RowFilter = "itemName='" + sName + "'";
1244            //
DataRow dRow = dt.NewRow();
1245            //
dRow["ItemName"] = sName;
1246            //
dRow["Flag"] = Flag;
1247            //
dt.Rows.Add(dRow);
1248            //
foreach (DataRowView row2 in dtData.DefaultView)
1249            //
{
1250

1251            //
    int iMonth = Convert.ToInt32(row2["Month"]);
1252            //
    dRow[sa[iMonth - 1]] = row2["TotalMoney"];
1253            //    dtData.Rows.Remove(row2.Row);//
删除,已经用完。
1254            //    //
dtData.DefaultView.Delete(0);
1255            //}

1256            ////dtData.DefaultView.RowFilter = "";
1257

1258
1259            //每次都以第一条记录作为月汇总的ItemName,因为使用完后会删除掉。

1260            string sName = Convert.ToString(dtData.Rows[0]["ItemName"]);
1261            string unitName = Convert.ToString(dtData.Rows[0]["UnitName"
]);
1262            dtData.DefaultView.RowFilter = "UnitName='" + unitName + "' and itemName='" + sName + "'"
;
1263            DataRow dRow =
 dt.NewRow();
1264            dRow["UnitName"=
 unitName;
1265            dRow["ItemName"=
 sName;
1266            dRow["Flag"=
 Flag;
1267
            dt.Rows.Add(dRow);
1268            foreach (DataRowView row2 in
 dtData.DefaultView)
1269            
{
1270

1271                int iMonth = Convert.ToInt32(row2["Month"
]);
1272                dRow[sa[iMonth - 1]] = row2["TotalMoney"
];
1273                dtData.Rows.Remove(row2.Row);//
删除,已经用完。
1274                //dtData.DefaultView.Delete(0);

1275            }

1276            //dtData.DefaultView.RowFilter = "";
1277        }

1278    }

1279    /// <summary>
1280    /// 添加合计信息
1281    /// </summary>

1282    /// <param name="dt"></param>
1283    /// <param name="ItemName"></param>
1284    /// <param name="Flag"></param>
1285    /// <param name="Filter"></param>

1286    private static void AddSum(ref DataTable dt, string UnitName, string ItemName, int Flag, string Filter)
1287    
{
1288        DataRow row =
 dt.NewRow();
1289        row["UnitName"=
 UnitName;
1290        row["ItemName"=
 ItemName;
1291        row["Flag"=
 Flag;
1292        string[] sa = new string[] "jan""feb""mar""apr""may""jun""jul""aug""sep""oct""nov""dec" }
;
1293        foreach (string s in
 sa)
1294        
{
1295            row[s] = AmbitsWY_CLR.Globe.ConvertToDecimal(dt.Compute(string.Format("sum({0})"
, s), Filter));
1296        }

1297        dt.Rows.Add(row);
1298    }

1299    /// <summary>
1300    /// 获取公司的所有下级公司ID列表,避免重复查询。
1301    /// </summary>

1302    /// <param name="CompanyID"></param>
1303    /// <returns></returns>

1304    private static string GetCompanyList(string CompanyID)
1305    
{
1306

1307        string Err = null
;
1308        string sCode = AmbitsWY_CLR.Globe.GetSqlStringValue("select UnitCode from System_Framework where UnitCode=" + CompanyID, "--"
);
1309        if (sCode == "--"
)
1310            throw new Exception("获取公司编码出错"
);
1311        DataSet ds = AmbitsWY_CLR.Globe.ExecSqlForDataSet("select UnitID from System_Framework where UnitCode like '" + sCode + "%'"out
 Err);
1312        if (Err != null
)
1313        
{
1314            throw new Exception("获取公司信息出错:" +
 Err);
1315

1316        }

1317        string IDList = "";
1318        foreach (DataRow row in ds.Tables[0
].Rows)
1319        
{
1320            IDList += (string.IsNullOrEmpty(IDList) ? "" : ","+ Convert.ToString(row["UnitID"
]);
1321        }

1322        return IDList;
1323    }

1324    private static string GetUnitList(string CompanyID)
1325    
{
1326

1327        string Err = null
;
1328        string sqlstr = string.Format("select unitcode From System_FrameWork Where UnitCode in(select Context from CW_F_GetStringsTable_CLR('{0}',','))"
, CompanyID);
1329        DataSet ds = AmbitsWY_CLR.Globe.ExecSqlForDataSet(sqlstr, out
 Err);
1330        if (Err != null
)
1331        
{
1332            throw new Exception("获取公司信息出错:" +
 Err);
1333

1334        }

1335        string IDList = "";
1336        foreach (DataRow row in ds.Tables[0].Rows)
1337        {
1338            string sCode = row["unitcode"].ToString();
1339            DataSet ds1 = AmbitsWY_CLR.Globe.ExecSqlForDataSet("select UnitID from System_Framework where UnitCode like '" + sCode + "%'"out Err);
1340            foreach (DataRow thisRow in ds1.Tables[0].Rows)
1341            {
1342                IDList += (string.IsNullOrEmpty(IDList) ? "" : ","+ Convert.ToString(thisRow["UnitID"]);
1343            }

1344
1345        }

1346        return IDList;
1347    }

1348    //填充返回表的行的方法,这个方法有一定的规定:
1349    //一定是空返回的void类型,并且入参的第一个必须为object,其后面的参数都必须为out类型
1350    //参数的类型,个数和顺序由返回表的列结构决定!(在TableDefinition = " String nvarchar(200)"中定义的表结构)
1351    public static void FillRow3(object row, out int ID, out string UnitName, out string ItemName, out decimal jan, out decimal feb, out decimal mar, out decimal apr, out decimal may, out decimal jun, out decimal jul, out decimal aug, out decimal sep, out decimal oct, out decimal nov, out decimal dec, out decimal Total, out int Flag)
1352    {
1353        //这个object 其实就是GetStrings(string x,char y)函数返回的迭代,这样你直接赋值给那个列就可以了。
1354        DataRow objRow = row as DataRow;
1355        if (objRow == null)
1356        {
1357            ID = 0;
1358            UnitName = "";
1359            ItemName = "";
1360            jan = 0;
1361            feb = 0;
1362            mar = 0;
1363            apr = 0;
1364            may = 0;
1365            jun = 0;
1366            jul = 0;
1367            aug = 0;
1368            sep = 0;
1369            oct = 0;
1370            nov = 0;
1371            dec = 0;
1372            Total = 0;
1373            Flag = 0;
1374        }

1375        else
1376        {
1377            ID = AmbitsWY_CLR.Globe.ConvertToInt(objRow["tID"], -1);
1378            UnitName = Convert.ToString(objRow["UnitName"]);
1379            ItemName = Convert.ToString(objRow["ItemName"]);
1380            jan = AmbitsWY_CLR.Globe.ConvertToDecimal(objRow["jan"]);
1381            feb = AmbitsWY_CLR.Globe.ConvertToDecimal(objRow["feb"]);
1382            mar = AmbitsWY_CLR.Globe.ConvertToDecimal(objRow["mar"]);
1383            apr = AmbitsWY_CLR.Globe.ConvertToDecimal(objRow["apr"]);
1384            may = AmbitsWY_CLR.Globe.ConvertToDecimal(objRow["may"]);
1385            jun = AmbitsWY_CLR.Globe.ConvertToDecimal(objRow["jun"]);
1386            jul = AmbitsWY_CLR.Globe.ConvertToDecimal(objRow["jul"]);
1387            aug = AmbitsWY_CLR.Globe.ConvertToDecimal(objRow["aug"]);
1388            sep = AmbitsWY_CLR.Globe.ConvertToDecimal(objRow["sep"]);
1389            oct = AmbitsWY_CLR.Globe.ConvertToDecimal(objRow["oct"]);
1390            nov = AmbitsWY_CLR.Globe.ConvertToDecimal(objRow["nov"]);
1391            dec = AmbitsWY_CLR.Globe.ConvertToDecimal(objRow["dec"]);
1392
1393            Total = AmbitsWY_CLR.Globe.ConvertToDecimal(objRow["total"]);
1394            Flag = AmbitsWY_CLR.Globe.ConvertToInt(objRow["Flag"]);
1395        }

1396    }

1397    #endregion

1398
1399}

1400
1401
1402
1403
posted on 2009-11-17 15:32  冰云  阅读(874)  评论(0编辑  收藏  举报