use uts_db;

-- backup
create table uts_zongqing_tbl_shipping20230629 like uts_zongqing_tbl_shipping;
insert into uts_zongqing_tbl_shipping20230629 select * from uts_zongqing_tbl_shipping;

-- add columns
alter table uts_zongqing_tbl_shipping add (
MachineType varchar(255),
MachineAmount int(10),
ManufactureOrderId int(11)
)

-- machine type is the id of TBL_Project
alter table uts_zongqing_tbl_shipping modify MachineType int(11)

-- add client name,  can be selected from TBL_Project, or just input.
alter table uts_zongqing_tbl_shipping add ClientName varchar(255)

alter table uts_zongqing_tbl_shipping change MachineType ProjectID int(11)
alter table uts_zongqing_tbl_shipping change MachineAmount QTY int(11)
alter table uts_zongqing_tbl_shipping change ManufactureOrderId MO int(11)
alter table uts_zongqing_tbl_shipping change ClientName Customer varchar(255)


alter table uts_zongqing_tbl_shipping modify MO varchar(255)


create table uts_zongqing_tbl_shipping20230630 like uts_zongqing_tbl_shipping;
insert into uts_zongqing_tbl_shipping20230630 select * from uts_zongqing_tbl_shipping;






DELIMITER // 
CREATE PROCEDURE uts_zongqing_bindShippingToStationInfo(IN outboxcode varchar(255), IN shippingcode varchar(255))
BEGIN
    DECLARE @proID int;
    DECLARE @pardata varchar(255);


    UPDATE uts_zongqing_tbl_importinfo SET ShippingCode=@shippingcode, ShippingDatetime=now() where OutBoxCode=@outboxcode;
    
    -- write log
    select ProjectID into @proID from uts_zongqing_tbl_importinfo where  OutBoxCode=@outboxcode;
    CONCAT('Pwd:,','DBName:uts_zongqing,','Key:')

    -- importinfo_Log.PartData = String.Format("Pwd:{0},DBName:{1},Key:{2},JsonData:{3},ProjectID:{4}", Pwd, DBName, Key, JsonData, ProjectID);
    -- INSERT INTO uts_importinfo_log (ProjectID, Dbname, Operationtpye, ImportDateTime, PartData) VALUES (@proID, 'uts_zongqing', '批量修改', now(), )
END //
DELIMITER;

DROP PROCEDURE IF EXISTS uts_zongqing_bindShippingToStationInfo

DELIMITER // 
CREATE PROCEDURE uts_zongqing_unbindShippingToStationInfo(IN outboxcode varchar(255), IN shippingcode varchar(255))
BEGIN
    UPDATE uts_zongqing_tbl_importinfo SET ShippingCode=null, ShippingDatetime=null where OutBoxCode=@outboxcode and ShippingCode=@shippingcode;
END //
DELIMITER;

DROP PROCEDURE IF EXISTS uts_zongqing_unbindShippingToStationInfo




CREATE TABLE facedeviceRxTxInfo (
	id int not null auto_increment, 
	pmsid int, 
	sn varchar(256), 
	msgid varchar(256),
	cmd varchar(50),
	data varchar(700),
	datatime DATETIME(3),
	direction varchar(10),
	trresult int,
	ipaddr varchar(50),
	iplocation varchar(256)
);

CREATE TABLE facedeviceRxTxInfo ( id int not null auto_increment, pmsid int, sn varchar(256), msgid varchar(256), cmd varchar(50), data varchar(700), datatime DATETIME(3), direction varchar(10), trresult int, ipaddr varchar(50), iplocation varchar(256));

CREATE TABLE facedeviceRxTxInfo ( id int not null auto_increment PRIMARY KEY, pmsid int, sn varchar(256), msgid varchar(256), cmd varchar(50), data varchar(700), datatime DATETIME(3), direction varchar(10), trresult int, ipaddr varchar(50), iplocation varchar(256));






-- backup
create table uts_zongqing_tbl_shipping20230721 like uts_zongqing_tbl_shipping;
insert into uts_zongqing_tbl_shipping20230721 select * from uts_zongqing_tbl_shipping;

-- backup
create table uts_zongqing_tbl_importinfo20230721 like uts_zongqing_tbl_importinfo;
insert into uts_zongqing_tbl_importinfo20230721 select * from uts_zongqing_tbl_importinfo;




-- create/find test data



-- 1 records for shipping code
-- 未装箱状态一条, 流水号使用比正常短的整数, StartDateTime似乎必须有值，不然老系统会报错
-- 装6个产品就满了。
-- 可以使用此条数据测试，从未装箱， 装箱中，装箱满，结单的流程。。
insert into uts_zongqing_tbl_shipping (ShippingCode, Creator,CreateDateTime, StartDateTime, Loader, Status, ShippingType, Destination, ProjectID, QTY, MO, Customer) 
values (111222333444001, '测试创建运单人员', NOW(), NOW(), '测试装箱人员' , 1, '测试运输方式', '测试目的地', 3, 4, 'MP2-740;MP3-10', '测试客户')
insert into uts_zongqing_tbl_shipping (ShippingCode, Creator,CreateDateTime, StartDateTime, Loader, Status, ShippingType, Destination, ProjectID, QTY, MO, Customer) 
values (111222333444001, '测试创建运单人员机型1', NOW(), NOW(), '测试装箱人员1' , 1, '测试运输方式1', '测试目的地1', 1, 2, 'MP2-740;MP3-10', '测试客户1')
-- 结单后，如果要重新测试，用SQL改数据库中这条记录的Status为1
-- update uts_zongqing_tbl_shipping set Status=1 where ShippingCode=111222333444001
-- update uts_zongqing_tbl_shipping set Status=1 where ShippingCode=111222333444002

SELECT * from uts_zongqing_tbl_shipping where ShippingCode in (111222333444001,111222333444002)



-- 8 records for product, 
-- the first, second and third outbox has 2 machine, which 机型 is 3
-- the fourth outbox has 2 machines, and  which 机型  is 1
SELECT * from uts_zongqing_tbl_importinfo WHERE OutBoxCode in ('UTSTESTO3523','UTSTESTO3667','UTSTESTO3456','UTSTESTO1556')
-- 机型3
SELECT Barcode, ColorBox_BarCode, MO, ShippingCode, ShippingDateTime, Result1, Result2, Result3, Result4, Result5, Result6, Result7, Result8, Result9, Result10, Result11, Result12  from uts_zongqing_tbl_importinfo WHERE OutBoxCode ='UTSTESTO3523'
-- UTSTESTB123G	UTSTESTC123G	MP2-740		2023-06-15 11:34:15	1	1	1	1	1	1	1	1	1	1	1	1
-- UTSTESTB123C	UTSTESTC123H	MP2-740		2023-06-30 02:42:41	1	1	1	1	1	1	1	1	1	1	1	1
-- 机型3
SELECT Barcode, ColorBox_BarCode, MO, ShippingCode, ShippingDateTime, Result1, Result2, Result3, Result4, Result5, Result6, Result7, Result8, Result9, Result10, Result11, Result12  from uts_zongqing_tbl_importinfo WHERE OutBoxCode ='UTSTESTO3667'
-- UTSTESTB123D	UTSTESTC123G	MP3-10	133325783113073359	2023-06-30 02:42:41	1	1	1	1	1	1	1	1	0	0	1	0
-- UTSTESTB123E	UTSTESTC123E	MP3-10	133325783113073359	2023-06-30 02:42:41	1	1	1	1	1	1	1	1	1	1	1	1
-- 机型3
SELECT Barcode, ColorBox_BarCode, MO, ShippingCode, ShippingDateTime, Result1, Result2, Result3, Result4, Result5, Result6, Result7, Result8, Result9, Result10, Result11, Result12  from uts_zongqing_tbl_importinfo WHERE OutBoxCode ='UTSTESTO3456'
-- UTSTESTB123H	UTSTESTC123K	MP3-10			1	1	1	1	1	1	1	1	1	1	1	1
-- UTSTESTB123J	UTSTESTC123J	MP3-10			1	1	1	1	1	1	1	1	1	1	1	1

-- 机型1
SELECT Barcode, ColorBox_BarCode, MO, ShippingCode, ShippingDateTime, Result1, Result2, Result3, Result4, Result5, Result6, Result7 from uts_zongqing_tbl_importinfo WHERE OutBoxCode ='UTSTESTO1556'
-- UTSTESTB1239	UTSTESTC123V	MAR1			1	1	1	1	1	1	1
-- UTSTESTB1238	UTSTESTC1231	MAR1			1	1	1	1	1	1	1


SELECT * FROM `uts_zongqing_tbl_importinfo`  -- 16457
-- 外箱码正常的，比如： PTHP087848381513974340,  PZMP088587560385188673
-- 不正常的使用 123456, 321354 等等
SELECT distinct(OutBoxCode) FROM `uts_zongqing_tbl_importinfo` -- 1946
-- 彩盒码正常的： CTHM08711191261031335， CZMP08899515039919398
-- 不正常的使用： UTSTEST123H, colorbox-test-code-03 等等
SELECT distinct(ColorBox_BarCode) FROM `uts_zongqing_tbl_importinfo` -- 11392
-- 设备码正常的： 00785740027D， 31326D450E3636303930524A
-- 不正常的使用： 000000, UTSTEST123M, 123456 等等
SELECT distinct(Barcode) FROM `uts_zongqing_tbl_importinfo` -- 11392
SELECT * FROM `uts_zongqing_tbl_importinfo` WHERE ColorBox_BarCode like 'UTSTEST%' or ColorBox_BarCode like 'colorbox-test%'



SELECT * from uts_zongqing_tbl_shipping WHERE ShippingCode ='111222333444001'
