SendWcsTaskAction.cs 13.5 KB
using System;
using System.Collections.Generic;
using System.Data;
using Infrastructure;
using Quartz;
using WebApp;
using WebRepository;

namespace WebMvc
{
    /// <summary>
    /// 单据结果回传
    /// </summary>

    /// <summary>
    /// 入库单结果回传
    /// </summary>
    [PersistJobDataAfterExecution]
    [DisallowConcurrentExecution]
    public class SendWcsTaskAction
    {
        private string ConnString { set; get; }
        IJobExecutionContext Context { set; get; }

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

        public void Execute(JobContainer jobContainer)
        {
            string sql = "";
            string upsql = "";
            string sqls = "";
            string sqld = "";
            string uptask = "";

            DbHelp dbHelp = new DbHelp(ConnString);
            #region 执行任务语句
            try
            {
                sql = string.Format(@"SELECT * FROM [dbo].[task_detail] WHERE status = {0}
                    AND taskNo IN(SELECT taskNo FROM[dbo].[task] WHERE lastStatus = {0}) 
                    AND
                    (
                        (
                            taskType IN('{2}','{6}')
                            AND containerCode NOT IN(SELECT code FROM dbo.container WHERE IsLock = 1)
                        )
                        OR taskType in ('{1}','{3}','{4}','{5}')
                    )
                    ORDER BY priority DESC; ",
                    TaskStatus.待下发任务, TaskType.空容器入库, TaskType.容器出库, TaskType.容器回库, TaskType.站台到站台, TaskType.空容器出库,TaskType.直接出库
                );
                DataSet ds = dbHelp.SelectGet(sql);

                List<TaskDetail> tdlist = new List<TaskDetail>();
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    //foreach (DataColumn dc in ds.Tables[0].Columns)
                    //{
                    TaskDetail tdel = new TaskDetail();
                    tdel.TaskNo = dr["taskNo"].ToString();
                    tdel.TaskType = dr["taskType"].ToString();
                    tdel.ContainerCode = dr["containerCode"].ToString();
                    tdel.SourceLocation = dr["sourceLocation"].ToString();
                    tdel.DestinationLocation = dr["destinationLocation"].ToString();
                    tdel.Roadway = int.Parse(dr["roadway"].ToString());
                    tdel.Station = dr["station"].ToString();
                    tdel.Status = int.Parse(dr["status"].ToString());
                    tdel.Priority = int.Parse(dr["priority"].ToString());
                    // }
                    tdlist.Add(tdel);
                }
                if (tdlist.Count == ds.Tables[0].Rows.Count)
                {
                    foreach (TaskDetail td in tdlist)
                    {
                        string containerFilter = "";
                        string sqlSta = "";

                        //if (td.TaskType == TaskType.直接出库 || td.TaskType == TaskType.站台到站台)
                        //{
                        //    sqlSta = string.Format("SELECT taskNo FROM dbo.task_detail WHERE status > {2} AND status < {3} AND taskType NOT IN('{0}','{1}') AND destinationLocation = {4}", TaskType.站台到站台,TaskType.直接出库,TaskStatus.待下发任务,TaskStatus.已经完成,td.DestinationLocation);
                        //    DataSet sqlStads = dbHelp.SelectGet(sqlSta);
                        //    if (sqlStads.Tables[0].Rows.Count > 0)
                        //    {
                        //        sql = string.Format("UPDATE dbo.task_detail SET error = 'WMS:该站台有任务' WHERE taskNo = '{0}'", td.TaskNo);
                        //        dbHelp.DataOperator(sql);
                        //        continue;
                        //    }
                        //}

                        if (td.TaskType == TaskType.容器出库 || td.TaskType == TaskType.直接出库 || td.TaskType == TaskType.站台到站台)
                        {
                            containerFilter = " AND (containercode IS NULL OR containercode='') AND isOut = 1 AND isStop = 0";
                        }
                        else if (td.TaskType == TaskType.空容器出库)
                        {
                            containerFilter = " AND (containercode IS NULL OR containercode='') AND isOut = 1 AND isStop = 0 AND isEmpty = 1";
                        }
                        else
                        {
                            containerFilter = "AND isStop = 0";
                        }

                        if (!string.IsNullOrEmpty(td.Station))
                        {

                            sqls = string.Format("SELECT top 1 id,code,name,containercode,isIn,isOut,isEmpty,isStop,type FROM dbo.station WHERE code = '{0}' {1}; ", td.Station, containerFilter);
                        }
                        else
                        {
                            sqls = string.Format("SELECT top 1 id,code,name,containercode,isIn,isOut,isEmpty,isStop,type FROM dbo.station WHERE code in (SELECT stationCode FROM dbo.station_roadway WHERE roadWay = '{0}' ) {1}; ", td.Roadway, containerFilter);
                        }

                        DataSet sds = dbHelp.SelectGet(sqls);
                        if (sds.Tables[0].Rows.Count > 0)
                        {
                            if (td.TaskType == TaskType.容器出库 || td.TaskType == TaskType.直接出库)
                            {
                                sqls = string.Format("SELECT top 1 locationCode FROM dbo.inventory WHERE containerCode = '{0}'; ", td.ContainerCode);
                                DataSet dsi = dbHelp.SelectGet(sqls);
                                if (dsi.Tables[0].Rows.Count > 0)
                                {
                                    if (td.SourceLocation != dsi.Tables[0].Rows[0]["locationCode"].ToString())
                                    {
                                        sql = string.Format("UPDATE dbo.task_detail SET error = 'WMS:容器出库起始仓位与实际仓位不符' WHERE taskNo = '{0}'", td.TaskNo);
                                        dbHelp.DataOperator(sql);
                                        throw new Exception("容器出库任务中起始仓位 与 目前实际仓位不符!");
                                        //td.SourceLocation = dsi.Tables[0].Rows[0]["locationCode"].ToString();
                                    }
                                }
                            }

                            td.Station = sds.Tables[0].Rows[0]["code"].ToString();

                            ApiRequest apiRequest = new ApiRequest("WCS");
                            //WCSResponse<WcsTask> _WCSResponse = apiRequest.Post<WCSResponse<WcsTask>>(JsonHelper.Instance.Serialize(td), "WcsWebApi/TaskAssign", "任务下发");
                            WCSResponse<WcsTask> _WCSResponse = apiRequest.Post<WCSResponse<WcsTask>>(JsonHelper.Instance.Serialize(td), "IWcsTaskTest/CreateWcsTask", "任务下发");
                            if (_WCSResponse.Code == 200)
                            {
                                //锁定仓位和占用站台及锁定容器
                                if (td.TaskType == TaskType.容器出库 || td.TaskType == TaskType.空容器出库 || td.TaskType == TaskType.直接出库 || td.TaskType == TaskType.站台到站台)//托盘出库任务
                                {
                                    upsql = string.Format("update [dbo].[task_detail] set status={2},station= '{1}',destinationLocation= '{1}' WHERE  taskNo= '{0}';", td.TaskNo, sds.Tables[0].Rows[0]["code"].ToString(), TaskStatus.下达任务);
                                    string outloc = string.Format("update [dbo].[location] set status='{1}' WHERE  code= '{0}';", td.SourceLocation, LocationStatus.任务锁定中);
                                    dbHelp.DataOperator(outloc);

                                    string upstation = string.Format("update [dbo].[station] set containercode= '{1}' WHERE  code= '{0}';", sds.Tables[0].Rows[0]["code"].ToString(), td.ContainerCode);
                                    dbHelp.DataOperator(upstation);

                                    string lockcontainer = string.Format("update [dbo].[container] set IsLock=1 WHERE  code= '{0}';", td.ContainerCode);
                                    dbHelp.DataOperator(lockcontainer);
                                }
                                else//托盘入库任务
                                {
                                    if (td.TaskType == TaskType.空容器入库)
                                    {
                                        string upssql = string.Format("update [dbo].[station] set containercode= '{1}' WHERE  code= '{0}';", sds.Tables[0].Rows[0]["code"].ToString(), td.ContainerCode);
                                        dbHelp.DataOperator(upssql);
                                    }
                                    upsql = string.Format("update [dbo].[task_detail] set status={1} WHERE  taskNo= '{0}';", td.TaskNo, TaskStatus.下达任务);
                                    string inloc = string.Format("update [dbo].[location] set status= '{0}' WHERE  code= '{1}';", LocationStatus.任务锁定中, td.DestinationLocation);
                                    dbHelp.DataOperator(inloc);
                                }

                                //更新托盘出入库任务状态 
                                dbHelp.DataOperator(upsql);
                                uptask = string.Format("update [dbo].[task] set firstStatus={1},lastStatus={1} WHERE  taskNo= '{0}';", td.TaskNo, TaskStatus.下达任务);
                                dbHelp.DataOperator(uptask);

                                //更新单据出入库主任务头尾状态和明细任务状态
                                sqld = string.Format("UPDATE dbo.task_detail SET status = {0},station='{1}' WHERE containerCode = '{2}' AND status >= {3} AND status < {4} AND taskType NOT IN('{5}');",
                                    TaskStatus.下达任务, sds.Tables[0].Rows[0]["code"].ToString(), td.ContainerCode, TaskStatus.新建任务, TaskStatus.已经完成,TaskType.站台到站台);
                                dbHelp.DataOperator(sqld);

                                string tasksql = string.Format("select * from [dbo].[task] where taskNo in (select taskNo from dbo.task_detail WHERE containerCode = '{0}' AND status >= {1} AND status < {2}); ",
                                    td.ContainerCode, TaskStatus.新建任务, TaskStatus.已经完成);
                                DataSet taskds = dbHelp.SelectGet(tasksql);
                                for (int i = 0; i < taskds.Tables[0].Rows.Count; i++)
                                {
                                    string taskdetailsql = string.Format("select top 1 status from dbo.task_detail WHERE taskNo = '{0}' AND status >= {1} AND status < {2} ORDER BY status; ",
                                        taskds.Tables[0].Rows[i]["taskNo"].ToString(), TaskStatus.新建任务, TaskStatus.已经完成);
                                    DataSet taskdetailds = dbHelp.SelectGet(taskdetailsql);
                                    int minstatus = int.Parse(taskdetailds.Tables[0].Rows[0]["status"].ToString());
                                    //更新单据任务的头状态
                                    if (int.Parse(taskds.Tables[0].Rows[i]["firstStatus"].ToString()) < TaskStatus.下达任务)
                                    {
                                        string uptaskdjf = string.Format("update [dbo].[task] set firstStatus={0} WHERE  taskNo= '{1}';", TaskStatus.下达任务, taskds.Tables[0].Rows[i]["taskNo"].ToString());
                                        dbHelp.DataOperator(uptaskdjf);
                                    }
                                    //更新单据任务的尾状态
                                    if (int.Parse(taskds.Tables[0].Rows[i]["lastStatus"].ToString()) < minstatus)
                                    {
                                        string uptaskdjl = string.Format("update [dbo].[task] set lastStatus='{0}' WHERE  taskNo= '{1}';", minstatus, taskds.Tables[0].Rows[i]["taskNo"].ToString());
                                        dbHelp.DataOperator(uptaskdjl);
                                    }
                                }
                                sql = string.Format("UPDATE dbo.task_detail SET error = '' WHERE taskNo = '{0}'", td.TaskNo);
                                dbHelp.DataOperator(sql);
                            }
                            else
                            {
                                sql = string.Format("UPDATE dbo.task_detail SET error = 'WCS:"+ _WCSResponse.Message + "' WHERE taskNo = '{0}'", td.TaskNo);
                                dbHelp.DataOperator(sql);
                            }
                        }
                        else
                        {
                            sql = string.Format("UPDATE dbo.task_detail SET error = 'WMS:未找到可用站台' WHERE taskNo = '{0}'", td.TaskNo);
                            dbHelp.DataOperator(sql);
                        }
                    }
                }



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