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 Infrastructure;
using ZR.Service.Business.IBusinessService.Inspect;
using ZR.Model.Dto.Inspect;
using ZR.Model.Models.Inspect;
using Mapster;
using System.Collections.Generic;

namespace ZR.Service.Business.Inspect
{
    /// <summary>
    /// 质检管理/质检记录表Service业务层处理
    ///
    /// @author admin
    /// @date 2023-05-05
    /// </summary>
    [AppService(ServiceType = typeof(IQaInspectRecordService), ServiceLifetime = LifeTime.Transient)]
    public class QaInspectRecordService : BaseService<QaInspectRecord>, IQaInspectRecordService
    {
        #region 业务逻辑代码

        /// <summary>
        /// 查询质检管理/质检记录表列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<QaInspectRecordDto> GetList(QaInspectRecordQueryDto parm, bool isPdaQuery = false)
        {
            //开始拼装查询条件
            var predicate = Expressionable.Create<QaInspectRecord>();
            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(!string.IsNullOrEmpty(parm.DeliverNo), d => d.DeliverNo.Contains(parm.DeliverNo));
            predicate.AndIF(!string.IsNullOrEmpty(parm.BomNo), d => d.BomNo.Contains(parm.BomNo));
            predicate.AndIF(!string.IsNullOrEmpty(parm.InspectNo), d => d.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), d => d.BnSnCode.Contains(parm.BnSnCode));
            predicate.AndIF(!string.IsNullOrEmpty(parm.Category), d => d.Category == parm.Category);
            predicate.AndIF(!string.IsNullOrEmpty(parm.Result), d => d.Result == parm.Result);
            predicate.AndIF(parm.KeyFlag != null, d => d.KeyFlag == parm.KeyFlag);
            //搜索条件查询语法参考Sqlsugar
            var response = Queryable()
                .Where(predicate.ToExpression())
                .ToPage<QaInspectRecord, QaInspectRecordDto>(parm);

            return response;
        }


        /// <summary>
        /// 添加质检管理/质检记录表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddQaInspectRecord(QaInspectRecord model)
        {
            return Add(model, true);
        }

        /// <summary>
        /// 修改质检管理/质检记录表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateQaInspectRecord(QaInspectRecord model)
        {
            //var response = Update(w => w.InspectId == model.InspectId, it => new QaInspectRecord()
            //{
            //    InspectNo = model.InspectNo,
            //    DeliverNo = model.DeliverNo,
            //    BomNo = model.BomNo,
            //    RowNo = model.RowNo,
            //    Category = model.Category,
            //    MaterialCode = model.MaterialCode,
            //    MaterialName = model.MaterialName,
            //    MaterialSpec = model.MaterialSpec,
            //    KeyFlag = model.KeyFlag,
            //    BnSnCode = model.BnSnCode,
            //    Qty = model.Qty,
            //    CheckQty = model.CheckQty,
            //    InspectedBy = model.InspectedBy,
            //    InspectTime = model.InspectTime,
            //    Result = model.Result,
            //    UpdateBy = model.UpdateBy,
            //    UpdateTime = model.UpdateTime,
            //    Remark = model.Remark,
            //});
            //return response;
            return Update(model, true);
        }

        /// <summary>
        /// 清空质检管理/质检记录表
        /// </summary>
        /// <returns></returns>
        public void TruncateQaInspectRecord()
        {
            Truncate();
        }
        #endregion

        #region 2023 0524 孙亚龙 新增
        public List<SapDeliverDto> GetCheckedDeliverData(QaInspectRecordQueryNoPageDto parm)
        {
            //开始拼装查询条件
            var predicate = Expressionable.Create<QaInspectRecord>();
            predicate.AndIF(parm != null && !string.IsNullOrEmpty(parm.DeliverNo), x => x.DeliverNo.Contains(parm.DeliverNo));
            predicate.AndIF(parm != null && !string.IsNullOrEmpty(parm.BomNo), x => x.BomNo.Contains(parm.BomNo));
            predicate.And(x => x.CheckQty > x.InstoreQty);
            //搜索条件查询语法参考Sqlsugar
            var response = Queryable()
                .Distinct()
                .Where(predicate.ToExpression())
                .OrderBy(x => x.InspectId).Select(x => new { x.DeliverNo }).ToList();

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

        public List<QaInspectRecordDto> GetCheckedSapDeliverRecordList(string deliverNo, string materialSpecCode = null)
        {
            //开始拼装查询条件
            var predicate = Expressionable.Create<QaInspectRecord>();
            predicate.And(x => x.DeliverNo == deliverNo);
            predicate.AndIF(!string.IsNullOrEmpty(materialSpecCode), x => x.MaterialSpec == materialSpecCode);
            //predicate.And(x => x.Qty > x.CheckQty);
            //搜索条件查询语法参考Sqlsugar
            var response = Queryable()
                .Where(predicate.ToExpression())
                .OrderBy(x => x.InspectId).ToList();

            return response.Adapt<List<QaInspectRecordDto>>();
        }
        #endregion
    }
}