CyclecountDetailApp.cs 20.5 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 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
using Infrastructure;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using WebRepository;

namespace WebApp
{
    /// <summary>
	/// 盘点明细表
	/// </summary>
    
    public partial class CyclecountDetailApp
    {
        private IUnitWork _unitWork;
        public IRepository<CyclecountDetail> _app;
        private static IHostingEnvironment _hostingEnvironment;
        ExcelHelper imp = new ExcelHelper(_hostingEnvironment);
        private BaseDBContext _context;
        public IRepository<TaskDetail> _apptd;
        public IRepository<Task> _appt;
        public IRepository<Inventory> _appi;

        public CyclecountDetailApp(IUnitWork unitWork, IRepository<Inventory> inventory, IRepository<CyclecountDetail> repository, IHostingEnvironment hostingEnvironment, IRepository<Task> task, IRepository<TaskDetail> taskDetail, BaseDBContext context)
        {
            _unitWork = unitWork;
            _app = repository;
            _hostingEnvironment = hostingEnvironment;
            _apptd = taskDetail;
            _appt = task;
            _context = context;
            _appi = inventory;
        }
        
        public CyclecountDetailApp SetLoginInfo(LoginInfo loginInfo)
        {
            _app._loginInfo = loginInfo;
            _appt._loginInfo = loginInfo;
            _apptd._loginInfo = loginInfo;
            _appi._loginInfo = loginInfo;
            return this;
        }
        
        public TableData Load(PageReq pageRequest, CyclecountDetail entity)
        {
            return _app.Load(pageRequest, entity);
        }

        public void Ins(CyclecountDetail entity)
        {
            _app.Add(entity);
        }

        public void BatchIns(List<CyclecountDetail> entitys)
        {
            _app.BatchAdd(entitys.ToArray());
        }

        public void Upd(CyclecountDetail entity)
        {
            _app.Update(entity);
        }

        public void DelByIds(int[] ids)
        {
            //删除盘点明细,更新库存的盘点状态
            foreach(var item in ids)
            {
                var detail = _app.FindSingle(u => u.Id == item);
                if (detail.Status == ReceiptHeaderStatus.回传)
                {
                    _app.Delete(detail);
                }
            }
        }
        
        public CyclecountDetail FindSingle(Expression<Func<CyclecountDetail, bool>> exp)
        {
            return _app.FindSingle(exp);
        }

        public IQueryable<CyclecountDetail> Find(Expression<Func<CyclecountDetail, bool>> exp)
        {
            return _app.Find(exp);
        }

        public Response ImportIn(IFormFile excelfile)
        {
            Response result = new Infrastructure.Response();
            List<CyclecountDetail> exp = imp.ConvertToModel<CyclecountDetail>(excelfile);
            string sErrorMsg = "";

            for (int i = 0; i < exp.Count; i++)
            {
                try
                {
                    CyclecountDetail e = exp[i];
                    e.Id = null;
                    _app.Add(e);
                }
                catch (Exception ex)
                {
                    sErrorMsg += "第" + (i + 2) + "行:" + ex.Message + "<br>";
                    result.Message = sErrorMsg;
                    break;
                }
            }
            if (sErrorMsg.Equals(string.Empty))
            {
                if (exp.Count == 0)
                {
                    sErrorMsg += "没有发现有效数据, 请确定模板是否正确, 或是否有填充数据!";
                    result.Message = sErrorMsg;
                }
                else
                {
                    result.Message = "导入完成";
                }
            }
            else
            {
                result.Status = false;
                result.Message = result.Message;
            }
            return result;
        }

        public TableData ExportData(CyclecountDetail entity)
        {
            return _app.ExportData(entity);
        }

        public TableData Query(CyclecountDetail entity)
        {
            var result = new TableData();
            var data = _app.Find(EntityToExpression<CyclecountDetail>.GetExpressions(entity));

            GetData(data, result);
            result.count = data.Count();

            return result;
        }

        public void GetData(IQueryable<CyclecountDetail> data, TableData result, PageReq pageRequest = null)
        {
            _app.GetData(data, result, pageRequest);
        }

        public string Insert(CyclecountDetail entity)
        {
            string str = "";
            try
            {
                string containerCode = entity.ContainerCode;
                string materialCode = entity.MaterialCode;

                if (_unitWork.IsExist<CyclecountDetail>(n => n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成 && n.MaterialCode == materialCode && n.ContainerCode == containerCode))
                {
                    return str += "物料||托盘:" + entity.MaterialCode + "||" + entity.ContainerCode + ":" + "已有该物料和托盘的盘点任务" + "<br>";
                }

                Inventory inventory = _unitWork.Find<Inventory>(n => n.MaterialCode == materialCode && n.ContainerCode == containerCode).FirstOrDefault();
                int count = _unitWork.Find<TaskDetail>(n => n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成 && n.ContainerCode == containerCode && !n.TaskType.Contains("C")).Count();
                Container container = _unitWork.Find<Container>(n => n.Code == containerCode).FirstOrDefault();

                if (inventory == null || container == null)
                {
                    str = "该托盘或物料不存在!";
                    return str;
                }

                if (container.IsLock == 0 && count == 0)
                {
                    if (inventory.TaskStatus == "idle")
                    {
                        List<Inventory> inventories = _unitWork.Find<Inventory>(n => n.ContainerCode == inventory.ContainerCode).ToList();
                        foreach (Inventory inv in inventories)
                        {
                            inventory.TaskStatus = "cyclecountLock";
                            _appi.UpdateByTracking(inventory);
                        }
                    }
                    entity.Status = ReceiptHeaderStatus.分配完成;
                    entity.SystemQty = inventory.Qty;
                    entity.CountedQty = 0;
                    _app.Add(entity);

                    str += AddTaskInfo(entity, inventory);


                    CyclecountHeader cycher = _unitWork.Find<CyclecountHeader>(n => n.Code == entity.Code).FirstOrDefault();
                    CyclecountDetail cycdelEnd = _unitWork.Find<CyclecountDetail>(n => n.Code == entity.Code).OrderBy(n => n.Status).FirstOrDefault();
                    if (cycdelEnd.Status >= entity.Status)
                    {
                        cycher.Status = (short)entity.Status;
                        _unitWork.Update(cycher);
                    }
                }
                else{
                    str = "该托盘状态不正确或有任务正在执行!";
                    return str;
                }
            }
            catch (Exception ex)
            {
                str += "物料:" + entity.MaterialCode + ":" + ex.Message + "<br>";
            }
            return str;
        }

        public string BatchInsert(List<CyclecountDetail> entitys)
        {
            string str = "";
            CyclecountHeader cycher = _unitWork.Find<CyclecountHeader>(n => n.Code == entitys.FirstOrDefault().Code).FirstOrDefault();
            foreach (var entity in entitys)
            {
                try
                {
                    string containerCode = entity.ContainerCode;
                    string materialCode = entity.MaterialCode;

                    if (_unitWork.IsExist<CyclecountDetail>(n => n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成 && n.MaterialCode == materialCode && n.ContainerCode == containerCode))
                    {
                        str += "物料||托盘:" + entity.MaterialCode + "||" + entity.ContainerCode + ":" + "已有该物料和托盘的盘点任务" + "<br>";
                        continue;
                    }

                    Inventory inventory = _unitWork.Find<Inventory>(n => n.MaterialCode == materialCode && n.ContainerCode == containerCode).FirstOrDefault();
                    int count = _unitWork.Find<TaskDetail>(n => n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成 && n.ContainerCode == containerCode && !n.TaskType.Contains("C")).Count();
                    Container container = _unitWork.Find<Container>(n => n.Code == containerCode).FirstOrDefault();

                    if (inventory == null || container == null)
                    {
                        str += "物料 || 托盘:" + entity.MaterialCode + " || " + entity.ContainerCode + ":" +"该托盘或物料不存在!"+ " < br > ";
                        continue;
                    }

                    if (container.IsLock == 0 && count == 0)
                    {
                        if (inventory.TaskStatus == "idle")
                        {
                            inventory.TaskStatus = "cyclecountLock";
                            _unitWork.Update(inventory);
                        }

                        entity.Status = ReceiptHeaderStatus.分配完成;
                        entity.SystemQty = inventory.Qty;
                        entity.CountedQty = 0;
                        _app.Add(entity);

                        str += AddTaskInfo(entity, inventory);
                    }
                    CyclecountDetail cycdelEnd = _unitWork.Find<CyclecountDetail>(n => n.Code == entity.Code).OrderBy(n => n.Status).FirstOrDefault();
                    if (cycdelEnd.Status >= entity.Status)
                    {
                        cycher.Status = (short)entity.Status;
                        _unitWork.Update(cycher);
                    }
                }
                catch (Exception ex)
                {
                    str += "物料 || 托盘:" + entity.MaterialCode + " || " + entity.ContainerCode + ":" + ex.Message + "<br>";
                }
            }
            return str;
        }

        public string AddTaskInfo(CyclecountDetail entity,Inventory inventory)
        {
            string str = "";
            using (var tran = _context.Database.BeginTransaction())
            {
                try
                {
                    Task task = new Task();
                    TaskDetail taskDetail = new TaskDetail();
                    CyclecountHeader cyclecountHeader = _unitWork.Find<CyclecountHeader>(n => n.Code == entity.Code).FirstOrDefault(); 
                    var tno = "";
                    int tcount = _unitWork.Find<Task>(n => n.OrderCode == entity.Code).Count();
                    if (tcount > 0)
                    {
                        var tasknum = _unitWork.Find<Task>(n => n.OrderCode == entity.Code).Select(a => a.TaskNo).First();
                        tno = tasknum;
                    }
                    else
                    {
                        tno = _app.GetTaskNo(TaskNo.盘点);
                        task.TaskNo = tno;
                        task.OrderCode = cyclecountHeader.Code;
                        task.BusinessType = BusinessType.出库_盘点出库;
                        task.FirstStatus = TaskStatus.待下发任务;
                        task.LastStatus = TaskStatus.待下发任务;
                        task.CreateTime = DateTime.Now;
                        _appt.Add(task);
                    }

                    if (inventory != null)
                    {
                        //任务明细状态更新
                        taskDetail.TaskType = TaskType.盘点;
                        taskDetail.ContainerQty = inventory.Qty;
                        taskDetail.TaskNo = tno;
                        taskDetail.OrderCode = entity.Code;
                        taskDetail.ContainerCode = entity.ContainerCode;

                        taskDetail.MaterialCode = entity.MaterialCode;
                        taskDetail.MaterialName = _unitWork.Find<Material>(n => n.Code == entity.MaterialCode).Select(a => a.Name).FirstOrDefault();
                        taskDetail.Batch = entity.Batch;
                        taskDetail.Lot = entity.Lot;
                        taskDetail.OderQty = inventory.Qty;
                        taskDetail.HadQty = 0;
                        Location loc = _unitWork.Find<Location>(n => n.Code == inventory.LocationCode && n.IsStop == false).FirstOrDefault();
                        taskDetail.SourceLocation = loc.Code;
                        taskDetail.DestinationLocation = loc.Code;
                        taskDetail.Roadway = _unitWork.Find<Location>(n => n.Code == inventory.LocationCode && n.IsStop == false).Select(a => a.Roadway).First(); ;
                        taskDetail.Priority = 0;
                        taskDetail.Status = TaskStatus.待下发任务;
                        taskDetail.CreateTime = DateTime.Now;
                        _apptd.Add(taskDetail);
                        //盘点明细状态更新
                        entity.Status = ReceiptHeaderStatus.分配完成;
                        entity.IsCreateTask = "true";
                        _app.UpdateByTracking(entity);

                        //建立容器出库任务
                        if (_unitWork.IsExist<TaskDetail>(n => (n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成) && n.MaterialCode == entity.MaterialCode && n.OrderCode == entity.Code))
                        {
                            int ptdcount = _unitWork.Find<TaskDetail>(n => (n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成) && n.ContainerCode == taskDetail.ContainerCode && n.TaskType == TaskType.容器出库).Count();
                            if (ptdcount < 1)
                            {
                                Task ptask = new Task();
                                TaskDetail ptaskDetail = new TaskDetail();
                                var taskNo = _app.GetTaskNo(TaskNo.查看容器出库);
                                ptask.TaskNo = taskNo;
                                ptask.OrderCode = taskNo;
                                ptask.BusinessType = BusinessType.出库_盘点出库;
                                ptask.FirstStatus = TaskStatus.待下发任务;
                                ptask.LastStatus = TaskStatus.待下发任务;
                                ptask.CreateTime = DateTime.Now;
                                _appt.Add(ptask);

                                ptaskDetail.TaskNo = taskNo;
                                ptaskDetail.OrderCode = taskNo;
                                ptaskDetail.TaskType = TaskType.容器出库;
                                ptaskDetail.ContainerCode = taskDetail.ContainerCode;
                                ptaskDetail.SourceLocation = taskDetail.SourceLocation;
                                ptaskDetail.DestinationLocation = taskDetail.DestinationLocation;
                                ptaskDetail.OderQty = 0;
                                ptaskDetail.ContainerQty = 0;
                                ptaskDetail.HadQty = 0;
                                ptaskDetail.Roadway = taskDetail.Roadway;
                                ptaskDetail.Station = taskDetail.Station;
                                ptaskDetail.Status = TaskStatus.待下发任务;
                                if (taskDetail.Priority != null)
                                {
                                    ptaskDetail.Priority = taskDetail.Priority;
                                }
                                else
                                {
                                    ptaskDetail.Priority = 0;
                                }
                                ptaskDetail.CreateTime = DateTime.Now;
                                _apptd.Add(ptaskDetail);
                            }
                        }
                    }

                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    str +="物料:"+ entity.MaterialCode + ":" + ex.Message +"<br>";
                }
            }
            return str;
        }

        public TableData FinshCyclecountApp(TaskDetail del)
        {
            TableData tab = new TableData
            {
                code = 200
            };
            try
            {
                Inventory inventory = _unitWork.Find<Inventory>(n => n.ContainerCode == del.ContainerCode).FirstOrDefault();
                CyclecountHeader cycher = _unitWork.Find<CyclecountHeader>(n => n.Code == del.OrderCode).FirstOrDefault();
                CyclecountDetail cycdel = _unitWork.Find<CyclecountDetail>(n => n.Status < ReceiptHeaderStatus.过账 && n.MaterialCode == del.MaterialCode && n.ContainerCode == del.ContainerCode && n.Code == del.OrderCode).FirstOrDefault();
                if (inventory.Qty == del.HadQty)
                {
                    TaskDetail detail = _unitWork.Find<TaskDetail>(n => n.Id == del.Id).FirstOrDefault();
                    detail.Status = TaskStatus.出库查看完成;
                    detail.HadQty = del.HadQty;
                    _unitWork.Update(detail);
                }
                else
                {
                    if (cycher != null && cycdel != null)
                    {
                        //更改记录
                        InventoryTransaction inventoryTransaction = new InventoryTransaction();
                        if (inventory != null)
                        {
                            inventoryTransaction.WarehouseType = inventory.WarehouseType;
                            inventoryTransaction.SourceCode = del.SourceCode;
                            inventoryTransaction.BillCode = del.OrderCode;
                            inventoryTransaction.Type = TransactionType.盘点;
                            inventoryTransaction.ContainerCode = inventory.ContainerCode;
                            inventoryTransaction.TaskSteps = inventory.TaskSteps;
                            inventoryTransaction.Position = inventory.Position;
                            inventoryTransaction.MaterialCode = inventory.MaterialCode;
                            inventoryTransaction.MaterialName = del.MaterialName;
                            inventoryTransaction.Batch = inventory.Batch;
                            inventoryTransaction.Lot = inventory.Lot;
                            inventoryTransaction.LocationCode = inventory.LocationCode;
                            inventoryTransaction.Qty = del.OderQty;
                            inventoryTransaction.TaskQty = del.HadQty;
                            inventoryTransaction.Status = inventory.Status;
                            inventoryTransaction.CreateBy = _appi._loginInfo.Account;
                            inventoryTransaction.CreateTime = DateTime.Now;
                            inventoryTransaction.UpdateTime = DateTime.Now;
                            _unitWork.Add(inventoryTransaction);

                            inventory.Qty = del.HadQty;
                            _appi.Update(inventory);

                            TaskDetail detail = _unitWork.Find<TaskDetail>(n => n.Id == del.Id).FirstOrDefault();
                            detail.Status = TaskStatus.出库查看完成;
                            detail.HadQty = del.HadQty;
                            _unitWork.Update(detail);
                        }
                    }
                }
                cycdel.Status = ReceiptHeaderStatus.过账;
                cycdel.CountedQty = del.HadQty;
                cycdel.GapQty = cycdel.SystemQty - del.HadQty;
                _app.Update(cycdel);

                CyclecountDetail cycdelEnd = _unitWork.Find<CyclecountDetail>(n => n.Code == cycher.Code).OrderBy(n => n.Status).FirstOrDefault();
                if (cycdelEnd.Status >= cycdel.Status)
                {
                    cycher.Status = (short)cycdel.Status;
                    _unitWork.Update(cycher);
                }
            }
            catch (Exception ex)
            {
                tab.code = 300;
                tab.msg += ex.Message;
            }
            return tab;
        }
    }
}