using MiniExcelLibs;
using Model;
using System.Collections.Generic;
using System;
using System.Linq;
using System.Web.UI.WebControls;
namespace BLL
{
    /// 
    /// 岗位培训标准
    /// 
    public static class PostTrainingStandardService
    {
        /// 
        /// 根据主键获取岗位培训标准信息
        /// 
        /// 
        /// 
        public static Model.PostTraining_Standard GetStandardById(string Id)
        {
            return Funs.DB.PostTraining_Standard.FirstOrDefault(e => e.Id == Id);
        }
        /// 
        /// 添加岗位培训标准
        /// 
        /// 
        public static void AddStandard(Model.PostTraining_Standard model)
        {
            Model.PostTraining_Standard newModel = new Model.PostTraining_Standard
            {
                Id = model.Id,
                Code = model.Code,
                //RoleId = model.RoleId,
                WorkPostId = model.WorkPostId,
                CourseId = model.CourseId,
                ClassHour = model.ClassHour,
                Cycle = model.Cycle,
                Method = model.Method,
                Outcome = model.Outcome,
                Teachers = model.Teachers,
                CompileDate = model.CompileDate,
                CompileMan = model.CompileMan,
                Remark = model.Remark
            };
            Funs.DB.PostTraining_Standard.InsertOnSubmit(newModel);
            Funs.DB.SubmitChanges();
        }
        /// 
        /// 修改
        /// 
        /// 
        public static void UpdateStandard(Model.PostTraining_Standard model)
        {
            Model.PostTraining_Standard newModel = Funs.DB.PostTraining_Standard.FirstOrDefault(e => e.Id == model.Id);
            if (newModel != null)
            {
                newModel.Code = model.Code;
                //newModel.RoleId = model.RoleId;
                newModel.WorkPostId = model.WorkPostId;
                newModel.CourseId = model.CourseId;
                newModel.ClassHour = model.ClassHour;
                newModel.Cycle = model.Cycle;
                newModel.Method = model.Method;
                newModel.Outcome = model.Outcome;
                newModel.Teachers = model.Teachers;
                newModel.Remark = model.Remark;
                Funs.DB.SubmitChanges();
            }
        }
        /// 
        /// 删除
        /// 
        /// 
        public static void DeleteStandardById(string Id)
        {
            Model.PostTraining_Standard model = Funs.DB.PostTraining_Standard.FirstOrDefault(e => e.Id == Id);
            if (model != null)
            {
                CommonService.DeleteAttachFileById(Id);
                Funs.DB.PostTraining_Standard.DeleteOnSubmit(model);
                Funs.DB.SubmitChanges();
            }
        }
        /// 
        /// 获取所有岗位培训标准List
        /// 
        /// 
        public static List GetStandardList()
        {
            return (from x in Funs.DB.PostTraining_Standard orderby x.Code select x).ToList();
        }
        /// 
        /// 获取所有岗位培训标准信息List
        /// 
        /// 
        public static List GetStandardInfoList()
        {
            var db = Funs.DB;
            var lst = (from x in db.PostTraining_Standard
                       join y in db.PostTraining_Course on x.CourseId equals y.Id
                       join z in db.Base_WorkPost on x.WorkPostId equals z.WorkPostId
                       //join z in db.PostTraining_Role on x.RoleId equals z.Id
                       orderby x.Code
                       select new PostTrainingStandardInfo
                       {
                           WorkPostId = x.WorkPostId,
                           WorkPostName = z.WorkPostName,
                           //RoleId = x.RoleId,
                           //RoleName = z.RoleName,
                           CourseId = x.CourseId,
                           CourseName = y.Name,
                           CategoryName = y.CategoryName,
                           ClassHour = x.ClassHour,
                           Cycle = x.Cycle,
                           Method = x.Method,
                           Outcome = x.Outcome,
                           Teachers = x.Teachers,
                           Remark = x.Remark
                       }).ToList();
            return lst;
        }
        /// 
        /// 导入数据
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static ResponeData ImportData(string OriFileName, string path, string projectid, string creatUserId, ref List errorList)
        {
            var responeData = new ResponeData();
            List temeplateDtoIns;
            try
            {
                temeplateDtoIns = MiniExcel.Query(path, startCell: "A1").ToList();
            }
            catch (Exception ex)
            {
                responeData.code = 0;
                responeData.message = "模板错误:" + ex.ToString();
                return responeData;
            }
            if (temeplateDtoIns.Count == 0)
            {
                responeData.code = 0;
                responeData.message = "导入数据为空!";
                return responeData;
            }
            //培训标准
            var lstStandard = GetStandardInfoList();
            //培训课程
            var lstCourse = PostTrainingCourseService.GetCourseList();
            ////培训角色
            //var lstRole = PostTrainingRoleService.GetRoleList();
            var lstWorkPost = WorkPostService.GetWorkPostList();
            //培训类别
            var lstCategory = PostTrainingCategoryService.GetCategoryList();
            //培训方式
            var lstMethod = PostTrainingMethodService.GetMethodList();
            //培训教资
            var lstTeachers = PostTrainingTeachersService.GetTeachersList();
            //培训效果
            var lstOutcome = GetPostTrainingOutcomeList();
            #region 数据校验
            string errorMsg = string.Empty;
            int rowIndex = 3;
            foreach (var item in temeplateDtoIns)
            {
                string roleName = !string.IsNullOrWhiteSpace(item.RoleName) ? item.RoleName.Trim() : string.Empty;
                string categoryName = !string.IsNullOrWhiteSpace(item.CategoryName) ? item.CategoryName.Trim() : string.Empty;
                string courseName = !string.IsNullOrWhiteSpace(item.CourseName) ? item.CourseName.Trim() : string.Empty;
                string classHourStr = !string.IsNullOrWhiteSpace(item.ClassHour) ? item.ClassHour.Trim() : string.Empty;
                string cycleStr = !string.IsNullOrWhiteSpace(item.Cycle) ? item.Cycle.Trim() : string.Empty;
                decimal classHour = 0;
                decimal cycle = 0;
                string method = !string.IsNullOrWhiteSpace(item.Method) ? item.Method.Trim() : string.Empty;
                string outcome = !string.IsNullOrWhiteSpace(item.Outcome) ? item.Outcome.Trim() : string.Empty;
                string teachers = !string.IsNullOrWhiteSpace(item.Teachers) ? item.Teachers.Trim() : string.Empty;
                string remark = !string.IsNullOrWhiteSpace(item.Remark) ? item.Remark.Trim() : string.Empty;
                if (!string.IsNullOrWhiteSpace(roleName))
                {
                    if (!lstWorkPost.Where(x => x.WorkPostName == roleName).Any())
                    {
                        Model.ErrorInfo errorInfo = new Model.ErrorInfo();
                        errorInfo.Row = rowIndex.ToString();
                        errorInfo.Column = "适用岗位角色";
                        errorInfo.Reason = $"角色不存在:{roleName}";
                        errorList.Add(errorInfo);
                    }
                }
                else
                {
                    Model.ErrorInfo errorInfo = new Model.ErrorInfo();
                    errorInfo.Row = rowIndex.ToString();
                    errorInfo.Column = "适用岗位角色";
                    errorInfo.Reason = "不可为空";
                    errorList.Add(errorInfo);
                }
                if (!string.IsNullOrWhiteSpace(categoryName))
                {
                    if (!lstCategory.Where(x => x.Name == categoryName).Any())
                    {
                        Model.ErrorInfo errorInfo = new Model.ErrorInfo();
                        errorInfo.Row = rowIndex.ToString();
                        errorInfo.Column = "培训类别";
                        errorInfo.Reason = $"培训类别不存在:{categoryName}";
                        errorList.Add(errorInfo);
                    }
                }
                else
                {
                    Model.ErrorInfo errorInfo = new Model.ErrorInfo();
                    errorInfo.Row = rowIndex.ToString();
                    errorInfo.Column = "培训类别";
                    errorInfo.Reason = "不可为空";
                    errorList.Add(errorInfo);
                }
                if (string.IsNullOrWhiteSpace(courseName))
                {
                    Model.ErrorInfo errorInfo = new Model.ErrorInfo();
                    errorInfo.Row = rowIndex.ToString();
                    errorInfo.Column = "培训课程";
                    errorInfo.Reason = "不可为空";
                    errorList.Add(errorInfo);
                }
                if (!string.IsNullOrWhiteSpace(categoryName) && !string.IsNullOrWhiteSpace(courseName))
                {
                    if (temeplateDtoIns.Where(x => x.RoleName == roleName && x.CourseName == courseName && x.CategoryName == categoryName).Count() > 1)
                    {
                        Model.ErrorInfo errorInfo = new Model.ErrorInfo();
                        errorInfo.Row = rowIndex.ToString();
                        errorInfo.Column = "培训课程";
                        errorInfo.Reason = $"【{roleName}】导入数据重复:{courseName}({categoryName})";
                        errorList.Add(errorInfo);
                    }
                    if (!lstCourse.Where(x => x.Name == courseName && x.CategoryName == categoryName).Any())
                    {
                        Model.ErrorInfo errorInfo = new Model.ErrorInfo();
                        errorInfo.Row = rowIndex.ToString();
                        errorInfo.Column = "培训课程";
                        errorInfo.Reason = $"【{roleName}】不存在培训课程:{courseName}({categoryName})";
                        errorList.Add(errorInfo);
                    }
                    else if (lstStandard.Where(x => x.CourseName == courseName && x.CategoryName == categoryName).Any())
                    {
                        Model.ErrorInfo errorInfo = new Model.ErrorInfo();
                        errorInfo.Row = rowIndex.ToString();
                        errorInfo.Column = "培训课程";
                        errorInfo.Reason = $"培训标准已存在:{courseName}({categoryName})";
                        errorList.Add(errorInfo);
                    }
                }
                if (!string.IsNullOrEmpty(classHourStr))
                {
                    try
                    {
                        classHour = decimal.Parse(classHourStr);
                        if (classHour <= 0)
                        {
                            Model.ErrorInfo errorInfo = new Model.ErrorInfo();
                            errorInfo.Row = rowIndex.ToString();
                            errorInfo.Column = "培训课时";
                            errorInfo.Reason = "请填写大于0的数值";
                            errorList.Add(errorInfo);
                        }
                    }
                    catch (Exception)
                    {
                        Model.ErrorInfo errorInfo = new Model.ErrorInfo();
                        errorInfo.Row = rowIndex.ToString();
                        errorInfo.Column = "培训课时";
                        errorInfo.Reason = "请填写大于0的数值";
                        errorList.Add(errorInfo);
                    }
                }
                else
                {
                    Model.ErrorInfo errorInfo = new Model.ErrorInfo();
                    errorInfo.Row = rowIndex.ToString();
                    errorInfo.Column = "培训课时";
                    errorInfo.Reason = "请填写大于0的数值";
                    errorList.Add(errorInfo);
                }
                if (!string.IsNullOrEmpty(cycleStr))
                {
                    try
                    {
                        cycle = decimal.Parse(cycleStr);
                        if (cycle <= 0)
                        {
                            Model.ErrorInfo errorInfo = new Model.ErrorInfo();
                            errorInfo.Row = rowIndex.ToString();
                            errorInfo.Column = "培训周期";
                            errorInfo.Reason = "请填写大于0的数值";
                            errorList.Add(errorInfo);
                        }
                    }
                    catch (Exception)
                    {
                        Model.ErrorInfo errorInfo = new Model.ErrorInfo();
                        errorInfo.Row = rowIndex.ToString();
                        errorInfo.Column = "培训周期";
                        errorInfo.Reason = "请填写大于0的数值";
                        errorList.Add(errorInfo);
                    }
                }
                else
                {
                    Model.ErrorInfo errorInfo = new Model.ErrorInfo();
                    errorInfo.Row = rowIndex.ToString();
                    errorInfo.Column = "培训周期";
                    errorInfo.Reason = "请填写大于0的数值";
                    errorList.Add(errorInfo);
                }
                if (!string.IsNullOrWhiteSpace(method))
                {
                    if (!lstMethod.Where(x => x.Name == method).Any())
                    {
                        Model.ErrorInfo errorInfo = new Model.ErrorInfo();
                        errorInfo.Row = rowIndex.ToString();
                        errorInfo.Column = "培训方式";
                        errorInfo.Reason = $"培训方式字典不存在:{method}";
                        errorList.Add(errorInfo);
                    }
                }
                if (!string.IsNullOrWhiteSpace(outcome))
                {
                    if (!lstOutcome.Where(x => x.Text == outcome).Any())
                    {
                        Model.ErrorInfo errorInfo = new Model.ErrorInfo();
                        errorInfo.Row = rowIndex.ToString();
                        errorInfo.Column = "培训效果";
                        errorInfo.Reason = $"培训效果字典不存在:{outcome}";
                        errorList.Add(errorInfo);
                    }
                }
                if (!string.IsNullOrWhiteSpace(teachers))
                {
                    if (!lstTeachers.Where(x => x.Name == teachers).Any())
                    {
                        Model.ErrorInfo errorInfo = new Model.ErrorInfo();
                        errorInfo.Row = rowIndex.ToString();
                        errorInfo.Column = "培训师资";
                        errorInfo.Reason = $"培训师资字典不存在:{teachers}";
                        errorList.Add(errorInfo);
                    }
                }
                rowIndex++;
            }
            if (errorList.Any())
            {
                responeData.code = 0;
                responeData.message = "存在异常数据!";
                return responeData;
            }
            #endregion
            foreach (var item in temeplateDtoIns)
            {
                string roleName = !string.IsNullOrWhiteSpace(item.RoleName) ? item.RoleName.Trim() : string.Empty;
                string categoryName = !string.IsNullOrWhiteSpace(item.CategoryName) ? item.CategoryName.Trim() : string.Empty;
                string courseName = !string.IsNullOrWhiteSpace(item.CourseName) ? item.CourseName.Trim() : string.Empty;
                decimal classHour = decimal.Parse(item.ClassHour);
                decimal cycle = decimal.Parse(item.Cycle);
                string method = !string.IsNullOrWhiteSpace(item.Method) ? item.Method.Trim() : string.Empty;
                string outcome = !string.IsNullOrWhiteSpace(item.Outcome) ? item.Outcome.Trim() : string.Empty;
                string teachers = !string.IsNullOrWhiteSpace(item.Teachers) ? item.Teachers.Trim() : string.Empty;
                string remark = !string.IsNullOrWhiteSpace(item.Remark) ? item.Remark.Trim() : string.Empty;
                Model.PostTraining_Standard newModel = new Model.PostTraining_Standard
                {
                    Id = SQLHelper.GetNewID(typeof(Model.PostTraining_Standard)),
                    Code = SQLHelper.RunProcNewId("SpGetNewCode5", "dbo.PostTraining_Standard", "Code", ""),
                    //ProjectId = this.ProjectId,
                    CompileDate = DateTime.Now,
                    CompileMan = creatUserId
                };
                newModel.CourseId = lstCourse.Where(x => x.Name == courseName).FirstOrDefault().Id;
                //newModel.RoleId = lstRole.Where(x => x.Name == roleName).FirstOrDefault().Id;
                newModel.WorkPostId = lstWorkPost.Where(x => x.WorkPostName == roleName).FirstOrDefault().WorkPostId;
                newModel.ClassHour = classHour;
                newModel.Cycle = cycle;
                newModel.Method = method;
                newModel.Outcome = outcome;
                newModel.Teachers = teachers;
                newModel.Remark = remark;
                AddStandard(newModel);
            }
            return responeData;
        }
        /// 
        /// 培训效果
        /// 
        /// 
        /// 
        public static void InitPostTrainingOutcomeDropDownList(FineUIPro.DropDownList dropName, bool isShowPlease)
        {
            dropName.DataValueField = "Text";
            dropName.DataTextField = "Text";
            dropName.DataSource = GetPostTrainingOutcomeList();
            dropName.DataBind();
            if (isShowPlease)
            {
                Funs.FineUIPleaseSelect(dropName);
            }
        }
        /// 
        /// 培训效果
        /// 
        /// 
        public static ListItem[] GetPostTrainingOutcomeList()
        {
            ListItem[] lis = null;
            lis = new ListItem[3];
            lis[0] = new ListItem("掌握", "0");
            lis[1] = new ListItem("熟悉", "1");
            lis[2] = new ListItem("了解", "2");
            return lis;
        }
    }
}