username@email.com
2024-09-09 626943b5ba84ce44bc19f4c3b8e8e94638bec733
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using CoreCms.Net.Configuration;
using CoreCms.Net.IRepository.UnitOfWork;
using CoreCms.Net.IServices;
using CoreCms.Net.IServices.baifenbingfa;
using CoreCms.Net.Model.Entities;
using CoreCms.Net.Utility.Helper;
using Nest;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace CoreCms.Net.Services.baifenbingfa
{
    /// <summary>
    /// 
    /// </summary>
    public class BfbfComAPIService : IBfbfComAPIService
    {
        
        private readonly ICoreCmsUserServices _userServices;
        private readonly IUnitOfWork _unitOfWork;
        private readonly ICoreCmsSettingServices _coreCmsSettingServices;
        public   BfbfComAPIService(ICoreCmsUserServices userServices,IUnitOfWork unitOfWork,   ICoreCmsSettingServices coreCmsSettingServices)
        {
            _userServices = userServices;
            _unitOfWork = unitOfWork;
            _coreCmsSettingServices = coreCmsSettingServices;
        }
        public async Task<List<SearchUserData>> SearchUserData(string s)
        {
           var ListData= await  _unitOfWork.GetDbClient().Queryable<CoreCmsUser>().Where(x => x.isDelete == false).Select(x => new SearchUserData
            {
                Id = x.id,
                name = x.mobile /*+ "|" + x.userName*/ + "|" + x.nickName,
 
            }).WithCache(50).ToListAsync();
            if (string.IsNullOrEmpty(s))
            {
               return ListData;
            }
            return ListData.Where(x => x.name.Contains(s)).ToList();
        }
 
        public async Task<bool> IsDictionary(int UserId)
        {
            if (UserId == 0)
                return false;
           var user=await  _unitOfWork.GetDbClient().Queryable<CoreCmsUser>().Includes(x=>x.gradeInfo).Where(x=>x.id== UserId).WithCache(10).SingleAsync();
            if (user == null)
                return false;
            if(user.gradeInfo==null)
                return false;
            var allConfigs = await _coreCmsSettingServices.GetConfigDictionaries();
            var kc = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.DictionaryUserGroupKey);
            if (user.gradeInfo.valueKey == kc)
                return true;
            return false;
 
        }
 
        public async Task<List<CoreCmsGoodsCategory>> GetGoodsCategory()
        {
         return await   _unitOfWork.GetDbClient().Queryable<CoreCmsGoodsCategory>().Where(x=>x.isShow==true).WithCache(10).ToTreeAsync(x=>x.children, x=>x.parentId,0);
        }
 
 
        public  decimal CommandCustomizable(int count)
        {
            if (count > 9999)
                return 0;
            return count * 1;
        }
 
        public List<DistributorSort> orderedDistributors(List<DistributorSort> DataList, string latitude, string longitude)
        {
          
 
            GeoCoordinate inputLocation = new GeoCoordinate(double.Parse(longitude), double.Parse(latitude)); // 输入的经纬度
 
            var sortedDistributors = DataList.OrderBy(d => CalculateDistance(inputLocation, new GeoCoordinate(double.Parse(d.longitude), double.Parse(d.latitude))))
                                                 .ToList();
            return sortedDistributors;
        }
 
 
        /// <summary>
        /// 计算经纬达距离
        /// </summary>
        /// <param name="location1"></param>
        /// <param name="location2"></param>
        /// <returns></returns>
        private static double CalculateDistance(GeoCoordinate location1, GeoCoordinate location2)
        {
            const double earthRadiusKm = 6371;
            var dLat = ToRadians(location2.Latitude - location1.Latitude);
            var dLon = ToRadians(location2.Longitude - location1.Longitude);
 
            var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
                    Math.Cos(ToRadians(location1.Latitude)) * Math.Cos(ToRadians(location2.Latitude)) *
                    Math.Sin(dLon / 2) * Math.Sin(dLon / 2);
            var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
            var distance = earthRadiusKm * c;
 
            return distance;
        }
 
        private static double ToRadians(double degree)
        {
            return degree * Math.PI / 180;
        }
 
        /// <summary>
        /// 获取有效校园代理
        /// </summary>
        /// <param name="PIds"></param>
        /// <returns></returns>
        public async Task< List<DistributorSort>> GetDistributorsList(List<int> PIds)
        {
          var blist=  await  _unitOfWork.GetDbClient().Queryable<CoreCmsDistribution>().Where(x=>x.profession=="校园代理"&&x.verifyStatus== (int)GlobalEnumVars.DistributionVerifyStatus.VerifyYes).ToListAsync();
            List < DistributorSort > retlist=new List<DistributorSort>();            
            foreach (var item in blist)
            {
                if (string.IsNullOrEmpty(item.storeCoordinate) || item.storeCoordinate == ",")
                    //没有定位,不加入
                    continue;
                var ldinate=   item.storeCoordinate.Split(',');
                retlist.Add(   new DistributorSort()
                {
                    id = item.id,
                    latitude = ldinate[0],
                    longitude = ldinate[1],
                    name = item.schoolName + ":" + item.name
 
 
 
                });
            }
            return retlist;
        }
 
 
 
        public async Task< List< int>> GetOderProductsIDs(string[] OderIds)
        {
          var list=   await  _unitOfWork.GetDbClient().Queryable<CoreCmsOrder>().Where(x => OderIds.Contains(x.orderId))              
                .Select(x =>  new { items = SqlFunc.Subqueryable<CoreCmsOrderItem>().Where(it => it.orderId == x.orderId).ToList() }).ToListAsync();
            var li=new List<int>();
            foreach (var item in list)
            {
                li.AddRange(item.items.Select(x=>x.id).ToList());
 
 
            }
            return li;
        }
    }
}