using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kingdee.BOS;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.Contracts.Report;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Report;
using Kingdee.BOS.Core.SqlBuilder;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;

namespace GZ_LTHReportForms.YingShouBiao
{
    [Description("应收款情况表(SBU)报表插件"),HotUpdate]
    class YingShouQKBiao : SysReportBaseService
    {
        //初始化方法
        public override void Initialize()
        {
            base.Initialize();
            //设置报表类型为普通类型
            this.ReportProperty.ReportType = ReportType.REPORTTYPE_NORMAL;
            //设置是否通过插件创建临时表
            this.IsCreateTempTableByPlugin = true;
            //设置是否分组汇总
            this.ReportProperty.IsGroupSummary = true;
        }
        //获取过滤条件信息(构造单据信息)
        public override ReportTitles GetReportTitles(IRptParams filter)
        {
            //创建标题对象
            ReportTitles reportTitles = new ReportTitles();
            //获取自定义过滤条件
            DynamicObject customFilter = filter.FilterParameter.CustomFilter;
            if(customFilter != null)
            {
                //获取组织名称
                string multiOrgnNameValues = this.GetMultiOrgnNameValues(customFilter["F_YKQC_OrgId_ca9"].ToString());
                //获取起始日期
                string startValue = (customFilter["F_YKQC_Date_apv"] == null) ? string.Empty : Convert.ToDateTime(customFilter["F_YKQC_Date_apv"])
                    .ToString("yyyy-MM-dd");
                //获取结束日期
                string endValue = (customFilter["F_YKQC_Date_tzk"] == null) ? string.Empty : Convert.ToDateTime(customFilter["F_YKQC_Date_tzk"])
                    .ToString("yyyy-MM-dd");
                //添加标题信息
                reportTitles.AddTitle("F_YKQC_OrgId_re5", multiOrgnNameValues);
                reportTitles.AddTitle("F_YKQC_Date_qtr", startValue);
                reportTitles.AddTitle("F_YKQC_Date_83g", endValue);
            }
            //返回标题
            return reportTitles;
        }
        //获取组织名称
        private string GetMultiOrgnNameValues(string orgIdStrings)
        {
            //创建组织名称列表
            List<string> list = new List<string>();
            //初始化结果字符串
            string result = string.Empty;
            //如果组织ID字符串不为空
            if (orgIdStrings.Trim().Length > 0)
            {
                //获取查询服务
                IQueryService service = Kingdee.BOS.Contracts.ServiceFactory.GetService<IQueryService>(base.Context);
                //创建查询参数
                QueryBuilderParemeter para = new QueryBuilderParemeter {
                    //表单ID
                    FormId = "ORG_Organizations",
                    //查询组织名称
                    SelectItems = SelectorItemInfo.CreateItems("FNAME"),
                    //过滤条件，根据组织ID和区域ID
                    FilterClauseWihtKey = string.Format("FORGID IN ({0}) AND FLOCALEID = {1}",orgIdStrings,base.Context.UserLocale.LCID)
                };
                //获取动态对象集合
                DynamicObjectCollection dynamicObjects = service.GetDynamicObjectCollection(base.Context,para,null);
                //遍历获取到的动态对象
                foreach (DynamicObject current in dynamicObjects)
                {
                    //将组织名称添加到列表
                    list.Add(current["FNAME"].ToString());
                }
                //如果列表不为空
                if (list.Count > 0)
                {
                    result = string.Join(",",list.ToArray());
                }
            }
            //返回组织名称字符串
            return result;
        }
        //设置单据列
        public override ReportHeader GetReportHeaders(IRptParams filter)
        {
            //创建表头对象
            ReportHeader header = new ReportHeader();

            return header;
        }

        //创建临时表
        public override void BuilderReportSqlAndTempTable(IRptParams filter, string tableName)
        {
            //获取过滤条件
            string Filter = GetFilterWhere(filter);
            //获取排序字段
            string seqFId = string.Format(base.KSQL_SEQ,OrderColumn(filter));
            /*
                取数SQL
                SQL查询字符串，提取所需数据并将结果存入临时表
             */
            string sql = string.Format(@"");
            //执行SQL并动态创建报表
            DBUtils.ExecuteDynamicObject(this.Context,sql);
        }

        //获取过滤条件
        private string GetFilterWhere(IRptParams fileter)
        {
            //获取自定义过滤条件
            DynamicObject customFilter = fileter.FilterParameter.CustomFilter;
            //创建字符串构建器
            StringBuilder strwhere = new StringBuilder();
            //初始化过滤条件
            strwhere.AppendLine("Where 1=1");
            //组织过滤条件
            string org = string.IsNullOrWhiteSpace(customFilter["F_YKQC_OrgId_ca9"].ToString()) ? "" : string.Format("AND A.FPURCHASEORGID IN ({0})",
                Convert.ToString(customFilter["F_YKQC_OrgId_ca9"]));
            //添加组织过滤条件
            strwhere.AppendLine(org);
            //日期
            string startValue = (customFilter["F_YKQC_Date_apv"] == null) ? string.Empty : 
                Convert.ToDateTime(customFilter["F_YKQC_Date_apv"]).ToString("yyyy-MM-dd");
            string endValue = (customFilter["F_YKQC_Date_tzk"] == null) ? string.Empty :
                Convert.ToDateTime(customFilter["F_YKQC_Date_tzk"]).ToString("yyyy-MM-dd");
            //添加日期过滤条件
            strwhere.AppendLine(string.Format("AND FDate >= '{0}' AND FDate <= '{1}'",startValue,endValue));
            return strwhere.ToString();
        }

        //获取排序语句
        private string OrderColumn(IRptParams filter)
        {
            //初始化排序字符串
            string OrderBy = "";
            string datasort = Convert.ToString(filter.FilterParameter.SortString);//获取排序条件
            if (datasort != "")
            {
                OrderBy = "" + datasort + "";
            }
            else
            {
                OrderBy = "FID";
            }

            return OrderBy;
        }
    }
}
