using System;
using SqlSugar;
using Infrastructure.Attribute;
using ZR.Model;
using ZR.Model.Dto;
using ZR.Model.Models;
using ZR.Repository;
using ZR.Service.Business.IBusinessService;
using System.Linq;
using System.Collections.Generic;
using Mapster;
using ZR.Model.Models.Inspect;
using ZR.Service.Business.IBusinessService.OutStore;
using ZR.Model.Dto.OutStore;
using Infrastructure;
using Org.BouncyCastle.Math.EC.Rfc7748;

namespace ZR.Service.Business.OutStore
{
    /// <summary>
    /// Service业务层处理
    ///
    /// @author admin
    /// @date 2023-05-10
    /// </summary>
    [AppService(ServiceType = typeof(ISapOutTaskRecordService), ServiceLifetime = LifeTime.Transient)]
    public class SapOutTaskRecordService : BaseService<SapOutTaskRecord>, ISapOutTaskRecordService
    {
        #region 业务逻辑代码

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<SapOutTaskRecordDto> GetList(SapOutTaskRecordQueryDto parm)
        {
            //开始拼装查询条件
            var predicate = Expressionable.Create<SapOutTaskRecord>();

            if (parm.BeginTime != null && parm.EndTime != null)
            {
                parm.BeginTime = DateTimeHelper.GetBeginTime(parm.BeginTime, -1);
                parm.EndTime = DateTimeHelper.GetBeginTime(parm.EndTime, 1);
                predicate.And(d => d.CreateTime >= parm.BeginTime && d.CreateTime <= parm.EndTime);
            }
            predicate.AndIF(parm != null && !string.IsNullOrEmpty(parm.OutTaskNo), d => d.OutTaskNo.Contains(parm.OutTaskNo));
            predicate.AndIF(parm != null && !string.IsNullOrEmpty(parm.BomNo), d => d.BomNo.Contains(parm.BomNo));
            predicate.AndIF(parm != null && !string.IsNullOrEmpty(parm.MaterialCode), d => d.MaterialCode.Contains(parm.MaterialCode));
            predicate.AndIF(parm != null && !string.IsNullOrEmpty(parm.MaterialName), d => d.MaterialName.Contains(parm.MaterialName));
            predicate.AndIF(parm != null && !string.IsNullOrEmpty(parm.MaterialSpec), d => d.MaterialSpec.Contains(parm.MaterialSpec));
            //搜索条件查询语法参考Sqlsugar
            var response = Queryable()
                .Where(predicate.ToExpression())
                .ToPage<SapOutTaskRecord, SapOutTaskRecordDto>(parm);

            return response;
        }

        public List<SapOutTaskDto> GetSapOutTaskList(SapOutTaskQueryNoPageDto parm)
        {
            //开始拼装查询条件
            var predicate = Expressionable.Create<SapOutTaskRecord>();
            predicate.AndIF(parm != null && !string.IsNullOrEmpty(parm.OutTaskNo), x => x.OutTaskNo.Contains(parm.OutTaskNo));
            predicate.AndIF(parm != null && !string.IsNullOrEmpty(parm.BomNo), x => x.BomNo.Contains(parm.BomNo));
            predicate.And(x => x.Qty > x.OutQty);
            //搜索条件查询语法参考Sqlsugar
            var response = Queryable()
                .Distinct()
                .Where(predicate.ToExpression())
                .OrderBy(x => x.Id).Select(x => new { x.OutTaskNo }).ToList();

            return response.Adapt<List<SapOutTaskDto>>();
        }

        public List<SapOutTaskRecordDto> GetSapOutTaskRecordList(string outTaskNo)
        {
            //开始拼装查询条件
            var predicate = Expressionable.Create<SapOutTaskRecord>();
            predicate.And(x => x.OutTaskNo == outTaskNo);
            //predicate.And(x => x.Qty > x.CheckQty);
            //搜索条件查询语法参考Sqlsugar
            var response = Queryable()
                .Where(predicate.ToExpression())
                .OrderBy(x => x.Id).ToList();

            return response.Adapt<List<SapOutTaskRecordDto>>();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddSapOutTaskRecord(SapOutTaskRecord model)
        {
            return Add(model, true);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateSapOutTaskRecord(SapOutTaskRecord model)
        {
            //var response = Update(w => w.Id == model.Id, it => new SapOutTaskRecord()
            //{
            //    OutTaskNo = model.OutTaskNo,
            //    BomNo = model.BomNo,
            //    Category = model.Category,
            //    MaterialCode = model.MaterialCode,
            //    MaterialName = model.MaterialName,
            //    MaterialSpec = model.MaterialSpec,
            //    Qty = model.Qty,
            //    OutQty = model.OutQty,
            //    Result = model.Result,
            //    UpdateBy = model.UpdateBy,
            //    UpdateTime = model.UpdateTime,
            //    Remark = model.Remark,
            //});
            //return response;
            return Update(model, true);
        }

        /// <summary>
        /// 导入数据
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public string ImportDatas(List<SapOutTaskRecord> list)
        {
            var x = Context.Storageable(list)
                .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("导入出库任务单错误：" + item.StorageMessage);
            }

            return msg;
        }

        /// <summary>
        /// 清空
        /// </summary>
        /// <returns></returns>
        public void TruncateSapOutTaskRecord()
        {
            Truncate();
        }
        #endregion
    }
}