public Result PauseManufactureCheck([FromBody] ManufactureOpTask opTask)
        {
            var tId        = opTask.TaskId;
            var account    = opTask.Account;
            var gId        = opTask.GId;
            var processors = ServerConfig.ApiDb.Query <Processor>(
                "SELECT a.Id, b.Account FROM `manufacture_processor` a " +
                "JOIN `accounts` b ON a.ProcessorId = b.Id " +
                $"WHERE {(gId == 0 ? "" : "GroupId = @gId AND ")}b.Account = @account AND a.`MarkedDelete` = 0;", new { account, gId });

            if (!processors.Any())
            {
                return(Result.GenError <DataResult>(Error.ManufactureProcessorNotExist));
            }

            var sql =
                "SELECT b.*, a.* FROM manufacture_plan_task a " +
                "LEFT JOIN `manufacture_plan` b ON a.PlanId = b.Id " +
                "WHERE a.State IN @state AND a.Person IN @pId AND a.MarkedDelete = 0 AND a.IsCheck = 1 ORDER BY a.`TotalOrder` LIMIT 1;";
            ManufacturePlanTask task = null;

            foreach (var validState in ValidStates)
            {
                task = ServerConfig.ApiDb.Query <ManufacturePlanTask>(sql, new { state = validState.Value, pId = processors.Select(x => x.Id) })
                       .FirstOrDefault();
                if (task != null)
                {
                    break;
                }
            }

            if (task != null)
            {
                if (task.Id != tId)
                {
                    return(Result.GenError <DataResult>(Error.ManufactureTaskStateError));
                }
                if (task.State != ManufacturePlanTaskState.Checking)
                {
                    return(Result.GenError <DataResult>(Error.ManufactureTaskStateError));
                }

                var now     = DateTime.Now;
                var oldTask = (ManufacturePlanTask)task.Clone();
                task.State     = ManufacturePlanTaskState.Pause;
                task.PauseTime = now.NoSecond();
                var totalSecond = (int)(task.PauseTime - task.ActualStartTime).TotalSeconds;
                task.ActualHour += totalSecond / 3600;
                task.ActualMin  += (totalSecond - task.ActualHour * 3600) / 60;
                if (oldTask.HaveChange(task, out var change))
                {
                    change.Time     = now;
                    change.Account  = account;
                    change.PlanId   = task.PlanId;
                    change.IsAssign = true;
                    change.TaskId   = task.TaskId;
                    change.ItemId   = task.Id;
                    change.Type     = ManufactureLogType.PauseTask;
                }

                ServerConfig.ApiDb.Execute("UPDATE manufacture_plan_task SET `State` = @State, `PauseTime` = @PauseTime, " +
                                           "`ActualHour` = @ActualHour, `ActualMin` = @ActualMin WHERE `Id` = @Id;", task);

                ManufactureLog.AddLog(new List <ManufactureLog> {
                    change
                });
                return(Result.GenError <DataResult>(Error.Success));
            }

            return(Result.GenError <DataResult>(Error.ManufactureNoTask));
        }
Example #2
0
        public Result FinishManufactureTask([FromBody] ManufactureOpTask opTask)
        {
            var tId        = opTask.TaskId;
            var account    = opTask.Account;
            var gId        = opTask.GId;
            var processors =
                ServerConfig.ApiDb.Query <Processor>("SELECT a.Id, b.Account FROM `manufacture_processor` a JOIN `accounts` b ON a.ProcessorId = b.Id " +
                                                     $"WHERE b.Account = @account AND a.`MarkedDelete` = 0 {(gId == 0 ? "" : " AND GroupId = @gId")};", new { account, gId });

            if (!processors.Any())
            {
                return(Result.GenError <DataResult>(Error.ManufactureProcessorNotExist));
            }

            var sql =
                "SELECT b.*, a.* FROM manufacture_plan_task a " +
                "LEFT JOIN `manufacture_plan` b ON a.PlanId = b.Id " +
                "WHERE a.State IN @state AND a.Person IN @pId AND a.MarkedDelete = 0 AND a.IsCheck = 0 ORDER BY a.`TotalOrder` LIMIT 1;";
            ManufacturePlanTask task = null;

            foreach (var validState in ValidStates)
            {
                task = ServerConfig.ApiDb.Query <ManufacturePlanTask>(sql, new { state = validState.Value, pId = processors.Select(x => x.Id) })
                       .FirstOrDefault();
                if (task != null)
                {
                    break;
                }
            }

            if (task != null)
            {
                if (task.Id != tId)
                {
                    return(Result.GenError <DataResult>(Error.ManufactureTaskStateError));
                }
                if (task.State != ManufacturePlanTaskState.Doing)
                {
                    return(Result.GenError <DataResult>(Error.ManufactureTaskStateError));
                }

                var now     = DateTime.Now;
                var changes = new List <ManufactureLog>();
                var oldTask = (ManufacturePlanTask)task.Clone();
                task.State         = ManufacturePlanTaskState.Done;
                task.ActualEndTime = now;

                var totalSecond = (int)(task.ActualEndTime - task.ActualStartTime).TotalSeconds;
                var totalHour   = totalSecond / 3600;
                task.ActualHour += totalHour;
                task.ActualMin  += (totalSecond - totalHour * 3600) / 60;

                //if (task.PauseTime != default(DateTime))
                //{
                //    var totalSecond = (int)(task.PauseTime - task.ActualStartTime).TotalSeconds;
                //    var totalHour = totalSecond / 3600;
                //    task.ActualHour += totalHour;
                //    task.ActualMin += (totalSecond - totalHour * 3600) / 60;
                //    task.PauseTime = default(DateTime);
                //}
                //else
                //{
                //    var totalSecond = (int)(task.ActualEndTime - task.ActualStartTime).TotalSeconds;
                //    task.ActualHour += totalSecond / 3600;
                //    task.ActualMin += (totalSecond - task.ActualHour * 3600) / 60;
                //}
                if (oldTask.HaveChange(task, out var change))
                {
                    change.Time     = now;
                    change.Account  = account;
                    change.PlanId   = task.PlanId;
                    change.IsAssign = true;
                    change.TaskId   = task.TaskId;
                    change.ItemId   = task.Id;
                    change.Type     = ManufactureLogType.FinishTask;
                    changes.Add(change);
                }
                var tasks = new List <ManufacturePlanTask> {
                    task
                };
                sql =
                    "SELECT * FROM `manufacture_plan_task` WHERE PlanId = @PlanId AND Relation = @Order AND IsCheck = 1 AND `State` = @state;";
                var checkTasks = ServerConfig.ApiDb.Query <ManufacturePlanTask>(sql, new { task.PlanId, task.Order, state = ManufacturePlanTaskState.Wait });

                if (checkTasks != null && checkTasks.Any())
                {
                    foreach (var checkTask in checkTasks)
                    {
                        var oldCheckTask = (ManufacturePlanTask)checkTask.Clone();
                        checkTask.State = ManufacturePlanTaskState.WaitCheck;
                        if (oldCheckTask.HaveChange(checkTask, out var changeCheck))
                        {
                            changeCheck.Time     = now;
                            changeCheck.Account  = account;
                            changeCheck.PlanId   = checkTask.PlanId;
                            changeCheck.IsAssign = true;
                            changeCheck.TaskId   = checkTask.TaskId;
                            changeCheck.ItemId   = checkTask.Id;
                            changeCheck.Type     = ManufactureLogType.TaskUpdate;
                            changes.Add(changeCheck);
                        }
                        tasks.Add(checkTask);
                    }
                }

                ServerConfig.ApiDb.Execute("UPDATE manufacture_plan_task SET `MarkedDateTime` = @MarkedDateTime, `State` = @State, `ActualEndTime` = @ActualEndTime, " +
                                           "`ActualHour` = @ActualHour, `ActualMin` = @ActualMin WHERE `Id` = @Id;", tasks);

                ManufactureLog.AddLog(changes);
                return(Result.GenError <DataResult>(Error.Success));
            }

            return(Result.GenError <DataResult>(Error.ManufactureNoTask));
        }
        public Result StartManufactureCheck([FromBody] ManufactureOpTask opTask)
        {
            var tId     = opTask.TaskId;
            var account = opTask.Account;
            var gId     = opTask.GId;

            var processors = ServerConfig.ApiDb.Query <Processor>(
                "SELECT a.Id, b.Account FROM `manufacture_processor` a " +
                "JOIN `accounts` b ON a.ProcessorId = b.Id " +
                $"WHERE {(gId == 0 ? "" : "GroupId = @gId AND ")}b.Account = @account AND a.`MarkedDelete` = 0;", new { account, gId });

            if (!processors.Any())
            {
                return(Result.GenError <DataResult>(Error.ManufactureProcessorNotExist));
            }

            var sql =
                "SELECT b.*, a.* FROM manufacture_plan_task a " +
                "LEFT JOIN `manufacture_plan` b ON a.PlanId = b.Id " +
                "WHERE a.State IN @state AND a.Person IN @pId AND a.MarkedDelete = 0 AND a.IsCheck = 1 ORDER BY a.`TotalOrder` LIMIT 1;";
            ManufacturePlanTask task = null;

            foreach (var validState in ValidStates)
            {
                task = ServerConfig.ApiDb.Query <ManufacturePlanTask>(sql, new { state = validState.Value, pId = processors.Select(x => x.Id) })
                       .FirstOrDefault();
                if (task != null)
                {
                    break;
                }
            }

            if (task != null)
            {
                if (task.Id != tId)
                {
                    return(Result.GenError <DataResult>(Error.ManufactureTaskStateError));
                }
                if (task.State != ManufacturePlanTaskState.WaitCheck && task.State != ManufacturePlanTaskState.Pause)
                {
                    return(Result.GenError <DataResult>(Error.ManufactureTaskStateError));
                }

                var changes = new List <ManufactureLog>();
                var now     = DateTime.Now;
                var oldTask = (ManufacturePlanTask)task.Clone();
                task.State = ManufacturePlanTaskState.Checking;

                if (task.FirstStartTime == default(DateTime))
                {
                    task.FirstStartTime = now;
                }

                if (task.IsRedo)
                {
                    task.RedoCount++;
                }
                task.ActualStartTime = now.NoSecond();
                if (oldTask.HaveChange(task, out var change))
                {
                    change.Time     = now;
                    change.Account  = account;
                    change.PlanId   = task.PlanId;
                    change.IsAssign = true;
                    change.TaskId   = task.TaskId;
                    change.ItemId   = task.Id;
                    change.Type     = ManufactureLogType.StartTask;
                    changes.Add(change);
                }
                if (!oldTask.IsCheckItem)
                {
                    task.IsCheckItem = true;
                    var manufacturePlanCheckItems =
                        ServerConfig.ApiDb.Query <ManufacturePlanCheckItem>("SELECT * FROM `manufacture_check_item` WHERE CheckId = @CheckId AND MarkedDelete = 0;", new { task.CheckId });

                    var createUserId   = Request.GetIdentityInformation();
                    var markedDateTime = DateTime.Now;
                    if (manufacturePlanCheckItems.Any())
                    {
                        foreach (var manufactureCheckItem in manufacturePlanCheckItems)
                        {
                            manufactureCheckItem.CreateUserId = createUserId;
                            manufactureCheckItem.PlanId       = oldTask.PlanId;
                            manufactureCheckItem.ItemId       = oldTask.Id;
                        }
                        ServerConfig.ApiDb.Execute(
                            "INSERT INTO manufacture_plan_check_item (`CreateUserId`, `PlanId`, `ItemId`, `Item`, `Method`) " +
                            "VALUES (@CreateUserId, @PlanId, @ItemId, @Item, @Method);",
                            manufacturePlanCheckItems);

                        changes.Add(new ManufactureLog
                        {
                            Time     = markedDateTime,
                            Account  = createUserId,
                            PlanId   = task.PlanId,
                            IsAssign = true,
                            TaskId   = task.TaskId,
                            ItemId   = task.Id,
                            Type     = ManufactureLogType.CheckAssigned
                        });
                    }
                }

                ServerConfig.ApiDb.Execute("UPDATE manufacture_plan_task SET `State` = @State, `IsCheckItem` = @IsCheckItem, `FirstStartTime` = @FirstStartTime, " +
                                           "`ActualStartTime` = @ActualStartTime, `RedoCount` = @RedoCount WHERE `Id` = @Id;", task);
                ManufactureLog.AddLog(changes);
                return(Result.GenError <DataResult>(Error.Success));
            }

            return(Result.GenError <DataResult>(Error.ManufactureNoTask));
        }
Example #4
0
        public Result StartManufactureTask([FromBody] ManufactureOpTask opTask)
        {
            var tId        = opTask.TaskId;
            var account    = opTask.Account;
            var gId        = opTask.GId;
            var processors =
                ServerConfig.ApiDb.Query <Processor>("SELECT a.Id, b.Account FROM `manufacture_processor` a JOIN `accounts` b ON a.ProcessorId = b.Id " +
                                                     $"WHERE b.Account = @account AND a.`MarkedDelete` = 0 {(gId == 0 ? "" : " AND GroupId = @gId")};", new { account, gId });

            if (!processors.Any())
            {
                return(Result.GenError <DataResult>(Error.ManufactureProcessorNotExist));
            }

            var sql =
                "SELECT b.*, a.* FROM manufacture_plan_task a " +
                "LEFT JOIN `manufacture_plan` b ON a.PlanId = b.Id " +
                "WHERE a.State IN @state AND a.Person IN @pId AND a.MarkedDelete = 0 AND a.IsCheck = 0 ORDER BY a.`TotalOrder` LIMIT 1;";
            ManufacturePlanTask task = null;

            foreach (var validState in ValidStates)
            {
                task = ServerConfig.ApiDb.Query <ManufacturePlanTask>(sql, new { state = validState.Value, pId = processors.Select(x => x.Id) })
                       .FirstOrDefault();
                if (task != null)
                {
                    break;
                }
            }

            if (task != null)
            {
                if (task.Id != tId)
                {
                    return(Result.GenError <DataResult>(Error.ManufactureTaskStateError));
                }
                if (task.State != ManufacturePlanTaskState.Wait && task.State != ManufacturePlanTaskState.WaitRedo && task.State != ManufacturePlanTaskState.Pause)
                {
                    return(Result.GenError <DataResult>(Error.ManufactureTaskStateError));
                }

                var now     = DateTime.Now;
                var oldTask = (ManufacturePlanTask)task.Clone();
                task.State = ManufacturePlanTaskState.Doing;

                if (task.FirstStartTime == default(DateTime))
                {
                    task.FirstStartTime = now;
                }

                if (task.IsRedo)
                {
                    task.RedoCount++;
                }

                task.ActualStartTime = now;
                if (oldTask.HaveChange(task, out var change))
                {
                    change.Time     = now;
                    change.Account  = account;
                    change.PlanId   = task.PlanId;
                    change.IsAssign = true;
                    change.TaskId   = task.TaskId;
                    change.ItemId   = task.Id;
                    change.Type     = ManufactureLogType.StartTask;
                }
                ServerConfig.ApiDb.Execute("UPDATE manufacture_plan_task SET `State` = @State, `FirstStartTime` = @FirstStartTime, `ActualStartTime` = @ActualStartTime, " +
                                           "`RedoCount` = @RedoCount WHERE `Id` = @Id;", task);
                ManufactureLog.AddLog(new List <ManufactureLog> {
                    change
                });
                return(Result.GenError <DataResult>(Error.Success));
            }

            return(Result.GenError <DataResult>(Error.ManufactureNoTask));
        }