using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Sugar.Enties;
using System;
using System.Collections.Generic;
using WebAPIBase.Utils;

namespace WebAPIBase.API.Controllers
{
    [Produces("application/json;charset=UTF-8")]
    [Route("api/Progress")]
    [ServiceFilter(typeof(Filter.TokenAuthorize))]
    public class ProgressController : BaseController
    {
        /// <summary>
        /// 获取进度填报列表
        /// </summary>
        /// <param name="projectCode">项目编号</param>
        /// <param name="userCode">用户编号</param>
        /// <param name="searchValue">搜索项</param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetPmProgressDTOs")]
        public ActionResult GetPmProgressDTOs(string projectCode, string userCode, string searchValue = "")
        {
            logger.Info($"【GetPmProgressDTOs】projectCode:{projectCode},userCode:{userCode}");
            var manager = new pmProgressManager();
            string expressIds = "";
            var list = manager.GetPmProgresses(m => (m.ProjectCode == projectCode || m.ProjectCode == "0") && (m.IsMaster == 1 || m.IsMaster == 2) && m.State == 2);
            if (list.Count > 0)
            {
                foreach (var item in list)
                {
                    expressIds += item.Id.ToString() + ",";
                }
            }
            else
            {
                expressIds = "0";
            }
            if (expressIds.EndsWith(","))
            {
                expressIds = expressIds.Substring(0, expressIds.Length - 1);
            }
            logger.Info($"【GetPmProgressDTOs】expressIds:{expressIds}");
            var list1 = manager.GetPmProgressDTOs(expressIds, searchValue);
            var list2 = new List<pmProgressDTO>();
            foreach (var item in list1)
            {
                var list3 = manager.GetAllParentProgressNodeId(item.ProgressNodeId);
                if (list3.Count > 0)
                {
                    logger.Info($"【GetPmProgressDTOs】list3:{JsonConvert.SerializeObject(list3)}");
                    var usercodeList = manager.GetNodeRightUserCodeList(list3);
                    if (usercodeList.Contains(userCode))
                    {
                        list2.Add(item);
                    }
                }

            }
            logger.Info($"【GetPmProgressDTOs】list2 count:{list2.Count}");
            return Json(list2);
        }
        /// <summary>
        /// 获取进度主项步骤，先按名称排序，再按实际执行时间倒序，并获取查看权限
        /// </summary>
        /// <param name="projectCode"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetPmProgressItemAndReports")]
        public ActionResult GetPmProgressItemAndReports(string projectCode, string userCode)
        {
            var manager = new pmProgressManager();

            var list = manager.GetPmProgresses(m => (m.ProjectCode == projectCode || m.ProjectCode == "0") && (m.IsMaster == 1 || m.IsMaster == 2) && (m.State == 2 || m.State == 1 || m.State == 0));
            //初次获取的进度计划列表
            logger.Info($"【GetPmProgressItemAndReports】list:{JsonConvert.SerializeObject(list)}");
            List<pmProgress> list2 = new List<pmProgress>();
            foreach (var item in list)
            {

                if (item.PBSUnitCode == projectCode) //项目编号与PBSUnitCode相同，则加入
                {
                    list2.Add(item);
                }
                else    //编制对象编号为空
                {
                    if (item.MasterProgressId.IsNotNullAndEmpty())     //主项计划编号不为空
                    {
                        var entity = list.Find(m => m.Id == item.MasterProgressId);   //找寻其父级项，查看器父级项编制对象编号是否与项目编号一致
                        if (entity != null)
                        {
                            if (entity.PBSUnitCode == projectCode)   //父级编制对象编号与项目编号一致
                            {
                                list2.Add(item);
                            }
                        }
                    }
                }

            }

            var listProgress = new List<ProgressDTO>();

            foreach (var item in list2)
            {
                //获取查看人权限用户列表
                var usercodeList = manager.GetProgressRightUserCodeList(item.Id);
                if (usercodeList.Contains(userCode))
                {
                    var progressDto = new ProgressDTO();
                    progressDto.IsCollapse = false;
                    progressDto.ProgressId = item.Id;
                    progressDto.ProgressName = item.ProgressName;
                    var list1 = manager.GetPmProgressDTOs(item.Id.ToString());
                    progressDto.ProgressList = list1;
                    //var list2 = new List<pmProgressDTO>();
                    //foreach (var item1 in list1)
                    //{

                    //     list2.Add(item1);

                    //}
                    //progressDto.ProgressList = list2;
                    listProgress.Add(progressDto);
                }




            }

            return Json(listProgress);
        }

        /// <summary>
        /// 插入进度报告
        /// </summary>
        /// <param name="report"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("InsertProgressReport")]
        public ActionResult InsertProgressReport([FromBody] pmProgressReport report)
        {
            logger.Info($"【InsertProgressReport】report:{JsonConvert.SerializeObject(report)}");
            report.InputDate = DateTime.Now;
            var manager = new pmProgressManager();
            var id = manager.InsertProgressReport(report);
            return Json(id);
        }
        /// <summary>
        /// 进度报告列表
        /// </summary>
        /// <param name="progressNodeId"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetpmProgressReportDTOs")]
        public ActionResult GetpmProgressReportDTOs(int progressNodeId)
        {
            var manager = new pmProgressManager();
            var list = manager.GetpmProgressReportDTOs(progressNodeId);
            return Json(list);
        }
        /// <summary>
        /// 根据进度报告id获取进度报告实例
        /// </summary>
        /// <param name="progressReportId"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetpmProgressReportDTO")]
        public ActionResult GetpmProgressReportDTO(int progressReportId)
        {
            //流程名
            var procedureName = "进度报告审核";
            var manager = new pmProgressManager();
            var entity = manager.GetpmProgressReportDTO(progressReportId);
            var data = new
            {
                entity,
                procedureName
            };
            return Json(data);
        }
    }
}
