using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Web;
using System.Web.Http;
using ICSSoft.ASKDataProject;
using ICSSoft.ASKEntity;
using Newtonsoft.Json;

namespace ICSSoft.WebAPI.ASKControllers
{
    public class ASKController : ApiController
    {
        private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        /// <summary>
        /// 创建成品入库单(含连副产品)
        /// </summary>
        /// <param name="RJsonData">入库数据</param>
        /// <param name="LJsonData">连副产品</param>
        /// <returns></returns>
        [Route("api/ManufactureReceiveDocASK/Create")]
        [HttpPost]
        public HttpResponseMessage CreateManufactureReceiveDoc([FromBody]object JsonData)
        {

            log.Info("接口:api/ManufactureReceiveDocASK/Create");
            log.Info("创建成品入库单传入值:" + JsonData);
            HttpResponseMessage result = new HttpResponseMessage();
            Result res = new Result();
            string str = string.Empty;
            try
            {
                if (JsonData != null && !string.IsNullOrWhiteSpace(JsonData.ToString()) && JsonData.ToString() != "[]")
                {

                    try
                    {
                        List<ICSManufactureReceiveDoc> infos = new List<ICSManufactureReceiveDoc>();
                       
                        try
                        {
                            infos = JsonConvert.DeserializeObject<List<ICSManufactureReceiveDoc>>(JsonData.ToString());
                           
                        }
                        catch (Exception ex)
                        {
                            log.Error("转换失败:" + ex.ToString());
                            res.Success = false;
                            res.Message = "JSON格式不正确!";
                        }
                        ManufactureReceiveDoc action = new ManufactureReceiveDoc();
                        string resultStr = action.CreateManufactureReceiveDoc(infos);
                        if (!string.IsNullOrWhiteSpace(resultStr))
                        {
                            res.Success = true;
                            res.Message = "接口调用成功!";
                            res.Data = resultStr;
                        }
                        else
                        {
                            res.Success = false;
                            res.Message = resultStr;
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error("调用后台失败:" + ex.ToString());
                        res.Success = false;
                        res.Message = ex.Message;
                    }
                }
                else
                {
                    res.Success = false;
                    res.Message = "请传入参数";
                }
            }
            catch (Exception ex)
            {
                log.Error("参数检验失败:" + ex.ToString());
                res.Success = false;
                res.Message = ex.Message;
            }
            finally
            {
                str = JsonConvert.SerializeObject(res);
                result.Content = new StringContent(str, Encoding.GetEncoding("UTF-8"), "application/json");
            }
            log.Debug("创建成品入库单返回值:" + str);
            return result;
        }

    }
}