using System;
using SqlSugar;
using Infrastructure.Attribute;
using ZR.Model;
using ZR.Repository;
using System.Linq;
using Infrastructure;
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-14
    /// </summary>
    [AppService(ServiceType = typeof(IInRetreatRecordService), ServiceLifetime = LifeTime.Transient)]
    public class InRetreatRecordService : BaseService<InRetreatRecord>, IInRetreatRecordService
    {
        #region 业务逻辑代码

        /// <summary>
        /// 查询入库管理/退料记录表列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<InRetreatRecordDto> GetList(InRetreatRecordQueryDto parm)
        {
            //开始拼装查询条件
            var predicate = Expressionable.Create<InRetreatRecord>();
            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.MaterialCode), it => it.MaterialCode.Contains(parm.MaterialCode));
            predicate.AndIF(!string.IsNullOrEmpty(parm.MaterialName), it => it.MaterialName.Contains(parm.MaterialName));
            predicate.AndIF(!string.IsNullOrEmpty(parm.MaterialSpec), it => it.MaterialSpec.Contains(parm.MaterialSpec));
            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);
            //搜索条件查询语法参考Sqlsugar
            var response = Queryable()
                .Where(predicate.ToExpression())
                .ToPage<InRetreatRecord, InRetreatRecordDto>(parm);

            return response;
        }


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

        /// <summary>
        /// 修改入库管理/退料记录表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateInRetreatRecord(InRetreatRecord model)
        {
            //var response = Update(w => w.RetreatId == model.RetreatId, it => new InRetreatRecord()
            //{
            //    RetreatNo = model.RetreatNo,
            //    BomNo = model.BomNo,
            //    MaterialCode = model.MaterialCode,
            //    MaterialName = model.MaterialName,
            //    Category = model.Category,
            //    KeyFlag = model.KeyFlag,
            //    RetreatQty = model.RetreatQty,
            //    Result = model.Result,
            //    UpdateBy = model.UpdateBy,
            //    UpdateTime = model.UpdateTime,
            //    Remark = model.Remark,
            //    BomDtlId = model.BomDtlId,
            //    MaterialSpec = model.MaterialSpec,
            //    BnSnCode = model.BnSnCode,
            //    ReceiveQty = model.ReceiveQty,
            //    ReasonType = model.ReasonType,
            //    ReasonDesc = model.ReasonDesc,
            //});
            //return response;
            return Update(model, true);
        }

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