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

namespace ZR.Service.Business.OutStore
{
    /// <summary>
    /// 出库管理/装配记录表Service业务层处理
    ///
    /// @author admin
    /// @date 2023-04-14
    /// </summary>
    [AppService(ServiceType = typeof(IOutAssembleRecordService), ServiceLifetime = LifeTime.Transient)]
    public class OutAssembleRecordService : BaseService<OutAssembleRecord>, IOutAssembleRecordService
    {
        #region 业务逻辑代码

        /// <summary>
        /// 查询出库管理/装配记录表列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<OutAssembleRecordDto> GetList(OutAssembleRecordQueryDto parm, bool isPdaQuery = false)
        {
            //开始拼装查询条件
            var predicate = Expressionable.Create<OutAssembleRecord>();
            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.BomNo), it => it.BomNo.Contains(parm.BomNo));

            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.KeyFlag != null, it => it.KeyFlag == parm.KeyFlag);
            predicate.AndIF(parm.Result != null, it => it.Result == parm.Result);
            //搜索条件查询语法参考Sqlsugar
            var response = Queryable()
                .Where(predicate.ToExpression())
                .ToPage<OutAssembleRecord, OutAssembleRecordDto>(parm);

            return response;
        }


        /// <summary>
        /// 添加出库管理/装配记录表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddOutAssembleRecord(OutAssembleRecord model)
        {
            return Add(model, true);
        }

        /// <summary>
        /// 修改出库管理/装配记录表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateOutAssembleRecord(OutAssembleRecord model)
        {
            //var response = Update(w => w.AssembleId == model.AssembleId, it => new OutAssembleRecord()
            //{
            //    BomNo = model.BomNo,
            //    MaterialCode = model.MaterialCode,
            //    MaterialName = model.MaterialName,
            //    MaterialSpec = model.MaterialSpec,
            //    Category = model.Category,
            //    KeyFlag = model.KeyFlag,
            //    AssembleQty = model.AssembleQty,
            //    Picture = model.Picture,
            //    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 TruncateOutAssembleRecord()
        {
            Truncate();
        }
        #endregion
    }
}