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

namespace GZ_LTHReportForms.YeJiPaiHangBang
{
    [Description("【报表服务插件】销售业绩排行榜_TH"), HotUpdate]
    public class PHBSaleOrderReport : SysReportBaseService
    {
        /// <summary>
        /// 初始化
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();
            this.ReportProperty.ReportName = new LocaleValue("销售业绩排行榜", base.Context.UserLocale.LCID);
            this.ReportProperty.IsUIDesignerColumns = false;
            // 简单账表类型：普通、树形、分页
            this.ReportProperty.ReportType = ReportType.REPORTTYPE_NORMAL;
            this.IsCreateTempTableByPlugin = true;
            // 
            this.IsCreateTempTableByPlugin = true;
            // 
            this.ReportProperty.IsGroupSummary = true;
            // 
            this.ReportProperty.SimpleAllCols = false;
            // 单据主键：两行FID相同，则为同一单的两条分录，单据编号可以不重复显示
            this.ReportProperty.PrimaryKeyFieldName = "FID";
            // 
            this.ReportProperty.IsDefaultOnlyDspSumAndDetailData = true;

            // 设置精度控制
            var list = new List<DecimalControlField>
            {
                new DecimalControlField("FDECIMALS", "OrigInvoiceAmount"),
                new DecimalControlField("FDECIMALS", "ExchangeRate"),
                new DecimalControlField("FDECIMALS", "InvoiceAmountTHB"),
                new DecimalControlField("FDECIMALS", "MonthEndExRate"),
                new DecimalControlField("FDECIMALS", "RevaluationAountInTHB"),
                new DecimalControlField("FDECIMALS", "UnrealizedGainLoss"),
            };
            this.ReportProperty.DecimalControlFieldList = list;
        }

        /// <summary>
        /// 报表列表头
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public override ReportHeader GetReportHeaders(IRptParams filter)
        {
            DynamicObject customFilter = filter.FilterParameter.CustomFilter;

            base.GetReportHeaders(filter);

            ReportHeader header = new ReportHeader();

            // 下单时间
            string fDate = customFilter["F_SDate"]?.ToString();

            int year = Convert.ToInt32(fDate.Split('-')[0]);

            header.AddChild("部门_SBU", new LocaleValue("部门(SBU)"), SqlStorageType.Sqlvarchar);
            header.AddChild("区域明细", new LocaleValue("区域明细"), SqlStorageType.Sqlvarchar);
            header.AddChild("职位名称", new LocaleValue("职位名称"), SqlStorageType.Sqlvarchar);
            header.AddChild("姓名", new LocaleValue("姓名"), SqlStorageType.Sqlvarchar);
            header.AddChild("入司时间1", new LocaleValue("入司时间"), SqlStorageType.SqlDatetime);
            header.AddChild("担任本职位日期1", new LocaleValue("担任本职位日期"), SqlStorageType.SqlDatetime);
            header.AddChild("担任销售日期1", new LocaleValue("担任销售日期"), SqlStorageType.SqlDatetime);
            header.AddChild("入职年限", new LocaleValue("入职年限"), SqlStorageType.Sqlvarchar);
            header.AddChild("派诺集团_已进行集团业绩调整_含往年订单变更", new LocaleValue("派诺集团-已进行集团业绩调整(含往年订单变更)"), SqlStorageType.SqlDecimal);
            header.AddChild("派诺业绩_含往年订单变更", new LocaleValue("派诺业绩(含往年订单变更)"), SqlStorageType.SqlDecimal);
            header.AddChild("兴诺业绩_含往年订单变更", new LocaleValue("兴诺业绩(含往年订单变更)"), SqlStorageType.SqlDecimal);
            header.AddChild("武汉派诺业绩_含往年订单变更", new LocaleValue("武汉派诺业绩(含往年订单变更)"), SqlStorageType.SqlDecimal);
            header.AddChild("广东碳索业绩_含往年订单变更", new LocaleValue("广东碳索业绩(含往年订单变更)"), SqlStorageType.SqlDecimal);
            header.AddChild("香港派诺业绩_含往年订单变更", new LocaleValue("香港派诺业绩(含往年订单变更)"), SqlStorageType.SqlDecimal);
            header.AddChild("华夏云联业绩_含往年订单变更", new LocaleValue("华夏云联业绩(含往年订单变更)"), SqlStorageType.SqlDecimal);
            header.AddChild("当年累计业绩", new LocaleValue("当年累计业绩"), SqlStorageType.SqlDecimal);
            header.AddChild("往年变更合同额", new LocaleValue("往年变更合同额"), SqlStorageType.SqlDecimal);
            header.AddChild("集团回款合计", new LocaleValue("集团回款合计"), SqlStorageType.SqlDecimal);
            header.AddChild("派诺累计回款额", new LocaleValue("派诺累计回款额"), SqlStorageType.SqlDecimal);
            header.AddChild("兴诺累计回款额", new LocaleValue("兴诺累计回款额"), SqlStorageType.SqlDecimal);
            header.AddChild("武汉派诺累计回款额", new LocaleValue("武汉派诺累计回款额"), SqlStorageType.SqlDecimal);
            header.AddChild("广东碳索累计回款额", new LocaleValue("广东碳索累计回款额"), SqlStorageType.SqlDecimal);
            header.AddChild("香港派诺累计回款额", new LocaleValue("香港派诺累计回款额"), SqlStorageType.SqlDecimal);
            header.AddChild("华夏云联累计回款额", new LocaleValue("华夏云联累计回款额"), SqlStorageType.SqlDecimal);
            header.AddChild("本年销售目标-新", new LocaleValue(year + "年销售目标-新"), SqlStorageType.SqlDecimal);
            header.AddChild("本年达标率_不含往年订单变更_新", new LocaleValue(year + "年达标率(不含往年订单变更)-新"), SqlStorageType.SqlDecimal);
            header.AddChild("本年销售目标-原", new LocaleValue(year + "年销售目标-原"), SqlStorageType.SqlDecimal);
            header.AddChild("本年达标率_不含往年订单变更_原", new LocaleValue(year + "年达标率(不含往年订单变更)-原"), SqlStorageType.SqlDecimal);
            header.AddChild("目标调整金额", new LocaleValue("目标调整金额"), SqlStorageType.SqlDecimal);
            header.AddChild("年度销售额1", new LocaleValue((year - 1) + "年度销售额"), SqlStorageType.SqlDecimal);
            header.AddChild("年度销售额2", new LocaleValue((year - 2) + "年度销售额"), SqlStorageType.SqlDecimal);
            header.AddChild("年度销售额3", new LocaleValue((year - 3) + "年度销售额"), SqlStorageType.SqlDecimal);
            header.AddChild("年度销售额4", new LocaleValue((year - 4) + "年度销售额"), SqlStorageType.SqlDecimal);
            header.AddChild("年度销售额5", new LocaleValue((year - 5) + "年度销售额"), SqlStorageType.SqlDecimal);
            header.AddChild("年度销售额6", new LocaleValue((year - 6) + "年度销售额"), SqlStorageType.SqlDecimal);


            foreach (var item in header.GetChilds())
            {
                item.Width = 160;
            }
            return header;
        }

        /// <summary>
        /// 显示报表数据
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="tableName"></param>
        public override void BuilderReportSqlAndTempTable(IRptParams filter, string tableName)
        {
            //获取过滤条件
            string Filter = GetFilterWhere(filter);

            DynamicObject customFilter = filter.FilterParameter.CustomFilter;
            base.BuilderReportSqlAndTempTable(filter, tableName);

            //获取开始日期
            string FStrartDate = (customFilter["F_SDate"] == null) ? "" : Convert.ToDateTime(
                customFilter["F_SDate"]).ToString("yyyy-MM-dd");

            //获取结束日期
            string FEndDate = (customFilter["F_EDate"] == null) ? "" : Convert.ToDateTime(
                customFilter["F_EDate"]).ToString("yyyy-MM-dd");

            //获取多选组织的FID
            var ZZS = customFilter["F_YKQC_MulBase_83g"] as DynamicObjectCollection;
            var OrgFids = "";
            if (ZZS.Count > 0) {
                List<string> zzList = new List<string>();
                //循环获取多选组织FID
                foreach (DynamicObject dobj in ZZS)
                {
                    DynamicObject dynamic = dobj["F_YKQC_MulBase_83g"] as DynamicObject;
                    zzList.Add(dynamic["ID"].ToString());
                }
                OrgFids = string.Join(",", zzList);
            }

            string sql = string.Format(@"/*dialect*/
            EXEC GZYJPHB_TH '{0}','{1}','{2}'

            SELECT 
            ROW_NUMBER() OVER (ORDER BY [部门_SBU] DESC) AS FID,
            ROW_NUMBER() OVER (ORDER BY [部门_SBU] DESC) AS FIDENTITYID,
            FORMAT(入司时间,'yyyy-MM-dd') AS 入司时间1,
            FORMAT(担任本职位日期,'yyyy-MM-dd') AS 担任本职位日期1,
            FORMAT(担任销售日期,'yyyy-MM-dd') AS 担任销售日期1,
            * INTO {3} FROM YJPHB_GZTH {4}
            ", FStrartDate,FEndDate,OrgFids,tableName, Filter);
            DBUtils.Execute(this.Context, sql);
        }

        //报表过滤条件-多选销售员
        private string GetFilterWhere(IRptParams filter)
        {
            DynamicObject customFilter = filter.FilterParameter.CustomFilter;
            StringBuilder strwhere = new StringBuilder(); //创建字符串构建器;
            strwhere.AppendLine("WHERE 1=1");
            var xsy = customFilter["F_YKQC_MulBase_re5"] as DynamicObjectCollection;
            if (xsy.Count > 0)
            {
                List<string> xsyList = new List<string>();
                //循环获取物料信息
                foreach (DynamicObject dobj in xsy)
                {
                    DynamicObject dynamic = dobj["F_YKQC_MulBase_re5"] as DynamicObject;
                    xsyList.Add(dynamic["Name"].ToString());
                }
                strwhere.AppendLine(string.Format(@"AND 姓名 IN ({0})", string.Join(",", xsyList.Select(x => $"'{x}'"))));
            }
            return strwhere.ToString();
        }

        //设置报表合计列
        public override List<SummaryField> GetSummaryColumnInfo(IRptParams filter)
        {
            var result = base.GetSummaryColumnInfo(filter);
            result.Add(new SummaryField("派诺集团_已进行集团业绩调整_含往年订单变更", BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("派诺业绩_含往年订单变更", BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("兴诺业绩_含往年订单变更", BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("武汉派诺业绩_含往年订单变更", BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("广东碳索业绩_含往年订单变更", BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("香港派诺业绩_含往年订单变更", BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("华夏云联业绩_含往年订单变更", BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("当年累计业绩", BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("往年变更合同额", BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("集团回款合计", BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("派诺累计回款额", BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("兴诺累计回款额", BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("武汉派诺累计回款额", BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("广东碳索累计回款额", BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("香港派诺累计回款额", BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("华夏云联累计回款额", BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("年度销售额1", BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("年度销售额2", BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("年度销售额3", BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("年度销售额4", BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("年度销售额5", BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("年度销售额6", BOSEnums.Enu_SummaryType.SUM));
            return result;
        }
    }
}


