using Edu.Cache.User;
using Edu.Common.API;
using Edu.Common.Enum.Sale;
using Edu.Common.Plugin;
using Edu.Model.ViewModel.StudyAbroad;
using Edu.Module.StudyAbroad;
using Edu.WebApi.Filter;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Edu.WebApi.Controllers.StudyAbroad
{
    /// <summary>
    /// 留学就业控制器相关
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiExceptionFilter]
    [ApiController]
    [EnableCors("AllowCors")]
    public class StudyAbroadController : BaseController
    {
        /// <summary>
        /// 留学就业处理对象
        /// </summary>
        private readonly StudyAbroadModule studyAbroadModule = AOP.AOPHelper.CreateAOPObject<StudyAbroadModule>();

        /// <summary>
        /// 供应商处理对象
        /// </summary>
        private readonly SupplierModule supplierModule = new SupplierModule();

        #region 供应商管理
        /// <summary>
        /// 获取供应商分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetSupplierPage()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = new RB_Supplier_ViewModel()
            {
                Name = base.ParmJObj.GetStringValue("Name"),
                Type=(SupplierTypeEnum)base.ParmJObj.GetInt("Type"),
            };
            query.Group_Id = base.UserInfo.Group_Id;
            List<object> result = new List<object>();
            var list = supplierModule.GetSupplierPageModule(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            foreach (var item in list)
            {
                if (item.CreateBy > 0)
                {
                    item.CreateByName = UserReidsCache.GetUserLoginInfo(item.CreateBy)?.AccountName ?? "";
                }
                if (item.UpdateBy > 0)
                {
                    item.UpdateByName = UserReidsCache.GetUserLoginInfo(item.UpdateBy)?.AccountName ?? "";
                }
                result.Add(new
                {
                    item.Id,
                    item.Type,
                    TypeName=Common.Plugin.EnumHelper.ToName(item.Type),
                    item.Name,
                    item.LinkMan,
                    item.LinkTel,
                    item.LinkAddress,
                    item.Remark,
                    item.CreateByName,
                    item.CommissionType,
                    item.CommissionMoney,
                    item.ContractUrl,
                    CreateTimeStr = Common.ConvertHelper.FormatTime(item.CreateTime),
                    item.UpdateByName,
                    UpdateTimeStr = Common.ConvertHelper.FormatTime(item.UpdateTime),
                });
            }
            pageModel.Count = rowsCount;
            pageModel.PageData = result;
            return ApiResult.Success(data: pageModel);
        }

        /// <summary>
        /// 获取供应商列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetSupplierList()
        {
            //是够添加默选项
            int IsAddDefault = base.ParmJObj.GetInt("IsAddDefault");
            var query = new RB_Supplier_ViewModel()
            {
                Name = base.ParmJObj.GetStringValue("Name"),
                Type = (SupplierTypeEnum)base.ParmJObj.GetInt("Type"),
            };
            query.Group_Id = base.UserInfo.Group_Id;
            query.School_Id = base.UserInfo.School_Id;
            var list = supplierModule.GetSupplierListModule(query);
            if (list == null)
            {
                list = new List<RB_Supplier_ViewModel>();
            }
            if (IsAddDefault == 1)
            {
                list.Insert(0, new RB_Supplier_ViewModel()
                {
                    Id = 0,
                    Name = "请选择"
                });
            }
            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 新增修改供应商
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetSupplier()
        {
            var extModel = new RB_Supplier_ViewModel()
            {
                Id = base.ParmJObj.GetInt("Id"),
                Type =(SupplierTypeEnum)base.ParmJObj.GetInt("Type"),
                Name = base.ParmJObj.GetStringValue("Name"),
                LinkMan = base.ParmJObj.GetStringValue("LinkMan"),
                LinkTel = base.ParmJObj.GetStringValue("LinkTel"),
                LinkAddress = base.ParmJObj.GetStringValue("LinkAddress"),
                Remark = base.ParmJObj.GetStringValue("Remark"),
                CreateBy = UserInfo.Id,
                CreateTime = DateTime.Now,
                UpdateBy = UserInfo.Id,
                UpdateTime = DateTime.Now,
                Group_Id = this.UserInfo.Group_Id,
                School_Id = base.ParmJObj.GetInt("School_Id"),
                CommissionMoney=base.ParmJObj.GetDecimal("CommissionMoney"),
                CommissionType=base.ParmJObj.GetInt("CommissionType"),
                ContractUrl=base.ParmJObj.GetStringValue("ContractUrl")
            };
            bool flag = supplierModule.SetSupplierModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 获取供应商信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetSupplier()
        {
            var Id = base.ParmJObj.GetInt("Id", 0);
            var extModel = supplierModule.GetSupplierModule(Id);
            var obj = new
            {
                Id = extModel?.Id ?? 0,
                Type = extModel?.Type ?? 0,
                Name = extModel?.Name ?? "",
                LinkMan = extModel?.LinkMan ?? "",
                LinkTel = extModel?.LinkTel ?? "",
                LinkAddress = extModel?.LinkAddress ?? "",
                Remark = extModel?.Remark ?? "",
                CommissionType = extModel?.CommissionType ?? 0,
                CommissionMoney = extModel?.CommissionMoney ?? 0,
                ContractUrl=extModel?.ContractUrl??""
            };
            return ApiResult.Success(data: obj);
        }

        /// <summary>
        /// 更新供应商状态
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult RemoveSupplier()
        {
            var Id = base.ParmJObj.GetInt("Id", 0);
            var Status = base.ParmJObj.GetInt("Status", 0);
            var flag = supplierModule.RemoveSupplierModule(Id, Status);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 获取供应商类型列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetSupplierType()
        {
            var list = Common.Plugin.EnumHelper.EnumToList(typeof(SupplierTypeEnum));
            return ApiResult.Success(data: list);
        }
        #endregion

        #region 留学就业管理

        /// <summary>
        /// 获取留学就业分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetStudyAbroadPage()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = new RB_StudyAbroad_ViewModel()
            {
                Name = base.ParmJObj.GetStringValue("Name"),
                Type = base.ParmJObj.GetInt("Type"),
                SupplierId = base.ParmJObj.GetInt("SupplierId"),
                IsQPrice = base.ParmJObj.GetInt("IsQPrice"),
                AuditType=base.ParmJObj.GetInt("AuditType"),
                SaleState=(SaleStateEnum)base.ParmJObj.GetInt("SaleState"),
                AuditState=base.ParmJObj.GetInt("AuditState"),
            };
            query.Group_Id = base.UserInfo.Group_Id;
            List<object> result = new List<object>();
            var list = studyAbroadModule.GetStudyAbroadPageModule(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            foreach (var item in list)
            {
                if (item.CreateBy > 0)
                {
                    item.CreateByName = UserReidsCache.GetUserLoginInfo(item.CreateBy)?.AccountName ?? "";
                }
                if (item.UpdateBy > 0)
                {
                    item.UpdateByName = UserReidsCache.GetUserLoginInfo(item.UpdateBy)?.AccountName ?? "";
                }
                if (item.DirectorId > 0)
                {
                    item.DirectorName = UserReidsCache.GetUserLoginInfo(item.DirectorId)?.AccountName ?? "";
                }
                if (item.ManagerId > 0)
                {
                    item.ManagerName = UserReidsCache.GetUserLoginInfo(item.ManagerId)?.AccountName ?? "";
                }
                result.Add(new
                {
                    item.Id,
                    item.Type,
                    item.Name,
                    item.SupplierId,
                    item.SupplierName,
                    item.School_Id,
                    item.SchoolName,
                    SupplierContract=item.ContractUrl,
                    item.SuggestPrice,
                    item.SellPrice,
                    item.Remark,
                    item.SaleState,
                    SaleStateName=Common.Plugin.EnumHelper.ToName(item.SaleState),
                    item.CreateByName,
                    CreateTimeStr = Common.ConvertHelper.FormatTime(item.CreateTime),
                    item.UpdateByName,
                    UpdateTimeStr = Common.ConvertHelper.FormatTime(item.UpdateTime),
                    item.PreferentialList,
                    item.DirectorName,
                    item.DirectorId,
                    item.DirectorStatus,
                    item.DirectorOpinion,
                    DirectorAuditTime = Common.ConvertHelper.FormatTime(item.DirectorAuditTime),
                    item.ManagerName,
                    item.ManagerId,
                    item.ManagerStatus,
                    item.ManagerOpinion,
                    ManagerAuditTime = Common.ConvertHelper.FormatTime(item.ManagerAuditTime),
                    item.ImgCover,
                    item.Status,
                });
            }
            pageModel.Count = rowsCount;
            pageModel.PageData = result;
            return ApiResult.Success(data: pageModel);
        }


        /// <summary>
        /// 获取留学就业审核分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetStudyAbroadAuditPage()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = new RB_StudyAbroad_ViewModel()
            {
                Name = base.ParmJObj.GetStringValue("Name"),
                Type = base.ParmJObj.GetInt("Type"),
                SupplierId = base.ParmJObj.GetInt("SupplierId"),
                IsQPrice = base.ParmJObj.GetInt("IsQPrice"),
                AuditType = base.ParmJObj.GetInt("AuditType"),
                AuditState = base.ParmJObj.GetInt("AuditState"),
            };
            query.AuditManId = base.UserInfo.Id;
            query.Group_Id = base.UserInfo.Group_Id;
            List<object> result = new List<object>();
            var list = studyAbroadModule.GetStudyAbroadAuditPageModule(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            foreach (var item in list)
            {
                if (item.CreateBy > 0)
                {
                    item.CreateByName = UserReidsCache.GetUserLoginInfo(item.CreateBy)?.AccountName ?? "";
                }
                if (item.UpdateBy > 0)
                {
                    item.UpdateByName = UserReidsCache.GetUserLoginInfo(item.UpdateBy)?.AccountName ?? "";
                }
                if (item.DirectorId > 0)
                {
                    item.DirectorName = UserReidsCache.GetUserLoginInfo(item.DirectorId)?.AccountName ?? "";
                }
                if (item.ManagerId > 0)
                {
                    item.ManagerName = UserReidsCache.GetUserLoginInfo(item.ManagerId)?.AccountName ?? "";
                }
                result.Add(new
                {
                    item.Id,
                    item.Type,
                    item.Name,
                    item.SupplierId,
                    item.SupplierName,
                    SupplierContract = item.ContractUrl,
                    item.SuggestPrice,
                    item.SellPrice,
                    item.Remark,
                    item.SaleState,
                    SaleStateName = Common.Plugin.EnumHelper.ToName(item.SaleState),
                    item.CreateByName,
                    CreateTimeStr = Common.ConvertHelper.FormatTime(item.CreateTime),
                    item.UpdateByName,
                    UpdateTimeStr = Common.ConvertHelper.FormatTime(item.UpdateTime),
                    item.PreferentialList,
                    item.DirectorName,
                    item.DirectorId,
                    item.DirectorStatus,
                    item.DirectorOpinion,
                    DirectorAuditTime = Common.ConvertHelper.FormatTime(item.DirectorAuditTime),
                    item.ManagerName,
                    item.ManagerId,
                    item.ManagerStatus,
                    item.ManagerOpinion,
                    ManagerAuditTime = Common.ConvertHelper.FormatTime(item.ManagerAuditTime),
                    item.ImgCover,
                });
            }
            pageModel.Count = rowsCount;
            pageModel.PageData = result;
            return ApiResult.Success(data: pageModel);
        }

        /// <summary>
        /// 获取留学就业列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetStudyAbroadList()
        {
            //是够添加默选项
            int IsAddDefault = base.ParmJObj.GetInt("IsAddDefault");
            var query = new RB_StudyAbroad_ViewModel()
            {
                Name = base.ParmJObj.GetStringValue("Name"),
                Type = base.ParmJObj.GetInt("Type"),
                IsQPrice = base.ParmJObj.GetInt("IsQPrice"),
                SaleState=(SaleStateEnum)base.ParmJObj.GetInt("SaleState"),
            };
            query.Group_Id = base.UserInfo.Group_Id;
            query.School_Id = base.UserInfo.School_Id;
            var list = studyAbroadModule.GetStudyAbroadListModule(query);
            if (list == null)
            {
                list = new List<RB_StudyAbroad_ViewModel>();
            }
            if (IsAddDefault == 1)
            {
                list.Insert(0, new RB_StudyAbroad_ViewModel()
                {
                    Id = 0,
                    Name = "请选择"
                });
            }
            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 新增修改留学就业产品
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetStudyAbroad()
        {
            var extModel = new RB_StudyAbroad_ViewModel()
            {
                Id = base.ParmJObj.GetInt("Id"),
                Type = base.ParmJObj.GetInt("Type"),
                Name = base.ParmJObj.GetStringValue("Name"),
                SupplierId = base.ParmJObj.GetInt("SupplierId"),
                SupplierContract = base.ParmJObj.GetStringValue("SupplierContract"),
                Remark = base.ParmJObj.GetStringValue("Remark"),
                CreateBy = UserInfo.Id,
                CreateTime = DateTime.Now,
                UpdateBy = UserInfo.Id,
                UpdateTime = DateTime.Now,
                Group_Id = this.UserInfo.Group_Id,
                School_Id = base.ParmJObj.GetInt("School_Id"),
                ImgCover=base.ParmJObj.GetStringValue("ImgCover"),
            };
            extModel.SaleState =Common.Enum.Sale.SaleStateEnum.NoPerfect;
            bool flag = studyAbroadModule.SetStudyAbroadModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 批量设置留学就业价格和优惠
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult BatchStudyAbroad()
        {
            var studyAbroadObj = JObject.Parse(base.ParmJObj.GetStringValue("studyAbroadObj"));
            var studyModel = new RB_StudyAbroad_ViewModel()
            {
                Id = studyAbroadObj.GetInt("Id"),
                SellPrice = studyAbroadObj.GetDecimal("SellPrice"),
                SuggestPrice = studyAbroadObj.GetDecimal("SuggestPrice"),
            };
            var priceObj = base.ParmJObj.GetStringValue("priceList");
            var list = new List<RB_StudyaBroad_Preferential_ViewModel>();
            try
            {
                list = Common.Plugin.JsonHelper.DeserializeObject<List<RB_StudyaBroad_Preferential_ViewModel>>(priceObj);
                if (list != null && list.Count > 0)
                {
                    foreach (var extModel in list)
                    {
                        extModel.CreateTime = DateTime.Now;
                        extModel.CreateBy = base.UserInfo.Id;
                        extModel.UpdateBy = base.UserInfo.Id;
                        extModel.UpdateTime = DateTime.Now;
                        extModel.Group_Id = this.UserInfo.Group_Id;
                        extModel.School_Id = this.UserInfo.School_Id;
                        extModel.StudyabroadId = studyModel.Id;
                    }
                }
            }
            catch (Exception ex)
            {
                Common.Plugin.LogHelper.Write(ex, "BatchStudyAbroad");
            }
            bool flag = studyAbroadModule.SetStudyAbroadPreferentialListModule(studyModel, list);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 根据编号获取留学就业产品信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetStudyAbroad()
        {
            var Id = base.ParmJObj.GetInt("Id", 0);
            int IsGetPrice = base.ParmJObj.GetInt("IsGetPrice");
            var extModel = studyAbroadModule.GetStudyAbroadModule(Id, IsGetPrice: IsGetPrice);
            if (extModel == null)
            {
                extModel = new RB_StudyAbroad_ViewModel();
            }
            var obj = new
            {
                extModel.Id,
                extModel.Type,
                extModel.Name,
                extModel.SupplierId,
                extModel.SupplierName,
                extModel.School_Id,
                SupplierContract= extModel.ContractUrl,
                extModel.SuggestPrice,
                extModel.SellPrice,
                extModel.Remark,
                extModel.SaleState,
                SaleStateName = Common.Plugin.EnumHelper.ToName(extModel.SaleState),
                extModel.CreateBy,
                CreateByName= UserReidsCache.GetUserLoginInfo(extModel.CreateBy)?.AccountName ?? "",
                CreateTimeStr=Common.ConvertHelper.FormatTime(extModel.CreateTime),
                extModel.DirectorId,
                extModel.DirectorStatus,
                extModel.DirectorOpinion,
                DirectorAuditTimeStr= Common.ConvertHelper.FormatTime(extModel.DirectorAuditTime),
                DirectorName= UserReidsCache.GetUserLoginInfo(extModel.DirectorId)?.AccountName ?? "",
                extModel.ManagerId,
                extModel.ManagerStatus,
                extModel.ManagerOpinion,
                ManagerAuditTimeStr= Common.ConvertHelper.FormatTime(extModel.ManagerAuditTime),
                ManagerName = UserReidsCache.GetUserLoginInfo(extModel.ManagerId)?.AccountName ?? "",
                extModel.PreferentialList,
                extModel.ImgCover,
            };
            return ApiResult.Success(data: obj);
        }

        /// <summary>
        /// 更新留学就业产品状态
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult RemoveStudyAbroad()
        {
            var Id = base.ParmJObj.GetInt("Id", 0);
            var Status = base.ParmJObj.GetInt("Status", 0);
            var flag = studyAbroadModule.RemoveStudyAbroadModule(Id, Status,out string message);
            return flag ? ApiResult.Success(message: message) : ApiResult.Failed(message: message);
        }

        /// <summary>
        /// OP提交审核
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetSaleState()
        {
            var Id = base.ParmJObj.GetInt("Id", 0);
            var SaleState = base.ParmJObj.GetInt("SaleState", 0);
            var flag = studyAbroadModule.SetSaleStateModule(Id, SaleState);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 部门主管审核
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetDirectorAudit()
        {
            var extModel = new RB_StudyAbroad_ViewModel()
            {
                Id = base.ParmJObj.GetInt("Id"),
                DirectorOpinion = base.ParmJObj.GetStringValue("DirectorOpinion"),
                DirectorAuditTime = DateTime.Now,
                DirectorStatus = base.ParmJObj.GetInt("DirectorStatus"),
            };
            var flag = studyAbroadModule.SetDirectorAuditModule(extModel, base.UserInfo);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 部门负责人审核
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetManagerAudit()
        {
            var extModel = new RB_StudyAbroad_ViewModel()
            {
                Id = base.ParmJObj.GetInt("Id"),
                ManagerOpinion = base.ParmJObj.GetStringValue("ManagerOpinion"),
                DirectorAuditTime = DateTime.Now,
                ManagerStatus = base.ParmJObj.GetInt("ManagerStatus"),
            };
            var flag = studyAbroadModule.SetManagerAuditModule(extModel, base.UserInfo);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 获取留学就业销售状态列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetSaleStateList()
        {
            var list = Common.Plugin.EnumHelper.EnumToList(typeof(SaleStateEnum));
            return ApiResult.Success(data: list);
        }
        #endregion
    }
}
