using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Pub_Class;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web.Http;
using WebAPI.Models;
using System.IO;
using System.Web;
using WebAPI.Controllers.SCGL.日计划管理;
namespace WebAPI.Controllers
{
//客户余额查询报表Controller
public class Xs_CusBalanceReportController : ApiController
{
//获取系统参数
Pub_Class.ClsXt_SystemParameter oSystemParameter = new Pub_Class.ClsXt_SystemParameter();
public DBUtility.ClsPub.Enum_BillStatus BillStatus;
private json objJsonResult = new json();
SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
DataSet ds;
#region 客户余额查询报表 查询
///
///参数:string sql。
///返回值:object。
///
[Route("Xs_CusBalanceReport/list")]
[HttpGet]
public object getXs_CusBalanceReport(string sWhere, string user)
{
try
{
List columnNameList = new List();
////查看权限
if (!DBUtility.ClsPub.Security_Log("Xs_CusBalanceReport_Query", 1, false, user))
{
objJsonResult.code = "0";
objJsonResult.count = 0;
objJsonResult.Message = "无查看权限!";
objJsonResult.data = null;
return objJsonResult;
}
//获取客户
if (sWhere == null || sWhere.Equals(""))
{
string sql = "select a.HItemID HCusID,a.HNumber HCusNumber,a.HName HCusName,b.HItemID HCurID,b.HName HCurName " +
" from Gy_Customer as a " +
" left join Gy_Currency as b on a.HCurID = b.HItemID " +
" order by a.HNumber ";
ds = oCN.RunProcReturn(sql, "Gy_Customer");
}
else
{
string sql1 = "select a.HItemID HCusID,a.HNumber HCusNumber,a.HName HCusName,b.HItemID HCurID,b.HName HCurName " +
" from Gy_Customer as a " +
" inner join Gy_Currency as b on a.HCurID = b.HItemID " +
" where 1=1 ";
string sql = sql1 + sWhere + " order by a.HNumber ";
ds = oCN.RunProcReturn(sql, "Gy_Customer");
}
//创建临时表
DataTable tb2 = new DataTable("dt2");
//模板缺少列 但需要从数据库中查询出来显示在页面的字段
tb2.Columns.Add("客户内码", typeof(string));
tb2.Columns.Add("客户代码", typeof(string));
tb2.Columns.Add("客户名称", typeof(string));
tb2.Columns.Add("币别内码", typeof(string));
tb2.Columns.Add("币别名称", typeof(string));
tb2.Columns.Add("客户余额", typeof(string));
tb2.Columns.Add("占用额度", typeof(string));
tb2.Columns.Add("可用额度", typeof(string));
for(int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
string HCusID = ds.Tables[0].Rows[i]["HCusID"].ToString();
string HCusNumber = ds.Tables[0].Rows[i]["HCusNumber"].ToString();
string HCusName = ds.Tables[0].Rows[i]["HCusName"].ToString();
string HCurID = ds.Tables[0].Rows[i]["HCurID"].ToString();
string HCurName = ds.Tables[0].Rows[i]["HCurName"].ToString();
//获取对应客户的客户余额、信用额度、可用余额
string sql = "exec Xs_CusBalanceReport_Query " + HCusID;
DataSet ds2 = oCN.RunProcReturn(sql, "Xs_CusBalanceReport_Query");
if (ds2 == null||ds2.Tables.Count==0)
{
objJsonResult.code = "0";
objJsonResult.count = 0;
objJsonResult.Message = "Exception!未获取到对应客户[" + HCusNumber + "][" + HCusName + "]的余额信息!!";
objJsonResult.data = null;
return objJsonResult;
}
if (Double.Parse(ds2.Tables[0].Rows[0]["HCusBalance"].ToString()) <= 0)
{
continue;
}
//向临时表插入数据
DataRow row = tb2.NewRow();
row[0] = HCusID;
row[1] = HCusNumber;
row[2] = HCusName;
row[3] = HCurID;
row[4] = HCurName;
row[5] = ds2.Tables[0].Rows[0]["HCusBalance"].ToString();
row[6] = Double.Parse(ds2.Tables[0].Rows[0]["HCusBalance"].ToString()) + Double.Parse(ds2.Tables[0].Rows[0]["HCreditRating_Now"].ToString()) - Double.Parse(ds2.Tables[0].Rows[0]["HAvailableBalance"].ToString());
row[7] = ds2.Tables[0].Rows[0]["HAvailableBalance"].ToString();
tb2.Rows.Add(row);
}
//添加列名
foreach (DataColumn col in tb2.Columns)
{
Type dataType = col.DataType;
string ColmString = "{\"ColmCols\":\"" + col.ColumnName + "\",\"ColmType\":\"" + dataType.Name + "\"}";
columnNameList.Add(JsonConvert.DeserializeObject(ColmString));//获取到DataColumn列对象的列名
}
objJsonResult.code = "1";
objJsonResult.count = 1;
objJsonResult.Message = "Sucess!";
objJsonResult.data = tb2;
objJsonResult.list = columnNameList;
return objJsonResult;
}
catch (Exception e)
{
objJsonResult.code = "0";
objJsonResult.count = 0;
objJsonResult.Message = "Exception!" + e.ToString();
objJsonResult.data = null;
return objJsonResult;
}
}
#endregion
}
}