<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Phoebe.Core.BL
{
    using SqlSugar;
    using Phoebe.Base.Framework;
    using Phoebe.Base.System;
    using Phoebe.Core.Entity;
    using Phoebe.Core.View;
    using Phoebe.Core.Utility;

    /// <summary>
    /// 搬运出库任务业务类
    /// </summary>
    public class CarryOutTaskBusiness : AbstractBusiness<CarryOutTask, string>, IBaseBL<CarryOutTask, string>
    {
        #region Function
        /// <summary>
        /// 生成临时搬运出库任务
        /// </summary>
        /// <param name="outTask"></param>
        /// <param name="position"></param>
        /// <param name="viceShelf"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        private CarryOutTask GenerateTempOutTask(StockOutTaskView outTask, PositionView position, bool viceShelf, SqlSugarClient db = null)
        {
            if (db == null)
                db = GetInstance();

            CarryOutTask task = new CarryOutTask();
            task.Id = Guid.NewGuid().ToString();
            task.Type = (int)CarryOutTaskType.Temp;
            task.StockOutTaskId = outTask.Id;

            // find store
            var store = db.Queryable<StoreView>().Single(r => r.PositionId == position.Id && r.Status == (int)EntityStatus.StoreIn);
            if (store != null)
            {
                task.StoreId = store.Id;
                task.StoreCount = store.StoreCount;
                task.MoveCount = store.StoreCount;
                task.StoreWeight = store.StoreWeight;
                task.MoveWeight = store.StoreWeight;
                task.Place = store.Place;
            }
            else
            {
                return null;
            }

            if (viceShelf)
                task.ShelfCode = position.ViceShelfCode;
            else
                task.ShelfCode = position.ShelfCode;
            task.PositionId = position.Id;

            task.CreateTime = DateTime.Now;
            task.Status = (int)EntityStatus.StockOutReady;

            return task;
        }

        private CarryInTask GenerateTempInTask(CarryOutTask outTask, SqlSugarClient db = null)
        {
            if (db == null)
                db = GetInstance();

            CarryInTask task = new CarryInTask();
            task.Id = Guid.NewGuid().ToString();
            task.CreateTime = DateTime.Now;
            task.CheckTime = DateTime.Now;
            //task.TaskCode = recordBusiness.GetNextSequence(db, "CarryInTask", entity.CreateTime);
            task.Status = (int)EntityStatus.StockInCheck;

            return null;
        }
        #endregion //Function

        #region Method
        /// <summary>
        /// 批量添加搬运出库任务
        /// 指同一出库任务下的搬运任务，并且生成相关临时搬运出库任务
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public (bool success, string errorMessage) Create(List<CarryOutTask> data)
        {
            var db = GetInstance();

            try
            {
                db.Ado.BeginTran();

                SequenceRecordBusiness recordBusiness = new SequenceRecordBusiness();
                var now = DateTime.Now;

                var stockOutTask = db.Queryable<StockOutTaskView>().InSingle(data.First().StockOutTaskId);

                // 设置出库的搬运任务信息
                foreach (var item in data)
                {
                    item.Id = Guid.NewGuid().ToString();
                    item.Type = (int)CarryOutTaskType.Out;
                    item.CreateTime = now;
                    item.TaskCode = recordBusiness.GetNextSequence(db, "CarryOutTask", item.CreateTime);

                    var store = db.Queryable<StoreView>().InSingle(item.StoreId);
                    item.PositionId = store.PositionId;
                    item.Place = store.Place;
                    item.Status = (int)EntityStatus.StockOutReady;

                    db.Insertable(item).ExecuteReturnEntity();
                }

                List<CarryOutTask> tempOutTasks = new List<CarryOutTask>();
                foreach (var carryOuTask in data)
                {
                    // 搬运出库任务的仓位
                    var currentPos = db.Queryable<PositionView>().InSingle(carryOuTask.PositionId);

                    // 找到该货物同一排的所有仓位
                    var positions = db.Queryable<PositionView>()
                        .Where(r => r.ShelfId == currentPos.ShelfId && r.Row == currentPos.Row && r.Layer == currentPos.Layer).ToList();

                    if (currentPos.ShelfCode == carryOuTask.ShelfCode)
                    {
                        var outPos = positions.Where(r => r.Depth < currentPos.Depth).ToList();
                        foreach (var pos in outPos)
                        {
                            if (tempOutTasks.Any(r => r.PositionId == pos.Id))
                                continue;

                            var tempTask = GenerateTempOutTask(stockOutTask, pos, false, db);
                            if (tempTask != null)
                                tempOutTasks.Add(tempTask);
                        }
                    }
                    else if (currentPos.ViceShelfCode == carryOuTask.ShelfCode)
                    {
                        var outPos = positions.Where(r => r.Depth > currentPos.Depth).ToList();
                        foreach (var pos in outPos)
                        {
                            if (tempOutTasks.Any(r => r.PositionId == pos.Id))
                                continue;

                            var tempTask = GenerateTempOutTask(stockOutTask, pos, false, db);
                            if (tempTask != null)
                                tempOutTasks.Add(tempTask);
                        }
                    }
                    else
                    {
                        continue;
                    }
                }

                foreach (var item in tempOutTasks)
                {
                    item.TaskCode = recordBusiness.GetNextSequence(db, "CarryOutTask", item.CreateTime);
                    db.Insertable(item).ExecuteReturnEntity();
                }

                db.Ado.CommitTran();
                return (true, "");
            }
            catch (Exception e)
            {
                db.Ado.RollbackTran();
                return (false, e.Message);
            }
        }

        /// <summary>
        /// 出库接单<commit_msg>insert temp carry out task.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Phoebe.Core.BL
{
    using SqlSugar;
    using Phoebe.Base.Framework;
    using Phoebe.Base.System;
    using Phoebe.Core.Entity;
    using Phoebe.Core.View;
    using Phoebe.Core.Utility;

    /// <summary>
    /// 搬运出库任务业务类
    /// </summary>
    public class CarryOutTaskBusiness : AbstractBusiness<CarryOutTask, string>, IBaseBL<CarryOutTask, string>
    {
        #region Function
        /// <summary>
        /// 生成临时搬运出库任务
        /// </summary>
        /// <param name="outTask"></param>
        /// <param name="position"></param>
        /// <param name="viceShelf"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        private CarryOutTask GenerateTempOutTask(StockOutTaskView outTask, PositionView position, bool viceShelf, SqlSugarClient db = null)
        {
            if (db == null)
                db = GetInstance();

            CarryOutTask task = new CarryOutTask();
            task.Id = Guid.NewGuid().ToString();
            task.Type = (int)CarryOutTaskType.Temp;
            task.StockOutTaskId = outTask.Id;

            // find store
            var store = db.Queryable<StoreView>().Single(r => r.PositionId == position.Id && r.Status == (int)EntityStatus.StoreIn);
            if (store != null)
            {
                task.StoreId = store.Id;
                task.StoreCount = store.StoreCount;
                task.MoveCount = store.StoreCount;
                task.StoreWeight = store.StoreWeight;
                task.MoveWeight = store.StoreWeight;
                task.TrayCode = store.TrayCode;
                task.PositionId = store.PositionId;
                task.Place = store.Place;
            }
            else
            {
                return null;
            }

            if (viceShelf)
                task.ShelfCode = position.ViceShelfCode;
            else
                task.ShelfCode = position.ShelfCode;         

            task.CreateTime = DateTime.Now;
            task.Status = (int)EntityStatus.StockOutReady;

            return task;
        }

        private CarryInTask GenerateTempInTask(CarryOutTask outTask, SqlSugarClient db = null)
        {
            if (db == null)
                db = GetInstance();

            CarryInTask task = new CarryInTask();
            task.Id = Guid.NewGuid().ToString();
            task.CreateTime = DateTime.Now;
            task.CheckTime = DateTime.Now;
            //task.TaskCode = recordBusiness.GetNextSequence(db, "CarryInTask", entity.CreateTime);
            task.Status = (int)EntityStatus.StockInCheck;

            return null;
        }
        #endregion //Function

        #region Method
        /// <summary>
        /// 批量添加搬运出库任务
        /// 指同一出库任务下的搬运任务，并且生成相关临时搬运出库任务
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public (bool success, string errorMessage) Create(List<CarryOutTask> data)
        {
            var db = GetInstance();

            try
            {
                db.Ado.BeginTran();

                SequenceRecordBusiness recordBusiness = new SequenceRecordBusiness();
                var now = DateTime.Now;

                var stockOutTask = db.Queryable<StockOutTaskView>().InSingle(data.First().StockOutTaskId);

                // 设置出库的搬运任务信息
                foreach (var item in data)
                {
                    item.Id = Guid.NewGuid().ToString();
                    item.Type = (int)CarryOutTaskType.Out;
                    item.CreateTime = now;
                    item.TaskCode = recordBusiness.GetNextSequence(db, "CarryOutTask", item.CreateTime);

                    var store = db.Queryable<StoreView>().InSingle(item.StoreId);
                    item.PositionId = store.PositionId;
                    item.Place = store.Place;
                    item.Status = (int)EntityStatus.StockOutReady;

                    db.Insertable(item).ExecuteReturnEntity();
                }

                // 检查是否有需要临时搬运的任务
                List<CarryOutTask> tempOutTasks = new List<CarryOutTask>();
                foreach (var carryOuTask in data)
                {
                    // 搬运出库任务的仓位
                    var currentPos = db.Queryable<PositionView>().InSingle(carryOuTask.PositionId);

                    // 找到该货物同一排的所有仓位
                    var positions = db.Queryable<PositionView>()
                        .Where(r => r.ShelfId == currentPos.ShelfId && r.Row == currentPos.Row && r.Layer == currentPos.Layer).ToList();

                    if (currentPos.ShelfCode == carryOuTask.ShelfCode)
                    {
                        var outPos = positions.Where(r => r.Depth < currentPos.Depth).ToList();
                        foreach (var pos in outPos)
                        {
                            if (tempOutTasks.Any(r => r.PositionId == pos.Id))
                                continue;

                            var tempTask = GenerateTempOutTask(stockOutTask, pos, false, db);
                            if (tempTask != null)
                                tempOutTasks.Add(tempTask);
                        }
                    }
                    else if (currentPos.ViceShelfCode == carryOuTask.ShelfCode)
                    {
                        var outPos = positions.Where(r => r.Depth > currentPos.Depth).ToList();
                        foreach (var pos in outPos)
                        {
                            if (tempOutTasks.Any(r => r.PositionId == pos.Id))
                                continue;

                            var tempTask = GenerateTempOutTask(stockOutTask, pos, true, db);
                            if (tempTask != null)
                                tempOutTasks.Add(tempTask);
                        }
                    }
                    else
                    {
                        continue;
                    }
                }

                // 添加临时搬运出库任务
                foreach (var item in tempOutTasks)
                {                    
                    item.TaskCode = recordBusiness.GetNextSequence(db, "CarryOutTask", item.CreateTime);
                    db.Insertable(item).ExecuteCommand();
                }

                // 设置库存状态
                foreach(var item in data)
                {
                    var store = db.Queryable<Store>().Single(r => r.Id == item.StoreId);
                    store.Status = (int)EntityStatus.StoreOutReady;
                    db.Updateable(store).ExecuteCommand();
                }
                foreach(var item in tempOutTasks)
                {
                    var store = db.Queryable<Store>().Single(r => r.Id == item.StoreId);
                    store.Status = (int)EntityStatus.StoreOutReady;
                    db.Updateable(store).ExecuteCommand();
                }

                db.Ado.CommitTran();
                return (true, "");
            }
            catch (Exception e)
            {
                db.Ado.RollbackTran();
                return (false, e.Message);
            }
        }

        /// <summary>
        /// 出库接单
{'fraction_non_alphanumeric': 0.10265165274246277, 'fraction_numerical': 0.0, 'mean_word_length': 1.266008230452675, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '360970', 'n_tokens_mistral': 3842, 'n_tokens_neox': 3645, 'n_words': 648}