@ -1,4 +1,5 @@
using BO ;
using BO.BO.BaseInfo ;
using BO.BO.Bill ;
using BO.Utils ;
using BO.Utils.BillRpc ;
@ -10,8 +11,10 @@ using System.Collections.Generic;
using System.ComponentModel ;
using System.Data ;
using System.Drawing ;
using System.IO ;
using System.IO.Ports ;
using System.Linq ;
using System.Reflection ;
using System.Text ;
using System.Threading ;
using System.Threading.Tasks ;
@ -33,19 +36,23 @@ namespace WeighAndGrading
}
#endregion
private delegate void InvokeHandler ( ) ;
const string DETAIL_PATH = "WeightDetailData" ;
const short TANG_TECH = 0 ;
const short MAO_TECH = 1 ;
List < GradeAndWeight > tangList ;
List < GradeAndWeight > maoList ;
List < GradeAndWeight_Detail > details ;
List < long > localTang , localMao ;
bool connection = false ;
SerialPort weightPort ;
int maxIndex = 0 ;
ConcurrentQueue < GradeAndWeight_Detail > noLivestockList = new ConcurrentQueue < GradeAndWeight_Detail > ( ) ;
ConcurrentQueue < GradeAndWeight_Detail > noWeightList = new ConcurrentQueue < GradeAndWeight_Detail > ( ) ;
Thread syncWork ;
#region weightNeed
private IDataFormat _dataFormat ;
private Thread _inQueryThread ;
@ -62,6 +69,7 @@ namespace WeighAndGrading
maoGridView . AutoGenerateColumns = false ;
maoGridView . DataSource = null ;
historyGrid . AutoGenerateColumns = false ;
connection = ButcherAppContext . Context . UserConfig . Connection ;
AddLivestockBtn ( ) ;
BuildDiscontPanel ( ) ;
weightPort = new SerialPort ( ) ;
@ -78,7 +86,14 @@ namespace WeighAndGrading
void AddLivestockBtn ( )
{
var livestocks = BaseInfoRpcUtil . GetLivestockList ( ) ;
var livestocks = new List < CTuple < long , string , short > > ( ) ;
if ( connection )
{
livestocks = BaseInfoRpcUtil . GetLivestockList ( ) ;
XmlUtil . SerializerObjToFile ( livestocks , "Livestocks.xml" ) ;
}
else
livestocks = XmlUtil . DeserializeFromFile < List < CTuple < long , string , short > > > ( "Livestocks.xml" ) ;
foreach ( var item in livestocks )
{
var btn = new Button ( ) { Name = "_" + item . Item1 , Text = item . Item2 , Tag = item , Size = new Size ( 9 0 , 7 5 ) , TextAlign = ContentAlignment . MiddleCenter , Margin = new Padding { All = 1 5 } , Font = new Font ( "宋体" , 1 8 ) } ;
@ -98,7 +113,25 @@ namespace WeighAndGrading
modifyDetail . Livestock_Name = livestockTag . Item2 ;
modifyDetail . Technics = livestockTag . Item3 ;
modifyDetail . Technics_Name = livestockTag . Item3 = = TANG_TECH ? "烫褪" : "毛剥" ;
GradeAndWeightRpc . UpdateLivestock ( modifyDetail . ID , modifyDetail . Livestock_ID , modifyDetail . Livestock_Name , modifyDetail . Technics , modifyDetail . Technics_Name ) ;
connection = LoginRpcUtil . TestConnection ( ) ;
SetImage ( ) ;
if ( connection )
GradeAndWeightRpc . UpdateLivestock ( modifyDetail . ID , modifyDetail . Livestock_ID , modifyDetail . Livestock_Name , modifyDetail . Technics , modifyDetail . Technics_Name ) ;
else
{
var local = GetCurrentDetail ( ) ;
var f = local . FirstOrDefault ( x = > x . Index = = modifyDetail . Index ) ;
if ( f = = null )
local . Add ( modifyDetail ) ;
else
{
f . Livestock_ID = modifyDetail . Livestock_ID ;
f . Livestock_Name = modifyDetail . Livestock_Name ;
f . Technics = modifyDetail . Technics ;
f . Technics_Name = modifyDetail . Technics_Name ;
}
SaveDetailToLocal ( local , null ) ;
}
historyGrid . Refresh ( ) ;
modifyDetail = null ;
modifyPanel . Hide ( ) ;
@ -137,13 +170,27 @@ namespace WeighAndGrading
private void syncBtn_Click ( object sender , EventArgs e )
{
details = GradeAndWeightRpc . GetDetails ( butcherTimeInput . Date . Value , 5 0 ) ;
if ( ! Directory . Exists ( DETAIL_PATH ) )
Directory . CreateDirectory ( DETAIL_PATH ) ;
connection = LoginRpcUtil . TestConnection ( ) ;
SetImage ( ) ;
if ( connection )
details = GradeAndWeightRpc . GetDetails ( butcherTimeInput . Date . Value , 5 0 ) ;
else
details = GetLocalDetail ( ) ;
FillQueue ( ) ;
BindDetailGrid ( ) ;
if ( details . Any ( ) )
{
maxIndex = details . First ( ) . Index ;
SaveLastIndex ( maxIndex ) ;
}
else
maxIndex = GetLastIndex ( ) ;
if ( syncWork = = null | | ! syncWork . IsAlive )
{
localMao = XmlUtil . DeserializeFromFile < List < long > > ( "maoList.xml" ) ;
localTang = XmlUtil . DeserializeFromFile < List < long > > ( "tangList.xml" ) ;
syncWork = new Thread ( Sync ) ;
syncWork . Start ( ) ;
syncBtn . BackColor = Color . FromArgb ( 1 5 , 2 1 5 , 1 0 7 ) ;
@ -163,8 +210,15 @@ namespace WeighAndGrading
{
this . Invoke ( new InvokeHandler ( delegate ( )
{
connection = LoginRpcUtil . TestConnection ( ) ;
SetImage ( ) ;
BindTangGrid ( ) ;
BindMaoGrid ( ) ;
if ( connection )
{
SyncLocalDetails ( ) ;
SyncLocalMainFinish ( ) ;
}
} ) ) ;
Thread . Sleep ( 5 0 0 0 ) ;
}
@ -194,7 +248,13 @@ namespace WeighAndGrading
void BindTangGrid ( )
{
tangList = GradeAndWeightRpc . GetGradeAndWeightList ( butcherTimeInput . Date . Value , true ) ;
if ( connection )
tangList = GradeAndWeightRpc . GetGradeAndWeightList ( butcherTimeInput . Date . Value , true ) ;
else
{
if ( tangList = = null )
tangList = new List < GradeAndWeight > ( ) ;
}
tangGridView . DataSource = tangList . OrderBy ( x = > x . Order ) . OrderBy ( x = > x . Finish ) . ToList ( ) ;
if ( tangEntity = = null & & tangGridView . CurrentRow ! = null )
{
@ -231,7 +291,13 @@ namespace WeighAndGrading
void BindMaoGrid ( )
{
maoList = GradeAndWeightRpc . GetGradeAndWeightList ( butcherTimeInput . Date . Value , false ) ;
if ( connection )
maoList = GradeAndWeightRpc . GetGradeAndWeightList ( butcherTimeInput . Date . Value , false ) ;
else
{
if ( maoList = = null )
maoList = new List < GradeAndWeight > ( ) ;
}
maoGridView . DataSource = maoList . OrderBy ( x = > x . Order ) . OrderBy ( x = > x . Finish ) . ToList ( ) ;
if ( maoEntity = = null & & maoGridView . CurrentRow ! = null )
{
@ -551,12 +617,34 @@ namespace WeighAndGrading
SetBtnUnCheck ( disBtn ) ;
disBtn = null ;
}
GradeAndWeightRpc . UpdateOrInsertDetail ( first , true ) ;
connection = LoginRpcUtil . TestConnection ( ) ;
SetImage ( ) ;
if ( connection )
GradeAndWeightRpc . UpdateOrInsertDetail ( first , true ) ;
else
{
var local = GetCurrentDetail ( ) ;
var f = local . FirstOrDefault ( x = > x . Index = = first . Index ) ;
if ( f = = null )
local . Add ( first ) ;
else
{
f . OrderDetail_ID = first . OrderDetail_ID ;
f . Date = first . Date ;
f . Livestock_ID = first . Livestock_ID ;
f . Livestock_Name = first . Livestock_Name ;
f . Technics = first . Technics ;
f . Technics_Name = first . Technics_Name ;
f . Weight = first . Weight ;
}
SaveDetailToLocal ( local , null ) ;
}
historyGrid . Refresh ( ) ;
}
else //add
{
maxIndex + + ;
SaveLastIndex ( maxIndex ) ;
var entity = new GradeAndWeight_Detail ( ) ;
entity . Index = maxIndex ;
if ( currentRow ! = null )
@ -577,7 +665,16 @@ namespace WeighAndGrading
if ( details . Count = = 5 0 )
details . RemoveAt ( 4 9 ) ;
details . Insert ( 0 , entity ) ;
GradeAndWeightRpc . UpdateOrInsertDetail ( entity , true ) ;
connection = LoginRpcUtil . TestConnection ( ) ;
SetImage ( ) ;
if ( connection )
GradeAndWeightRpc . UpdateOrInsertDetail ( entity , true ) ;
else
{
var local = GetCurrentDetail ( ) ;
local . Add ( entity ) ;
SaveDetailToLocal ( local , null ) ;
}
noWeightList . Enqueue ( entity ) ;
BindDetailGrid ( ) ;
}
@ -609,12 +706,26 @@ namespace WeighAndGrading
first . Weight = ( first . Weight ? ? 0 ) + weight ;
else
first . Weight = weight ;
GradeAndWeightRpc . UpdateOrInsertDetail ( first ) ;
connection = LoginRpcUtil . TestConnection ( ) ;
SetImage ( ) ;
if ( connection )
GradeAndWeightRpc . UpdateOrInsertDetail ( first ) ;
else
{
var local = GetCurrentDetail ( ) ;
var f = local . FirstOrDefault ( x = > x . Index = = first . Index ) ;
if ( f = = null )
local . Add ( first ) ;
else
f . Weight = first . Weight ;
SaveDetailToLocal ( local , null ) ;
}
historyGrid . Refresh ( ) ;
}
else //add
{
maxIndex + + ;
SaveLastIndex ( maxIndex ) ;
var entity = new GradeAndWeight_Detail ( ) ;
entity . Index = maxIndex ;
entity . Weight = ( entity . Weight ? ? 0 ) + weight ;
@ -623,7 +734,16 @@ namespace WeighAndGrading
if ( details . Count = = 5 0 )
details . RemoveAt ( 4 9 ) ;
details . Insert ( 0 , entity ) ;
GradeAndWeightRpc . UpdateOrInsertDetail ( entity ) ;
connection = LoginRpcUtil . TestConnection ( ) ;
SetImage ( ) ;
if ( connection )
GradeAndWeightRpc . UpdateOrInsertDetail ( entity ) ;
else
{
var local = GetCurrentDetail ( ) ;
local . Add ( entity ) ;
SaveDetailToLocal ( local , null ) ;
}
noLivestockList . Enqueue ( entity ) ;
BindDetailGrid ( ) ;
}
@ -713,7 +833,17 @@ namespace WeighAndGrading
if ( entity . Finish )
return ;
entity . Finish = true ;
GradeAndWeightRpc . SetGradeFinish ( entity . OrderDetail_ID , TANG_TECH ) ;
connection = LoginRpcUtil . TestConnection ( ) ;
SetImage ( ) ;
if ( connection )
GradeAndWeightRpc . SetGradeFinish ( entity . OrderDetail_ID , TANG_TECH ) ;
else
{
SaveEntityToLocal ( localTang , entity . OrderDetail_ID , true ) ;
var f = tangList . FirstOrDefault ( x = > x . OrderDetail_ID = = entity . OrderDetail_ID ) ;
if ( f ! = null )
f . Finish = true ;
}
BindTangGrid ( ) ;
}
@ -750,7 +880,18 @@ namespace WeighAndGrading
if ( entity . Finish )
return ;
entity . Finish = true ;
GradeAndWeightRpc . SetGradeFinish ( entity . OrderDetail_ID , MAO_TECH ) ;
connection = LoginRpcUtil . TestConnection ( ) ;
SetImage ( ) ;
if ( connection )
GradeAndWeightRpc . SetGradeFinish ( entity . OrderDetail_ID , MAO_TECH ) ;
else
{
SaveEntityToLocal ( localMao , entity . OrderDetail_ID , false ) ;
var f = maoList . FirstOrDefault ( x = > x . OrderDetail_ID = = entity . OrderDetail_ID ) ;
if ( f ! = null )
f . Finish = true ;
}
BindMaoGrid ( ) ;
}
@ -763,7 +904,16 @@ namespace WeighAndGrading
Button disBtn = null ;
void BuildDiscontPanel ( )
{
var disconts = GradeAndWeightRpc . GetBodyDiscontItem ( ) . Where ( x = > x . Discont > 0 ) . OrderBy ( x = > x . ID ) ;
var disconts = new List < BodyDiscontItem > ( ) ;
if ( connection )
{
disconts = GradeAndWeightRpc . GetBodyDiscontItem ( ) ;
XmlUtil . SerializerObjToFile ( disconts , "Disconts.xml" ) ;
}
else
disconts = XmlUtil . DeserializeFromFile < List < BodyDiscontItem > > ( "Disconts.xml" ) ;
disconts = disconts . Where ( x = > x . Discont > 0 ) . OrderBy ( x = > x . ID ) . ToList ( ) ;
discontPanel . Controls . Clear ( ) ;
foreach ( var item in disconts )
{
@ -785,7 +935,20 @@ namespace WeighAndGrading
else
{
modifyDetail . Weight = ( modifyDetail . Weight ? ? 0 ) - Convert . ToDecimal ( btn . Tag ) ;
GradeAndWeightRpc . UpdateWeight ( modifyDetail . ID , modifyDetail . Weight ) ;
connection = LoginRpcUtil . TestConnection ( ) ;
SetImage ( ) ;
if ( connection )
GradeAndWeightRpc . UpdateWeight ( modifyDetail . ID , modifyDetail . Weight ) ;
else
{
var local = GetCurrentDetail ( ) ;
var f = local . FirstOrDefault ( x = > x . Index = = modifyDetail . Index ) ;
if ( f = = null )
local . Add ( modifyDetail ) ;
else
f . Weight = modifyDetail . Weight ;
SaveDetailToLocal ( local , null ) ;
}
historyGrid . Refresh ( ) ;
modifyDetail = null ;
modifyPanel . Hide ( ) ;
@ -852,5 +1015,142 @@ namespace WeighAndGrading
historyGrid . FirstDisplayedScrollingRowIndex = rightRoll ;
} ;
}
void SaveEntityToLocal ( List < long > list , long? orderDetailID , bool tang )
{
lock ( _obj )
{
if ( orderDetailID . HasValue )
{
if ( ! list . Contains ( orderDetailID . Value ) )
list . Add ( orderDetailID . Value ) ;
}
var fileName = "maoList.xml" ;
if ( tang )
fileName = "tangList.xml" ;
XmlUtil . SerializerObjToFile ( list , fileName ) ;
}
}
void SaveDetailToLocal ( List < GradeAndWeight_Detail > details , string fileName )
{
lock ( _obj )
{
if ( string . IsNullOrEmpty ( fileName ) )
fileName = Path . Combine ( DETAIL_PATH , string . Format ( "{0:yyyyMMdd}_Data.xml" , butcherTimeInput . Date . Value ) ) ;
XmlUtil . SerializerObjToFile ( details , fileName ) ;
}
}
List < GradeAndWeight_Detail > GetLocalDetail ( )
{
var fileName = Path . Combine ( DETAIL_PATH , string . Format ( "{0:yyyyMMdd}_Data.xml" , butcherTimeInput . Date . Value ) ) ;
var q = XmlUtil . DeserializeFromFile < List < GradeAndWeight_Detail > > ( fileName ) . OrderByDescending ( x = > x . Index ) . ToList ( ) ;
var r = new List < GradeAndWeight_Detail > ( ) ;
foreach ( var item in q )
{
r . Add ( item ) ;
if ( r . Count = = 5 0 )
break ;
}
return r ;
}
List < GradeAndWeight_Detail > GetCurrentDetail ( )
{
var fileName = Path . Combine ( DETAIL_PATH , string . Format ( "{0:yyyyMMdd}_Data.xml" , butcherTimeInput . Date . Value ) ) ;
return XmlUtil . DeserializeFromFile < List < GradeAndWeight_Detail > > ( fileName ) ;
}
void SaveLastIndex ( int idx )
{
lock ( _obj )
{
var fileName = Path . Combine ( DETAIL_PATH , string . Format ( "{0:yyyyMMdd}_Index.xml" , butcherTimeInput . Date . Value ) ) ;
XmlUtil . SerializerObjToFile ( new DetailLastIndex ( ) { Index = idx } , fileName ) ;
}
}
int GetLastIndex ( )
{
var fileName = Path . Combine ( DETAIL_PATH , string . Format ( "{0:yyyyMMdd}_Index.xml" , butcherTimeInput . Date . Value ) ) ;
var last = XmlUtil . DeserializeFromFile < DetailLastIndex > ( fileName ) ;
return last . Index ;
}
void SyncLocalMainFinish ( )
{
var mao = XmlUtil . DeserializeFromFile < List < long > > ( "maoList.xml" ) ;
if ( mao . Any ( ) )
{
foreach ( var item in mao )
{
GradeAndWeightRpc . SetGradeFinish ( item , MAO_TECH ) ;
localMao . Remove ( item ) ;
}
mao . Clear ( ) ;
SaveEntityToLocal ( mao , null , false ) ;
}
var tang = XmlUtil . DeserializeFromFile < List < long > > ( "tangList.xml" ) ;
if ( tang . Any ( ) )
{
foreach ( var item in tang )
{
GradeAndWeightRpc . SetGradeFinish ( item , TANG_TECH ) ;
localTang . Remove ( item ) ;
}
tang . Clear ( ) ;
SaveEntityToLocal ( tang , null , true ) ;
}
}
void SyncLocalDetails ( )
{
var currentTime = string . Format ( "{0:yyyyMMdd}_Data.xml" , butcherTimeInput . Date . Value ) ;
var files = Directory . GetFiles ( DETAIL_PATH , "_Data.xml" ) ;
foreach ( var file in files )
{
bool math = file . StartsWith ( currentTime ) ;
var details = XmlUtil . DeserializeFromFile < List < GradeAndWeight_Detail > > ( file ) ;
var clone = details . ToList ( ) ;
bool needRefsh = false ;
foreach ( var item in clone )
{
GradeAndWeightRpc . UpdateOrInsertDetail ( item ) ;
var first = details . First ( x = > x . Index = = item . Index ) ;
details . Remove ( first ) ;
SaveDetailToLocal ( details , file ) ;
if ( math )
{
var showTag = details . FirstOrDefault ( x = > x . Index = = item . Index ) ;
if ( showTag ! = null )
{
if ( ! needRefsh )
needRefsh = true ;
showTag . ID = item . ID ;
}
}
historyGrid . Refresh ( ) ;
}
if ( ! math )
File . Delete ( file ) ;
}
}
bool last = false ;
void SetImage ( )
{
if ( last = = connection )
return ;
var png = "stop.png" ;
if ( connection )
png = "working.png" ;
var imgPath = Path . Combine ( Application . StartupPath , "BWP.WinFormControl.dll" ) ;
var s = Assembly . LoadFile ( imgPath ) . GetManifestResourceStream ( "BWP.WinFormControl.Images." + png ) ;
statePic . Image = Image . FromStream ( s ) ;
statePic . Refresh ( ) ;
last = connection ;
}
}
}