using System;
using SqlSugar;
using Infrastructure.Attribute;
using ZR.Model;
using ZR.Repository;
using System.Linq;
using Infrastructure;
using ZR.Model.System;
using ZR.Service.Business.IBusinessService.InStore;
using ZR.Model.Dto.InStore;
using ZR.Model.Models.InStore;

namespace ZR.Service.Business.InStore
{
    /// <summary>
    /// 入库管理/入库记录表Service业务层处理
    ///
    /// @author admin
    /// @date 2023-04-13
    /// </summary>
    [AppService(ServiceType = typeof(IInInstoreRecordService), ServiceLifetime = LifeTime.Transient)]
    public class InInstoreRecordService : BaseService<InInstoreRecord>, IInInstoreRecordService
    {
        #region 业务逻辑代码

        /// <summary>
        /// 查询入库管理/入库记录表列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<InInstoreRecordDto> GetList(InInstoreRecordQueryDto parm, bool isPdaQuery = false)
        {
            //开始拼装查询条件
            var predicate = Expressionable.Create<InInstoreRecord>();
            if (parm.BeginTime != null && parm.EndTime != null)
            {
                parm.BeginTime = DateTimeHelper.GetBeginTime(parm.BeginTime, -1);
                parm.EndTime = DateTimeHelper.GetBeginTime(parm.EndTime, 1);
                predicate.And(it => it.CreateTime >= parm.BeginTime && it.CreateTime <= parm.EndTime);
            }
            predicate.AndIF(!string.IsNullOrEmpty(parm.DeliverNo), it => it.DeliverNo.Contains(parm.DeliverNo));
            predicate.AndIF(!string.IsNullOrEmpty(parm.BomNo), it => it.BomNo.Contains(parm.BomNo));
            predicate.AndIF(!string.IsNullOrEmpty(parm.InspectNo), it => it.InspectNo.Contains(parm.InspectNo));

            if (!isPdaQuery)
            {
                predicate.AndIF(!string.IsNullOrEmpty(parm.MaterialCode), d => d.MaterialCode.Contains(parm.MaterialCode));
                predicate.AndIF(!string.IsNullOrEmpty(parm.MaterialName), d => d.MaterialName.Contains(parm.MaterialName));
                predicate.AndIF(!string.IsNullOrEmpty(parm.MaterialSpec), d => d.MaterialSpec.Contains(parm.MaterialSpec));
            }
            else
            {
                predicate.AndIF(!string.IsNullOrEmpty(parm.MaterialCode), d => d.MaterialCode.Contains(parm.MaterialCode) || d.MaterialName.Contains(parm.MaterialCode) || d.MaterialSpec.Contains(parm.MaterialCode));
            }

            predicate.AndIF(!string.IsNullOrEmpty(parm.BnSnCode), it => it.BnSnCode.Contains(parm.BnSnCode));
            predicate.AndIF(!string.IsNullOrEmpty(parm.Category), it => it.Category == parm.Category);
            predicate.AndIF(parm.Result != null, it => it.Result == parm.Result);
            //搜索条件查询语法参考Sqlsugar
            var response = Queryable()
                .Where(predicate.ToExpression())
                .ToPage<InInstoreRecord, InInstoreRecordDto>(parm);

            return response;
        }


        /// <summary>
        /// 添加入库管理/入库记录表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddInInstoreRecord(InInstoreRecord model)
        {
            return Add(model, true);
        }

        /// <summary>
        /// 修改入库管理/入库记录表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateInInstoreRecord(InInstoreRecord model)
        {
            //var response = Update(w => w.InStoreId == model.InStoreId, it => new InInstoreRecord()
            //{
            //    InStoreNo = model.InStoreNo,
            //    InspectNo = model.InspectNo,
            //    DeliverNo = model.DeliverNo,
            //    BomNo = model.BomNo,
            //    MaterialCode = model.MaterialCode,
            //    MaterialName = model.MaterialName,
            //    MaterialSpec = model.MaterialSpec,
            //    Category = model.Category,
            //    KeyFlag = model.KeyFlag,
            //    BatchQty = model.BatchQty,
            //    DemandQty = model.DemandQty,
            //    InStoreQty = model.InStoreQty,
            //    Result = model.Result,
            //    UpdateBy = model.UpdateBy,
            //    UpdateTime = model.UpdateTime,
            //    Remark = model.Remark,
            //    BnSnCode = model.BnSnCode,
            //});
            //return response;
            return Update(model, true);
        }

        /// <summary>
        /// 清空入库管理/入库记录表
        /// </summary>
        /// <returns></returns>
        public void TruncateInInstoreRecord()
        {
            Truncate();
        }
        #endregion
    }
}