using NLog;
using Org.BouncyCastle.Bcpg;
using System;
using WebAPIBase.API.Requests;
using WebAPIBase.Service.Interface;
using Newtonsoft.Json;
using System.Net.Http;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Net;
using Sugar.Enties;
using System.Text;
using WebAPIBase.Utils;
using Microsoft.Extensions.Configuration;
using System.IO;
using System.Collections.Specialized;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using WebAPIBase.NetCore.BusinessCore;

namespace WebAPIBase.API.Controllers
{
    [Produces("application/json;charset=UTF-8")]
    [Route("api/MaterialInOut")]
    public class MaterialInOutController : Controller
    {
        /// <summary>
        /// 日志操作
        /// </summary>
        private static Logger logger = NLog.LogManager.GetCurrentClassLogger();

        /// <summary>
        /// 入库单列表页
        /// </summary>
        /// <param name="ProjectCode"></param>
        /// <param name="permission"></param>
        /// <param name="search"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetMaterialIns")]
        public ActionResult GetMaterialIns(string ProjectCode, string userCode, string search)
        {
            logger.Info($"【GetMaterialIns】projectcode:{ProjectCode},userCode:{userCode},search:{search}");
            FdcUser fdcUser = new FdcUser(userCode);
            List<FdcGroupUnitRight> groupUnitRight = fdcUser.GetGroupUnitRight("150301");      //获取组查看权限
            logger.Info($"【GetMaterialIns】groupUnitRight:{JsonConvert.SerializeObject(groupUnitRight)}");
            var manager = new MaterialInManager();
            var list = manager.GetMaterialIns(ProjectCode, search, search);
            return Json(list);
        }

        /// <summary>
        /// 根据入库单主键获取入库单实体
        /// </summary>
        /// <param name="materialInCode"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetMaterialIn")]
        public ActionResult GetMaterialIn(string materialInCode)
        {
            var manager = new MaterialInManager();
            var entity = manager.GetMaterialIn(materialInCode);
            entity.SupplierName = manager.GetSupplierNameAndType(entity.SupplierCode, entity.SupplierTypeCode);
            entity.ThreeSupplierName = manager.GetSupplierNameAndType(entity.ThreeSupplierCode, entity.ThreeSupplierName);
            logger.Info($"【GetMaterialIn】entity:{JsonConvert.SerializeObject(entity)}");
            var inList = manager.GetV_MaterialInDtlList(materialInCode);
            var outList = manager.GetV_MaterialOutDtlList(materialInCode);
            var attachments = new RequisitionManager().GetAttachMents(materialInCode, "MaterialIn");
            var data = new
            {
                entity = entity,
                inList = inList,
                outList = outList,
                attachments = attachments
            };
            return Json(data);
        }

        /// <summary>
        /// 根据项目代码和合同编码获取合同中包含的资料
        /// </summary>
        /// <param name="projectCode">项目编码</param>
        /// <param name="contractCode">合同编码</param>
        /// <param name="searchValue">搜索串</param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetMaterials")]
        public ActionResult GetMaterials(string projectCode, string contractCode, string searchValue)
        {
            var manager = new MaterialInManager();
            var list = manager.GetMaterials(projectCode, contractCode, searchValue);
            return Json(list);
        }
        /// <summary>
        /// 插入入库单主表和子表
        /// </summary>
        /// <param name="materialIn">入库单主表实体</param>
        /// <param name="mDetailList">入库单子表实体集合</param>
        /// <returns></returns>
        [HttpPost]
        [Route("InsertMaterialIn")]
        public ActionResult InsertMaterialIn([FromBody] MaterialInUnion materialInUnion)
        {
            logger.Info($"【InsertMaterialIn】materialInUnion:{JsonConvert.SerializeObject(materialInUnion)}");
            //return Json(false);
            var manager = new MaterialInManager();
            var success = manager.InsertMaterialIn(materialInUnion.materialIn, materialInUnion.mDetailList, materialInUnion.materialOut);
            return Json(success);
        }
        /// <summary>
        /// 更新入库单主表和子表
        /// </summary>
        /// <param name="materialIn">入库单主表实体</param>
        /// <param name="mDetailList">入库单子表实体集合</param>
        /// <returns></returns>
        [HttpPost]
        [Route("UpdateMaterialIn")]
        public ActionResult UpdateMaterialIn([FromBody] MaterialInUnion materialInUnion)
        {
            logger.Info($"【UpdateMaterialIn】materialInUnion:{JsonConvert.SerializeObject(materialInUnion)}");
            //return Json(false);
            var manager = new MaterialInManager();
            var success = manager.UpdateMaterialIn(materialInUnion.materialIn, materialInUnion.mDetailList, materialInUnion.materialOut);
            return Json(success);
        }
        /// <summary>
        /// 获取编辑出入库单的主表和子表数据
        /// </summary>
        /// <param name="materialInCode"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetMaterialInByMaterialInCode")]
        public ActionResult GetMaterialInByMaterialInCode(string materialInCode)
        {
            var data = new
            {
                mainEntity = new MaterialIn(),
                materialOut = new MaterialOut(),
                subList = new List<MaterialDTO>()

            };
            if (!string.IsNullOrEmpty(materialInCode))
            {
                var manager = new MaterialInManager();
                var mainEntity = manager.GetById(materialInCode);
                var materialOut = manager.GetMaterialOutRelation(materialInCode);
                var subList = manager.GetMaterialDTOs(materialInCode);

                data = new
                {
                    mainEntity = mainEntity,
                    materialOut = materialOut,
                    subList = subList

                };
            }

            return Json(data);

        }
        /// <summary>
        /// 获取合同和供应商信息
        /// </summary>
        /// <param name="contractCode"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetContractAndSupplier")]
        public ActionResult GetContractAndSupplier(string contractCode)
        {
            var manager = new MaterialInManager();
            var entity = manager.GetContractAndSupplier(contractCode);
            return Json(entity);
        }
        /// <summary>
        /// 获取入库类型名
        /// </summary>
        /// <param name="contractCode"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetGroupName")]
        public ActionResult GetGroupName(string groupCode)
        {
            var manager = new MaterialInManager();
            var groupname = manager.GetGroupName(groupCode);
            return Json(groupname);
        }

        /// <summary>
        /// 根据项目代码获取领料列表
        /// </summary>
        /// <param name="projectCode"></param>
        /// <param name="searchValue"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetMaterialOuts")]
        public ActionResult GetMaterialOuts(string projectCode, string searchValue = "")
        {
            var list = new List<MaterialOutDTO>();
            if (string.IsNullOrEmpty(projectCode))
            {
                return Json(list);
            }
            var manager = new MaterialOutManager();
            list = manager.GetMaterialOuts(projectCode, searchValue);
            return Json(list);
        }
        /// <summary>
        /// 根据领料单号获取领料单主表和子表数据
        /// </summary>
        /// <param name="materialOutCode"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetMaterialOutDetail")]
        public ActionResult GetMaterialOutDetail(string materialOutCode)
        {
            var mainEntity = new MaterialOutDTO();
            var subList = new List<V_MaterialOutDtl>();
            if (string.IsNullOrEmpty(materialOutCode))
            {
                var data1 = new
                {
                    mainEntity = mainEntity,
                    subList = subList
                };
                return Json(data1);
            }
            var manager = new MaterialOutManager();
            mainEntity = manager.GetMaterialOut(materialOutCode);
            subList = new MaterialInManager().GetV_MaterialOutDtlList(m => m.MaterialOutCode == materialOutCode);
            var data = new
            {
                mainEntity = mainEntity,
                subList = subList
            };
            return Json(data);
        }
        /// <summary>
        /// 获取领料明细
        /// </summary>
        /// <param name="projectcode"></param>
        /// <param name="contractcode"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetMaterialsForOut")]
        public ActionResult GetMaterialsForOut(string projectcode, string contractcode)
        {
            var manager = new MaterialInManager();
            var list1 = manager.GetMaterialBudget(contractcode);
            if (list1?.Count <= 0)
            {
                return Json("没有可供选择的材料计划单");
            }
            var list = manager.GetMaterialsOfContract(projectcode, contractcode, list1);
            return Json(list);
        }

    }

    public class MaterialInUnion
    {
        /// <summary>
        /// 入库单主表
        /// </summary>
        public MaterialIn materialIn { get; set; }
        /// <summary>
        /// 领料单主表
        /// </summary>
        public MaterialOut materialOut { get; set; }
        /// <summary>
        /// 出入库单材料明细
        /// </summary>
        public List<MaterialInDtl> mDetailList { get; set; }
    }
}
