using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using DSWeb.Areas.Import.Models.RptFeeTotal;
using DSWeb.TruckMng.Helper;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace DSWeb.Areas.Import.Controllers
{
    /// <summary>
    /// 应收/实收 应付/实付 列表
    /// </summary>
    public class RptFeeTotal_2Controller : Controller
    {
        //
        // GET: /Import/RptFeeTotal_2/

        public ActionResult Index()
        {
            return View();
        }

        public ContentResult GetDataList(int start, int limit, string sort, string condition)
        {
            var dataList = GetDataList(condition);

            var list = dataList.Skip(start).Take(limit);
            
            var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", totalCount = dataList.Count, data = list.ToList() });
            return new ContentResult() { Content = json };
        }

        private static List<RptFeeTotal> GetDataList(string strCondition)
        {
            var strSql = new StringBuilder();
            strSql.Append(" select * from (select cf.gid,cf.customername,m.hth,cf.auditdate,cf.bsno,cf.feename,  ");
            strSql.Append(" cft.dotyperef,convert(varchar,cf.amount) Y,'' S,  ");
            strSql.Append(" cft.dotyperef+'结余:' name,cft.total-  ");
            strSql.Append(" (case when(select max(cft2.total) from ch_fee_total cft2   ");
            strSql.Append(" where cft2.customername=cf.customername  ");
            strSql.Append(" and cft2.dotype=cf.feetype+2 and cft2.auditdate<=cf.auditdate) is null then 0 else  ");
            strSql.Append(" (select max(cft2.total) from ch_fee_total cft2   ");
            strSql.Append(" where cft2.customername=cf.customername  ");
            strSql.Append(" and cft2.dotype=cf.feetype+2 and cft2.auditdate<=cf.auditdate) end) jy  ");
            strSql.Append(" from ch_fee cf  ");
            strSql.Append(" left join ch_fee_total cft on cft.parid=cf.gid  ");
            strSql.Append(" left join import_main m on m.contractno=cf.bsno  ");
            strSql.Append(" where cf.feestatus in(0,8,9)  ");
            strSql.Append(" union all  ");
            strSql.Append(" select cfd.gid,cfd.customername,m.hth hth,cfd.createtime auditdate,'' bsno,  ");
            strSql.Append(" '' feename,'实际结算 '+cft.dotyperef dotyperef,'' Y,convert(varchar,cfd.amount) S,  ");
            strSql.Append(" (select case cft.dotype when 3 then '应收' when 4 then '实收' end)  ");
            strSql.Append(" +'结余:' name,(select case max(cft2.total) when null then 0 else max(cft2.total)  ");
            strSql.Append(" end from ch_fee_total cft2  ");
            strSql.Append(" where cft2.dotype=cfd.feetype  ");
            strSql.Append(" and cft2.customername=cfd.customername and cft2.auditdate<cfd.createtime)-cft.total jy  ");
            strSql.Append(" from ch_fee_do cfd  ");
            strSql.Append(" left join ch_fee_total cft on cft.parid=cfd.gid ");
            strSql.Append(" left join import_main m on m.contractno=cfd.bsno ");
            strSql.Append(" where ((cfd.isdeleted is null) or (cfd.isdeleted<>1))  ");
            strSql.Append(" )tt   ");
            if (!string.IsNullOrEmpty(strCondition))
            {
                strSql.Append(" where " + strCondition);
            }            
            strSql.Append(" order by tt.customername,tt.auditdate,tt.jy,tt.hth ");



            return SetData(strSql);
        }

        private static List<RptFeeTotal> SetData(StringBuilder strSql)
        {
            var headList = new List<RptFeeTotal>();
            Database db = DatabaseFactory.CreateDatabase();
            using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (reader.Read())
                {
                    var data = new RptFeeTotal();
                    #region Set DB data to Object

                    data.gid = Convert.ToString(reader["gid"]);
                    data.HTH = Convert.ToString(reader["HTH"]);
                    data.CustomerName = Convert.ToString(reader["CustomerName"]);
                    data.AuditDate = Convert.ToString(reader["AuditDate"]);
                    data.bsno = Convert.ToString(reader["bsno"]);
                    data.FeeName = Convert.ToString(reader["FeeName"]);
                    data.dotyperef = Convert.ToString(reader["dotyperef"]);


                    data.Y = Convert.ToString(reader["Y"]);
                    data.S = Convert.ToString(reader["S"]);
                    data.name = Convert.ToString(reader["name"]);
                    data.jy = Convert.ToString(reader["jy"]);
                     #endregion
                    headList.Add(data);
                }
                reader.Close();
            }
            return headList;
        }

    }
}