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

namespace WebAPIBase.API.Controllers
{
    [Produces("application/json;charset=UTF-8")]
    [Route("api/DesignChanges")]
    [ServiceFilter(typeof(Filter.TokenAuthorize))]
    public class DesignChangesController : BaseController
    {
        /// <summary>
        /// 根据用户编号和用户代码获取待审变更列表
        /// </summary>
        /// <param name="projectCode"></param>
        /// <param name="userCode"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetDesignChangesList")]
        public ActionResult GetDesignChangesList(string projectCode, string userCode)
        {
            var manager = new DesignChangesManager();

            var list = manager.GetDesignChangesList(projectCode, userCode);
            logger.Info($"【GetDesignChangesList】{JsonConvert.SerializeObject(list)}");
            return Json(list);
        }
        /// <summary>
        /// 获取待审变更详情
        /// </summary>
        /// <param name="projectCode"></param>
        /// <param name="searchValue"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetDesignChanges")]
        public ActionResult GetDesignChanges(string designChangesCode)
        {
            //流程名
            var procedureName = string.Format("设计变更");
            var manager = new DesignChangesManager();
            var entity = manager.GetDesignChanges(designChangesCode);
            var contract = new ContractManager().GetById(entity.ContractCode);
            if (contract != null)
            {
                entity.SupplierName = new MaterialInManager().GetSupplierNameAndType(contract.SupplierCode, contract.SupplierTypeCode);
            }
            entity.PersonName = new SystemUserManager().GetById(entity.Person)?.UserName;
            entity.RequisitionName = new RequisitionManager().GetById(entity.RequisitionCode)?.RequisitionName;

            var manager1 = new UnitManager();
            var list = new DesignChangesManager().GetDesignChangesCostDTOs(designChangesCode);
            foreach (var item in list)
            {
                item.CostCodeName = manager1.GetCostCodeName(item.CostCode, item.CostBudgetSetCode);
            }
            var attachments = new RequisitionManager().GetAttachMents(designChangesCode, "DesignChanges");
            var data = new
            {
                mainEntity = entity,
                subList = list,
                procedureName,
                attachments = attachments
            };
            logger.Info($"【GetDesignChanges】data:{JsonConvert.SerializeObject(data)}");
            return Json(data);
        }
        /// <summary>
        /// 插入待审变更主表和明细表
        /// </summary>
        /// <param name="designChangesUnion"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("InsertDesignChanges")]
        public ActionResult InsertDesignChanges([FromBody] DesignChangesUnion designChangesUnion)
        {

            logger.Info($"【InsertDesignChanges】DesignChangesUnion:{JsonConvert.SerializeObject(designChangesUnion)}");
            var manager = new DesignChangesManager();

            var designChangesCode = manager.InsertDesignChanges(designChangesUnion.designChanges, designChangesUnion.designChangesCosts);
            return Json(designChangesCode);
        }

        /// <summary>
        /// 更新待变更主表和明细表
        /// </summary>
        /// <param name="designChangesUnion"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("UpdateDesignChanges")]
        public ActionResult UpdateDesignChanges([FromBody] DesignChangesUnion designChangesUnion)
        {
            logger.Info($"【UpdateDesignChanges】DesignChangesUnion:{JsonConvert.SerializeObject(designChangesUnion)}");
            var manager = new DesignChangesManager();
            var res = manager.UpdateDesignChanges(designChangesUnion.designChanges, designChangesUnion.designChangesCosts);
            return Json(res);
        }
        /// <summary>
        /// 待审变更编辑
        /// </summary>
        /// <param name="designChangesCode"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("DesignChangesEdit")]
        public ActionResult DesignChangesEdit(string designChangesCode)
        {
            var manager = new DesignChangesManager();
            var entity = manager.GetDesignChanges(designChangesCode);
            var contract = new ContractManager().GetById(entity.ContractCode);
            if (contract != null)
            {
                entity.SupplierName = new MaterialInManager().GetSupplierNameAndType(contract.SupplierCode, contract.SupplierTypeCode);
            }
            entity.PersonName = new SystemUserManager().GetById(entity.Person)?.UserName;
            entity.RequisitionName = new RequisitionManager().GetById(entity.RequisitionCode)?.RequisitionName;

            var manager1 = new UnitManager();
            var list = new DesignChangesManager().GetDesignChangesCostDTOs(designChangesCode);
            foreach (var item in list)
            {
                item.CostCodeName = manager1.GetCostCodeName(item.CostCode, item.CostBudgetSetCode);
            }
            var attachments = new RequisitionManager().GetAttachMents(designChangesCode, "DesignChanges");
            var data = new
            {
                mainEntity = entity,
                subList = list,
                attachments = attachments
            };
            logger.Info($"【DesignChangesEdit】data:{JsonConvert.SerializeObject(data)}");
            return Json(data);
        }

    }
    public class DesignChangesUnion
    {
        /// <summary>
        /// 待审变更
        /// </summary>
        public DesignChanges designChanges { get; set; }
        /// <summary>
        /// 待审变更费用
        /// </summary>
        public List<DesignChangesCost> designChangesCosts { get; set; }
    }
}
