123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- using Cksoft.Data;
- using DllEapEntity;
- using DllEapEntity.Dtos;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace DllEapDal.OFILM.MES
- {
- public class OfTrackMstDal
- {
- private IDatabase CurrDb = null;
- string[] pcodes = new string[4] { "DA", "WB", "LHA", "AA" };
- public OfTrackMstDal(IDatabase db)
- {
- CurrDb = db;
- }
- public int Insert(OfTrackMst trackMst, string userCode)
- {
- return CurrDb.InsertFor(trackMst, userCode);
- }
- /// <summary>
- /// 验证记录是否可以进出站
- /// </summary>
- /// <param name="trackDetail"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- public bool Check(OfTrackDetail trackDetail, ref string errorinfo)
- {
- return true;
- var mac = CurrDb.FindEntityFor<Machine>(trackDetail.MacId);
- var currentPCode = mac.PCode;
- var exists = CurrDb.FindListForCondition<OfTrackMst>($" and a.stripId='{trackDetail.StripId}'",
- ref errorinfo);
- if (exists == null || exists.Count() <= 0)
- {
- if (currentPCode != pcodes.First())
- {
- errorinfo = $"当前STRIP还没有生产[{pcodes.First()}]站";
- return false;
- }
- }
- else
- {
- var last = exists.OrderBy(c => c.Id).Last();
- var lastPcode = last.ProcessCode;
- if (lastPcode == currentPCode)
- {
- if (trackDetail.Type == 1)
- {
- if (last.TrackInTime != null)
- {
- errorinfo = $"当前STRIP已开始[{lastPcode}]站生产,请勿重复操作";
- return false;
- }
- }
- else
- {
- if (last.TrackInTime == null)
- {
- errorinfo = $"当前STRIP在[{lastPcode}]还未进站";
- return false;
- }
- }
- }
- else
- {
- var currentIndex = pcodes.ToList().IndexOf(currentPCode);
- var lastIndex = pcodes.ToList().IndexOf(lastPcode);
- if (lastIndex != currentIndex - 1)
- {
- errorinfo = $"当前STRIP还没有生产完前道工序";
- return false;
- }
- else
- {
- if (last.TrackOutTime == null)
- {
- errorinfo = $"当前STRIP在[{last.ProcessCode}]站还出站," +
- $"不能开始下一站";
- return false;
- }
- }
- }
- }
- return true;
- }
- /// <summary>
- /// TrackIn操作
- /// </summary>
- /// <param name="trackDetail"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- public int TrackIn(OfTrackDetail trackDetail, string userCode, ref string errorinfo)
- {
- if (!Check(trackDetail, ref errorinfo))
- return -1;
- var mst = new OfTrackMst
- {
- MacId = trackDetail.MacId,
- ModCode = userCode,
- ModTime = DateTime.Now,
- ProcessId = trackDetail.ProcessId,
- RecCode = userCode,
- RecTime = DateTime.Now,
- StripId = trackDetail.StripId,
- };
- if (CurrDb.InsertFor(mst, userCode) < 0)
- return -1;
- var sql = "select @@identity;";
- var mstId = Convert.ToInt32(CurrDb.FindList<string>(sql).FirstOrDefault() ?? "-1");
- trackDetail.MstId = mstId;
- trackDetail.ModCode = userCode;
- trackDetail.RecCode = userCode;
- trackDetail.FDate = DateTime.Now;
- trackDetail.IsSuccess = 1;
- trackDetail.Type = 1;
- if (CurrDb.InsertFor(trackDetail, userCode) < 0)
- return -1;
- mst = CurrDb.FindEntityFor<OfTrackMst>(mstId);
- mst.TrackInTime = DateTime.Now;
- if (CurrDb.UpdateFor(mst, userCode) < 0)
- return -1;
- return 1;
- }
- /// <summary>
- /// TrackOut操作
- /// </summary>
- /// <param name="trackDetail"></param>
- /// <param name="userCode"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- public int TrackOut(OfTrackDetail trackDetail, string userCode, ref string errorinfo)
- {
- if (!Check(trackDetail, ref errorinfo))
- return -1;
- var mst = CurrDb.FindListForCondition<OfTrackMst>($" and a.processId={trackDetail.ProcessId}" +
- $" and a.StripId='{trackDetail.StripId}'", ref errorinfo).FirstOrDefault();
- if (mst == null)
- {
- errorinfo = "未找到该Strip的进站记录";
- return -1;
- }
- var mstId = mst.Id;
- trackDetail.MstId = mstId;
- trackDetail.ModCode = userCode;
- trackDetail.RecCode = userCode;
- trackDetail.FDate = DateTime.Now;
- trackDetail.IsSuccess = 1;
- trackDetail.Type = -1;
- if (CurrDb.InsertFor(trackDetail, userCode) < 0)
- return -1;
- mst.TrackOutTime = DateTime.Now;
- if (CurrDb.UpdateFor(mst, userCode) < 0)
- return -1;
- return 1;
- }
- public IEnumerable<OfTrackMst> GetTrackMst(PagedQuery query, out int total)
- {
- string errorinfo = string.Empty;
- var entities = CurrDb.FindListForCondition<OfTrackMst>($" {query.Filter} " +
- $"order by {query.SortField} {query.SortOrder} limit {(query.PageIndex - 1) * query.PageSize},{query.PageSize}",
- ref errorinfo);
- var countSql = new OfTrackMst().GetCountSql() + $" where 1=1 {query.Filter}";
- total = Convert.ToInt32(CurrDb.FindList<string>(countSql).FirstOrDefault() ?? "-1");
- return entities;
- }
- public IEnumerable<OfTrackDetail> GetTrackDetail(PagedQuery query, out int total)
- {
- string errorinfo = string.Empty;
- var entities = CurrDb.FindListForCondition<OfTrackDetail>($" {query.Filter} " +
- $"order by {query.SortField} {query.SortOrder} limit {(query.PageIndex - 1) * query.PageSize},{query.PageSize}",
- ref errorinfo);
- var countSql = new OfTrackDetail().GetCountSql() + $" where 1=1 {query.Filter}";
- total = Convert.ToInt32(CurrDb.FindList<string>(countSql).FirstOrDefault() ?? "-1");
- return entities;
- }
- /// <summary>
- /// 根据STRIPID获取Strip的状态
- /// </summary>
- /// <param name="stripId"></param>
- /// <returns></returns>
- public OfTrackMst Get(string stripId, int processId)
- {
- string errorinfo = string.Empty;
- var entity = CurrDb.FindListForCondition<OfTrackMst>($" and a.StripID='{stripId}' " +
- $"and a.processId={processId}",
- ref errorinfo).FirstOrDefault();
- if (entity != null)
- {
- if (entity.TrackInTime == null)
- {
- entity.CurrentState = "未进站";
- }
- else if (entity.TrackOutTime == null)
- {
- entity.CurrentState = "已进站";
- }
- else
- {
- entity.CurrentState = "已出站";
- }
- return entity;
- }
- return new OfTrackMst
- {
- CurrentState = "未进站"
- };
- }
- /// <summary>
- /// 进出站
- /// </summary>
- /// <param name="entitys"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- public int TrackInOut(string macCode, string stripId, string userCode, int? type, ref string errorinfo)
- {
- try
- {
- OfTrackMstDal dal = new OfTrackMstDal(CurrDb);
- var machine = CurrDb.FindListForCondition<Machine>($" and a.fcode='{macCode}'",
- ref errorinfo).FirstOrDefault();
- var mst = CurrDb.FindListForCondition<OfTrackMst>($" and a.macid='{machine.ID}' " +
- $"and a.processid='{machine.ProcessId}' and stripId='{stripId}'", ref errorinfo).FirstOrDefault();
- var detail = new OfTrackDetail
- {
- MacId = machine.ID,
- FDate = DateTime.Now,
- StripId = stripId,
- IsSuccess = 1,
- ModTime = DateTime.Now,
- ProcessId = machine.ProcessId,
- OperatorType = 1,
- RecTime = DateTime.Now
- };
- if (type == null || type == 1)
- {
- detail.Type = 1;
- if (dal.TrackIn(detail, userCode, ref errorinfo) < 0)
- {
- return -1;
- }
- }
- else
- {
- detail.Type = -1;
- if (dal.TrackOut(detail, userCode, ref errorinfo) < 0)
- {
- return -1;
- }
- }
- //if (mst == null || mst.TrackInTime == null)
- //{
- // detail.Type = 1;
- // if (dal.TrackIn(detail, userCode, ref errorinfo) < 0)
- // {
- // return -1;
- // }
- //}
- //else
- //{
- // detail.Type = -1;
- // if (dal.TrackOut(detail, userCode, ref errorinfo) < 0)
- // {
- // return -1;
- // }
- //}
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- }
- }
|