using System;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using WaterCloud.Code;
using SqlSugar;
using WaterCloud.DataBase;
using WaterCloud.Domain.OrderManagement;
using System.Collections;
using static Serenity.Web.PropertyItemsScript;
using WaterCloud.Service.SystemOrganize;
using System.Diagnostics;

namespace WaterCloud.Service.OrderManagement
{
    /// <summary>
    /// 创 建：超级管理员
    /// 日 期：2025-03-16 01:39
    /// 描 述：用户订单服务类
    /// </summary>
    public class OrderCustomerService : BaseService<OrderCustomerEntity>, IDenpendency
    {
        public UserService UserService { get; set; }

        public OrderCustomerService(ISqlSugarClient context) : base(context)
        {
        }
        #region 获取数据
        public async Task<List<OrderCustomerEntity>> GetList(string keyword = "")
        {
            var data = repository.IQueryable();
            if (!string.IsNullOrEmpty(keyword))
            {
                data = data.Where(a => a.F_Name.Contains(keyword)
                || a.F_ContactPerson.Contains(keyword));
            }

            //普通用户仅可查看自己的数据
            if (!(currentuser.IsAdmin || currentuser.IsBoss || currentuser.IsSuperAdmin || currentuser.IsSenior))
            {
                data = data.Where(oo => oo.F_CreatorUserId == currentuser.UserId);
            }

            return await data.Where(a => a.F_DeleteMark == false).OrderBy(a => a.F_CreatorTime, OrderByType.Desc).ToListAsync();
        }

        public async Task<List<OrderCustomerEntity>> GetLookList(string keyword = "")
        {
            var query = repository.IQueryable().Where(a => a.F_DeleteMark == false);
            if (!string.IsNullOrEmpty(keyword))
            {
                //此处需修改
                query = query.Where(a => a.F_Name.Contains(keyword)
                || a.F_ContactPerson.Contains(keyword));
            }
            //权限过滤
            query = GetDataPrivilege("a", "", query);

            //普通用户仅可查看自己的数据
            if (!(currentuser.IsAdmin || currentuser.IsBoss || currentuser.IsSuperAdmin || currentuser.IsSenior))
            {
                query = query.Where(oo => oo.F_CreatorUserId == currentuser.UserId);
            }

            var pageList = await query.OrderBy(a => a.F_CreatorTime, OrderByType.Desc).ToListAsync();
            var users = await UserService.GetUserList(string.Empty);
            pageList.ForEach(item =>
            {
                var user = users.Find(oo => oo.F_Id == item.F_CreatorUserId);
                item.F_CreatorUserId = user == null ? "未知用户" : user.F_RealName;
            });
            return pageList;
        }

        public async Task<List<OrderCustomerEntity>> GetLookList(SoulPage<OrderCustomerEntity> pagination, string keyword = "", string id = "")
        {
            var query = repository.IQueryable().Where(a => a.F_DeleteMark == false);
            if (!string.IsNullOrEmpty(keyword))
            {
                query = query.Where(a => a.F_Name.Contains(keyword)
                || a.F_ContactPerson.Contains(keyword));
            }
            if (!string.IsNullOrEmpty(id))
            {
                query = query.Where(a => a.F_Id == id);
            }
            //权限过滤
            query = GetDataPrivilege("a", "", query);

            if (!(currentuser.IsAdmin || currentuser.IsBoss || currentuser.IsSuperAdmin || currentuser.IsSenior))
            {
                query = query.Where(oo => oo.F_CreatorUserId == currentuser.UserId);
            }

            var pageList = await query.ToPageListAsync(pagination);
            var users = await UserService.GetUserList(string.Empty);
            pageList.ForEach(item =>
            {
                var user = users.Find(oo => oo.F_Id == item.F_CreatorUserId);
                item.F_CreatorUserId = user == null ? "未知用户" : user.F_RealName;
            });
            return pageList;
        }

        public async Task<OrderCustomerEntity> GetForm(string keyValue)
        {
            var data = await repository.FindEntity(keyValue);
            return data;
        }
        #endregion

        public async Task<OrderCustomerEntity> GetLookForm(string keyValue)
        {
            var data = await repository.FindEntity(keyValue);
            return GetFieldsFilterData(data);
        }

        #region 提交数据
        public async Task SubmitForm(OrderCustomerEntity entity, string keyValue)
        {
            if (string.IsNullOrEmpty(keyValue))
            {
                //初始值添加
                entity.F_DeleteMark = false;
                entity.Create();
                await repository.Insert(entity);
            }
            else
            {
                if (!(currentuser.IsAdmin || currentuser.IsBoss || currentuser.IsSuperAdmin || currentuser.IsSenior) &&
    currentuser.UserId != entity.F_CreatorUserId)
                {
                    return;
                }

                //此处需修改
                entity.Modify(keyValue);
                await repository.Update(entity);
            }
        }

        public async Task DeleteForm(string keyValue)
        {
            var ids = keyValue.Split(',');

            foreach (var id in ids)
            {
                var data = await repository.FindEntity(id);
                if (data == null)
                    return;
                if (!(currentuser.IsAdmin || currentuser.IsBoss || currentuser.IsSuperAdmin || currentuser.IsSenior) &&
    currentuser.UserId != data.F_CreatorUserId)
                    throw new Exception($"{data.F_Id} {data.F_Name}违规删除");
            }


            await repository.Delete(a => ids.Contains(a.F_Id.ToString()));
        }
        #endregion

    }
}
