Bill.cs 8.53 KB
using HHWCS.Model;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HHWCS.Bill
{
    public class Bill
    {
        public static BillResult<List<TaskEntity>> getTasks(string status, string taskNo, string pallet, DateTime? beginTime, DateTime? endTime)
        {
            String sql = "select * from task where 1=1 ";
            if (!String.IsNullOrEmpty(status))
            {
                sql += " and status = '" + status + "'";
            }
            if (!String.IsNullOrEmpty(taskNo))
            {
                sql += " and id = " + taskNo;
            }
            if (!String.IsNullOrEmpty(pallet))
            {
                sql += " and containerCode = '" + pallet + "'";
            }
            if (beginTime != null)
            {
                sql += " and created>='" + beginTime.ToString() + "'";
            }
            if (endTime != null)
            {
                sql += " and created<='" + endTime.ToString() + "'";
            }


            DataSet ds = MySqlHelper.ExecuteDataset(AppCommon.ConnectionString, sql);
            if (ds == null || ds.Tables.Count == 0)
            {
                return BillResultFactory.Error<List<TaskEntity>>(null, "未找到数据");
            }
            return BillResultFactory.Sucess<List<TaskEntity>>(ds.Tables[0].AsEnumerable().Select(x => new TaskEntity
            {
                Id = (int)x["id"],
                WarehouseId = Convert.ToInt32(x["warehouseId"]),
                WarehouseCode = x["warehouseCode"]?.ToString(),
                CompanyId = x["companyId"] is DBNull ? null : (int?)x["companyId"],
                Priority = Convert.ToInt32(x["priority"]),
                Type = Convert.ToInt32(x["type"]),
                Station = x["station"] is DBNull ? null : (int?)x["station"],
                ContainerId = x["containerId"] is DBNull ? null : (int?)x["containerId"],
                ContainerCode = x["containerCode"]?.ToString(),
                SourceLocation = x["sourceLocation"]?.ToString(),
                DestinationLocation = x["destinationLocation"]?.ToString(),
                Status = Convert.ToInt32(x["status"]),
                Created = x["created"] is DBNull ? null : (DateTime?)x["created"],
                CreatedBy = x["createdBy"]?.ToString(),
                BeginTime = x["beginTime"] is DBNull ? null : (DateTime?)x["beginTime"],
                EndTime = x["endTime"] is DBNull ? null : (DateTime?)x["endTime"],
                LastUpdated = x["lastUpdated"] is DBNull ? null : (DateTime?)x["lastUpdated"],
            }).OrderByDescending(t => t.Created).ToList(), "成功");
        }

        /// <summary>
        /// 创建任务
        /// </summary>
        /// <param name="palletCode"></param>
        /// <param name="station"></param>
        /// <param name="sourceLocation"></param>
        /// <param name="destinationLocation"></param>
        /// <param name="status"></param>
        /// <param name="type"></param>
        /// <param name="priority"></param>
        /// <returns>注意,任务实体没有返回</returns>
        public static BillResult<TaskEntity> creatTask(string palletCode,int station,string sourceLocation,string destinationLocation,int status,int type,int priority)
        {
            try
            {
                //检查参数
                if (String.IsNullOrEmpty(palletCode))
                {
                    return BillResultFactory.Error<TaskEntity>(null, "托盘号不能为空");
                }
                else
                {
                    //校验托盘,查看托盘是否存在任务未完成
                    int result = Convert.ToInt32(MySqlHelper.ExecuteScalar(AppCommon.ConnectionString, "SELECT count(*) from task where containerCode = @pallet and `status`<40 and warehouseId= @warehouseId", new MySqlParameter("@pallet", palletCode), new MySqlParameter("@warehouseId", AppCommon.WarehouseId)));
                    if (result > 0)
                    {
                        return BillResultFactory.Error<TaskEntity>(null, "托盘已存在任务");
                    }
                }
                if (type == 100 || type == 500 || type == 800)
                {
                    if (String.IsNullOrEmpty(destinationLocation))
                    {
                        return BillResultFactory.Error<TaskEntity>(null, "对于整盘入库、空盘入库和移库目标库位不能为空");
                    }
                    else
                    {
                        var temp = Bill.checkLocationForCreateTask(destinationLocation, AppCommon.WarehouseId);
                        if (!temp.Success)
                        {
                            return temp;
                        }
                    }
                }
                if (type == 200 || type == 300 || type == 400 || type == 600 || type == 700 || type == 800)
                {
                    if (String.IsNullOrEmpty(sourceLocation))
                    {
                        return BillResultFactory.Error<TaskEntity>(null, "对于补充入库、整盘出库、分拣出库、空盘出库、盘点和移库源库位不能为空");
                    }
                    else
                    {
                        //校验库位
                        var temp = Bill.checkLocationForCreateTask(sourceLocation, AppCommon.WarehouseId);
                        if (!temp.Success)
                        {
                            return temp;
                        }
                    }
                }

                string sql = "insert into task(containerCode,station,sourceLocation,destinationLocation,status,type,priority,warehouseId,created,createdBy) " +
                    " VALUES(@pallet, @station, @sourceLocation, @destinationLocation, @status, @type, @priority, @warehouseId,@warehouseCode, NOW(),@createdBy)";
                List<MySqlParameter> mySqlParameters = new List<MySqlParameter>();
                mySqlParameters.Add(new MySqlParameter("@pallet", palletCode));
                mySqlParameters.Add(new MySqlParameter("@station", station));
                mySqlParameters.Add(new MySqlParameter("@sourceLocation", sourceLocation));
                mySqlParameters.Add(new MySqlParameter("@destinationLocation", destinationLocation));
                mySqlParameters.Add(new MySqlParameter("@status", status));
                mySqlParameters.Add(new MySqlParameter("@type", type));
                mySqlParameters.Add(new MySqlParameter("@priority", priority));
                mySqlParameters.Add(new MySqlParameter("@warehouseId", AppCommon.WarehouseId));
                mySqlParameters.Add(new MySqlParameter("@createdBy", AppCommon.User.UserName));
                mySqlParameters.Add(new MySqlParameter("@warehouseCode", AppCommon.WarehouseCode));
                int i = MySqlHelper.ExecuteNonQuery(AppCommon.ConnectionString, sql, mySqlParameters.ToArray());
                if (i == 1)
                {
                    return BillResultFactory.Sucess<TaskEntity>(null, "成功");
                }
                else
                {
                    return BillResultFactory.Error<TaskEntity>(null, "插入任务失败");
                }
            }
            catch (Exception ex)
            {
                return BillResultFactory.Error<TaskEntity>(null, "出现异常:"+ex.ToString());
            }
        }

        private static BillResult<TaskEntity> checkLocationForCreateTask(String code,int warehouseId)
        {
            //校验库位
            var ds = MySqlHelper.ExecuteDataset(AppCommon.ConnectionString, "SELECT * from location where `code` = @code and warehouseId = @warehouseId", new MySqlParameter("@code", code), new MySqlParameter("@warehouseId", AppCommon.WarehouseId));
            if (ds == null || ds.Tables.Count == 0||ds.Tables[0].Rows.Count==0)
            {
                return BillResultFactory.Error<TaskEntity>(null, "没有找到库位");
            }
            var dr = ds.Tables[0].AsEnumerable().FirstOrDefault();
            if (!String.IsNullOrEmpty(dr["containerCode"].ToString()))
            {
                return BillResultFactory.Error<TaskEntity>(null, "目标库位非空闲");
            }
            if (!String.IsNullOrEmpty(dr["containerCode"].ToString()))
            {
                return BillResultFactory.Error<TaskEntity>(null, "目标库位已有货,请不要重入");
            }
            return BillResultFactory.Sucess<TaskEntity>(null, "OK");
        }

    }
}