using Learun.Application.Organization;
using Learun.Application.TwoDevelopment.ZZDT_EC;
using Learun.Util;
using Learun.Util.SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using static Learun.Application.TwoDevelopment.ZZDT_EC.IO_WorkFlowService;

namespace Learun.Application.Web.Areas.ZZDT_EC.Controllers
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创 建：超级管理员
    /// 日 期：2023-02-23 11:30
    /// 描 述：信号通知表
    /// </summary>
    public class ec_WireGroupNoticeController : MvcControllerBase
    {
        #region 视图功能
        /// <summary>
        /// 主页面
        /// <summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }
        #endregion

        #region 获取数据
        /// <summary>
        /// 获取列表数据
        /// <summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetList(string queryJson)
        {
            var queryParam = queryJson.ToJObject();
            string projId = queryParam["ProjectId"]?.ToString();

            var NoticeTableName = ProjectSugar.TableName<ec_wire_group_noticeEntity>(projId);
            var SignalTableName = ProjectSugar.TableName<ec_Wire_GroupEntity>(projId);

            //SugarRedis 需要 NET Standard 2.1，不支持Framework
            var users = SqlSugarHelper.Db.Queryable<UserEntity>().ToList();
            #region 只需要对方的

            var _User = users.Single(x => x.F_UserId == LoginUserInfo.Get().userId);

            var _Company = SqlSugarHelper.Db.Queryable<CompanyEntity>().First(x => x.F_CompanyId == _User.F_CompanyId);
            var DepartmentStr = "";
            Department _Department;
            if (_Company != null)
            {
                DepartmentStr = _Company.F_FullName;
                if (DepartmentStr.Contains("轮机"))
                {
                    _Department = Department.轮机;
                }
                else
                {
                    _Department = Department.电气;
                }
            }
            else
            {
                _Department = Department.电气;
            }
            var res = SqlSugarHelper.Db.Queryable<ec_wire_group_noticeEntity>().AS(NoticeTableName).
                Where(x => !string.IsNullOrEmpty(x.WireGroupID) && !x.CheckFLG).
                OrderByDescending(x => x.CreateTime).
                Where(x => x.CompanyID == _Department).ToList();
            //Where(x => x.CompanyID == _Department).ToList();//收到本专业的通知即可
            #endregion
            foreach (ec_wire_group_noticeEntity noticeEntity in res)
            {
                var signal = SqlSugarHelper.Db.Queryable<ec_Wire_GroupEntity>().AS(SignalTableName).First(x => x.Wire_Group_ID == noticeEntity.WireGroupID);
                var user_Send = users.FirstOrDefault(x => x.F_UserId == noticeEntity?.CreateUserID);
                var user_Confrim = users.FirstOrDefault(x => x.F_UserId == noticeEntity?.UpdateUserID);
                if (signal != null)
                {
                    noticeEntity.Group_Name = signal.Group_Name;
                    noticeEntity.Group_Desc = signal.Group_Desc;
                    noticeEntity.Group_Desc_EN = signal.Group_Desc_EN;
                    noticeEntity.IO_Type = signal.IO_Type;
                    noticeEntity.CreateUserName = user_Send?.F_RealName;
                    noticeEntity.UpdateUserName = user_Confrim?.F_RealName;
                    noticeEntity.Action = noticeEntity.ActionID.ToString();
                }
                else
                {
                    //信号都没了
                    noticeEntity.WireGroupID = "-1";
                }
            }
            return Success(res.Where(x => !string.IsNullOrEmpty(x.Group_Name) || x.WireGroupID != "-1").ToList());
        }
        #endregion
        #region 提交数据
        /// <summary>
        /// 已读
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult Read(string ProjectId, string keyValue)
        {
            var bll = new ec_Wire_GroupBLL();
            List<string> NoticeIds = new List<string>();
            NoticeIds.Add(keyValue);
            var res = bll.ReadNotification(ProjectId, NoticeIds);
            return Success("OK");
        }
        /// <summary>
        /// 保存实体数据（新增、修改）
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult Reads(string ProjectId, List<string> NoticeIds)
        {
            var bll = new ec_Wire_GroupBLL();
            var res = bll.ReadNotification(ProjectId, NoticeIds);
            return Success("全部已阅！");
        }
        #endregion
    }
}