using System;
using SqlSugar;
using Infrastructure.Attribute;
using ZR.Model;
using ZR.Repository;
using System.Linq;
using System.Collections.Generic;
using ZR.Model.System;
using Infrastructure;
using Infrastructure.Extensions;
using ZR.Common;
using ZR.Service.Business.IBusinessService.BaseSet;
using ZR.Model.Dto.BaseSet;
using ZR.Model.Models.BaseSet;

namespace ZR.Service.Business
{
    /// <summary>
    /// 基础资料/库位信息表Service业务层处理
    ///
    /// @author admin
    /// @date 2023-04-11
    /// </summary>
    [AppService(ServiceType = typeof(IBasBinService), ServiceLifetime = LifeTime.Transient)]
    public class BasBinService : BaseService<BasBin>, IBasBinService
    {
        #region 业务逻辑代码

        /// <summary>
        /// 查询基础资料/库位信息表列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<BasBinDto> GetList(BasBinQueryDto parm)
        {
            //开始拼装查询条件
            var predicate = Expressionable.Create<BasBin>();

            predicate.AndIF(parm.RegionId > 0, b => b.RegionId == parm.RegionId);
            predicate.AndIF(!string.IsNullOrEmpty(parm.BinCode), b => b.BinCode.Contains(parm.BinCode));
            predicate.AndIF(parm.BinRow > 0, b => b.BinRow == parm.BinRow);
            predicate.AndIF(parm.BinColumn > 0, b => b.BinColumn == parm.BinColumn);
            predicate.AndIF(parm.BinLayer > 0, b => b.BinLayer == parm.BinLayer);
            predicate.AndIF(!string.IsNullOrEmpty(parm.BinType), b => b.BinType.Equals(parm.BinType));
            predicate.AndIF(!string.IsNullOrEmpty(parm.Status), b => b.Status.Equals(parm.Status));
            //搜索条件查询语法参考Sqlsugar
            var response = Queryable()
                .LeftJoin<BasRegion>((b, region) => b.RegionId == region.RegionId)
                .Where(predicate.ToExpression())
                .Select((b,region)=>new BasBin 
                {
                    RegionId = b.RegionId.SelectAll(),
                    RegionName=region.RegionName,
                    RegionCode=region.RegionCode,
                })
                .ToPage<BasBin, BasBinDto>(parm);

            return response;
        }


        /// <summary>
        /// 添加基础资料/库位信息表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddBasBin(BasBin model)
        {
            return Add(model, true);
        }

        /// <summary>
        /// 修改基础资料/库位信息表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateBasBin(BasBin model)
        {
            //var response = Update(w => w.BinId == model.BinId, it => new BasBin()
            //{
            //    BinCode = model.BinCode,
            //    BinName = model.BinName,
            //    BinType = model.BinType,
            //    RegionId = model.RegionId,
            //    BinRow = model.BinRow,
            //    BinColumn = model.BinColumn,
            //    BinLayer = model.BinLayer,
            //    Describe = model.Describe,
            //    Visible = model.Visible,
            //    Status = model.Status,
            //    UpdateBy = model.UpdateBy,
            //    UpdateTime = model.UpdateTime,
            //    Remark = model.Remark,
            //});
            //return response;
            return Update(model, true);
        }

        /// <summary>
        /// 清空基础资料/库位信息表
        /// </summary>
        /// <returns></returns>
        public void TruncateBasBin()
        {
            Truncate();
        }

        /// <summary>
        /// 导入数据
        /// </summary>
        /// <param name="bins"></param>
        /// <returns></returns>
        public string ImportDatas(List<BasBin> bins)
        {
            bins.ForEach(x =>
            {
                x.CreateTime = DateTime.Now;
                x.Status = "0";
                x.Remark = "数据导入";
            });
            var x = Context.Storageable(bins)
                .SplitInsert(it => !it.Any())
                .ToStorage();
            var result = x.AsInsertable.ExecuteCommand();//插入可插入部分;

            string msg = string.Format(" 插入{0} 更新{1} 错误数据{2} 不计算数据{3} 删除数据{4},总共{5}",
                               x.InsertList.Count,
                               x.UpdateList.Count,
                               x.ErrorList.Count,
                               x.IgnoreList.Count,
                               x.DeleteList.Count,
                               x.TotalList.Count);
            //输出统计                      
            Console.WriteLine(msg);

            //输出错误信息               
            foreach (var item in x.ErrorList)
            {
                Console.WriteLine("BinName为" + item.Item.BinName + " : " + item.StorageMessage);
            }

            return msg;
        }
        #endregion
    }
}