using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using Model.ModelDto.BllSoDto;
using Model.ModelDto.LogDto;
using SqlSugar;
using WMS.DAL; 
using WMS.Entity.Context;
using WMS.Entity.LogEntity;
using WMS.Entity.SysEntity;
using WMS.IBLL.ILogServer;

namespace WMS.BLL.LogServer
{
    public class TaskServer:DbHelper<LogTask>,ITaskServer
    {
        private static readonly SqlSugarScope Db = DataContext.Db;
        public TaskServer():base(Db)
        {
        }


        public List<TaskDto> GetTaskList(List<string> orderType, string type, string status, string taskNo, int isSuccess, string palletNo, string msg, int page, int limit, out int count)
        {
            try
            {
                Expression<Func<LogTask, bool>> item = Expressionable.Create<LogTask>()
                    .AndIF( orderType.Count>0, it => orderType.Contains(it.OrderType))
                    .AndIF(!string.IsNullOrWhiteSpace(type), it => it.Type == type)
                    .AndIF(!string.IsNullOrWhiteSpace(status), it => it.Status == status)
                    .AndIF(!string.IsNullOrWhiteSpace(taskNo), it => it.TaskNo.Contains(taskNo.Trim())) 
                    .AndIF(isSuccess != -1, it => it.IsSuccess == isSuccess)
                    .AndIF(!string.IsNullOrWhiteSpace(palletNo), it => it.PalletNo.Contains(palletNo.Trim()))
                    .AndIF(!string.IsNullOrWhiteSpace(msg), it => it.Msg.Contains(msg.Trim())) 
                    .ToExpression();//注意 这一句 不能少
                var total = 0;
                var data = GetAllWhereAsync(item)
                    .LeftJoin<SysUserInfor>((a,b)=>a.CreateUser == b.Id).
                    Select((a,b) => new TaskDto()
                    {
                        Id = a.Id,
                        TaskNo = a.TaskNo,
                        Sender = a.Sender,
                        Receiver = a.Receiver,
                        IsSuccess = a.IsSuccess,
                        Information = a.Information,
                        SendDate = a.SendDate,
                        BackDate = a.BackDate,
                        StartLocat = a.StartLocat,
                        EndLocat = a.EndLocat,
                        MessageDate = a.MessageDate,
                        PalletNo = a.PalletNo,
                        Msg = a.Msg,
                        IsSend = a.IsSend,
                        IsCancel = a.IsCancel,
                        IsFinish = a.IsFinish,
                        Status = a.Status,
                        Type = a.Type,
                        OrderType = a.OrderType,

                        CancelDate = a.CancelDate,
                        FinishDate = a.FinishDate,

                        CreateUserName =b.RealName,
                        CreateTime = a.CreateTime
                    })
                    .OrderByDescending(a => a.TaskNo)
                    .ToOffsetPage(page,limit,ref total);
                count = total;

                return data.OrderByDescending(m=>m.TaskNo).ToList();
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        //修改任务下发状态为成功
        public void EditTaskIssueOk(List<string> taskNo, DateTime sendTime, DateTime backTime)
        {
            try
            {
                var list = GetAllWhereAsync(m => taskNo.Contains(m.TaskNo)).ToList();
                foreach (var item in list)
                {
                    item.IsSuccess = 1;
                    item.Status = "1";
                    item.SendDate = sendTime;
                    item.BackDate = backTime;
                }

                Db.Updateable(list).ExecuteCommand();
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        //修改任务下发状态为失败异常信息
        public void EditTaskIssueNo(List<string> taskNo, DateTime sendTime, DateTime backTime, string information)
        {
            try
            {
                var list = GetAllWhereAsync(m => taskNo.Contains(m.TaskNo)).ToList();
                foreach (var item in list)
                {
                    item.SendDate = sendTime;
                    item.BackDate = backTime;
                    item.Information = information;
                }

                Db.Updateable(list).ExecuteCommand();
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        public string GetTaskOrderType(string taskNo)
        {
            try
            {
                var task = Db.Queryable<LogTask>().First(m => m.TaskNo == taskNo);
                if (task == null)
                {
                    throw new Exception($"未查询到{taskNo}任务号的任务信息");
                }

                return task.OrderType;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
    }
}