BaseBll.cs 20.3 KB
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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
using HHECS.Model;
using HHECS.Model.BllModel;
using HHECS.Model.Entities;
using HHECS.Model.Enums;
using MySql.Data.MySqlClient;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace HHECS.Bll
{
    public abstract class BaseBll
    {
        public string ConnectStr { get; set; }

        protected IDbConnection GetConnection()
        {
            return new MySqlConnection(ConnectStr);
        }

        #region 实例方法

        /// <summary>
        /// 检查按钮权限
        /// </summary>
        /// <param name="menuOperations"></param>
        /// <param name="controls"></param>
        public void CheckPermission(List<MenuOperation> menuOperations, UIElementCollection controls)
        {
            foreach (var item in controls)
            {
                if (item is Button)
                {
                    Button temp = item as Button;
                    if (temp.Tag is String)
                    {
                        string perms = (string)temp.Tag;
                        if (menuOperations.Count(t => t.Perms == perms) > 0)
                        {
                            temp.Visibility = Visibility.Visible;
                        }
                        else
                        {
                            temp.Visibility = Visibility.Collapsed;
                        }
                    }
                    else
                    {
                        temp.Visibility = Visibility.Collapsed;
                    }
                }
            }
        }


        /// <summary>
        /// 组合一组权限和他的子权限
        /// </summary>
        /// <param name="menuOperations"></param>
        /// <param name="all"></param>
        public void Combine(List<MenuOperation> menuOperations, List<MenuOperation> all)
        {
            foreach (var item in menuOperations)
            {
                item.Children.AddRange(all.FindAll(t => t.ParentId == item.Id));
                if (item.Children.Count > 0)
                {
                    Combine(item.Children, all);
                }
            }
        }

        /// <summary>
        /// 递归查出一组权限下的所有子权限Id
        /// </summary>
        /// <param name="menuOperations"></param>
        /// <param name="all"></param>
        /// <param name="ids"></param>
        public void GetMenuOperationIds(List<MenuOperation> menuOperations, List<MenuOperation> all, List<int> ids)
        {
            foreach (var item in menuOperations)
            {
                //item.Children.AddRange();
                ids.AddRange(all.FindAll(t => t.ParentId == item.Id).Select(t => t.Id.Value).ToList());
                if (all.FindAll(t => t.ParentId == item.Id).Count > 0)
                {
                    GetMenuOperationIds(all.FindAll(t => t.ParentId == item.Id), all, ids);
                }
            }
        }

        /// <summary>
        /// 发送LED
        /// </summary>
        /// <param name="station"></param>
        /// <param name="task"></param>
        /// <param name="LEDExcute"></param>
        public void SendLED(Equipment station, TaskEntity task, LEDExcute LEDExcute)
        {                
             LEDExcute.LedQueue.Enqueue(AppSession.Bll.GetTaskDetailsForLED(task));
        }

        #endregion

        #region Requst

        /// <summary>
        /// 这里用同步的方式,因为我们是在定时器线程中调用的,不影响UI线程
        /// </summary>
        /// <param name="list"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public BllResult FormPost(List<KeyValuePair<string, string>> list, WMSUrls url, HttpClient client, List<DictDetail> urls)
        {
            try
            {
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                string requestUri = urls.First(t => t.Code == url.ToString()).Value;
                HttpContent content = GetFormUrlEncodedContent(list);
                var ret = client.PostAsync(requestUri, content).Result;
                if (ret.IsSuccessStatusCode)
                {
                    ret.EnsureSuccessStatusCode();
                    string temp = ret.Content.ReadAsStringAsync().Result;
                    //LogExecute.WriteInfoLog($"请求WMS接口{url.ToString()}成功,返回值:{temp}");
                    var b = JsonConvert.DeserializeObject<ResponseModel<Object>>(temp);
                    if (b.Code == "200")
                    {
                        if (url != WMSUrls.Heartbeat)
                        {
                            Logger.Log($"请求WMS接口{url.ToString()}成功,返回值:{temp}", LogLevel.Success);
                        }
                        return BllResultFactory.Sucess(null, b.Msg);
                    }
                    else
                    {
                        Logger.Log($"请求WMS接口{url.ToString()}失败:{b.Msg}", LogLevel.Error);
                        return BllResultFactory.Error(null, b.Msg);
                    }
                }
                else
                {
                    Logger.Log($"请求WMS接口{url.ToString()}失败:{ret.StatusCode}", LogLevel.Error);
                    return BllResultFactory.Error(null, $"请求WMS失败,请检查网络连接,详情:{ret.StatusCode}");
                }
            }
            catch (Exception ex)
            {
                Logger.Log($"请求WMS接口{url.ToString()}失败:{ex.Message}", LogLevel.Error);
                return BllResultFactory.Error(null, $"请求WMS失败,请检查网络连接或者重启本程序,详情:{ex.Message}");
            }
        }

        /// <summary>
        /// 这里用同步的方式,因为我们是在定时器线程中调用的,不影响UI线程
        /// </summary>
        /// <param name="list"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public BllResult<T> FormPost<T>(List<KeyValuePair<string, string>> list, WMSUrls url, HttpClient client, List<DictDetail> urls)
        {
            try
            {
                var ret = client.PostAsync(urls.First(t => t.Code == url.ToString()).Value, GetFormUrlEncodedContent(list)).Result;
                if (ret.IsSuccessStatusCode)
                {
                    ret.EnsureSuccessStatusCode();
                    string temp = ret.Content.ReadAsStringAsync().Result;
                    var b = JsonConvert.DeserializeObject<ResponseModel<T>>(temp);
                    if (b.Code == "200")
                    {
                        return BllResultFactory<T>.Sucess(b.Data, b.Msg);
                    }
                    else
                    {
                        return BllResultFactory<T>.Error(default(T), b.Msg);
                    }
                }
                else
                {
                    return BllResultFactory<T>.Error(default(T), "请求WMS失败,HTTP响应失败,请检查网络连接");
                }
            }
            catch (Exception ex)
            {
                return BllResultFactory<T>.Error(default(T), $"请求WMS失败,原因:{ex.Message}");
            }
        }

        /// <summary>
        /// 这里用异步的方式,UI线程中请使用这个方法
        /// </summary>
        /// <param name="list"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public async Task<BllResult> FormPostAsync(List<KeyValuePair<string, string>> list, WMSUrls url, HttpClient client, List<DictDetail> urls)
        {
            try
            {
                var ret = await client.PostAsync(urls.First(t => t.Code == url.ToString()).Value, GetFormUrlEncodedContent(list));
                if (ret.IsSuccessStatusCode)
                {
                    ret.EnsureSuccessStatusCode();
                    string temp = ret.Content.ReadAsStringAsync().Result;
                    var b = JsonConvert.DeserializeObject<ResponseModel<Object>>(temp);
                    if (b.Code == "200")
                    {
                        return BllResultFactory.Sucess(null, b.Msg);
                    }
                    else
                    {
                        return BllResultFactory.Error(null, b.Msg);
                    }
                }
                else
                {
                    return BllResultFactory.Error(null, "请求WMS失败,请检查网络连接");
                }
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error(null, "请求WMS失败,请检查网络连接或者重启本程序");
            }
        }

        public HttpContent GetFormUrlEncodedContent(List<KeyValuePair<string, string>> keyValuePair)
        {
            HttpContent content = new FormUrlEncodedContent(keyValuePair);
            content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/x-www-form-urlencoded");
            return content;
        }

        #endregion

        #region 基础数据

        public abstract BllResult<User> GetUserWithRoles(string userName, string password, int warehouseId, string warehouseCode, HttpClient client, List<DictDetail> urls);
        public abstract BllResult<List<MenuOperation>> FindMenuOperation(List<Role> roles);
        public abstract BllResult<List<MenuOperation>> GetAllMenuOperation();
        public abstract BllResult<MenuOperation> SaveMenuOperation(MenuOperation currentMenuOperation);
        public abstract BllResult UpdateMenuOperation(MenuOperation currentMenuOperation);
        public abstract BllResult DeleteMenuOperationByIds(List<int> ids);
        public abstract BllResult<List<Role>> GetAllRole();
        public abstract BllResult<Role> GetRoleById(int value);
        public abstract BllResult<List<Config>> GetAllConfig();
        public abstract BllResult<Role> InsertRoleAndMenuOperations(Role currentRole, List<MenuOperation> list);
        public abstract BllResult<Role> UpdateRoleAndMenuOperations(Role currentRole, List<MenuOperation> list);
        public abstract BllResult<List<User>> GetUserByCondition(string sql);
        public abstract BllResult SetUserDisable(List<int?> list, int enable);
        public abstract BllResult<User> SaveUserWithRoles(User currentUser, List<int> RoleIds);
        public abstract BllResult<User> UpdateUserWithRoles(User currentUser, List<int> RoleIds);
        public abstract BllResult<Dict> GetDictWithDetails(string v);
        public abstract BllResult DeleteConfigByIds(List<int> list);
        public abstract BllResult DeleteRoleByIds(List<int> list);
        public abstract BllResult DeleteDictByIds(List<int> list);
        public abstract BllResult DeleteEuipmentTypeByIds(List<int> list);
        public abstract BllResult DeleteEquipmentTypePropTemplateByIds(List<int> list);
        public abstract BllResult DeleteEuipmentByIds(List<int> ids);
        public abstract BllResult SyncEquipmentProp(int? id);
        public abstract BllResult CopyEquipment(EquipmentType equipment);

        /// <summary>
        /// 根据索引和巷道获取堆垛机接出站台
        /// </summary>
        /// <param name="station"></param>
        /// <returns></returns>
        public abstract BllResult<Equipment> GetOutStationByIndex(int? station, int roadWay);

        /// <summary>
        /// 根据索引和巷道获取堆垛机接入站台
        /// </summary>
        /// <param name="station"></param>
        /// <returns></returns>
        public abstract BllResult<Equipment> GetInStationByIndex(int? station, int roadWay);

        /// <summary>
        /// 根据站台的软件索引获取站台
        /// </summary>
        /// <param name="stationIndex"></param>
        /// <returns></returns>
        public abstract BllResult<Equipment> GetStationByIndex(int? stationIndex);

        #endregion

        #region Job

        /// <summary>
        /// 执行一次指定的job
        /// </summary>
        /// <param name="value"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        public abstract Task<BllResult> ExcuteJobAsync(int value, HttpClient client);

        #endregion

        #region Data

        public abstract BllResult<List<T>> GetCommonModelByCondition<T>(string v);
        public abstract Task<BllResult<List<T>>> GetCommonModelByConditionAsync<T>(string v);
        public abstract BllResult<List<T>> GetCommonModelByCondition<T>(string v, IDbConnection connection, IDbTransaction tran);
        public abstract Task<BllResult<List<T>>> GetCommonModelByConditionAsync<T>(string v, IDbConnection connection, IDbTransaction tran);

        public abstract BllResult<int> GetCommonModelCount<T>(string sql);
        public abstract BllResult<int> GetCommonModelCount<T>(string sql, IDbConnection connection, IDbTransaction tran);

        public abstract BllResult<int?> SaveCommonModel<T>(T model);
        public abstract Task<BllResult<int?>> SaveCommonModelAsync<T>(T model);
        public abstract BllResult<int?> SaveCommonModel<T>(T model, IDbConnection connection, IDbTransaction tran);
        public abstract Task<BllResult<int?>> SaveCommonModelAsync<T>(T model, IDbConnection connection, IDbTransaction tran);

        public abstract BllResult UpdateCommonModel<T>(T model);
        public abstract Task<BllResult> UpdateCommonModelAsync<T>(T model);
        public abstract BllResult UpdateCommonModel<T>(T model, IDbConnection connection, IDbTransaction tran);
        public abstract Task<BllResult> UpdateCommonModelAsync<T>(T model, IDbConnection connection, IDbTransaction tran);

        public abstract BllResult DeleteCommonModelByIds<T>(List<int> list);
        public abstract Task<BllResult> DeleteCommonModelByIdsAsync<T>(List<int> list);
        public abstract BllResult DeleteCommonModelByIds<T>(List<int> list, IDbConnection connection, IDbTransaction tran);
        public abstract Task<BllResult> DeleteCommonModelByIdsAsync<T>(List<int> list, IDbConnection connection, IDbTransaction tran);

        public abstract BllResult<List<T>> GetCommonModeByPageCondition<T>(int pageNumber, int pageSize, string condition, string orderBy);
        public abstract Task<BllResult<List<T>>> GetCommonModeByPageConditionAsync<T>(int pageNumber, int pageSize, string condition, string orderBy);
        public abstract BllResult<List<T>> GetCommonModeByPageCondition<T>(IDbConnection connection, IDbTransaction tran,int pageNumber, int pageSize, string condition, string orderBy);
        public abstract Task<BllResult<List<T>>> GetCommonModeByPageConditionAsync<T>(IDbConnection connection, IDbTransaction tran,int pageNumber, int pageSize, string condition, string orderBy);


        public abstract BllResult<int> ExcuteCommonSqlForInsertOrUpdate(string sql);
        public abstract Task<BllResult<int>> ExcuteCommonSqlForInsertOrUpdateAsync(string sql);
        public abstract BllResult<int> ExcuteCommonSqlForInsertOrUpdate(string sql, IDbConnection connection, IDbTransaction tran);
        public abstract Task<BllResult<int>> ExcuteCommonSqlForInsertOrUpdateAsync(string sql, IDbConnection connection, IDbTransaction tran);

        public abstract BllResult<int> ExcuteCommonSqlForInsertOrUpdate(string sql, object param);
        public abstract Task<BllResult<int>> ExcuteCommonSqlForInsertOrUpdateAsync(string sql, object param);
        public abstract BllResult<int> ExcuteCommonSqlForInsertOrUpdate(string sql, object param, IDbConnection connection, IDbTransaction tran);
        public abstract Task<BllResult<int>> ExcuteCommonSqlForInsertOrUpdateAsync(string sql, object param, IDbConnection connection, IDbTransaction tran);

        public abstract BllResult<List<T>> GetCommonModelByConditionWithZero<T>(string v);
        public abstract Task<BllResult<List<T>>> GetCommonModelByConditionWithZeroAsync<T>(string v);
        public abstract BllResult<List<T>> GetCommonModelByConditionWithZero<T>(string v, IDbConnection connection, IDbTransaction tran);
        public abstract Task<BllResult<List<T>>> GetCommonModelByConditionWithZeroAsync<T>(string v, IDbConnection connection, IDbTransaction tran);

        #endregion

        #region Task

        public abstract BllResult SendTaskToWCS(int id, HttpClient client, List<DictDetail> urls);
        public abstract BllResult DeleteTask(int id);
        public abstract BllResult UpdateLocationStatus(string locationCode, int warehouseId, string newStatus);
        public abstract BllResult HeartBeat();
        public abstract BllResult CompleteTask(string taskId, HttpClient client, List<DictDetail> urls);
        public abstract BllResult EmptyOutHandle(TaskEntity task, HttpClient client, List<DictDetail> urls);
        public abstract BllResult TaskDoubleInHandle(string flag, String location, int taskId, int warehouseId);
        public abstract BllResult<TaskEntity> CreatTask(string palletCode, int station, string sourceLocation, string destinationLocation, int status, int type, int priority, string userCode, int warehouseId, string warehouseCode);
        public abstract BllResult<TaskEntity> CheckLocationForCreateTaskIn(String code, int warehouseId);
        public abstract BllResult<List<Location>> GetAllLocations(String containerCode, string row, string column, string layer, string roadway, string status, string code);
        public abstract BllResult TaskForkErrorHandle(int id, HttpClient client, List<DictDetail> urls);

        /// <summary>
        /// 根据托盘获取未完成任务
        /// </summary>
        /// <param name="palletCode"></param>
        /// <returns></returns>
        public abstract BllResult<TaskEntity> GetTaskUncompleteByPalletCode(string palletCode);

        /// <summary>
        /// 设置任务状态
        /// </summary>
        /// <param name="taskId"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public abstract BllResult SetTaskStatus(int taskId, int status);

        /// <summary>
        /// 请求WMS分配地址
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public abstract BllResult<T> GetDestinationLocation<T>(int id, int height, HttpClient client, List<DictDetail> urls);


        /// <summary>
        /// 全自动生成出库
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="station">站台</param>
        /// <param name="amount">托盘数量</param>
        /// <param name="client"></param>
        /// <param name="urls"></param>
        /// <returns></returns>
        public abstract BllResult<T> AutoShipment<T>(string station, HttpClient client, List<DictDetail> urls);

        /// <summary>
        /// 电梯口自动入库
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="client"></param>
        /// <param name="urls"></param>
        /// <returns></returns>
        public abstract BllResult<T> AutoReceipCache<T>(HttpClient client, List<DictDetail> urls);

        /// <summary>
        /// 分选机自动入库
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="client"></param>
        /// <param name="urls"></param>
        /// <returns></returns>
        public abstract BllResult<T> AutoReceipTray<T>(HttpClient client, List<DictDetail> urls);

        /// <summary>
        /// 模组下线
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="client"></param>
        /// <param name="urls"></param>
        public abstract BllResult<T> Produced<T>(HttpClient client, List<DictDetail> urls);

        /// <summary>
        /// 创建 空托盘入库 任务
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="ContaionCode">托盘编码</param>
        /// <param name="client"></param>
        /// <param name="urls"></param>
        /// <returns></returns>
        public abstract BllResult<T> ReceiptAirflare<T>(string ContaionCode, string containerType, HttpClient client, List<DictDetail> urls);

        public abstract string GetTaskDetailsForLED(TaskEntity task); 
        #endregion

    }
}