using APT.Infrastructure.Core;
using APT.BaseData.Domain.Entities.FM;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using APT.Utility;
using APT.BaseData.Domain.ApiModel.FM;
using Newtonsoft.Json;
using APT.BaseData.Domain.ApiModel.Platform;
using APT.BaseData.Domain.Entities;

namespace APT.FM.WebApi.Controllers.Api.FM
{
	/// <summary>
	/// 企业基本信息
	/// </summary>
	[Route("api/FM/FMBaseInfo")]
	public class BaseInfoController : AuthorizeApiController<T_FM_BASE_INFO>
	{
		/// <summary>
		/// 获得单条实体数据
		/// </summary>
		/// <param name="filter"></param>
		/// <returns></returns>
		[HttpPost, Route("GetBaseInfo")]
		public JsonActionResult<T_FM_BASE_INFO> GetBaseInfo([FromBody] KeywordFilter filter)
		{
			return WitEntity(null, filter);
		}

		/// <summary>
		/// 获得单条实体数据
		/// </summary>
		/// <param name="filter"></param>
		/// <returns></returns>
		[HttpPost, Route("PostComPanyForm")]
		public JsonActionResult<bool> PostComPanyForm([FromBody]ComPanyModel entity)
        {
			return SafeExecute<bool>(() =>
            {
                if (entity != null)
                {
                    if (entity.BaseInfo == null)
                        this.ThrowError("040010");
                    if (entity.BaseAdd == null)
                        this.ThrowError("040011");
                    if (entity.BaseEnergy == null)
                        this.ThrowError("040012");

                    this.UnifiedCommit(() =>
                    {
                        this.UpdateEntityNoCommit<T_FM_BASE_INFO>(entity.BaseInfo);
                        this.UpdateEntityNoCommit<T_FM_BASE_ADD>(entity.BaseAdd);
                        this.UpdateEntityNoCommit<T_FM_BASE_ENERGY>(entity.BaseEnergy);
                    });
                }
                else
                    this.ThrowError("040013");

                return true;
            });
        }

    }
}
