using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using NLog;
using Sugar.Enties;
using System.Collections.Generic;
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();

        #region 材料验收单
        /// <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="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);
        }
        #endregion

        #region 入库单
        /// <summary>
        /// 入库单列表页
        /// </summary>
        /// <param name="ProjectCode"></param>
        /// <param name="permission"></param>
        /// <param name="search"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetInStores")]
        public ActionResult GetInStores(string ProjectCode, string userCode, string search)
        {
            logger.Info($"【GetInStores】projectcode:{ProjectCode},userCode:{userCode},search:{search}");
            FdcUser fdcUser = new FdcUser(userCode);
            List<FdcGroupUnitRight> groupUnitRight = fdcUser.GetGroupUnitRight("150301");      //获取组查看权限
            logger.Info($"【GetInStores】groupUnitRight:{JsonConvert.SerializeObject(groupUnitRight)}");
            var manager = new MaterialInManager();
            var list = manager.GetInStores(ProjectCode, search, search);
            return Json(list);
        }
        /// <summary>
        /// 根据入库单主键获取入库单实体
        /// </summary>
        /// <param name="materialInCode"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetInStore")]
        public ActionResult GetInStore(string materialInCode)
        {
            var manager = new MaterialInManager();
            var entity = manager.GetInStore(materialInCode);
            entity.SupplierName = manager.GetSupplierNameAndType(entity.SupplierCode, entity.SupplierTypeCode);
            entity.ThreeSupplierName = manager.GetSupplierNameAndType(entity.ThreeSupplierCode, entity.ThreeSupplierName);
            logger.Info($"【GetInStore】entity:{JsonConvert.SerializeObject(entity)}");
            var inList = manager.GetV_MaterialInDtlList(materialInCode);
            var attachments = new RequisitionManager().GetAttachMents(materialInCode, "MaterialIn");
            var data = new
            {
                entity = entity,
                inList = inList,
                attachments = attachments
            };
            return Json(data);
        }
        /// <summary>
        /// 插入材料验收单主表和子表
        /// </summary>
        /// <param name="materialIn">入库单主表实体</param>
        /// <param name="mDetailList">入库单子表实体集合</param>
        /// <returns></returns>
        [HttpPost]
        [Route("InsertInStore")]
        public ActionResult InsertInStore([FromBody] MaterialInUnion materialInUnion)
        {
            logger.Info($"【InsertInStore】materialInUnion:{JsonConvert.SerializeObject(materialInUnion)}");
            //return Json(false);
            var manager = new MaterialInManager();
            var success = manager.InsertInStore(materialInUnion.materialIn, materialInUnion.mDetailList);
            return Json(success);
        }
        /// <summary>
        /// 更新材料验收单主表和子表
        /// </summary>
        /// <param name="materialIn">入库单主表实体</param>
        /// <param name="mDetailList">入库单子表实体集合</param>
        /// <returns></returns>
        [HttpPost]
        [Route("UpdateInStore")]
        public ActionResult UpdateInStore([FromBody] MaterialInUnion materialInUnion)
        {
            logger.Info($"【UpdateInStore】materialInUnion:{JsonConvert.SerializeObject(materialInUnion)}");
            //return Json(false);
            var manager = new MaterialInManager();
            var success = manager.UpdateInStore(materialInUnion.materialIn, materialInUnion.mDetailList);
            return Json(success);
        }
        #endregion

        /// <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="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="materialOutCode"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetMaterialOutByMaterialOutCode")]
        public ActionResult GetMaterialOutByMaterialOutCode(string materialOutCode)
        {
            var data = new
            {
                materialOut = new MaterialOut(),
                subList = new List<MaterialOutDtlDTO>()
                //attachments=new List<AttachMent>()

            };
            if (!string.IsNullOrEmpty(materialOutCode))
            {
                var manager = new MaterialInManager();
                var materialOut = manager.MaterialOutDb.GetById(materialOutCode);
                var subList = manager.GetMaterialOutDtlDTOs(materialOutCode);
                //var attachments = new RequisitionManager().GetAttachMents(materialOutCode, "MaterialOut");
                data = new
                {
                    materialOut = materialOut,
                    subList = subList
                    //attachments= attachments
                };
            }

            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);
        }

        #region 领料单
        /// <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>();
            var attachments = new List<AttachMent>();
            if (string.IsNullOrEmpty(materialOutCode))
            {
                var data1 = new
                {
                    mainEntity = mainEntity,
                    subList = subList,
                    attachments = attachments
                };
                return Json(data1);
            }
            var manager = new MaterialOutManager();
            mainEntity = manager.GetMaterialOut(materialOutCode);
            subList = new MaterialInManager().GetV_MaterialOutDtlList(m => m.MaterialOutCode == materialOutCode);
            attachments = new RequisitionManager().GetAttachMents(materialOutCode, "MaterialOut");
            var data = new
            {
                mainEntity = mainEntity,
                subList = subList,
                attachments = attachments
            };
            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)
        {
            logger.Info($"【GetMaterialsForOut】projectcode:{projectcode},contractcode:{contractcode}");
            var manager = new MaterialInManager();
            var list1 = manager.GetMaterialBudget(contractcode);
            if (list1 == null || list1?.Count <= 0)
            {
                return Json("没有可供选择的材料计划单");
            }
            var list = manager.GetMaterialsOfContract(projectcode, contractcode, list1);
            return Json(list);
        }
        /// <summary>
        /// 领料单页面获取领料明细
        /// </summary>
        /// <param name="projectcode"></param>
        /// <param name="contractcode"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetMaterialOutDtlOfContract")]
        public ActionResult GetMaterialOutDtlOfContract(string projectcode, string contractcode, string searchVal)
        {
            logger.Info($"【GetMaterialOutDtlOfContract】projectcode:{projectcode},contractcode:{contractcode}");
            var manager = new MaterialInManager();
            var list1 = manager.GetMaterialBudget(contractcode);
            if (list1 == null || list1?.Count <= 0)
            {
                return Json("没有可供选择的材料计划单");
            }
            var list = manager.GetMaterialOutDtlOfContract(projectcode, contractcode, list1, searchVal);
            return Json(list);
        }
        /// <summary>
        /// 插入领料单单主表和子表
        /// </summary>
        /// <param name="materialIn">入库单主表实体</param>
        /// <param name="mDetailList">入库单子表实体集合</param>
        /// <returns></returns>
        [HttpPost]
        [Route("InsertMaterialOut")]
        public ActionResult InsertMaterialOut([FromBody] MaterialInUnion materialInUnion)
        {
            logger.Info($"【InsertMaterialOut】materialInUnion:{JsonConvert.SerializeObject(materialInUnion)}");
            //return Json(false);
            var manager = new MaterialInManager();
            var success = manager.InsertMaterialOut(materialInUnion.materialOut, materialInUnion.mOutDetailList);
            return Json(success);
        }
        /// <summary>
        /// 更新领料单主表和子表
        /// </summary>
        /// <param name="materialIn">入库单主表实体</param>
        /// <param name="mDetailList">入库单子表实体集合</param>
        /// <returns></returns>
        [HttpPost]
        [Route("UpdateMaterialOut")]
        public ActionResult UpdateMaterialOut([FromBody] MaterialInUnion materialInUnion)
        {
            logger.Info($"【UpdateMaterialIn】materialInUnion:{JsonConvert.SerializeObject(materialInUnion)}");
            //return Json(false);
            var manager = new MaterialInManager();
            var success = manager.UpdateMaterialOut(materialInUnion.materialOut, materialInUnion.mOutDetailList);
            return Json(success);
        }
        #endregion

    }

    public class MaterialInUnion
    {
        /// <summary>
        /// 入库单主表
        /// </summary>
        public MaterialIn materialIn { get; set; }

        /// <summary>
        /// 入库单材料明细
        /// </summary>
        public List<MaterialInDtl> mDetailList { get; set; }

        /// <summary>
        /// 领料单主表
        /// </summary>
        public MaterialOut materialOut { get; set; }
        /// <summary>
        /// 领料单明细
        /// </summary>
        public List<MaterialOutDtlDTO> mOutDetailList { get; set; }
    }
}
