SendWmsTaskAction.cs 18 KB
using System;
using System.Collections.Generic;
using System.Data;
using Infrastructure;
using Quartz;
using WebApp;
using WebRepository;

namespace WebMvc
{
    /// <summary>
    /// 任务预警
    /// </summary>
    [PersistJobDataAfterExecution]
    [DisallowConcurrentExecution]
    public class SendWmsTaskAction
    {
        private string ConnString { set; get; }
        IJobExecutionContext Context { set; get; }

        public SendWmsTaskAction(string _ConnString, IJobExecutionContext _Context)
        {
            ConnString = _ConnString;
            Context = _Context;
        }

        public void Execute(JobContainer jobContainer)
        {
            DbHelp dbHelp = new DbHelp(ConnString);
            try
            {
                string sql = "";
                sql = string.Format(@"SELECT B.station,A.sourceCode,A.shipmentCode, A.materialCode, A.batch, A.lot,A.qty,A.status,A.createTime,B.priority
                                      FROM dbo.shipment_detail A
                                      JOIN shipment_header B ON A.shipmentCode = B.code AND A.status = {0} 
                                      ORDER BY A.createTime, A.sourceCode",
                                    ShipmentHeaderStatus.新建);
                DataSet ds = dbHelp.SelectGet(sql);

                string SourceCode = "";
                string ShipmentCode = "";
                string MaterialCode = "";
                string Batch = "";
                string Lot = "";
                int Qty = 0;
                int Priority = 0;
                string Station = "";
                int Status = 0;
                ShipmentDetail sd = new ShipmentDetail();
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    sd.SourceCode = dr["sourceCode"].ToString();
                    sd.ShipmentCode = dr["shipmentCode"].ToString();
                    sd.MaterialCode = dr["materialCode"].ToString();
                    sd.Batch = dr["batch"].ToString();
                    sd.Lot = dr["lot"].ToString();
                    sd.Qty = int.Parse(dr["qty"].ToString());
                    Station = dr["station"].ToString();
                    sd.Status = short.Parse(dr["status"].ToString());
                    Priority = int.Parse(dr["priority"].ToString());
                    AddTaskInfo(sd, Station, Priority);

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public string AddTaskInfo(ShipmentDetail shipmentDetail, string Station, int Priority)
        {
            string strError = "";
            string sqlsh = "";
            string sqltask = "";
            string sqlinv = "";
            string sqlShDeil = "";
            string sqlInsDeil = "";
            string sqldeil = "";
            string sqlType = "";
            string sqlStation = "";
            DbHelp dbHelp = new DbHelp(ConnString);
            try
            {
                //建立单据主任务和明细任务
                Task task = new Task();
                var tno = "";
                sqlsh = string.Format(@"SELECT A.taskNo,B.type,B.totalQty,B.totalLines FROM dbo.task A
                                            JOIN dbo.shipment_header B ON A.orderCode = B.code AND A.orderCode = '{0}'",
                                shipmentDetail.ShipmentCode);
                DataSet ds = dbHelp.SelectGet(sqlsh);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    tno = ds.Tables[0].Rows[0]["taskNo"].ToString();
                }
                else
                {
                    tno = GetTaskNo(TaskNo.出库自动分配);
                    sqltask = string.Format(@"INSERT INTO dbo.task (taskNo, sourceCode, orderCode, businessType, totalQty, totalLines, firstStatus, lastStatus, createTime, createBy)
                                            VALUES ('{0}','{1}','{2}','{3}',{4},{5},{6},{7})",
                                            tno, shipmentDetail.SourceCode, shipmentDetail.ShipmentCode, ds.Tables[0].Rows[0]["type"].ToString(), int.Parse(ds.Tables[0].Rows[0]["totalQty"].ToString()),
                                            int.Parse(ds.Tables[0].Rows[0]["totalLines"].ToString()), TaskStatus.待下发任务, TaskStatus.待下发任务, DateTime.Now.ToString("yyyyMMdd HH:mm:ss"), "System");
                    dbHelp.DataOperator(sqltask);
                }

                sqlinv = string.Format(@"SELECT A.materialCode,A.containerCode,A.qty,A.locationCode,C.roadway FROM dbo.inventory A
                                            JOIN container B ON A.containerCode = B.code 
                                            AND A.taskStatus = 'idle' AND B.IsLock != 2 AND A.materialCode = '{0}'
                                            JOIN dbo.location C ON C.code = A.locationCode
                                            ORDER BY A.lot",
                                        shipmentDetail.MaterialCode);
                DataSet dinv = dbHelp.SelectGet(sqlinv);
                var sqty = shipmentDetail.Qty;
                int invQty = 0;
                foreach (DataRow dr in dinv.Tables[0].Rows)
                {
                    invQty = int.Parse(dr["qty"].ToString());
                    sqlShDeil = string.Format(@"SELECT SUM(containerQty) containerQty FROM dbo.task_detail
                                                WHERE materialCode = '{0}' AND status >= {1} AND status < {2} AND caterialCode = '{3}' AND taskNo NOT LIKE 'I%' AND station = '{4}'",
                                        shipmentDetail.MaterialCode, TaskStatus.新建任务, TaskStatus.已经完成, dr["containerCode"].ToString(), Station);
                    DataSet dShDeil = dbHelp.SelectGet(sqlShDeil);
                    if (dShDeil.Tables[0].Rows.Count != 0)
                    {
                        int taskhadqty = int.Parse(dShDeil.Tables[0].Rows[0]["containerQty"].ToString());
                        invQty = invQty - taskhadqty;
                    }

                    if (invQty == 0)
                    {
                        continue;
                    }

                    //暂存站台判断
                    sqlType = string.Format(@"SELECT code
                                            FROM dbo.station
                                            WHERE type = 6 AND code = '{0}'",
                                            Station);
                    DataSet dType = dbHelp.SelectGet(sqlType);
                    if (dType.Tables[0].Rows.Count > 0)
                    {
                        DataSet dStation = new DataSet();
                        if (int.Parse(dr["roadway"].ToString()) == 4)
                        {
                            sqlStation = string.Format(@"SELECT TOP 1 A.code FROM dbo.station A
                                                        JOIN station_roadway B ON A.code = B.stationCode AND A.type = 6 AND B.roadWay = {0} AND A.containercode = '' 
                                                        ORDER BY A.code DESC",
                        int.Parse(dr["roadway"].ToString()));
                            dStation = dbHelp.SelectGet(sqlStation);
                        }
                        else
                        {
                            sqlStation = string.Format(@"SELECT TOP 1 A.code FROM dbo.station A
                                                        JOIN station_roadway B ON A.code = B.stationCode AND A.type = 6 AND B.roadWay = {0} AND A.containercode = '' 
                                                        ORDER BY A.code",
                        int.Parse(dr["roadway"].ToString()));
                            dStation = dbHelp.SelectGet(sqlStation);
                        }
                        //占用暂存台
                        if (dStation.Tables[0].Rows.Count == 0)
                        {
                            sqlStation = string.Format(@"SELECT A.code,COUNT(C.station) num FROM dbo.station A
                                                        JOIN station_roadway B ON A.code = B.stationCode AND A.type = 6 AND B.roadWay = {0}
                                                        LEFT JOIN task_detail C ON A.code = C.station AND C.taskType = 'POUT'
                                                        GROUP BY A.code
                                                        ORDER BY num, A.code DESC",
                                                    int.Parse(dr["roadway"].ToString()));
                            dStation = dbHelp.SelectGet(sqlStation);
                        }
                        Station = dStation.Tables[0].Rows[0]["code"].ToString();
                    }

                    if (invQty >= sqty)
                    {
                        //出库主表任务明细
                        TaskDetail taskDetail = new TaskDetail();
                        sqlInsDeil = string.Format(@"INSERT INTO dbo.task_alert (taskNo, taskType, sourceCode, containerCode, orderCode, materialCode, batch, lot, oderQty, containerQty, hadQty, sourceLocation, destinationLocation, roadway, station, status, priority, createTime)
                                                VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}',{8},{9},{10},'{11}','{12}',{13},{14},{15},'{16}')",
                                                    tno, TaskType.分拣出库, shipmentDetail.SourceCode, dr["containerCode"].ToString(), shipmentDetail.ShipmentCode, shipmentDetail.MaterialCode, shipmentDetail.Batch,
                                                    shipmentDetail.Lot, shipmentDetail.Qty, invQty, sqty, dr["locationCode"].ToString(), Station, dr["roadway"].ToString(),
                                                    Station, TaskStatus.待下发任务, Priority, DateTime.Now.ToString("yyyyMMdd HH:mm:ss"));
                        dbHelp.DataOperator(sqlInsDeil);
                        sqty = sqty - invQty;
                        //建立容器出库任务
                        sqldeil = string.Format(@"SELECT taskNo FROM dbo.task_detail
                                            WHERE orderCode = '{0}' AND sourceCode = '{1}' AND materialCode = '{2}' AND status >= {3} AND status < {4} AND taskType = '{5}' AND containerCode = '{6}'",
                                            shipmentDetail.ShipmentCode, shipmentDetail.SourceCode, shipmentDetail.MaterialCode, TaskStatus.新建任务, TaskStatus.已经完成, TaskType.容器出库, dr["containerCode"].ToString());
                        DataSet dDeil = dbHelp.SelectGet(sqldeil);
                        if (dDeil.Tables[0].Rows.Count == 0)
                        {
                            //容器出库主任务
                            var taskNo = GetTaskNo(TaskNo.容器出库);
                            sqltask = string.Format(@"INSERT INTO dbo.task (taskNo, sourceCode, orderCode, businessType, firstStatus, lastStatus, createTime, createBy)
                                            VALUES ('{0}','{1}','{2}','{3}',{4},{5},{6},{7})",
                                                taskNo, shipmentDetail.SourceCode, shipmentDetail.ShipmentCode, BusinessType.出库_其他出库单
                                                , TaskStatus.待下发任务, TaskStatus.待下发任务, DateTime.Now.ToString("yyyyMMdd HH:mm:ss"), "System");
                            dbHelp.DataOperator(sqltask);
                            //容器出库子任务
                            sqlInsDeil = string.Format(@"INSERT INTO dbo.task_alert (taskNo, taskType, sourceCode, containerCode, orderCode, materialCode, batch, lot, oderQty, containerQty, hadQty, sourceLocation, destinationLocation, roadway, station, status, priority, createTime)
                                                VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}',{8},{9},{10},'{11}','{12}',{13},{14},{15},'{16}')",
                                taskNo, TaskType.容器出库, shipmentDetail.SourceCode, dr["containerCode"].ToString(), shipmentDetail.ShipmentCode, shipmentDetail.MaterialCode, shipmentDetail.Batch,
                                shipmentDetail.Lot, 0, 0, 0, dr["locationCode"].ToString(), Station, dr["roadway"].ToString(),
                                Station, TaskStatus.待下发任务, Priority, DateTime.Now.ToString("yyyyMMdd HH:mm:ss"));
                            dbHelp.DataOperator(sqlInsDeil);
                            break;
                        }
                    }
                    else
                    {
                        //出库主表任务明细
                        TaskDetail taskDetail = new TaskDetail();
                        sqlInsDeil = string.Format(@"INSERT INTO dbo.task_alert (taskNo, taskType, sourceCode, containerCode, orderCode, materialCode, batch, lot, oderQty, containerQty, hadQty, sourceLocation, destinationLocation, roadway, station, status, priority, createTime)
                                                VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}',{8},{9},{10},'{11}','{12}',{13},{14},{15},'{16}')",
                                                    tno, TaskType.整盘出库, shipmentDetail.SourceCode, dr["containerCode"].ToString(), shipmentDetail.ShipmentCode, shipmentDetail.MaterialCode, shipmentDetail.Batch,
                                                    shipmentDetail.Lot, shipmentDetail.Qty, invQty, sqty, dr["locationCode"].ToString(), Station,int.Parse(dr["roadway"].ToString()),
                                                    Station, TaskStatus.待下发任务, Priority, DateTime.Now.ToString("yyyyMMdd HH:mm:ss"));
                        dbHelp.DataOperator(sqlInsDeil);
                        sqty = sqty - invQty;
                        //建立容器出库任务
                        sqldeil = string.Format(@"SELECT taskNo FROM dbo.task_detail
                                            WHERE orderCode = '{0}' AND sourceCode = '{1}' AND materialCode = '{2}' AND status >= {3} AND status < {4} AND taskType = '{5}' AND containerCode = '{6}'",
                                            shipmentDetail.ShipmentCode, shipmentDetail.SourceCode, shipmentDetail.MaterialCode, TaskStatus.新建任务, TaskStatus.已经完成, TaskType.容器出库, dr["containerCode"].ToString());
                        DataSet dDeil = dbHelp.SelectGet(sqldeil);

                        if (dDeil.Tables[0].Rows.Count == 0)
                        {
                           

                            //容器出库主任务
                            var taskNo = GetTaskNo(TaskNo.容器出库);
                            sqltask = string.Format(@"INSERT INTO dbo.task (taskNo, sourceCode, orderCode, businessType, firstStatus, lastStatus, createTime, createBy)
                                            VALUES ('{0}','{1}','{2}','{3}',{4},{5},{6},{7})",
                                                taskNo, shipmentDetail.SourceCode, shipmentDetail.ShipmentCode, BusinessType.出库_其他出库单
                                                , TaskStatus.待下发任务, TaskStatus.待下发任务, DateTime.Now.ToString("yyyyMMdd HH:mm:ss"), "System");
                            dbHelp.DataOperator(sqltask);
                            //容器出库子任务
                            sqlInsDeil = string.Format(@"INSERT INTO dbo.task_alert (taskNo, taskType, sourceCode, containerCode, orderCode, materialCode, batch, lot, oderQty, containerQty, hadQty, sourceLocation, destinationLocation, roadway, station, status, priority, createTime)
                                                VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}',{8},{9},{10},'{11}','{12}',{13},{14},{15},'{16}')",
                                taskNo, TaskType.容器出库, shipmentDetail.SourceCode, dr["containerCode"].ToString(), shipmentDetail.ShipmentCode, shipmentDetail.MaterialCode, shipmentDetail.Batch,
                                shipmentDetail.Lot, 0, 0, 0, dr["locationCode"].ToString(), Station, dr["roadway"].ToString(),
                                Station, TaskStatus.待下发任务, Priority, DateTime.Now.ToString("yyyyMMdd HH:mm:ss"));
                            dbHelp.DataOperator(sqlInsDeil);
                        }
                    }
                    }
                }
            catch (Exception ex)
            {
                strError = ex.Message;
                return strError;
            }
            return strError;
        }

        public string GetTaskNo(string TaskType, int SeqLength = 4)
        {
            string Value = "1";
            string sql = "";
            string sqlup = "";
            DbHelp dbHelp = new DbHelp(ConnString);
            string.Format(@"SELECT Type, Value
                            FROM dbo.sys_count
                            WHERE Type = '{0}'",
                            TaskType);
            DataSet ds = dbHelp.SelectGet(sql);
            string sysValue = ds.Tables[0].Rows[0]["Value"].ToString();
            if (sysValue == null)
            {
                Value = TaskType + DateTime.Now.ToString("yyyyMMdd") + Value.PadLeft(SeqLength, '0');
                sqlup = string.Format(@"UPDATE dbo.sys_count
                        SET Value = '{0}'
                        WHERE Type = '{1}'",
                        Value, TaskType);
                dbHelp.DataOperator(sqlup);
            }
            else
            {
                string Date = sysValue.Substring(TaskType.Length, 8);

                if (Date == DateTime.Now.ToString("yyyyMMdd"))
                {
                    Value = Date + (int.Parse(sysValue.Substring(sysValue.Length - 4, 4)) + 1).ToString().PadLeft(SeqLength, '0');
                }
                else
                {
                    Value = DateTime.Now.ToString("yyyyMMdd") + Value.PadLeft(SeqLength, '0');
                }
                Value = TaskType + Value;
                sqlup = string.Format(@"UPDATE dbo.sys_count
                        SET Value = '{0}'
                        WHERE Type = '{1}'",
                        Value, TaskType);
                dbHelp.DataOperator(sqlup);
            }
            return Value;
        }
    }
}