using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QMAPP.MESReport.Entity.LineQTY;
using QMAPP.MESReport.DAL.LineQTY;
using QMAPP.BLL;
using QMFrameWork.Data;
using QMAPP.Entity;
using System.Data;
using QMAPP.BLL.Sys;
using QMAPP.Entity.Sys;
using QMAPP.MD.BLL;
using QMAPP.MD.Entity;
using QMAPP.MESReport.Entity.Tables;

namespace QMAPP.MESReport.BLL.LineQTY
{
    /// <summary>
    /// 返修率统计
    /// 于子清
    /// 2017-10-25
    /// </summary>
    public class RepairRateCountBLL : BaseBLL
    {
        int tday = -7;
        //报废统计-柱状图
        public DataResult<List<QTYCompletionRateDModel>> GetAllList(QTYCompletionRateDModel condition)
        {
            DataResult<List<QTYCompletionRateDModel>> result = new DataResult<List<QTYCompletionRateDModel>>();
            try
            {
                result.Result = new RepairRateCountDAL().GetAllList(condition);
                result.IsSuccess = true;
                return result;
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.Msg = ex.ToString();
                return result;
            }
        }
        //注塑-报废统计
        public DataResult<List<QTYCompletionRateDModel>> GetAllListNew1(QTYCompletionRateDModel condition)
        {
            DataResult<List<QTYCompletionRateDModel>> result = new DataResult<List<QTYCompletionRateDModel>>();
            try
            {
                string sql = "SELECT count(1) as COMPLETE_QTY,MATERIAL_CODE FROM T_AW_MAINOPERATION where CREATEDATE between DATEADD(day,-7,getdate()) and getdate() and WORKCENTER_CODE='INJECT' group by MATERIAL_CODE";
                var qtys = new RepairRateCountDAL().GetAllListNew("INJECT", tday, sql);
               //var materials = new MaterialBLL().GetAllList(new Material());
               //foreach (QTYCompletionRateDModel item in qtys)
               // {
               //     //处理人
               //     if (!string.IsNullOrEmpty(item.MATERIAL_CODE))
               //     {
               //         var material = materials.FirstOrDefault(x => x.MATERIAL_CODE == item.MATERIAL_CODE);
               //         if (material != null)
               //         {
               //             item.MATERIAL_NAME = material.MATERIAL_NAME;
               //             item.MATERIAL_SHORT = material.MATERIAL_SHORT;
               //         }
               //     }
               // }
                result.Result = qtys;
                result.IsSuccess = true;
                return result;
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.Msg = ex.ToString();
                return result;
            }
        }
        //门板-报废统计
        public DataResult<List<QTYCompletionRateDModel>> GetAllListNew2(QTYCompletionRateDModel condition)
        {
            DataResult<List<QTYCompletionRateDModel>> result = new DataResult<List<QTYCompletionRateDModel>>();
            try
            {
                string sql = "SELECT count(1) as COMPLETE_QTY,.T_AW_PRODUCT.MATERIAL_CODE FROM T_AW_PRODUCT left join T_MD_MATERIAL M ON T_AW_PRODUCT.MATERIAL_CODE=M.MATERIAL_CODE where T_AW_PRODUCT.CREATEDATE between DATEADD(day,-7,getdate()) and getdate() and  M.PROJECTCODE='VW331 DP' and exists(select 1 from T_MD_MATERIAL_CLASS mc where mc.MATERIAL_TYPE_CODE=MATERIAL_TYPE and mc.MATERIAL_ATTRIBUTE='2') group by T_AW_PRODUCT.MATERIAL_CODE";
                var qtys = new RepairRateCountDAL().GetAllListNew("BORADPASSY", tday, sql);
                //var materials = new MaterialBLL().GetAllList(new Material());
                //foreach (QTYCompletionRateDModel item in qtys)
                //{
                //    //处理人
                //    if (!string.IsNullOrEmpty(item.MATERIAL_CODE))
                //    {
                //        var material = materials.FirstOrDefault(x => x.MATERIAL_CODE == item.MATERIAL_CODE);
                //        if (material != null)
                //        {
                //            item.MATERIAL_NAME = material.MATERIAL_NAME;
                //            item.MATERIAL_SHORT = material.MATERIAL_SHORT;
                //        }
                //    }
                //}
                result.Result = qtys;
                result.IsSuccess = true;
                return result;
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.Msg = ex.ToString();
                return result;
            }
        }
        //仪表板-报废统计
        public DataResult<List<QTYCompletionRateDModel>> GetAllListNew3(QTYCompletionRateDModel condition)
        {
            DataResult<List<QTYCompletionRateDModel>> result = new DataResult<List<QTYCompletionRateDModel>>();
            try
            {
                string sql = "SELECT count(1) as COMPLETE_QTY,.T_AW_PRODUCT.MATERIAL_CODE FROM T_AW_PRODUCT left join T_MD_MATERIAL M ON T_AW_PRODUCT.MATERIAL_CODE=M.MATERIAL_CODE where T_AW_PRODUCT.CREATEDATE between DATEADD(day,-7,getdate()) and getdate() and  M.PROJECTCODE='VW331/5CN IP' and exists(select 1 from T_MD_MATERIAL_CLASS mc where mc.MATERIAL_TYPE_CODE=MATERIAL_TYPE and mc.MATERIAL_ATTRIBUTE='2') group by T_AW_PRODUCT.MATERIAL_CODE";
                var qtys = new RepairRateCountDAL().GetAllListNew("BORAIPASSY", tday, sql);
                //var materials = new MaterialBLL().GetAllList(new Material());
                //foreach (QTYCompletionRateDModel item in qtys)
                //{
                //    //处理人
                //    if (!string.IsNullOrEmpty(item.MATERIAL_CODE))
                //    {
                //        var material = materials.FirstOrDefault(x => x.MATERIAL_CODE == item.MATERIAL_CODE);
                //        if (material != null)
                //        {
                //            item.MATERIAL_NAME = material.MATERIAL_NAME;
                //            item.MATERIAL_SHORT = material.MATERIAL_SHORT;
                //        }
                //    }
                //}
                result.Result = qtys;
                result.IsSuccess = true;
                return result;
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.Msg = ex.ToString();
                return result;
            }
        }
        /// <summary>
        /// 注塑返修率
        /// </summary>
        /// <returns></returns>
        public DataResult<List<QTYCompletionRateDModel>> GetInjectReworkRate()
        {
            DataResult<List<QTYCompletionRateDModel>> result = new DataResult<List<QTYCompletionRateDModel>>();
            try
            {
                var qtys = new RepairRateCountDAL().GetInjectReworkRate();
                result.Result = qtys;
                result.IsSuccess = true;
                return result;
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.Msg = ex.ToString();
                return result;
            }
            
        }
        /// <summary>
        /// 仪表板、门板返修率
        /// </summary>
        /// <returns></returns>
        public DataResult<List<QTYCompletionRateDModel>> GetReworkRate(string project)
        {
            DataResult<List<QTYCompletionRateDModel>> result = new DataResult<List<QTYCompletionRateDModel>>();
            try
            {
                var qtys = new RepairRateCountDAL().GetReworkRate(project);
                result.Result = qtys;
                result.IsSuccess = true;
                return result;
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.Msg = ex.ToString();
                return result;
            }
            
        }

        #region 导出数据
        /// <summary>
        /// 获取导出的数据
        /// </summary>
        /// <param name="user">查询条件</param>
        /// <returns>数据</returns>
        public DataResult<DataTable> GetExportData(QTYCompletionRateDModel model)
        {
            DataResult<DataTable> result = new DataResult<DataTable>();
            try
            {
                result.IsSuccess = true;
                result.Result = new RepairRateCountDAL().GetExportData(model);
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.Msg = "导出错误!";
            }
            return result;
        }
        public DataResult<DataTable> GetExportDataReason(RepairRateCountDModel model)
        {
            DataResult<DataTable> result = new DataResult<DataTable>();
            try
            {
                result.IsSuccess = true;
                result.Result = new RepairRateCountDAL().GetExportDataReason(model);
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.Msg = "导出错误!";
            }
            return result;
        }
        #endregion

        //报废原因、趋势统计-饼状图-柱状图
        public DataResult<List<RepairRateCountDModel>> GetMenderReason(RepairRateCountDModel condition)
        {
            DataResult<List<RepairRateCountDModel>> result = new DataResult<List<RepairRateCountDModel>>();
            try
            {
                result.Result = new RepairRateCountDAL().GetMenderReason(condition);
                result.IsSuccess = true;
                return result;
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.Msg = ex.ToString();
                return result;
            }
        }


    }
}