/* =============================================
-- Author:		<Author,,zhangkun>
-- Create date: <Create Date,,20200609>
-- Description:	<Description,,商品 库存 商品类别相关操作）>
-- ============================================*/
using System;
using System.Collections.Generic;
using System.Common;
using System.Common.Helpers;
using System.Data;
using System.IRepositories;
using System.Linq;
using System.Linq.Expressions;
using System.Model;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using SqlSugar;
using TVShoppingCallCenter_ZLJ.Controllers.Order;
using TVShoppingCallCenter_ZLJ.Models.Dtos;
using TVShoppingCallCenter_ZLJ.Models.Inputs;

namespace TVShoppingCallCenter_ZLJ.Controllers.Product
{
    [Route("api/[controller]")]
    public class ProductController : BaseController
    {

        private readonly IBus_ProductRepository _productRepository;
        private readonly IBus_ProductClassRepository _productClassRepository;
        private readonly IBus_TagRepository _tagRepository;
        private readonly OrderFunctionController busOrderFunctionController;
        private readonly IConfiguration config;
        public ProductController(IConfiguration _config, OrderFunctionController _busOrderFunctionController, IBus_ProductRepository productRepository, IBus_ProductClassRepository productClassRepository, IBus_TagRepository tagRepository)
        {
            _productRepository = productRepository;
            _productClassRepository = productClassRepository;
            _tagRepository = tagRepository;
            busOrderFunctionController = _busOrderFunctionController;
            config = _config;
        }
        [AllowAnonymous]
        [HttpGet("/api/test")]
        public IActionResult Test()
        {
            return new JsonResult(new
            {
                Status = false,
                Message = "访问信息"
            });
        }
        

        #region 商品操作

        /// <summary>
        /// 导入商品
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("importproduct")]
        public async Task<IActionResult> ImportProduct()
        {
            Microsoft.AspNetCore.Http.IFormFile _upfile = Request.Form.Files[0];
            if (!_upfile.ContentType.Equals("application/vnd.ms-excel") && !_upfile.ContentType.Equals("application/x-xls") && !_upfile.ContentType.Equals("application/x-xlsx") && !_upfile.ContentType.Equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") && !_upfile.ContentType.Equals("application/octet-stream"))
                return Error($"请正确上传Excel文件：file.ContentType={_upfile.ContentType}");

            //将数据导入数据库
            int headrow = 0;
            NPOIHelper npoi = new NPOIHelper();
            DataTable dtExcel = npoi.ExcelToTable1(_upfile, headrow);
            if (dtExcel.Rows.Count > 0)
            {
                Dictionary<string, string> dirList = new Dictionary<string, string>();
                string[] arrykey = config["Import:productkey"].ToString().ToLower().Split(',');
                string[] arryvalue = config["Import:productvalue"].ToString().Split(',');
                for (int i=0;i< arrykey.Length;i++)
                {
                    dirList.Add(arrykey[i], arryvalue[i]);
                }
                try
                {
                    IList<ProductInput> IProducts = ModelConvertHelper<ProductInput>.ConvertToModelByColumns(dtExcel, dirList);
                    List<ProductInput> Products = ModelConvertHelper<ProductInput>.ConvertIListToList(IProducts);
                    int num = Products.Count;
                    List<ProductInput> inputlist = new List<ProductInput>();
                    //判断表格内是否有重复
                    for (int i = 0; i < Products.Count - 1; i++)
                    {
                        for (int j = i + 1; j < Products.Count; j++)
                        {
                            if (Products[i].ProductId == Products[j].ProductId)
                            {
                                return Error(Products[i].ProductId + "ID有重复:" + Products[j].ProductId);
                            }
                            if (Products[i].ProductName == Products[j].ProductName)
                            {
                                return Error(Products[i].ProductId + " 名称有重复:" + Products[j].ProductId);
                            }
                            if (Products[i].ProductShortName == Products[j].ProductShortName)
                            {
                                return Error(Products[i].ProductId + "商品副标题 有重复:" + Products[j].ProductId);
                            }
                        }

                    }
                    //判断
                    foreach (ProductInput input in Products)
                    {
                        Expression<Func<T_Bus_Product, bool>> eq = a => a.F_ProductName == input.ProductName;



                        if (await _productRepository.GetCount(eq) > 0)
                        {
                            return Error("商品名称有重复:" + input.ProductName);
                        }
                        eq = b => b.F_ProductId == input.ProductId;
                        if (await _productRepository.GetCount(eq) > 0)
                        {
                            return Error("商品ID有重复:" + input.ProductId);
                        }
                        eq = b => b.F_ProductShortName == input.ProductShortName;
                        if (await _productRepository.GetCount(eq) > 0)
                        {
                            return Error("商品副标题有重复:" + input.ProductShortName);
                        }
                        input.IsDelete = 0;
                        input.IsSale = 1;
                        if (!string.IsNullOrEmpty(input.ProductId))
                            inputlist.Add(input);
                    }
                    if (await AddProductFun(inputlist))
                    {
                        return Success("已导入数据");
                    }
                    else
                    {
                        return Error("导入失败");
                    }
                }
                catch
                {
                    return Error("模板错误，请选择正确的模板");
                }
                
               

               
            }
            return Success("请选择要导入的文件");
        }



        /// <summary>
        /// 新增商品 库存只通过两种方式变动 1 （我们下订单后，对方库存发别的地儿）对方库存变动回调我们，我们更新相关商品库存  2  我们定时去拿库存统一更新
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("addproduct")]
        public async Task<IActionResult> AddProduct(ProductInput input)
        {

            #region 验证 参数

            if (input.ClassId == 0)
                return Error("请选择分类名称");
            if (string.IsNullOrEmpty(input.ProductName))
                return Error("请输入商品名称");
            if (string.IsNullOrEmpty(input.ProductShortName))
                return Error("请输入商品简写");
            if (string.IsNullOrEmpty(input.ProductNumber))
                return Error("请输入商品款号");
            if (string.IsNullOrEmpty(input.ClassName))
                return Error("请选择分类名称");
            if (input.MarketPrice <= 0)
                return Error("请输入正确市场价");
            if (input.MemberPrice <= 0)
                return Error("请输入正确会员价");
            if (input.SpecialPrice <= 0)
                return Error("请输入正确特惠价");
            if (string.IsNullOrEmpty(input.ProductId))
                return Error("请输入正确商品编码");

            #endregion

            if (await _productRepository.GetCount(a => a.F_ProductName == input.ProductName) > 0)
            {
                return Error("商品名称 有重复");
            }
            if (await _productRepository.GetCount(b => b.F_ProductId == input.ProductId) > 0)
            {
                return Error("商品ID 有重复");
            }
            if (await _productRepository.GetCount(b => b.F_ProductShortName == input.ProductShortName) > 0)
            {
                return Error("商品副标题 有重复");
            }
            if (!string.IsNullOrEmpty(input.Tag))
            {
                int arrynum = input.Tag.Length - input.Tag.Replace(".", "").Length;
                if (arrynum > 1)
                {
                    return Error("活动标签只能有一个");
                }
            }
            input.IsDelete = 1;
            List<ProductInput> inputlist = new List<ProductInput>();
            inputlist.Add(input);
            if (await AddProductFun(inputlist))
            {
                return Success("新增商品成功");
            }
            else
            {
                return Error("新增商品失败,请重试！");
            }
        }

        private async Task<bool> AddProductFun(List<ProductInput> inputlist)
        {
            List<T_Bus_Product> list = new List<T_Bus_Product>();
            foreach (ProductInput input in inputlist)
            {
                T_Bus_Product T_Bus_ProductModel = new T_Bus_Product();

                ModelConvertHelper<T_Bus_Product, ProductInput>.ModeToModelDefault(T_Bus_ProductModel, input);
                //T_Bus_ProductModel.F_ProductId = input.ProductId;
                //T_Bus_ProductModel.F_ProductName = input.ProductName;
                //T_Bus_ProductModel.F_ProductShortName = input.ProductShortName;
                //T_Bus_ProductModel.F_ProductNumber = input.ProductNumber;
                //T_Bus_ProductModel.F_ProductNumberName = input.ProductNumberName;

                //T_Bus_ProductModel.F_ClassId = input.ClassId;
                //T_Bus_ProductModel.F_ClassName = input.ClassName;

                //T_Bus_ProductModel.F_PinyinShort = input.PinyinShort;
                //T_Bus_ProductModel.F_MarketPrice = input.MarketPrice;
                //T_Bus_ProductModel.F_MemberPrice = input.MemberPrice;
                //T_Bus_ProductModel.F_SpecialPrice = input.SpecialPrice;
                //T_Bus_ProductModel.F_Score = input.Score;

                //T_Bus_ProductModel.F_Address = input.Address;
                //T_Bus_ProductModel.F_Des = input.Des;
                //T_Bus_ProductModel.F_Tag = input.Tag;
                //T_Bus_ProductModel.F_Factory = input.Factory;
                //T_Bus_ProductModel.F_Supplier = input.Supplier;

                //T_Bus_ProductModel.F_IsSale = input.IsSale;//0赠品 1正常商品
                //T_Bus_ProductModel.F_Weight = input.Weight;
                T_Bus_ProductModel.F_IsDelete = 1;


                if (input.OnSaleBegin != null)
                { T_Bus_ProductModel.F_OnSaleBegin = input.OnSaleBegin.GetValueOrDefault().ToString("yyyy-MM-dd 00:00:01").ObjToDate(); }
                if (input.OnSaleEnd != null)
                { T_Bus_ProductModel.F_OnSaleEnd = input.OnSaleEnd.GetValueOrDefault().ToString("yyyy-MM-dd 23:59:59").ObjToDate(); }



                T_Bus_ProductModel.F_UserName = UserLogin.UserName;
                T_Bus_ProductModel.F_UserId =UserLogin.UserId.ObjToInt();
                list.Add(T_Bus_ProductModel);
            }
            if (await _productRepository.AddMany(list))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 修改商品 库存只通过两种方式变动 1 （我们下订单后，对方库存发别的地儿）对方库存变动回调我们，我们更新相关商品库存  2  我们定时去拿库存统一更新
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("updateproduct")]
        public async Task<IActionResult> UpdateProduct(ProductInput input)
        {

            #region 验证 参数

            if (input.ClassId == 0)
                return Error("请选择分类名称");
            if (string.IsNullOrEmpty(input.ProductName))
                return Error("请输入商品名称");
            if (string.IsNullOrEmpty(input.ProductShortName))
                return Error("请输入商品简写");
            if (string.IsNullOrEmpty(input.ProductNumber))
                return Error("请输入商品条形码");
            if (string.IsNullOrEmpty(input.ClassName))
                return Error("请选择分类名称");
            if (input.MarketPrice <= 0)
                return Error("请输入正确市场价");
            if (input.MemberPrice <= 0)
                return Error("请输入正确会员价");
            if (input.SpecialPrice <= 0)
                return Error("请输入正确特惠价");
            if (string.IsNullOrEmpty(input.ProductId))
                return Error("商品ID不能为空");
            if (string.IsNullOrEmpty(input.PinyinShort))
                return Error("请输入正确拼音简码");
            #endregion

            T_Bus_Product T_Bus_ProductModel = await _productRepository.GetSingle(a => a.F_ProductId == input.ProductId);

            if (T_Bus_ProductModel == null)
                return Error("如需新添加，请点击新增按钮，此记录不存在");

            //此处如果id可修改，则输入新ID 修改重复条件
            List<Expression<Func<T_Bus_Product, bool>>> listeq = new List<Expression<Func<T_Bus_Product, bool>>>();
            listeq.Add( a => a.F_ProductName == input.ProductName);
            listeq.Add(a => a.F_ProductId == input.ProductId);
            listeq.Add(a => a.F_ProductShortName == input.ProductShortName);
            int n= 1;
            foreach (Expression<Func<T_Bus_Product, bool>> eq in listeq)
            {
                List<T_Bus_Product> T_Bus_ProductModelSearch = await _productRepository.GetListALL(eq);
                if (T_Bus_ProductModelSearch.Count > 1)
                {
                    if (n == 1) { return Error("商品名称 有重复"); }
                    else if (n == 2) { return Error("商品ID 有重复"); }
                    else if (n == 3) { return Error("副标题/商品副标题 有重复"); }
                }
                if (T_Bus_ProductModelSearch.Count == 1 && T_Bus_ProductModelSearch[0].F_ProductId != input.ProductId)
                {
                    if (n == 1) { return Error("商品名称 有重复"); }
                    else if (n == 2) { return Error("商品ID 有重复"); }
                    else if (n == 3) { return Error("副标题/商品副标题 有重复"); }
                }
                n++;
            }

            //ModelConvertHelper<T_Bus_Product, ProductInput>.ModeToModelDefault(T_Bus_ProductModel, input);
            T_Bus_ProductModel.F_ProductName = input.ProductName;
            T_Bus_ProductModel.F_ProductShortName = input.ProductShortName;
            T_Bus_ProductModel.F_ProductNumber = input.ProductNumber;
            T_Bus_ProductModel.F_ProductNumberName = input.ProductNumberName;
            T_Bus_ProductModel.F_PinyinShort = input.PinyinShort;

            T_Bus_ProductModel.F_ClassId = input.ClassId;
            T_Bus_ProductModel.F_ClassName = input.ClassName;


            T_Bus_ProductModel.F_MarketPrice = input.MarketPrice;
            T_Bus_ProductModel.F_MemberPrice = input.MemberPrice;
            T_Bus_ProductModel.F_SpecialPrice = input.SpecialPrice;
            T_Bus_ProductModel.F_Score = input.Score;

            T_Bus_ProductModel.F_Address = input.Address;
            T_Bus_ProductModel.F_Des = input.Des;
            T_Bus_ProductModel.F_Tag = input.Tag;
            T_Bus_ProductModel.F_Factory = input.Factory;
            T_Bus_ProductModel.F_Supplier = input.Supplier;

            T_Bus_ProductModel.F_IsSale = input.IsSale;
            T_Bus_ProductModel.F_Weight = input.Weight;
            if (input.OnSaleBegin != null)
                T_Bus_ProductModel.F_OnSaleBegin = input.OnSaleBegin.GetValueOrDefault().ToString("yyyy-MM-dd 00:00:01").ObjToDate();
            if (input.OnSaleEnd != null)
                T_Bus_ProductModel.F_OnSaleEnd = input.OnSaleEnd.GetValueOrDefault().ToString("yyyy-MM-dd 23:59:59").ObjToDate();



            T_Bus_ProductModel.F_UserName = UserLogin.UserName;
            T_Bus_ProductModel.F_UserId = UserLogin.UserId.ObjToInt();


            if (await _productRepository.Update(T_Bus_ProductModel))
            {
                return Success("保存商品成功");
            }
            else
            {
                return Error("保存商品失败,请重试！");
            }
        }

        /// <summary>
        /// 删除商品 硬删除
        /// </summary>
        /// <param name="ids">格式 ,1,2,3,</param>
        /// <returns></returns>
        [HttpPost("deleteproductbyids")]
        public async Task<IActionResult> DeleteProductByIds(string ids)
        {

            #region 验证 参数

            if (string.IsNullOrEmpty(ids))
                return Error("请输入删除id");
            if (ids.Replace(",", "") == "")
                return Error("请输入删除id");
            #endregion
            ids = ids.Trim(',');
            string[] idarry = ids.Split(',');
            if (await _productRepository.Delete(q => idarry.Contains(q.F_ProductId.ToString())))
            {
                return Success("删除商品成功");
            }
            else
            {
                return Error("删除商品失败,请重试！");
            }
        }
        /// <summary>
        /// 删除商品 软删除
        /// </summary>
        /// <param name="ids">格式 ,1,2,3,</param>
        /// <returns></returns>
        [HttpPost("delproductbyids")]
        public async Task<IActionResult> DelProduct(string ids)
        {

            #region 验证 参数

            if (string.IsNullOrEmpty(ids))
                return Error("请输入删除id");
            if (ids.Replace(",", "") == "")
                return Error("请输入删除id");
            #endregion
            ids = ids.Trim(',');
            string[] idarry = ids.Split(',');
            if (await _productRepository.Delete( q => idarry.Contains(q.F_ProductId.ToString())))
                return Success("删除商品成功");
            else
                return Success("删除商品失败");
        }


        /// <summary>
        /// 查询商品
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("getproduct")]
        public async Task<IActionResult> ProductList(ProductInput input)
        {

            #region  拼接条件
            if (input.Limit <= 0)
            {
                input.Limit = 100000;
            }
            Expression<Func<T_Bus_Product, bool>> eq = a => a.F_IsDelete == 1;

            if (input.ClassId > 0)
                eq = eq.And(a => a.F_ProductId == input.ProductId);
            if (!string.IsNullOrEmpty(input.ClassName))
            {
                eq = eq.And(a => a.F_ClassName.Contains(input.ClassName));
                // eq.Or(a => a.F_ClassShortName.Contains(input.F_ClassName));
            }
            //商品名/标题
            if (!string.IsNullOrEmpty(input.ProductName))
            {
                eq = eq.And(a => a.F_ProductName.Contains(input.ProductName));
            }
            //指定分类
            if (input.ClassId > 0)
            {
                eq = eq.And(a => a.F_ClassId == input.ClassId);
            }
            //分类名称
            if (!string.IsNullOrEmpty(input.ClassName))
            {
                eq = eq.And(a => a.F_ClassName.Contains(input.ClassName));
            }
            //拼音简码
            if (!string.IsNullOrEmpty(input.PinyinShort))
            {
                eq = eq.And(a => a.F_PinyinShort == input.PinyinShort);
            }

            //条形码
            if (!string.IsNullOrEmpty(input.ProductNumber))
            {
                eq = eq.And(a => a.F_ProductNumber == input.ProductNumber);
            }
            //简称/编码/副标题
            if (!string.IsNullOrEmpty(input.ProductShortName))
            {
                eq = eq.And(a => a.F_ProductShortName == input.ProductShortName);
            }
            //标签
            if (!string.IsNullOrEmpty(input.Tag))
            {
                eq = eq.And(a => a.F_Tag.Contains(input.Tag));
            }
            if (input.IsSale > -1)
            {
                eq = eq.And(a => a.F_IsSale == input.IsSale);
            }

            #endregion

            RefAsync<int> count = 0;
            var list = await _productRepository.GetListByPage(eq, new MyPageModel() { PageIndex = 1, PageSize = input.Limit, PageCount = count });
            if (input.Stock == 1)
            {
                if (list.Totals > 0)
                {
                    List<T_Bus_Product> reslist = new List<T_Bus_Product>();
                    //获取库存
                    string sku = "";
                    foreach (T_Bus_Product p in list.Rows)
                    {
                        sku += p.F_ProductId + ",";
                    }
                    List<EBossStock> listStock = await busOrderFunctionController.GetStock(sku.Trim(','), "山西仓");
                    List<EBossStock> listStock1 = await busOrderFunctionController.GetStock(sku.Trim(','), "新乡仓");
                    foreach (T_Bus_Product p in list.Rows)
                    {
                        if (listStock != null)
                        {
                            foreach (EBossStock a in listStock)
                            {
                                if (a.sku == p.F_ProductId)
                                {
                                    p.F_Stock = a.qty;
                                    break;
                                }
                            }
                        }
                        if (listStock1 != null)
                        {
                            foreach (EBossStock a in listStock1)
                            {
                                if (a.sku == p.F_ProductId)
                                {
                                    p.F_Stock1 = a.qty;
                                    break;
                                }
                            }
                        }
                    }

                }
            }
            return Success("成功", list);

        }
        /// <summary>
        /// 分页获取商品列表
        /// </summary>
        /// <param name="input">筛选字段</param>
        /// <returns></returns>
        [HttpPost("getlistbypage")]
        public async Task<IActionResult> GetListByPage([FromBody]ProductInput input)
        {
            int pageindex = input.pageindex;
            int pagesize = input.pagesize;
            List<IConditionalModel> conModels = new List<IConditionalModel>();
            #region 条件筛选


            conModels.Add(new ConditionalModel() { FieldName = "F_IsDelete", ConditionalType = ConditionalType.GreaterThanOrEqual, FieldValue = "0" });
            if (input.IsDelete > -2)
            {
                conModels.Add(new ConditionalModel() { FieldName = "F_IsSale", ConditionalType = ConditionalType.Equal, FieldValue = input.IsDelete.ToString() });
            }
            if (!string.IsNullOrEmpty(input.ProductId))
            {
                conModels.Add(new ConditionalModel() { FieldName = "F_ProductId", ConditionalType = ConditionalType.Equal, FieldValue = input.ProductId });
            }
            //指定分类
            if (input.ClassId > 0)
            {
                conModels.Add(new ConditionalModel() { FieldName = "F_ClassId", ConditionalType = ConditionalType.Equal, FieldValue = input.ClassId.ToString() });
            }

            //商品名/标题
            if (!string.IsNullOrEmpty(input.ProductName))
            {
                conModels.Add(new ConditionalModel() { FieldName = "F_ProductName", ConditionalType = ConditionalType.Like, FieldValue = input.ProductName });
            }
            //分类名称
            if (!string.IsNullOrEmpty(input.ClassName))
            {
                conModels.Add(new ConditionalModel() { FieldName = "F_ClassName", ConditionalType = ConditionalType.Like, FieldValue = input.ClassName });
            }
            //拼音简码
            if (!string.IsNullOrEmpty(input.PinyinShort))
            {
                conModels.Add(new ConditionalModel() { FieldName = "F_PinyinShort", ConditionalType = ConditionalType.Equal, FieldValue = input.PinyinShort });
            }

            //条形码
            if (!string.IsNullOrEmpty(input.ProductNumber))
            {
                conModels.Add(new ConditionalModel() { FieldName = "F_ProductNumber", ConditionalType = ConditionalType.Equal, FieldValue = input.ProductNumber });
            }
            //简称/编码/副标题
            if (!string.IsNullOrEmpty(input.ProductShortName))
            {
                conModels.Add(new ConditionalModel() { FieldName = "F_ProductShortName", ConditionalType = ConditionalType.Like, FieldValue = input.ProductShortName });
            }
            //标签
            if (!string.IsNullOrEmpty(input.Tag))
            {
                conModels.Add(new ConditionalModel() { FieldName = "F_Tag", ConditionalType = ConditionalType.Like, FieldValue = input.Tag });
            }
            if (input.IsSale > -1)
            {
                conModels.Add(new ConditionalModel() { FieldName = "F_IsSale", ConditionalType = ConditionalType.Equal, FieldValue = input.IsSale.ToString() });
            }


            #endregion
            int recordCount = 0;
            PageData<T_Bus_Product> list = await _productRepository.GetListByPage(conModels, new MyPageModel() { PageIndex = pageindex, PageSize = pagesize, PageCount = recordCount }, " F_AddTime DESC");
            if (input.Stock == 1)
            {
                if (list.Totals > 0)
                {
                    List<T_Bus_Product> reslist = new List<T_Bus_Product>();
                    //获取库存
                    string sku = "";
                    foreach (T_Bus_Product p in list.Rows)
                    {
                        sku += p.F_ProductId + ",";
                    }
                    List<EBossStock> listStock = await busOrderFunctionController.GetStock(sku.Trim(','), "山西仓");
                    List<EBossStock> listStock1 = await busOrderFunctionController.GetStock(sku.Trim(','), "新乡仓");
                    foreach (T_Bus_Product p in list.Rows)
                    {
                        if (listStock != null)
                        {
                            foreach (EBossStock a in listStock)
                            {
                                if (a.sku == p.F_ProductId)
                                {
                                    p.F_Stock = a.qty;
                                    break;
                                }
                            }
                        }
                        if (listStock1 != null)
                        {
                            foreach (EBossStock a in listStock1)
                            {
                                if (a.sku == p.F_ProductId)
                                {
                                    p.F_Stock1 = a.qty;
                                    break;
                                }
                            }
                        }
                    }

                }
            }
            return Success("成功", list);
        }


        #endregion


        #region 库存对接

        /// <summary>
        /// 更新库存（对方库存变化  入库 出库 时做更新）  订单发货时，对面库存 出库要对应订单，以便跟踪
        /// </summary>
        /// <param name="input">目前暂定Id唯一标识更新如果不方便可以以F_ProductName，如果那边更好操作，顺序上这边先有商品的话可以以id操作，或者以条形码操作</param>
        /// 基类BaseController继承的不是[ApiController]，也没有加[ApiController]头标  所以用到json序列化传值 和测试工具raw传值必须加[FromBody]
        /// <returns></returns>
        [HttpPost("updatestock")]
        public async Task<IActionResult> UpdateStock([FromBody]List<ProductInput> input)
        {

            #region 验证 参数
            if (input.Count <= 0)
                return Error("参数缺失，没有数据");
            List<T_Bus_Product> listmodel = new List<T_Bus_Product>();

            foreach (ProductInput m in input)
            {
                if (string.IsNullOrEmpty(m.ProductId))
                    return Error("参数缺失商品id");

                T_Bus_Product model = await _productRepository.GetSingle(a => a.F_ProductId == m.ProductId);


                if (model != null)
                {
                    if (m.Stock >= 0)
                        model.F_Stock = m.Stock;
                    if (m.Stock1 >= 0)
                        model.F_Stock = m.Stock1;
                    if (m.FreezeStock >= 0)
                        model.F_FreezeStock = m.FreezeStock;
                    if (m.FreezeStock1 >= 0)
                        model.F_FreezeStock1 = m.FreezeStock1;
                    listmodel.Add(model);
                }
            }

            #endregion
            //此字段更新单独字段
            //if (await _productRepository.UpdateListToColumns(listmodel, it => new { it.F_Stock, it.F_FreezeStock, it.F_Stock1, it.F_FreezeStock1, }))
            return Success("更新库存成功");
            //else
            //    return Success("更新库存失败");
        }


        #endregion
    }
}