/*
 Navicat MySQL Data Transfer

 Source Server         : aaa
 Source Server Type    : MySQL
 Source Server Version : 50528
 Source Host           : localhost:3306
 Source Schema         : gmms

 Target Server Type    : MySQL
 Target Server Version : 50528
 File Encoding         : 65001

 Date: 13/05/2024 13:12:34
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for ATAB
-- ----------------------------
DROP TABLE IF EXISTS `ATAB`;
CREATE TABLE `ATAB`  (
  `gg` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for A_JiXieHand
-- ----------------------------
DROP TABLE IF EXISTS `A_JiXieHand`;
CREATE TABLE `A_JiXieHand`  (
  `begin_time` datetime NULL DEFAULT NULL COMMENT '接收时间',
  `end_time` datetime NULL DEFAULT NULL COMMENT '响应时间',
  `content` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '内容'
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for A_LengLipian
-- ----------------------------
DROP TABLE IF EXISTS `A_LengLipian`;
CREATE TABLE `A_LengLipian`  (
  `create_time` datetime NULL DEFAULT NULL COMMENT '进入时间',
  `end_time` datetime NULL DEFAULT NULL COMMENT '结束时间',
  `content` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '内容'
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for A_LengLipian_copy1
-- ----------------------------
DROP TABLE IF EXISTS `A_LengLipian_copy1`;
CREATE TABLE `A_LengLipian_copy1`  (
  `create_time` datetime NULL DEFAULT NULL COMMENT '进入时间',
  `end_time` datetime NULL DEFAULT NULL COMMENT '结束时间',
  `content` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '内容'
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for A_Robotlog
-- ----------------------------
DROP TABLE IF EXISTS `A_Robotlog`;
CREATE TABLE `A_Robotlog`  (
  `groups` bigint(20) NULL DEFAULT NULL,
  `glassid` bigint(20) NULL DEFAULT NULL,
  `beigntime` datetime NULL DEFAULT NULL,
  `endtime` datetime NULL DEFAULT NULL,
  `state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `processcard_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for A_youhua
-- ----------------------------
DROP TABLE IF EXISTS `A_youhua`;
CREATE TABLE `A_youhua`  (
  `processcard_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '优化流程卡',
  `count` int(11) NULL DEFAULT NULL COMMENT '优化数量',
  `youhuashijian` datetime NULL DEFAULT NULL COMMENT '优化时间'
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for Cold_Work_Stock
-- ----------------------------
DROP TABLE IF EXISTS `Cold_Work_Stock`;
CREATE TABLE `Cold_Work_Stock`  (
  `processcard_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '流程卡号',
  `state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '出库/入库',
  `in_stock_time` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '入库时间',
  `out_stock_time` datetime NULL DEFAULT NULL COMMENT '出库时间',
  PRIMARY KEY (`processcard_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for GanghuaTuiJian
-- ----------------------------
DROP TABLE IF EXISTS `GanghuaTuiJian`;
CREATE TABLE `GanghuaTuiJian`  (
  `liuchengka` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `baogongbianhao` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `gyqrxh` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for SemiFinishedProductsCold
-- ----------------------------
DROP TABLE IF EXISTS `SemiFinishedProductsCold`;
CREATE TABLE `SemiFinishedProductsCold`  (
  `id` bigint(20) NOT NULL,
  `LocationNum` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '库位',
  `LocationArea` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '库区',
  `CarNumber` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '流程卡号',
  `State` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '状态',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for TemperingFinsh
-- ----------------------------
DROP TABLE IF EXISTS `TemperingFinsh`;
CREATE TABLE `TemperingFinsh`  (
  `machineid` bigint(20) NULL DEFAULT 0,
  `glassid` bigint(20) NULL DEFAULT NULL,
  `Finshtime` datetime NULL DEFAULT NULL,
  `stoprank` bigint(20) NULL DEFAULT NULL,
  `shelfrankid` bigint(20) NULL DEFAULT NULL,
  `processcard` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for Y_Edg_Statistics
-- ----------------------------
DROP TABLE IF EXISTS `Y_Edg_Statistics`;
CREATE TABLE `Y_Edg_Statistics`  (
  `Glassid` bigint(20) NOT NULL COMMENT '玻璃ID',
  `SendTime` datetime NULL DEFAULT NULL COMMENT '发送时间',
  `Width` double NULL DEFAULT NULL COMMENT '宽',
  `Height` double NULL DEFAULT NULL COMMENT '高',
  `Thickess` double NULL DEFAULT NULL COMMENT '厚',
  `SizeText` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '尺寸字符串',
  `SendText` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '发送内容',
  `Recordsid` bigint(20) NULL DEFAULT NULL COMMENT '记录ID',
  `Line` int(11) NULL DEFAULT NULL COMMENT '线路',
  `RecordsidW` double NULL DEFAULT NULL COMMENT '记录ID宽',
  `RecordsidH` double NULL DEFAULT NULL COMMENT '记录ID高',
  `banzu` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '班组',
  `report_time` date NULL DEFAULT NULL COMMENT '报工日期'
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for aaaa
-- ----------------------------
DROP TABLE IF EXISTS `aaaa`;
CREATE TABLE `aaaa`  (
  `MachineName` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `groups` int(11) NULL DEFAULT NULL,
  `data1` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `Content` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '内容',
  `id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '任务id',
  `Sendtext` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for app_glassType
-- ----------------------------
DROP TABLE IF EXISTS `app_glassType`;
CREATE TABLE `app_glassType`  (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '膜系中文变化成英文',
  `old_glassType` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `new_glassType` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `creat_time` datetime NOT NULL,
  `update_time` datetime NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 30 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for app_yuanpianpandian
-- ----------------------------
DROP TABLE IF EXISTS `app_yuanpianpandian`;
CREATE TABLE `app_yuanpianpandian`  (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '原片盘点',
  `shelf_id` int(11) NULL DEFAULT NULL,
  `dateshijian` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `glass` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `width_height` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `kucunshuliang` int(11) NULL DEFAULT NULL,
  `pandianshuliang` int(11) NULL DEFAULT NULL,
  `shenheshuliang` int(11) NOT NULL,
  `jianlitime` datetime NOT NULL,
  `xiugaitime` datetime NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 72 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for banzu
-- ----------------------------
DROP TABLE IF EXISTS `banzu`;
CREATE TABLE `banzu`  (
  `banzubianhao` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `banzumingcheng` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `gongxu` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `shebeibianhao` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `shebei` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `gongyi` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `shenhezhuangtai` int(11) NULL DEFAULT NULL,
  `shenherengonghao` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `shenheren` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `jianlitime` datetime NULL DEFAULT NULL,
  `xiugaitime` datetime NULL DEFAULT NULL
) ENGINE = FEDERATED CHARACTER SET = utf8 COLLATE = utf8_general_ci CONNECTION = 'mysql://root1:beibo.123@10.12.0.104:3306/sappp/banzu' ROW_FORMAT = Dynamic;

-- ----------------------------
-- Table structure for cipoleixing
-- ----------------------------
DROP TABLE IF EXISTS `cipoleixing`;
CREATE TABLE `cipoleixing`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `cipoleixing` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `shenhezhuangtai` int(11) NULL DEFAULT NULL,
  `shenherengonghao` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `shenheren` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `jianlitime` datetime NULL DEFAULT NULL,
  `xiugaitime` datetime NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING REMOTE
) ENGINE = FEDERATED CHARACTER SET = utf8 COLLATE = utf8_general_ci CONNECTION = 'mysql://root1:beibo.123@10.12.0.104:3306/sappp/cipoleixing' ROW_FORMAT = Dynamic;

-- ----------------------------
-- Table structure for cipoyuanyin
-- ----------------------------
DROP TABLE IF EXISTS `cipoyuanyin`;
CREATE TABLE `cipoyuanyin`  (
  `xuhao` int(11) NOT NULL AUTO_INCREMENT,
  `cipoyuanyin` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `shenhezhuangtai` int(11) NULL DEFAULT NULL,
  `shenherengonghao` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `shenheren` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `cipoleixing` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `cipofenxi` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `jianlitime` datetime NULL DEFAULT NULL,
  `xiugaitime` datetime NULL DEFAULT NULL,
  PRIMARY KEY (`xuhao`) USING REMOTE
) ENGINE = FEDERATED CHARACTER SET = utf8 COLLATE = utf8_general_ci CONNECTION = 'mysql://root1:beibo.123@10.12.0.104:3306/sappp/cipoyuanyin' ROW_FORMAT = Dynamic;

-- ----------------------------
-- Table structure for e_pierceds
-- ----------------------------
DROP TABLE IF EXISTS `e_pierceds`;
CREATE TABLE `e_pierceds`  (
  `processcard_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '流程卡',
  `type_num` bigint(20) NULL DEFAULT NULL COMMENT '玻璃类型',
  `pieces` bigint(20) NULL DEFAULT NULL COMMENT '数量',
  `pierced_type` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '次破类型',
  `pierced_cause` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '次破原因',
  `is_report` bigint(20) NULL DEFAULT 0 COMMENT '是否报工（0：未报工；1：可报工；2：准备报工；3：已报工；4：人工拿走）',
  `glass_id` bigint(20) NULL DEFAULT NULL COMMENT '玻璃id',
  `create_time` datetime NULL DEFAULT NULL COMMENT '次破时间',
  `process` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工序',
  `line` bigint(20) NULL DEFAULT NULL COMMENT '线路',
  `gc_number` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工程号',
  `cs_number` int(11) NULL DEFAULT NULL COMMENT '原因次数',
  `team_name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '班组名称',
  `report_time` date NULL DEFAULT NULL COMMENT '报工时间',
  INDEX `create_time`(`create_time`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for e_record_hollow
-- ----------------------------
DROP TABLE IF EXISTS `e_record_hollow`;
CREATE TABLE `e_record_hollow`  (
  `processcard_id` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '流程卡',
  `glass_id` bigint(20) NULL DEFAULT NULL COMMENT '玻璃id',
  `width` double(20, 2) NULL DEFAULT NULL COMMENT '宽度',
  `height` double(20, 2) NULL DEFAULT NULL COMMENT '高度',
  `thinkess` double(20, 2) NULL DEFAULT NULL COMMENT '厚度',
  `color` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '颜色',
  `sequence` bigint(20) NULL DEFAULT NULL COMMENT '架上顺序',
  `create_time` datetime NULL DEFAULT NULL COMMENT '中空完成时间',
  `line` bigint(20) NULL DEFAULT NULL COMMENT '线路',
  `team_name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '班组名称',
  `type_num` int(11) NULL DEFAULT NULL COMMENT '玻璃类型',
  `is_report` int(11) NULL DEFAULT NULL COMMENT '是否报工（0：未报工；1：可报工；2：准备报工；3：已报工）',
  `report_time` date NULL DEFAULT NULL COMMENT '报工时间',
  INDEX `create_time`(`create_time`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for e_record_stock
-- ----------------------------
DROP TABLE IF EXISTS `e_record_stock`;
CREATE TABLE `e_record_stock`  (
  `processcard_id` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '流程卡',
  `glass_id` bigint(20) NULL DEFAULT NULL COMMENT '玻璃id',
  `width` double(20, 2) NULL DEFAULT NULL COMMENT '宽度',
  `height` double(20, 2) NULL DEFAULT NULL COMMENT '高度',
  `thinkess` double(20, 2) NULL DEFAULT NULL COMMENT '厚度',
  `color` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '颜色',
  `sequence` bigint(20) NULL DEFAULT NULL COMMENT '架上顺序',
  `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '钢化完成时间',
  `line` bigint(20) NULL DEFAULT NULL COMMENT '线路',
  `team_name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '班组名称',
  `type_num` int(11) NULL DEFAULT NULL COMMENT '玻璃类型',
  `is_report` int(11) NULL DEFAULT NULL COMMENT '是否报工（0：未报工；1：可报工；2：准备报工；3：已报工）',
  `rank` int(11) NULL DEFAULT NULL COMMENT '架子号',
  `report_time` date NULL DEFAULT NULL COMMENT '报工时间',
  INDEX `create_time`(`create_time`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for e_record_tempering
-- ----------------------------
DROP TABLE IF EXISTS `e_record_tempering`;
CREATE TABLE `e_record_tempering`  (
  `processcard_id` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '流程卡',
  `glass_id` bigint(20) NULL DEFAULT NULL COMMENT '玻璃id',
  `width` double(20, 2) NULL DEFAULT NULL COMMENT '宽度',
  `height` double(20, 2) NULL DEFAULT NULL COMMENT '高度',
  `thinkess` double(20, 2) NULL DEFAULT NULL COMMENT '厚度',
  `color` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '颜色',
  `sequence` bigint(20) NULL DEFAULT NULL COMMENT '架上顺序',
  `create_time` datetime NULL DEFAULT NULL COMMENT '钢化完成时间',
  `line` bigint(20) NULL DEFAULT NULL COMMENT '线路',
  `team_name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '班组名称',
  `type_num` int(11) NULL DEFAULT NULL COMMENT '玻璃类型',
  `is_report` int(11) NULL DEFAULT NULL COMMENT '是否报工（0：未报工；1：可报工；2：准备报工；3：已报工）',
  `rank` int(11) NULL DEFAULT NULL COMMENT '架子号',
  `report_time` date NULL DEFAULT NULL COMMENT '报工时间',
  `furnace` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '炉子号',
  INDEX `create_time`(`create_time`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for event_tab
-- ----------------------------
DROP TABLE IF EXISTS `event_tab`;
CREATE TABLE `event_tab`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `time_stamp` datetime NULL DEFAULT NULL,
  `eventname` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `eventid` int(11) NULL DEFAULT NULL,
  `machineid` int(11) NULL DEFAULT NULL,
  `oldmsg` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `newmsg` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `flag` int(11) NULL DEFAULT NULL,
  `iswrite` int(11) NULL DEFAULT NULL,
  `invoke` int(11) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 7 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for galss_C
-- ----------------------------
DROP TABLE IF EXISTS `galss_C`;
CREATE TABLE `galss_C`  (
  `id` bigint(20) NOT NULL,
  `color` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '颜色  （白玻）',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_biao_table
-- ----------------------------
DROP TABLE IF EXISTS `gmms_biao_table`;
CREATE TABLE `gmms_biao_table`  (
  `id` int(11) NOT NULL,
  `dongzuo_id` int(11) NULL DEFAULT NULL,
  `da_biao_mo_ban` int(11) NULL DEFAULT NULL,
  `note` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `biao_type` int(11) NULL DEFAULT NULL,
  `color_type` int(11) NULL DEFAULT NULL,
  `fangxiang_type` int(11) NULL DEFAULT NULL,
  `line` int(11) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_cutmanage
-- ----------------------------
DROP TABLE IF EXISTS `gmms_cutmanage`;
CREATE TABLE `gmms_cutmanage`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `width` double NULL DEFAULT NULL COMMENT '宽度',
  `height` double NULL DEFAULT NULL COMMENT '高度',
  `thickness` double NULL DEFAULT NULL COMMENT '厚度',
  `color` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '颜色',
  `pieces` int(11) NULL DEFAULT NULL COMMENT '数量',
  `groups` bigint(20) NULL DEFAULT NULL COMMENT '线路',
  `statrtime` datetime NULL DEFAULT NULL COMMENT '时间',
  `engineering_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工程号',
  `production_time` datetime NULL DEFAULT NULL COMMENT '生产日期',
  `batch_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '批次号',
  `warehousing_time` datetime NULL DEFAULT NULL COMMENT '进仓日期',
  `dengluname` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '切割班组',
  `BgTime` datetime NULL DEFAULT NULL COMMENT '报工时间',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 167574 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_device_state
-- ----------------------------
DROP TABLE IF EXISTS `gmms_device_state`;
CREATE TABLE `gmms_device_state`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `device_id` bigint(20) NULL DEFAULT NULL COMMENT '设备ID',
  `enable` bit(1) NULL DEFAULT NULL COMMENT '总使能',
  `line` int(11) NULL DEFAULT NULL,
  `device_name` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 7 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_dongzuo
-- ----------------------------
DROP TABLE IF EXISTS `gmms_dongzuo`;
CREATE TABLE `gmms_dongzuo`  (
  `number` int(11) NOT NULL,
  `mode` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `note` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `da_biao_mo_ban` int(11) NOT NULL,
  `qing_xi_hou_zhua` int(11) NOT NULL,
  `gang_qian_zhua` int(11) NOT NULL,
  `gang_qian_bu_zhuan` int(11) NOT NULL,
  `gang_qian_zhuan` int(11) NOT NULL,
  `gang_hou_bu_zhuan` int(11) NOT NULL,
  `gang_hou_zhuan` int(11) NOT NULL,
  `gang_hou_zhua` int(11) NOT NULL,
  `zhong_kong_fang` int(11) NOT NULL,
  PRIMARY KEY (`number`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_dongzuo_111
-- ----------------------------
DROP TABLE IF EXISTS `gmms_dongzuo_111`;
CREATE TABLE `gmms_dongzuo_111`  (
  `number` int(11) NOT NULL,
  `mode` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `bo_li_fan_mian` int(11) NOT NULL,
  `da_biao_mo_ban` int(11) NOT NULL,
  `qing_xi_hou_zhua` int(11) NOT NULL,
  `gang_qian_zhua` int(11) NOT NULL,
  `gang_qian_bu_zhuan` int(11) NOT NULL,
  `gang_qian_zhuan` int(11) NOT NULL,
  `gang_hou_bu_zhuan` int(11) NOT NULL,
  `gang_hou_zhuan` int(11) NOT NULL,
  `gang_hou_zhua` int(11) NOT NULL,
  `zhong_kong_fang` int(11) NOT NULL,
  PRIMARY KEY (`number`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_dongzuo_table_copy1
-- ----------------------------
DROP TABLE IF EXISTS `gmms_dongzuo_table_copy1`;
CREATE TABLE `gmms_dongzuo_table_copy1`  (
  `number` int(11) NOT NULL,
  `mode` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `bo_li_fan_mian` int(11) NOT NULL,
  `da_biao_mo_ban` int(11) NOT NULL,
  `qing_xi_hou_zhua` int(11) NOT NULL,
  `gang_qian_zhua` int(11) NOT NULL,
  `gang_qian_bu_zhuan` int(11) NOT NULL,
  `gang_qian_zhuan` int(11) NOT NULL,
  `gang_hou_bu_zhuan` int(11) NOT NULL,
  `gang_hou_zhuan` int(11) NOT NULL,
  `gang_hou_zhua` int(11) NOT NULL,
  `zhong_kong_fang` int(11) NOT NULL,
  PRIMARY KEY (`number`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_drop_frame
-- ----------------------------
DROP TABLE IF EXISTS `gmms_drop_frame`;
CREATE TABLE `gmms_drop_frame`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `drop_frame` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '架号',
  `processcard_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '流程卡号',
  `engineering_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工程号',
  `state` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '0 无架  1 空架 2 非空架  3 半完成（多流程卡有一个完成） 4 完成架',
  `processcard_count` int(11) NOT NULL COMMENT '总流程卡数',
  `finsh_count` int(11) NOT NULL COMMENT '完成数量',
  `groups` bigint(20) NULL DEFAULT NULL,
  `stops` int(11) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 16 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_drop_frame_2
-- ----------------------------
DROP TABLE IF EXISTS `gmms_drop_frame_2`;
CREATE TABLE `gmms_drop_frame_2`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `drop_frame` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '架号',
  `processcard_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '流程卡号',
  `engineering_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工程号',
  `state` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '0 无架  1 空架 2 非空架  3 半完成（多流程卡有一个完成） 4 完成架',
  `processcard_count` int(11) NOT NULL COMMENT '总流程卡数',
  `surplus_count` int(11) NOT NULL COMMENT '剩余数量',
  `groups` bigint(20) NULL DEFAULT NULL COMMENT '线路',
  `stops` int(11) NULL DEFAULT NULL COMMENT '位置',
  `noodles` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT 'A/B面',
  `flag` int(11) NOT NULL COMMENT '标记',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 15 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_drop_frame_3
-- ----------------------------
DROP TABLE IF EXISTS `gmms_drop_frame_3`;
CREATE TABLE `gmms_drop_frame_3`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `drop_frame` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '架号',
  `processcard_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '流程卡号',
  `engineering_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工程号',
  `state` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '0 无架  1 空架 2 非空架  3 半完成（多流程卡有一个完成） 4 完成架',
  `processcard_count` int(11) NOT NULL COMMENT '总流程卡数',
  `finsh_count` int(11) NOT NULL COMMENT '完成数量',
  `groups` bigint(20) NULL DEFAULT NULL,
  `stops` int(11) NULL DEFAULT NULL,
  `rank` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '架子',
  `orientation` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '朝向',
  `stop_rank` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '位置',
  `flag` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '状态',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 9 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_drop_frame_4
-- ----------------------------
DROP TABLE IF EXISTS `gmms_drop_frame_4`;
CREATE TABLE `gmms_drop_frame_4`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `drop_frame` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '架号',
  `processcard_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '流程卡号',
  `engineering_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工程号',
  `state` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '0 无架  1 空架 2 非空架  3 半完成（多流程卡有一个完成） 4 完成架',
  `processcard_count` int(11) NOT NULL COMMENT '总流程卡数',
  `finsh_count` int(11) NOT NULL COMMENT '完成数量',
  `groups` bigint(20) NULL DEFAULT NULL,
  `stops` int(11) NULL DEFAULT NULL,
  `rank` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '架子',
  `orientation` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '朝向',
  `stop_rank` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '位置',
  `flag` int(11) NOT NULL COMMENT '状态',
  `sign` int(11) NOT NULL COMMENT '标记',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 100 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_drop_frame_5
-- ----------------------------
DROP TABLE IF EXISTS `gmms_drop_frame_5`;
CREATE TABLE `gmms_drop_frame_5`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `groups_` bigint(20) NULL DEFAULT NULL COMMENT '线路 : 钢化后1/理片2/中空3/旋转4',
  `drop_type` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工位类型：钢化后/理片/中空/旋转',
  `drop_groups` bigint(20) NULL DEFAULT NULL COMMENT '工位线路',
  `stops` int(11) NULL DEFAULT NULL COMMENT '工位序号',
  `stop_rank` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工位编号',
  `orientation` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '朝向',
  `rank` bigint(255) NULL DEFAULT NULL COMMENT '架子ID',
  `sign` int(11) NOT NULL COMMENT '1进   2正在进  3进完成  4 出  5正在出  6出完成      0需要任务  7不需要任务   ',
  `flag` int(11) NOT NULL COMMENT '状态  ：  启用1 /禁用0',
  `all_groups` bigint(20) NULL DEFAULT NULL COMMENT '任务优先级(\r\n吊装移库400，\r\n中空旋转（旋转退库）300，\r\n中空移库200，\r\n移库100\r\n)',
  `position` double(20, 2) NULL DEFAULT NULL COMMENT '所在位置',
  `rankposition` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '料架出库前的工位',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1042 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_edg_perimeter
-- ----------------------------
DROP TABLE IF EXISTS `gmms_edg_perimeter`;
CREATE TABLE `gmms_edg_perimeter`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `width` decimal(6, 1) NULL DEFAULT NULL COMMENT '宽度',
  `height` decimal(6, 1) NULL DEFAULT NULL COMMENT '高度',
  `start_time` datetime NULL DEFAULT NULL COMMENT '建立时间',
  `groups` bigint(20) NULL DEFAULT NULL COMMENT '线路',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1550998 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_edg_speed
-- ----------------------------
DROP TABLE IF EXISTS `gmms_edg_speed`;
CREATE TABLE `gmms_edg_speed`  (
  `id` int(11) NOT NULL,
  `speed1` int(11) NULL DEFAULT NULL COMMENT '磨边机速度',
  `speed2` int(11) NULL DEFAULT NULL COMMENT '清洗机速度',
  `groups` int(255) NULL DEFAULT NULL COMMENT '线路',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_fragments
-- ----------------------------
DROP TABLE IF EXISTS `gmms_fragments`;
CREATE TABLE `gmms_fragments`  (
  `id` int(11) NOT NULL,
  `state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `fragments_rank` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `width` double NULL DEFAULT NULL,
  `height` double NULL DEFAULT NULL,
  `thickness` double NULL DEFAULT NULL,
  `flag` int(11) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_furnace
-- ----------------------------
DROP TABLE IF EXISTS `gmms_furnace`;
CREATE TABLE `gmms_furnace`  (
  `id` int(11) NOT NULL,
  `width` int(11) NOT NULL,
  `length` int(11) NOT NULL,
  `xinterval` int(11) NOT NULL,
  `yinterval` int(11) NOT NULL,
  `maxLoadingRate` int(3) NOT NULL COMMENT '单炉最大装载率',
  `maxTolerance` int(3) NOT NULL COMMENT '单炉最大尺寸比例',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_furnace_config
-- ----------------------------
DROP TABLE IF EXISTS `gmms_furnace_config`;
CREATE TABLE `gmms_furnace_config`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `width` int(11) NOT NULL COMMENT '宽',
  `length` int(11) NOT NULL COMMENT '长',
  `xinterval` int(11) NOT NULL COMMENT 'x轴间隔',
  `yinterval` int(11) NOT NULL COMMENT 'y轴间隔',
  `description` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '配置描述',
  `line` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '产线',
  `IntRemark` int(255) NULL DEFAULT NULL,
  `StrRemark` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 28 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_galss_task
-- ----------------------------
DROP TABLE IF EXISTS `gmms_galss_task`;
CREATE TABLE `gmms_galss_task`  (
  `id` bigint(20) NOT NULL,
  `rank` bigint(20) NULL DEFAULT 0 COMMENT '测量队列',
  `type_id` bigint(20) NULL DEFAULT NULL COMMENT '类型编号',
  `galss_id` bigint(20) NOT NULL COMMENT '玻璃编号',
  `pattern_id` bigint(20) NULL DEFAULT NULL COMMENT '版面编号',
  `width` double(6, 1) NULL DEFAULT NULL COMMENT '宽度',
  `height` double(6, 1) NULL DEFAULT NULL COMMENT '高度',
  `thickness` double(6, 1) NULL DEFAULT NULL COMMENT '厚度',
  `color` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '颜色',
  `edg_width` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '磨边宽度',
  `edg_height` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '磨边高度',
  `chamfer_size` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '倒角大小',
  `galss_type` int(11) NULL DEFAULT NULL COMMENT '玻璃类型 散单/整单',
  `edg_speed1` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '磨边速度',
  `edg_speed2` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '速度',
  `fine_throwing` int(11) NULL DEFAULT NULL COMMENT '精抛/粗磨',
  `work_state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工作状态',
  `work_procedure` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工序',
  `slicecage_rank` bigint(20) NULL DEFAULT 0 COMMENT '理片笼架子',
  `drop_frame` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '落架',
  `slicecage_flag` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '0' COMMENT '理片笼标志（备用）',
  `out_rank` bigint(20) NOT NULL COMMENT '出理片笼序号',
  `start_time` datetime NOT NULL COMMENT '创建时间',
  `finsh_time` datetime NULL DEFAULT NULL COMMENT '完成时间',
  `stop_position` int(11) NOT NULL COMMENT '下片位置',
  `processcard_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '流程卡号',
  `engineering_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工程号',
  `broken_state` int(11) NOT NULL COMMENT '破片标记，非0破片',
  `stop_rank` int(11) NOT NULL COMMENT '片序',
  `work_procedure_number` int(11) NULL DEFAULT NULL COMMENT '工序标记',
  `gc_number` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '没用',
  `gc_width` double NULL DEFAULT NULL COMMENT '没用',
  `gc_height` double NULL DEFAULT NULL COMMENT '没用',
  `gc_stockid` int(11) NULL DEFAULT NULL COMMENT '没用',
  `marking_flag` int(11) NOT NULL COMMENT '打标',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `pattern_id`(`pattern_id`) USING BTREE,
  INDEX `galss_id`(`galss_id`) USING BTREE,
  INDEX `type_id`(`type_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_galss_task_2
-- ----------------------------
DROP TABLE IF EXISTS `gmms_galss_task_2`;
CREATE TABLE `gmms_galss_task_2`  (
  `id` bigint(20) NOT NULL,
  `rank` bigint(20) NULL DEFAULT 0,
  `type_id` bigint(20) NULL DEFAULT NULL,
  `galss_id` bigint(20) NOT NULL,
  `pattern_id` bigint(20) NULL DEFAULT NULL,
  `width` double(6, 1) NULL DEFAULT NULL,
  `height` double(6, 1) NULL DEFAULT NULL,
  `thickness` double(6, 1) NULL DEFAULT NULL,
  `color` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `edg_width` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `edg_height` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `chamfer_size` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '倒角大小',
  `galss_type` int(11) NULL DEFAULT NULL COMMENT '玻璃类型 散单/整单',
  `edg_speed1` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '磨边速度',
  `edg_speed2` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `fine_throwing` int(11) NULL DEFAULT NULL COMMENT '精抛/粗磨',
  `work_state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `slicecage_rank` bigint(20) NULL DEFAULT 0 COMMENT '理片笼架子',
  `work_procedure` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `drop_frame` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '落架',
  `slicecage_flag` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '0' COMMENT '理片笼标志（备用）',
  `out_rank` bigint(20) NOT NULL COMMENT '出理片笼序号',
  `start_time` datetime NOT NULL,
  `finsh_time` datetime NULL DEFAULT NULL,
  `stop_position` int(11) NOT NULL COMMENT '下片位置',
  `processcard_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '流程卡号',
  `engineering_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工程号',
  `broken_state` int(11) NOT NULL COMMENT '破片标记，非0破片',
  `stop_rank` int(11) NOT NULL,
  `work_procedure_number` int(11) NULL DEFAULT NULL,
  `gc_number` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `gc_width` double NULL DEFAULT NULL,
  `gc_height` double NULL DEFAULT NULL,
  `gc_stockid` int(11) NULL DEFAULT NULL,
  `marking_flag` int(11) NOT NULL COMMENT '打标',
  `orders` int(11) NULL DEFAULT NULL COMMENT '顺序',
  `grouporder` int(11) NULL DEFAULT NULL COMMENT '组',
  `furnace` int(11) NULL DEFAULT NULL COMMENT '炉',
  `angle` int(11) NULL DEFAULT NULL COMMENT '旋转角度  0.0度 1.90度 2.-90度 3.180度 4.-180度',
  `distance` int(11) NULL DEFAULT NULL COMMENT '距离  /mm',
  `groups` bigint(20) NULL DEFAULT 0 COMMENT '线路',
  `pendulum_piece` int(255) NULL DEFAULT NULL COMMENT '1.传送完后等待  2.传送完后传送至钢化片台 3.传送完后传送至钢化摆片台并启动钢化摆片台',
  `banzu` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '班组',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `pattern_id`(`pattern_id`) USING BTREE,
  INDEX `galss_id`(`galss_id`) USING BTREE,
  INDEX `type_id`(`type_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_galss_task_2_copy1
-- ----------------------------
DROP TABLE IF EXISTS `gmms_galss_task_2_copy1`;
CREATE TABLE `gmms_galss_task_2_copy1`  (
  `id` bigint(20) NOT NULL,
  `rank` bigint(20) NULL DEFAULT 0,
  `type_id` bigint(20) NULL DEFAULT NULL,
  `galss_id` bigint(20) NOT NULL,
  `pattern_id` bigint(20) NULL DEFAULT NULL,
  `width` double(6, 1) NULL DEFAULT NULL,
  `height` double(6, 1) NULL DEFAULT NULL,
  `thickness` double(6, 1) NULL DEFAULT NULL,
  `color` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `edg_width` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `edg_height` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `chamfer_size` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '倒角大小',
  `galss_type` int(11) NULL DEFAULT NULL COMMENT '玻璃类型 散单/整单',
  `edg_speed1` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '磨边速度',
  `edg_speed2` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `fine_throwing` int(11) NULL DEFAULT NULL COMMENT '精抛/粗磨',
  `work_state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `slicecage_rank` bigint(20) NULL DEFAULT 0 COMMENT '理片笼架子',
  `work_procedure` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `drop_frame` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '落架',
  `slicecage_flag` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '0' COMMENT '理片笼标志（备用）',
  `out_rank` bigint(20) NOT NULL COMMENT '出理片笼序号',
  `start_time` datetime NOT NULL,
  `finsh_time` datetime NULL DEFAULT NULL,
  `stop_position` int(11) NOT NULL COMMENT '下片位置',
  `processcard_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '流程卡号',
  `engineering_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工程号',
  `broken_state` int(11) NOT NULL COMMENT '破片标记，非0破片',
  `stop_rank` int(11) NOT NULL,
  `work_procedure_number` int(11) NULL DEFAULT NULL,
  `gc_number` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `gc_width` double NULL DEFAULT NULL,
  `gc_height` double NULL DEFAULT NULL,
  `gc_stockid` int(11) NULL DEFAULT NULL,
  `marking_flag` int(11) NOT NULL COMMENT '打标',
  `orders` int(11) NULL DEFAULT NULL COMMENT '顺序',
  `grouporder` int(11) NULL DEFAULT NULL COMMENT '组',
  `furnace` int(11) NULL DEFAULT NULL COMMENT '炉',
  `angle` int(11) NULL DEFAULT NULL COMMENT '旋转角度  0.0度 1.90度 2.-90度 3.180度 4.-180度',
  `distance` int(11) NULL DEFAULT NULL COMMENT '距离  /mm',
  `groups` bigint(20) NULL DEFAULT 0 COMMENT '线路',
  `pendulum_piece` int(255) NULL DEFAULT NULL COMMENT '1.传送完后等待  2.传送完后传送至钢化片台 3.传送完后传送至钢化摆片台并启动钢化摆片台',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `pattern_id`(`pattern_id`) USING BTREE,
  INDEX `galss_id`(`galss_id`) USING BTREE,
  INDEX `type_id`(`type_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_galss_task_buffer
-- ----------------------------
DROP TABLE IF EXISTS `gmms_galss_task_buffer`;
CREATE TABLE `gmms_galss_task_buffer`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `rank` bigint(20) NULL DEFAULT 0,
  `type_id` bigint(20) NULL DEFAULT NULL,
  `galss_id` bigint(20) NOT NULL,
  `pattern_id` bigint(20) NULL DEFAULT NULL,
  `width` double NULL DEFAULT NULL COMMENT '切割的宽',
  `height` double NULL DEFAULT NULL,
  `thickness` double NULL DEFAULT NULL,
  `color` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `edg_width` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '磨后的宽',
  `edg_height` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `chamfer_size` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '倒角大小',
  `galss_type` int(11) NULL DEFAULT NULL COMMENT '玻璃类型 散单/整单',
  `edg_speed1` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '磨边速度',
  `edg_speed2` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `fine_throwing` int(11) NULL DEFAULT NULL COMMENT '精抛/粗磨',
  `work_state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `work_procedure` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `slicecage_rank` bigint(20) NULL DEFAULT 0 COMMENT '理片笼架子',
  `drop_frame` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '落架',
  `slicecage_flag` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '0' COMMENT '理片笼标志（备用）',
  `out_rank` bigint(20) NOT NULL COMMENT '出理片笼序号',
  `start_time` datetime NOT NULL,
  `finsh_time` datetime NULL DEFAULT NULL,
  `stop_position` int(11) NULL DEFAULT NULL COMMENT '下片位置',
  `processcard_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '流程卡号',
  `engineering_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工程号',
  `broken_state` int(11) NOT NULL COMMENT '破片标记，非0破片',
  `stop_rank` int(11) NOT NULL COMMENT '落架顺序',
  `work_procedure_number` int(11) NULL DEFAULT NULL,
  `gc_number` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `gc_width` double NULL DEFAULT NULL,
  `gc_height` double NULL DEFAULT NULL,
  `gc_stockid` int(11) NULL DEFAULT NULL,
  `marking_flag` int(11) NOT NULL COMMENT '打标，(0不钢化横标1不钢化竖标2钢化横标3钢化竖标)',
  `w` double(11, 0) NOT NULL DEFAULT 0,
  `h` double(11, 0) NOT NULL DEFAULT 0,
  `loucengbianhao` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '楼层编号',
  `marking_flag_ex` int(11) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `pattern_id`(`pattern_id`) USING BTREE,
  INDEX `galss_id`(`galss_id`) USING BTREE,
  INDEX `type_id`(`type_id`) USING BTREE,
  INDEX `gc`(`engineering_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1779094 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_galss_task_details
-- ----------------------------
DROP TABLE IF EXISTS `gmms_galss_task_details`;
CREATE TABLE `gmms_galss_task_details`  (
  `id` bigint(20) NOT NULL,
  `rank` bigint(20) NULL DEFAULT 0 COMMENT '测量队列',
  `type_id` bigint(20) NULL DEFAULT NULL COMMENT '类型编号',
  `galss_id` bigint(20) NOT NULL COMMENT '玻璃编号',
  `pattern_id` bigint(20) NULL DEFAULT NULL COMMENT '版面编号',
  `width` double(6, 1) NULL DEFAULT NULL COMMENT '宽度',
  `height` double(6, 1) NULL DEFAULT NULL COMMENT '高度',
  `thickness` double(6, 1) NULL DEFAULT NULL COMMENT '厚度',
  `color` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '颜色',
  `edg_width` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '磨边宽度',
  `edg_height` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '磨边高度',
  `chamfer_size` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '倒角大小',
  `galss_type` int(11) NULL DEFAULT NULL COMMENT '玻璃类型 散单/整单',
  `edg_speed1` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '磨边速度',
  `edg_speed2` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '速度',
  `fine_throwing` int(11) NULL DEFAULT NULL COMMENT '精抛/粗磨',
  `work_state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工作状态',
  `work_procedure` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工序',
  `slicecage_rank` bigint(20) NULL DEFAULT 0 COMMENT '理片笼架子',
  `drop_frame` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '落架',
  `slicecage_flag` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '0' COMMENT '理片笼标志（备用）',
  `out_rank` bigint(20) NOT NULL COMMENT '出理片笼序号',
  `start_time` datetime NOT NULL COMMENT '创建时间',
  `finsh_time` datetime NULL DEFAULT NULL COMMENT '完成时间',
  `stop_position` int(11) NOT NULL COMMENT '下片位置',
  `processcard_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '流程卡号',
  `engineering_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工程号',
  `broken_state` int(11) NOT NULL COMMENT '破片标记，非0破片',
  `stop_rank` int(11) NOT NULL COMMENT '片序',
  `work_procedure_number` int(11) NULL DEFAULT NULL COMMENT '工序标记',
  `gc_number` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '没用',
  `gc_width` double NULL DEFAULT NULL COMMENT '没用',
  `gc_height` double NULL DEFAULT NULL COMMENT '没用',
  `gc_stockid` int(11) NULL DEFAULT NULL COMMENT '没用',
  `marking_flag` int(11) NOT NULL COMMENT '打标',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `pattern_id`(`pattern_id`) USING BTREE,
  INDEX `galss_id`(`galss_id`) USING BTREE,
  INDEX `type_id`(`type_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_galss_task_hollow
-- ----------------------------
DROP TABLE IF EXISTS `gmms_galss_task_hollow`;
CREATE TABLE `gmms_galss_task_hollow`  (
  `id` bigint(20) NOT NULL,
  `rank` bigint(20) NULL DEFAULT 0 COMMENT '测量队列',
  `type_id` bigint(20) NULL DEFAULT NULL COMMENT '类型编号',
  `galss_id` bigint(20) NOT NULL COMMENT '玻璃编号',
  `pattern_id` bigint(20) NULL DEFAULT NULL COMMENT '版面编号',
  `width` double(6, 1) NULL DEFAULT NULL COMMENT '宽度',
  `height` double(6, 1) NULL DEFAULT NULL COMMENT '高度',
  `thickness` double(6, 1) NULL DEFAULT NULL COMMENT '厚度',
  `color` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '颜色',
  `edg_width` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '磨边宽度',
  `edg_height` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '磨边高度',
  `chamfer_size` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '倒角大小',
  `galss_type` int(11) NULL DEFAULT NULL COMMENT '玻璃类型 散单/整单',
  `edg_speed1` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '磨边速度',
  `edg_speed2` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '速度',
  `fine_throwing` int(11) NULL DEFAULT NULL COMMENT '精抛/粗磨',
  `work_state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工作状态',
  `work_procedure` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工序',
  `slicecage_rank` bigint(20) NULL DEFAULT 0 COMMENT '理片笼架子',
  `drop_frame` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '落架',
  `slicecage_flag` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '0' COMMENT '理片笼标志（备用）',
  `out_rank` bigint(20) NOT NULL COMMENT '出理片笼序号',
  `start_time` datetime NOT NULL COMMENT '创建时间',
  `finsh_time` datetime NULL DEFAULT NULL COMMENT '完成时间',
  `stop_position` int(11) NOT NULL COMMENT '下片位置',
  `processcard_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '流程卡号',
  `engineering_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工程号',
  `broken_state` int(11) NOT NULL COMMENT '破片标记，非0破片',
  `stop_rank` int(11) NOT NULL COMMENT '片序',
  `work_procedure_number` int(11) NULL DEFAULT NULL COMMENT '工序标记',
  `gc_number` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '没用',
  `gc_width` double NULL DEFAULT NULL COMMENT '没用',
  `gc_height` double NULL DEFAULT NULL COMMENT '没用',
  `gc_stockid` int(11) NULL DEFAULT NULL COMMENT '没用',
  `marking_flag` int(11) NOT NULL COMMENT '打标',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `pattern_id`(`pattern_id`) USING BTREE,
  INDEX `galss_id`(`galss_id`) USING BTREE,
  INDEX `type_id`(`type_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_galss_task_interrupt
-- ----------------------------
DROP TABLE IF EXISTS `gmms_galss_task_interrupt`;
CREATE TABLE `gmms_galss_task_interrupt`  (
  `id` bigint(20) NOT NULL,
  `rank` bigint(20) NULL DEFAULT 0 COMMENT '测量队列',
  `type_id` bigint(20) NULL DEFAULT NULL COMMENT '类型编号',
  `galss_id` bigint(20) NOT NULL COMMENT '玻璃编号',
  `pattern_id` bigint(20) NULL DEFAULT NULL COMMENT '版面编号',
  `width` double(6, 1) NULL DEFAULT NULL COMMENT '宽度',
  `height` double(6, 1) NULL DEFAULT NULL COMMENT '高度',
  `thickness` double(6, 1) NULL DEFAULT NULL COMMENT '厚度',
  `color` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '颜色',
  `edg_width` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '磨边宽度',
  `edg_height` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '磨边高度',
  `chamfer_size` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '倒角大小',
  `galss_type` int(11) NULL DEFAULT NULL COMMENT '玻璃类型 散单/整单',
  `edg_speed1` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '磨边速度',
  `edg_speed2` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '速度',
  `fine_throwing` int(11) NULL DEFAULT NULL COMMENT '精抛/粗磨',
  `work_state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工作状态',
  `work_procedure` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工序',
  `slicecage_rank` bigint(20) NULL DEFAULT 0 COMMENT '理片笼架子',
  `drop_frame` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '落架',
  `slicecage_flag` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '0' COMMENT '理片笼标志（备用）',
  `out_rank` bigint(20) NOT NULL COMMENT '出理片笼序号',
  `start_time` datetime NOT NULL COMMENT '创建时间',
  `finsh_time` datetime NULL DEFAULT NULL COMMENT '完成时间',
  `stop_position` int(11) NOT NULL COMMENT '下片位置',
  `processcard_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '流程卡号',
  `engineering_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工程号',
  `broken_state` int(11) NOT NULL COMMENT '破片标记，非0破片',
  `stop_rank` int(11) NOT NULL COMMENT '片序',
  `work_procedure_number` int(11) NULL DEFAULT NULL COMMENT '工序标记',
  `gc_number` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '没用',
  `gc_width` double NULL DEFAULT NULL COMMENT '没用',
  `gc_height` double NULL DEFAULT NULL COMMENT '没用',
  `gc_stockid` int(11) NULL DEFAULT NULL COMMENT '没用',
  `marking_flag` int(11) NOT NULL COMMENT '打标',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `pattern_id`(`pattern_id`) USING BTREE,
  INDEX `galss_id`(`galss_id`) USING BTREE,
  INDEX `type_id`(`type_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_galss_task_shelf
-- ----------------------------
DROP TABLE IF EXISTS `gmms_galss_task_shelf`;
CREATE TABLE `gmms_galss_task_shelf`  (
  `id` bigint(20) NOT NULL,
  `rank` bigint(20) NULL DEFAULT 0,
  `type_id` bigint(20) NULL DEFAULT NULL,
  `galss_id` bigint(20) NOT NULL,
  `pattern_id` bigint(20) NULL DEFAULT NULL,
  `width` double(6, 1) NULL DEFAULT NULL,
  `height` double(6, 1) NULL DEFAULT NULL,
  `thickness` double(6, 1) NULL DEFAULT NULL,
  `color` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `edg_width` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `edg_height` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `chamfer_size` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '倒角大小',
  `galss_type` int(11) NULL DEFAULT NULL COMMENT '玻璃类型 散单/整单',
  `edg_speed1` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '磨边速度',
  `edg_speed2` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `fine_throwing` int(11) NULL DEFAULT NULL COMMENT '精抛/粗磨',
  `work_state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `work_procedure` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `slicecage_rank` bigint(20) NULL DEFAULT 0 COMMENT '理片笼架子',
  `drop_frame` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '落架',
  `slicecage_flag` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '0' COMMENT '理片笼标志（备用）',
  `out_rank` bigint(20) NOT NULL COMMENT '出理片笼序号',
  `start_time` datetime NOT NULL,
  `finsh_time` datetime NULL DEFAULT NULL,
  `stop_position` int(11) NOT NULL COMMENT '下片位置',
  `processcard_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '流程卡号',
  `engineering_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工程号',
  `broken_state` int(11) NOT NULL COMMENT '破片标记，非0破片',
  `stop_rank` int(11) NOT NULL,
  `work_procedure_number` int(11) NULL DEFAULT NULL,
  `gc_number` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `gc_width` double NULL DEFAULT NULL,
  `gc_height` double NULL DEFAULT NULL,
  `gc_stockid` int(11) NULL DEFAULT NULL,
  `marking_flag` int(11) NOT NULL COMMENT '打标',
  `groupsss` int(11) NULL DEFAULT NULL COMMENT '线路',
  `banzu` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '班组',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `pattern_id`(`pattern_id`) USING BTREE,
  INDEX `galss_id`(`galss_id`) USING BTREE,
  INDEX `type_id`(`type_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_galss_task_shelf_copy1
-- ----------------------------
DROP TABLE IF EXISTS `gmms_galss_task_shelf_copy1`;
CREATE TABLE `gmms_galss_task_shelf_copy1`  (
  `id` bigint(20) NOT NULL,
  `rank` bigint(20) NULL DEFAULT 0,
  `type_id` bigint(20) NULL DEFAULT NULL,
  `galss_id` bigint(20) NOT NULL,
  `pattern_id` bigint(20) NULL DEFAULT NULL,
  `width` double(6, 1) NULL DEFAULT NULL,
  `height` double(6, 1) NULL DEFAULT NULL,
  `thickness` double(6, 1) NULL DEFAULT NULL,
  `color` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `edg_width` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `edg_height` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `chamfer_size` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '倒角大小',
  `galss_type` int(11) NULL DEFAULT NULL COMMENT '玻璃类型 散单/整单',
  `edg_speed1` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '磨边速度',
  `edg_speed2` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `fine_throwing` int(11) NULL DEFAULT NULL COMMENT '精抛/粗磨',
  `work_state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `work_procedure` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `slicecage_rank` bigint(20) NULL DEFAULT 0 COMMENT '理片笼架子',
  `drop_frame` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '落架',
  `slicecage_flag` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '0' COMMENT '理片笼标志（备用）',
  `out_rank` bigint(20) NOT NULL COMMENT '出理片笼序号',
  `start_time` datetime NOT NULL,
  `finsh_time` datetime NULL DEFAULT NULL,
  `stop_position` int(11) NOT NULL COMMENT '下片位置',
  `processcard_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '流程卡号',
  `engineering_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工程号',
  `broken_state` int(11) NOT NULL COMMENT '破片标记，非0破片',
  `stop_rank` int(11) NOT NULL,
  `work_procedure_number` int(11) NULL DEFAULT NULL,
  `gc_number` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `gc_width` double NULL DEFAULT NULL,
  `gc_height` double NULL DEFAULT NULL,
  `gc_stockid` int(11) NULL DEFAULT NULL,
  `marking_flag` int(11) NOT NULL COMMENT '打标',
  `youhuashijian` datetime NULL DEFAULT NULL,
  `youhua` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `pattern_id`(`pattern_id`) USING BTREE,
  INDEX `galss_id`(`galss_id`) USING BTREE,
  INDEX `type_id`(`type_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_galss_task_shelf_copy2
-- ----------------------------
DROP TABLE IF EXISTS `gmms_galss_task_shelf_copy2`;
CREATE TABLE `gmms_galss_task_shelf_copy2`  (
  `id` bigint(20) NOT NULL,
  `rank` bigint(20) NULL DEFAULT 0,
  `type_id` bigint(20) NULL DEFAULT NULL,
  `galss_id` bigint(20) NOT NULL,
  `pattern_id` bigint(20) NULL DEFAULT NULL,
  `width` double(6, 1) NULL DEFAULT NULL,
  `height` double(6, 1) NULL DEFAULT NULL,
  `thickness` double(6, 1) NULL DEFAULT NULL,
  `color` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `edg_width` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `edg_height` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `chamfer_size` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '倒角大小',
  `galss_type` int(11) NULL DEFAULT NULL COMMENT '玻璃类型 散单/整单',
  `edg_speed1` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '磨边速度',
  `edg_speed2` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `fine_throwing` int(11) NULL DEFAULT NULL COMMENT '精抛/粗磨',
  `work_state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `work_procedure` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `slicecage_rank` bigint(20) NULL DEFAULT 0 COMMENT '理片笼架子',
  `drop_frame` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '落架',
  `slicecage_flag` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '0' COMMENT '理片笼标志（备用）',
  `out_rank` bigint(20) NOT NULL COMMENT '出理片笼序号',
  `start_time` datetime NOT NULL,
  `finsh_time` datetime NULL DEFAULT NULL,
  `stop_position` int(11) NOT NULL COMMENT '下片位置',
  `processcard_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '流程卡号',
  `engineering_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工程号',
  `broken_state` int(11) NOT NULL COMMENT '破片标记，非0破片',
  `stop_rank` int(11) NOT NULL,
  `work_procedure_number` int(11) NULL DEFAULT NULL,
  `gc_number` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `gc_width` double NULL DEFAULT NULL,
  `gc_height` double NULL DEFAULT NULL,
  `gc_stockid` int(11) NULL DEFAULT NULL,
  `marking_flag` int(11) NOT NULL COMMENT '打标',
  `groupsss` int(11) NULL DEFAULT NULL COMMENT '线路',
  `banzu` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '班组',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `pattern_id`(`pattern_id`) USING BTREE,
  INDEX `galss_id`(`galss_id`) USING BTREE,
  INDEX `type_id`(`type_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_galss_task_shelf_copy3
-- ----------------------------
DROP TABLE IF EXISTS `gmms_galss_task_shelf_copy3`;
CREATE TABLE `gmms_galss_task_shelf_copy3`  (
  `id` bigint(20) NOT NULL,
  `rank` bigint(20) NULL DEFAULT 0,
  `type_id` bigint(20) NULL DEFAULT NULL,
  `galss_id` bigint(20) NOT NULL,
  `pattern_id` bigint(20) NULL DEFAULT NULL,
  `width` double(6, 1) NULL DEFAULT NULL,
  `height` double(6, 1) NULL DEFAULT NULL,
  `thickness` double(6, 1) NULL DEFAULT NULL,
  `color` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `edg_width` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `edg_height` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `chamfer_size` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '倒角大小',
  `galss_type` int(11) NULL DEFAULT NULL COMMENT '玻璃类型 散单/整单',
  `edg_speed1` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '磨边速度',
  `edg_speed2` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `fine_throwing` int(11) NULL DEFAULT NULL COMMENT '精抛/粗磨',
  `work_state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `work_procedure` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `slicecage_rank` bigint(20) NULL DEFAULT 0 COMMENT '理片笼架子',
  `drop_frame` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '落架',
  `slicecage_flag` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '0' COMMENT '理片笼标志（备用）',
  `out_rank` bigint(20) NOT NULL COMMENT '出理片笼序号',
  `start_time` datetime NOT NULL,
  `finsh_time` datetime NULL DEFAULT NULL,
  `stop_position` int(11) NOT NULL COMMENT '下片位置',
  `processcard_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '流程卡号',
  `engineering_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工程号',
  `broken_state` int(11) NOT NULL COMMENT '破片标记，非0破片',
  `stop_rank` int(11) NOT NULL,
  `work_procedure_number` int(11) NULL DEFAULT NULL,
  `gc_number` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `gc_width` double NULL DEFAULT NULL,
  `gc_height` double NULL DEFAULT NULL,
  `gc_stockid` int(11) NULL DEFAULT NULL,
  `marking_flag` int(11) NOT NULL COMMENT '打标',
  `groupsss` int(11) NULL DEFAULT NULL COMMENT '线路',
  `banzu` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '班组',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `pattern_id`(`pattern_id`) USING BTREE,
  INDEX `galss_id`(`galss_id`) USING BTREE,
  INDEX `type_id`(`type_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_galss_task_shelf_log
-- ----------------------------
DROP TABLE IF EXISTS `gmms_galss_task_shelf_log`;
CREATE TABLE `gmms_galss_task_shelf_log`  (
  `id` bigint(20) NOT NULL,
  `rank` bigint(20) NULL DEFAULT 0,
  `type_id` bigint(20) NULL DEFAULT NULL,
  `galss_id` bigint(20) NOT NULL,
  `pattern_id` bigint(20) NULL DEFAULT NULL,
  `width` double(6, 1) NULL DEFAULT NULL,
  `height` double(6, 1) NULL DEFAULT NULL,
  `thickness` double(6, 1) NULL DEFAULT NULL,
  `color` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `edg_width` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `edg_height` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `chamfer_size` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '倒角大小',
  `galss_type` int(11) NULL DEFAULT NULL COMMENT '玻璃类型 散单/整单',
  `edg_speed1` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '磨边速度',
  `edg_speed2` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `fine_throwing` int(11) NULL DEFAULT NULL COMMENT '精抛/粗磨',
  `work_state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `work_procedure` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `slicecage_rank` bigint(20) NULL DEFAULT 0 COMMENT '理片笼架子',
  `drop_frame` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '落架',
  `slicecage_flag` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '0' COMMENT '理片笼标志（备用）',
  `out_rank` bigint(20) NOT NULL COMMENT '出理片笼序号',
  `start_time` datetime NOT NULL,
  `finsh_time` datetime NULL DEFAULT NULL,
  `stop_position` int(11) NOT NULL COMMENT '下片位置',
  `processcard_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '流程卡号',
  `engineering_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工程号',
  `broken_state` int(11) NOT NULL COMMENT '破片标记，非0破片',
  `stop_rank` int(11) NOT NULL,
  `work_procedure_number` int(11) NULL DEFAULT NULL,
  `gc_number` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `gc_width` double NULL DEFAULT NULL,
  `gc_height` double NULL DEFAULT NULL,
  `gc_stockid` int(11) NULL DEFAULT NULL,
  `marking_flag` int(11) NOT NULL COMMENT '打标',
  `groupsss` int(11) NULL DEFAULT NULL COMMENT '线路',
  `banzu` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '班组',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `pattern_id`(`pattern_id`) USING BTREE,
  INDEX `galss_id`(`galss_id`) USING BTREE,
  INDEX `type_id`(`type_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_galss_task_slicecage
-- ----------------------------
DROP TABLE IF EXISTS `gmms_galss_task_slicecage`;
CREATE TABLE `gmms_galss_task_slicecage`  (
  `id` bigint(20) NOT NULL,
  `rank` bigint(20) NULL DEFAULT 0 COMMENT '测量队列',
  `type_id` bigint(20) NULL DEFAULT NULL COMMENT '类型编号',
  `galss_id` bigint(20) NOT NULL COMMENT '玻璃编号',
  `pattern_id` bigint(20) NULL DEFAULT NULL COMMENT '版面编号',
  `width` double(6, 1) NULL DEFAULT NULL COMMENT '宽度',
  `height` double(6, 1) NULL DEFAULT NULL COMMENT '高度',
  `thickness` double(6, 1) NULL DEFAULT NULL COMMENT '厚度',
  `color` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '颜色',
  `edg_width` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '磨边宽度',
  `edg_height` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '磨边高度',
  `chamfer_size` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '倒角大小',
  `galss_type` int(11) NULL DEFAULT NULL COMMENT '玻璃类型 散单/整单',
  `edg_speed1` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '磨边速度',
  `edg_speed2` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '速度',
  `fine_throwing` int(11) NULL DEFAULT NULL COMMENT '精抛/粗磨',
  `work_state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工作状态',
  `work_procedure` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工序',
  `slicecage_rank` bigint(20) NULL DEFAULT 0 COMMENT '理片笼架子',
  `drop_frame` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '落架',
  `slicecage_flag` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '0' COMMENT '理片笼标志（备用）',
  `out_rank` bigint(20) NOT NULL COMMENT '出理片笼序号',
  `start_time` datetime NOT NULL COMMENT '创建时间',
  `finsh_time` datetime NULL DEFAULT NULL COMMENT '完成时间',
  `stop_position` int(11) NOT NULL COMMENT '下片位置',
  `processcard_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '流程卡号',
  `engineering_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工程号',
  `broken_state` int(11) NOT NULL COMMENT '破片标记，非0破片',
  `stop_rank` int(11) NOT NULL COMMENT '片序',
  `work_procedure_number` int(11) NULL DEFAULT NULL COMMENT '工序标记',
  `gc_number` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '没用',
  `gc_width` double NULL DEFAULT NULL COMMENT '没用',
  `gc_height` double NULL DEFAULT NULL COMMENT '没用',
  `gc_stockid` int(11) NULL DEFAULT NULL COMMENT '没用',
  `marking_flag` int(11) NOT NULL COMMENT '打标',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `pattern_id`(`pattern_id`) USING BTREE,
  INDEX `galss_id`(`galss_id`) USING BTREE,
  INDEX `type_id`(`type_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_galss_task_stock
-- ----------------------------
DROP TABLE IF EXISTS `gmms_galss_task_stock`;
CREATE TABLE `gmms_galss_task_stock`  (
  `id` bigint(20) NOT NULL,
  `rank` bigint(20) NULL DEFAULT 0 COMMENT '测量队列',
  `type_id` bigint(20) NULL DEFAULT NULL COMMENT '类型编号',
  `galss_id` bigint(20) NOT NULL COMMENT '玻璃编号',
  `pattern_id` bigint(20) NULL DEFAULT NULL COMMENT '版面编号',
  `width` double(6, 1) NULL DEFAULT NULL COMMENT '宽度',
  `height` double(6, 1) NULL DEFAULT NULL COMMENT '高度',
  `thickness` double(6, 1) NULL DEFAULT NULL COMMENT '厚度',
  `color` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '颜色',
  `edg_width` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '磨边宽度',
  `edg_height` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '磨边高度',
  `chamfer_size` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '倒角大小',
  `galss_type` int(11) NULL DEFAULT NULL COMMENT '玻璃类型 散单/整单',
  `edg_speed1` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '磨边速度',
  `edg_speed2` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '速度',
  `fine_throwing` int(11) NULL DEFAULT NULL COMMENT '精抛/粗磨',
  `work_state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工作状态',
  `work_procedure` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工序',
  `slicecage_rank` bigint(20) NULL DEFAULT 0 COMMENT '理片笼架子',
  `drop_frame` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '落架',
  `slicecage_flag` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '0' COMMENT '理片笼标志（备用）',
  `out_rank` bigint(20) NOT NULL COMMENT '出理片笼序号',
  `start_time` datetime NOT NULL COMMENT '创建时间',
  `finsh_time` datetime NULL DEFAULT NULL COMMENT '完成时间',
  `stop_position` int(11) NOT NULL COMMENT '下片位置',
  `processcard_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '流程卡号',
  `engineering_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工程号',
  `broken_state` int(11) NOT NULL COMMENT '破片标记，非0破片',
  `stop_rank` int(11) NOT NULL COMMENT '片序',
  `work_procedure_number` int(11) NULL DEFAULT NULL COMMENT '工序标记',
  `gc_number` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '没用',
  `gc_width` double NULL DEFAULT NULL COMMENT '没用',
  `gc_height` double NULL DEFAULT NULL COMMENT '没用',
  `gc_stockid` int(11) NULL DEFAULT NULL COMMENT '没用',
  `marking_flag` int(11) NOT NULL COMMENT '打标',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `pattern_id`(`pattern_id`) USING BTREE,
  INDEX `galss_id`(`galss_id`) USING BTREE,
  INDEX `type_id`(`type_id`) USING BTREE,
  INDEX `finsh_time`(`finsh_time`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_galss_task_stock1
-- ----------------------------
DROP TABLE IF EXISTS `gmms_galss_task_stock1`;
CREATE TABLE `gmms_galss_task_stock1`  (
  `id` bigint(20) NOT NULL,
  `rank` bigint(20) NULL DEFAULT 0,
  `type_id` bigint(20) NULL DEFAULT NULL,
  `galss_id` bigint(20) NOT NULL,
  `pattern_id` bigint(20) NULL DEFAULT NULL,
  `width` double(6, 1) NULL DEFAULT NULL,
  `height` double(6, 1) NULL DEFAULT NULL,
  `thickness` double(6, 1) NULL DEFAULT NULL,
  `color` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `edg_width` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `edg_height` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `chamfer_size` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '倒角大小',
  `galss_type` int(11) NULL DEFAULT NULL COMMENT '玻璃类型 散单/整单',
  `edg_speed1` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '磨边速度',
  `edg_speed2` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `fine_throwing` int(11) NULL DEFAULT NULL COMMENT '精抛/粗磨',
  `work_state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `work_procedure` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `slicecage_rank` bigint(20) NULL DEFAULT 0 COMMENT '理片笼架子',
  `drop_frame` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '落架',
  `slicecage_flag` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '0' COMMENT '理片笼标志（备用）',
  `out_rank` bigint(20) NOT NULL COMMENT '出理片笼序号',
  `start_time` datetime NOT NULL,
  `finsh_time` datetime NULL DEFAULT NULL,
  `stop_position` int(11) NOT NULL COMMENT '下片位置',
  `processcard_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '流程卡号',
  `engineering_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工程号',
  `broken_state` int(11) NOT NULL COMMENT '破片标记，非0破片',
  `stop_rank` int(11) NOT NULL,
  `work_procedure_number` int(11) NULL DEFAULT NULL,
  `gc_number` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `gc_width` double NULL DEFAULT NULL,
  `gc_height` double NULL DEFAULT NULL,
  `gc_stockid` int(11) NULL DEFAULT NULL,
  `marking_flag` int(11) NOT NULL COMMENT '打标',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `pattern_id`(`pattern_id`) USING BTREE,
  INDEX `galss_id`(`galss_id`) USING BTREE,
  INDEX `type_id`(`type_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_ganghuatuijian_log
-- ----------------------------
DROP TABLE IF EXISTS `gmms_ganghuatuijian_log`;
CREATE TABLE `gmms_ganghuatuijian_log`  (
  `Id` int(11) NOT NULL AUTO_INCREMENT,
  `Parameter` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '参数',
  `Result` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '结果',
  `Result_Detail` text CHARACTER SET utf8 COLLATE utf8_general_ci NULL,
  `StrRemark` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `IntRemark` int(255) NULL DEFAULT NULL,
  `CreateTime` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`Id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 35 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_glass_log
-- ----------------------------
DROP TABLE IF EXISTS `gmms_glass_log`;
CREATE TABLE `gmms_glass_log`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `main_id` bigint(20) NULL DEFAULT NULL,
  `glass_id` bigint(20) NULL DEFAULT NULL COMMENT '玻璃ID',
  `segment_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `work_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `line` int(11) NULL DEFAULT NULL COMMENT '所在线',
  `begin_time` datetime NULL DEFAULT NULL COMMENT '开始时间',
  `end_time` datetime NULL DEFAULT NULL COMMENT '结束时间',
  `status` int(11) NULL DEFAULT NULL COMMENT '状态标志',
  `parent_id` bigint(20) NULL DEFAULT NULL,
  `area` decimal(20, 7) NULL DEFAULT NULL COMMENT '面积',
  `freeze` bit(1) NULL DEFAULT b'0' COMMENT '冻结,不可改',
  `time_out` bit(1) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `end_time_idx`(`end_time`) USING BTREE,
  INDEX `gid`(`glass_id`) USING BTREE,
  CONSTRAINT `g_id_f` FOREIGN KEY (`glass_id`) REFERENCES `gmms_galss_task_buffer` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE = InnoDB AUTO_INCREMENT = 3949683 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_glass_log_config
-- ----------------------------
DROP TABLE IF EXISTS `gmms_glass_log_config`;
CREATE TABLE `gmms_glass_log_config`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `work_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '记录名',
  `status` int(11) NULL DEFAULT NULL COMMENT '1   第一道   2  中间工序   3 最后一道  ',
  `work_proc` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT 'mes 工作名',
  `work_status` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT 'mes 工作状态',
  `line` int(11) NULL DEFAULT NULL COMMENT '配置所属线号',
  `segment` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '配置所属工艺段',
  `segment_number` int(11) NULL DEFAULT NULL COMMENT '分段编号   相同代表相同工艺  数字大的在后面',
  `work_number` int(11) NULL DEFAULT NULL COMMENT '工艺序号   在本工艺分段中的编号，大的在后',
  `segment_status` tinyint(4) NULL DEFAULT NULL COMMENT ' 0  开始    1  中间   2  完成   3 破损（破损暂时不用）',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 89 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_glass_log_main
-- ----------------------------
DROP TABLE IF EXISTS `gmms_glass_log_main`;
CREATE TABLE `gmms_glass_log_main`  (
  `id` bigint(11) NOT NULL AUTO_INCREMENT,
  `segment` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `gongchenghao` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `create_time` datetime NULL DEFAULT NULL,
  `line` int(11) NULL DEFAULT NULL,
  `state` int(11) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 377 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_glass_number_color
-- ----------------------------
DROP TABLE IF EXISTS `gmms_glass_number_color`;
CREATE TABLE `gmms_glass_number_color`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `glass_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '玻璃类型',
  `flag` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '标记',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 94 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_glass_yuliao
-- ----------------------------
DROP TABLE IF EXISTS `gmms_glass_yuliao`;
CREATE TABLE `gmms_glass_yuliao`  (
  `Id` int(11) NOT NULL AUTO_INCREMENT,
  `gongchenghao` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工程号',
  `wuliaodaima` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '物料代码',
  `bantuxuhao` int(255) NULL DEFAULT NULL COMMENT '版图序号',
  `moxi` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '膜系',
  `houdu` int(255) NULL DEFAULT NULL COMMENT '厚度',
  `kuan` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '宽',
  `gao` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '高',
  `shuliang` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '数量',
  `mianji` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '面积',
  `ruku` int(255) NULL DEFAULT 0 COMMENT '是否入库：0否1是，默认0',
  `shiyong` int(255) NULL DEFAULT 0 COMMENT '是否使用：0否1是，默认0',
  `shanchu` int(255) NULL DEFAULT 0 COMMENT '是否删除：0否1是，默认0',
  `IntRemark` int(255) NULL DEFAULT NULL COMMENT '预留列，占用时注明用途',
  `StrRemark` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '预留列，占用是注明用途',
  `UpdateTime` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `CreateTime` datetime NULL DEFAULT NULL COMMENT '创建时间',
  PRIMARY KEY (`Id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 341468 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_globel_var
-- ----------------------------
DROP TABLE IF EXISTS `gmms_globel_var`;
CREATE TABLE `gmms_globel_var`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `line` int(255) NULL DEFAULT NULL,
  `var_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `int_value` int(255) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_horizontal
-- ----------------------------
DROP TABLE IF EXISTS `gmms_horizontal`;
CREATE TABLE `gmms_horizontal`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `slicecage_rank` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '架号',
  `state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '状态',
  `type_id` bigint(20) NOT NULL COMMENT '类型编号',
  `count` int(11) NOT NULL DEFAULT 0 COMMENT '数量',
  `flag` int(20) NOT NULL DEFAULT 0 COMMENT '0/1',
  `gourp` bigint(20) NULL DEFAULT NULL COMMENT '线路',
  `processcard_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '流程卡号',
  `Update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `glass_id`(`type_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 101 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_identify_error
-- ----------------------------
DROP TABLE IF EXISTS `gmms_identify_error`;
CREATE TABLE `gmms_identify_error`  (
  `id` int(11) NOT NULL,
  `widtherror` double(4, 1) NULL DEFAULT NULL COMMENT '宽度误差',
  `heigtherror` double(4, 1) NULL DEFAULT NULL COMMENT '高度误差',
  `groups` int(11) NULL DEFAULT NULL COMMENT '线路',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_identify_error_details
-- ----------------------------
DROP TABLE IF EXISTS `gmms_identify_error_details`;
CREATE TABLE `gmms_identify_error_details`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `galssid` bigint(20) NULL DEFAULT NULL COMMENT '玻璃编号',
  `width` double(6, 1) NULL DEFAULT NULL COMMENT '宽度',
  `heigth` double(6, 1) NULL DEFAULT NULL COMMENT '高度',
  `error_width` double(6, 1) NULL DEFAULT NULL COMMENT '测量宽度',
  `error_heigth` double(6, 1) NULL DEFAULT NULL COMMENT '测量高度',
  `groups` bigint(20) NULL DEFAULT NULL COMMENT '线路',
  `times` datetime NULL DEFAULT NULL COMMENT '时间',
  `flag` int(11) NOT NULL,
  `content` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '匹配内容',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2588657 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_identify_mark
-- ----------------------------
DROP TABLE IF EXISTS `gmms_identify_mark`;
CREATE TABLE `gmms_identify_mark`  (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `Line` tinyint(11) NULL DEFAULT NULL COMMENT '生产线',
  `GHMark` tinyint(1) NULL DEFAULT NULL COMMENT '钢化是否打标：0不打标1打标',
  `DBMark` tinyint(1) NULL DEFAULT NULL COMMENT '不钢化是否打标：0不大标1打标',
  `IntRemark` int(11) NULL DEFAULT NULL COMMENT '预留列',
  `StrRemark` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '预留列',
  `Update_Time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`ID`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 11 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_identify_mark_update
-- ----------------------------
DROP TABLE IF EXISTS `gmms_identify_mark_update`;
CREATE TABLE `gmms_identify_mark_update`  (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `gongchenghao` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `DBMark` tinyint(255) NULL DEFAULT 0 COMMENT '记录非钢化打标类型，1打标0不打标',
  `GHMark` tinyint(255) NULL DEFAULT 0 COMMENT '记录钢化打标类型，0不打标1打标',
  `line` tinyint(4) NULL DEFAULT NULL COMMENT '生产线',
  `IntRemark` int(255) NULL DEFAULT NULL COMMENT '预留列',
  `StrRemark` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '预留列',
  `Create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`ID`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 39 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_journal
-- ----------------------------
DROP TABLE IF EXISTS `gmms_journal`;
CREATE TABLE `gmms_journal`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `rank` bigint(20) NULL DEFAULT 0,
  `type_id` bigint(20) NULL DEFAULT NULL,
  `galss_id` bigint(20) NOT NULL,
  `pattern_id` bigint(20) NULL DEFAULT NULL,
  `width` double NULL DEFAULT NULL,
  `height` double NULL DEFAULT NULL,
  `thickness` double NULL DEFAULT NULL,
  `color` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `edg_width` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `edg_height` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `chamfer_size` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '倒角大小',
  `galss_type` int(11) NULL DEFAULT NULL COMMENT '玻璃类型 散单/整单',
  `edg_speed1` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '磨边速度',
  `edg_speed2` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `fine_throwing` int(11) NULL DEFAULT NULL COMMENT '精抛/粗磨',
  `work_state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `work_procedure` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `slicecage_rank` bigint(20) NULL DEFAULT 0 COMMENT '理片笼架子',
  `drop_frame` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '落架',
  `slicecage_flag` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '0' COMMENT '理片笼标志',
  `out_rank` bigint(20) NOT NULL COMMENT '出理片笼序号',
  `start_time` datetime NOT NULL,
  `finsh_time` datetime NULL DEFAULT NULL,
  `stop_position` int(11) NULL DEFAULT NULL COMMENT '下片位置',
  `processcard_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '流程卡号',
  `engineering_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工程号',
  `broken_state` int(11) NOT NULL COMMENT '破片标记，非0破片',
  `work_procedure_number` int(11) NULL DEFAULT NULL,
  `message` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '报文',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `pattern_id`(`pattern_id`) USING BTREE,
  INDEX `galss_id`(`galss_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_liuchengka
-- ----------------------------
DROP TABLE IF EXISTS `gmms_liuchengka`;
CREATE TABLE `gmms_liuchengka`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `liuchengka` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '流程卡',
  `gongchenghao` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '对应工程号（单片才有）',
  `parent_liuchengka` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '父流程卡',
  `main_liuchengka` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `is_single` bit(1) NULL DEFAULT NULL COMMENT '是单片',
  `child_count` tinyint(4) NULL DEFAULT NULL COMMENT '合片层数',
  `current_status` varchar(11) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '未开始   开始    结束   撤销\r\n',
  `current_work_position` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '当前工作位置',
  `current_frame` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '当前所属架',
  `last_changed_time` datetime NULL DEFAULT NULL,
  `piece_count` int(11) NULL DEFAULT NULL COMMENT '小片数',
  `gy_gang` bit(1) NOT NULL DEFAULT b'0',
  `gy_jiajiao` bit(1) NOT NULL DEFAULT b'0',
  `gy_zhongkong` bit(1) NOT NULL DEFAULT b'0',
  `gongyi` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '\0',
  `Occupied` int(11) NULL DEFAULT 0 COMMENT '占用，0初始 其他数字表示架子位//12.06新增',
  `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间，自动记录，//12.06新增',
  `getdatatime` datetime NULL DEFAULT NULL COMMENT '领取时间',
  `getgroups` bigint(20) NULL DEFAULT NULL COMMENT '领取线路',
  `isyou` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '是否在优化',
  `Suction_sheet_mode` int(11) NULL DEFAULT 0 COMMENT '吸片模式，0初始 其他数字表示反吸//6.19新增',
  `ischu` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '是否出库',
  `xiapianbiaoji` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `isgang` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '',
  `minArea` double NULL DEFAULT NULL COMMENT '最小面积',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `main_liuchengka_idx`(`main_liuchengka`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 78834 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_liuchengka_buffer
-- ----------------------------
DROP TABLE IF EXISTS `gmms_liuchengka_buffer`;
CREATE TABLE `gmms_liuchengka_buffer`  (
  `ID` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `liuchengkahao` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '流程卡号',
  `liuchengkahaoceng` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '流程卡号/层',
  `gongyiliucheng` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工程流程',
  `loucengbianhao` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '楼层编号',
  `mianji` decimal(7, 2) NULL DEFAULT NULL COMMENT '总面积',
  `shuliang` int(50) NULL DEFAULT NULL COMMENT '总数量',
  `ceng` int(10) NULL DEFAULT NULL COMMENT '层',
  `moxi` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '膜系',
  `houdu` int(20) NULL DEFAULT NULL COMMENT '厚度',
  `hepianshunxu` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '合片顺序',
  `lowe` int(255) NULL DEFAULT NULL COMMENT '是否lowe',
  `suoshudingdan` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '所属订单',
  `gongchenghao` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工程号',
  `IntRemark` int(255) NULL DEFAULT NULL COMMENT '预留列，占用时请标明',
  `StrRemark` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '预留列，占用时请标明',
  `Create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`ID`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 61757 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_liuchengka_copy1
-- ----------------------------
DROP TABLE IF EXISTS `gmms_liuchengka_copy1`;
CREATE TABLE `gmms_liuchengka_copy1`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `liuchengka` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '流程卡',
  `gongchenghao` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '对应工程号（单片才有）',
  `parent_liuchengka` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '父流程卡',
  `main_liuchengka` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `is_single` bit(1) NULL DEFAULT NULL COMMENT '是单片',
  `child_count` tinyint(4) NULL DEFAULT NULL COMMENT '合片层数',
  `current_status` varchar(11) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '未开始   开始    结束   撤销\r\n',
  `current_work_position` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '当前工作位置',
  `current_frame` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '当前所属架',
  `last_changed_time` datetime NULL DEFAULT NULL,
  `piece_count` int(11) NULL DEFAULT NULL COMMENT '小片数',
  `gy_gang` bit(1) NOT NULL DEFAULT b'0',
  `gy_jiajiao` bit(1) NOT NULL DEFAULT b'0',
  `gy_zhongkong` bit(1) NOT NULL DEFAULT b'0',
  `gongyi` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '\0',
  `Occupied` int(11) NULL DEFAULT 0 COMMENT '占用，0初始 其他数字表示架子位//12.06新增',
  `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间，自动记录，//12.06新增',
  `getdatatime` datetime NULL DEFAULT NULL COMMENT '领取时间',
  `getgroups` bigint(20) NULL DEFAULT NULL COMMENT '领取线路',
  `isyou` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '是否在优化',
  `Suction_sheet_mode` int(11) NULL DEFAULT 0 COMMENT '吸片模式，0初始 其他数字表示反吸//6.19新增',
  `ischu` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '是否出库',
  `xiapianbiaoji` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `isgang` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `main_liuchengka_idx`(`main_liuchengka`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 45084 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_liuchengka_copy2
-- ----------------------------
DROP TABLE IF EXISTS `gmms_liuchengka_copy2`;
CREATE TABLE `gmms_liuchengka_copy2`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `liuchengka` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '流程卡',
  `gongchenghao` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '对应工程号（单片才有）',
  `parent_liuchengka` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '父流程卡',
  `main_liuchengka` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `is_single` bit(1) NULL DEFAULT NULL COMMENT '是单片',
  `child_count` tinyint(4) NULL DEFAULT NULL COMMENT '合片层数',
  `current_status` varchar(11) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '未开始   开始    结束   撤销\r\n',
  `current_work_position` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '当前工作位置',
  `current_frame` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '当前所属架',
  `last_changed_time` datetime NULL DEFAULT NULL,
  `piece_count` int(11) NULL DEFAULT NULL COMMENT '小片数',
  `gy_gang` bit(1) NOT NULL DEFAULT b'0',
  `gy_jiajiao` bit(1) NOT NULL DEFAULT b'0',
  `gy_zhongkong` bit(1) NOT NULL DEFAULT b'0',
  `gongyi` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '\0',
  `Occupied` int(11) NULL DEFAULT 0 COMMENT '占用，0初始 其他数字表示架子位//12.06新增',
  `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间，自动记录，//12.06新增',
  `getdatatime` datetime NULL DEFAULT NULL COMMENT '领取时间',
  `getgroups` bigint(20) NULL DEFAULT NULL COMMENT '领取线路',
  `isyou` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '是否在优化',
  `Suction_sheet_mode` int(11) NULL DEFAULT 0 COMMENT '吸片模式，0初始 其他数字表示反吸//6.19新增',
  `ischu` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '是否出库',
  `xiapianbiaoji` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `isgang` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `main_liuchengka_idx`(`main_liuchengka`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 45592 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_liuchengka_dayin_buffer
-- ----------------------------
DROP TABLE IF EXISTS `gmms_liuchengka_dayin_buffer`;
CREATE TABLE `gmms_liuchengka_dayin_buffer`  (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `baozhuangfangshi` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '包装方式',
  `liuchengkahao` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '流程卡号',
  `ceng` int(11) NULL DEFAULT NULL COMMENT '层',
  `kehumingcheng` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '客户名称',
  `xiangmumingcheng` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '项目名称',
  `gongyiliucheng` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工艺流程',
  `danpianmingcheng` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '单片名称',
  `chanpinmingcheng` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '产品名称',
  `jiagongyaoqiu` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '加工要求',
  `pici` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '批次',
  `houdu` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '厚度',
  `qiepian` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '切片类型',
  `mobian` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '磨边类型',
  `yinbiao` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '印标类型',
  `jiajiao` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '夹胶类型',
  `zhongkong` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '中空类型',
  `baozhuang` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '包装类型',
  `tianfenID` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '天分',
  `saleID` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `zongpianshu` int(255) NULL DEFAULT NULL COMMENT '总片数',
  `zongliuchengshu` int(255) NULL DEFAULT NULL COMMENT '销售单总流程数',
  `gongchenghao` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工程号',
  `Create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `IntRemark` int(255) NULL DEFAULT NULL COMMENT '预留列，使用需注明',
  `StrRemark` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '预留列，使用需注明',
  PRIMARY KEY (`ID`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 53854 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_load_rack
-- ----------------------------
DROP TABLE IF EXISTS `gmms_load_rack`;
CREATE TABLE `gmms_load_rack`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `number` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '架子编号',
  `machine_id` bigint(20) NULL DEFAULT NULL COMMENT '机器编号',
  `falg` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '启用标记',
  `state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工作状态',
  `shelf_task_id` bigint(20) NULL DEFAULT NULL COMMENT '仓储任务编号',
  `falg_shelf` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '标记是否有玻璃',
  `type_falg` int(11) NOT NULL DEFAULT 0 COMMENT '上片位类型',
  `groups` bigint(20) NOT NULL COMMENT '线路',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `machine_id`(`machine_id`) USING BTREE,
  INDEX `shelf_task_id`(`shelf_task_id`) USING BTREE,
  CONSTRAINT `gmms_load_rack_ibfk_1` FOREIGN KEY (`machine_id`) REFERENCES `gmms_machine` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
  CONSTRAINT `gmms_load_rack_ibfk_2` FOREIGN KEY (`shelf_task_id`) REFERENCES `gmms_shelf_task` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 11 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_log_glass
-- ----------------------------
DROP TABLE IF EXISTS `gmms_log_glass`;
CREATE TABLE `gmms_log_glass`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `glass_id` bigint(20) NULL DEFAULT NULL COMMENT '玻璃ID',
  `segment` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工艺段(冷加工,钢化,中空)',
  `work_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '(小工艺段)',
  `line` int(11) NULL DEFAULT NULL COMMENT '所属线号',
  `begin_time` datetime NULL DEFAULT NULL COMMENT '开始时间',
  `end_time` datetime NULL DEFAULT NULL COMMENT '结束时间',
  `liuchengka` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '所属流程卡',
  `broken` bit(1) NULL DEFAULT NULL COMMENT '破损',
  `time_out` bit(1) NULL DEFAULT NULL COMMENT '过时_无效记录（返工重做，会将之前日志无效掉）',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_machine
-- ----------------------------
DROP TABLE IF EXISTS `gmms_machine`;
CREATE TABLE `gmms_machine`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `machine_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '机器名字',
  `machine_type_id` bigint(20) NOT NULL COMMENT '类型编号',
  `ip_address` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT 'IP地址',
  `port` int(11) NULL DEFAULT NULL COMMENT '端口',
  `state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '机器状态',
  `connect_state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '连接状态',
  `work_state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工作状态',
  `work_id` bigint(20) NULL DEFAULT NULL COMMENT '线路',
  `flag` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '启用标记',
  `remarks` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '备注',
  `software_connect` int(11) NOT NULL,
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `machine_type_id`(`machine_type_id`) USING BTREE,
  CONSTRAINT `gmms_machine_ibfk_1` FOREIGN KEY (`machine_type_id`) REFERENCES `gmms_machine_type` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 76 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_machine_copy1
-- ----------------------------
DROP TABLE IF EXISTS `gmms_machine_copy1`;
CREATE TABLE `gmms_machine_copy1`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `machine_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `machine_type_id` bigint(20) NOT NULL,
  `ip_address` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `port` int(11) NULL DEFAULT NULL,
  `state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `connect_state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `work_state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `work_id` bigint(20) NULL DEFAULT NULL,
  `flag` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `remarks` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `machine_type_id`(`machine_type_id`) USING BTREE,
  CONSTRAINT `gmms_machine_copy1_ibfk_1` FOREIGN KEY (`machine_type_id`) REFERENCES `gmms_machine_type` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 20 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_machine_dispose
-- ----------------------------
DROP TABLE IF EXISTS `gmms_machine_dispose`;
CREATE TABLE `gmms_machine_dispose`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `machine_type_id` bigint(20) NULL DEFAULT NULL COMMENT '机器类型编号',
  `plc_font_type` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT 'plc字类型',
  `plc_font_value` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT 'plc字内容',
  `mes_font_type` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT 'mes字类型',
  `mes_font_value` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT 'mes字内容',
  `return_value` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '返回值',
  `logic` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '对应操作步骤',
  `remarks` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '备注',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `machine_type_id`(`machine_type_id`) USING BTREE,
  CONSTRAINT `gmms_machine_dispose_ibfk_1` FOREIGN KEY (`machine_type_id`) REFERENCES `gmms_machine_type` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 101 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_machine_font
-- ----------------------------
DROP TABLE IF EXISTS `gmms_machine_font`;
CREATE TABLE `gmms_machine_font`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `machine_type_id` bigint(20) NULL DEFAULT NULL COMMENT '机器类型编号',
  `flag` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '位置标记',
  `font_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '字类型',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `machine_type_id`(`machine_type_id`) USING BTREE,
  CONSTRAINT `gmms_machine_font_ibfk_1` FOREIGN KEY (`machine_type_id`) REFERENCES `gmms_machine_type` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 84 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_machine_message
-- ----------------------------
DROP TABLE IF EXISTS `gmms_machine_message`;
CREATE TABLE `gmms_machine_message`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `machine_id` bigint(20) NULL DEFAULT NULL COMMENT '机器编号',
  `message` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' COMMENT '通讯内容',
  `mod_time` datetime NULL DEFAULT NULL COMMENT '修改时间',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `machine_id`(`machine_id`) USING BTREE,
  CONSTRAINT `gmms_machine_message_ibfk_1` FOREIGN KEY (`machine_id`) REFERENCES `gmms_machine` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 76 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_machine_state
-- ----------------------------
DROP TABLE IF EXISTS `gmms_machine_state`;
CREATE TABLE `gmms_machine_state`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `device_id` bigint(50) NOT NULL,
  `tick` bigint(20) NOT NULL,
  `time_stamp` datetime NULL DEFAULT NULL,
  `var_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `var_value` varchar(500) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 6 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_machine_type
-- ----------------------------
DROP TABLE IF EXISTS `gmms_machine_type`;
CREATE TABLE `gmms_machine_type`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `type` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '机器类型名称',
  `type_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '机器类型名称',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 24 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_password
-- ----------------------------
DROP TABLE IF EXISTS `gmms_password`;
CREATE TABLE `gmms_password`  (
  `id` int(11) NOT NULL,
  `password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '密码',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_pattern_task
-- ----------------------------
DROP TABLE IF EXISTS `gmms_pattern_task`;
CREATE TABLE `gmms_pattern_task`  (
  `id` bigint(20) NOT NULL,
  `rank` bigint(20) NULL DEFAULT NULL COMMENT '顺序',
  `raw_type` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '玻璃类型厚度',
  `raw_width` double NULL DEFAULT NULL COMMENT '宽度',
  `raw_height` double NULL DEFAULT NULL COMMENT '高度',
  `raw_thickness` double NULL DEFAULT NULL COMMENT '厚度',
  `work_state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工作状态',
  `color` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '颜色',
  `content` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '没用',
  `task_id` bigint(20) NULL DEFAULT NULL COMMENT '没用',
  `raw_id` bigint(20) NULL DEFAULT NULL COMMENT '任务编号',
  `finsh_time` datetime NULL DEFAULT NULL COMMENT '完成时间',
  `flag` int(11) NOT NULL COMMENT '标记',
  `groups` bigint(20) NOT NULL COMMENT '线路',
  `Update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `task_id`(`task_id`) USING BTREE,
  INDEX `raw_id`(`raw_id`) USING BTREE,
  CONSTRAINT `gmms_pattern_task_ibfk_2` FOREIGN KEY (`raw_id`) REFERENCES `gmms_raw_file` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_pattern_task_buffer
-- ----------------------------
DROP TABLE IF EXISTS `gmms_pattern_task_buffer`;
CREATE TABLE `gmms_pattern_task_buffer`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `rank` bigint(20) NULL DEFAULT NULL,
  `raw_type` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `raw_width` double NULL DEFAULT NULL,
  `raw_height` double NULL DEFAULT NULL,
  `raw_thickness` double NULL DEFAULT NULL,
  `work_state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `color` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `content` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `task_id` bigint(20) NULL DEFAULT NULL,
  `finsh_time` datetime NULL DEFAULT NULL,
  `flag` int(11) NOT NULL,
  `engineering_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `groups` bigint(20) NOT NULL,
  `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `task_id`(`task_id`) USING BTREE,
  INDEX `engineering_id`(`engineering_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 190667 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_pattern_task_interrupt
-- ----------------------------
DROP TABLE IF EXISTS `gmms_pattern_task_interrupt`;
CREATE TABLE `gmms_pattern_task_interrupt`  (
  `id` bigint(20) NOT NULL,
  `rank` bigint(20) NULL DEFAULT NULL COMMENT '顺序',
  `raw_type` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '玻璃类型厚度',
  `raw_width` double NULL DEFAULT NULL COMMENT '宽度',
  `raw_height` double NULL DEFAULT NULL COMMENT '高度',
  `raw_thickness` double NULL DEFAULT NULL COMMENT '厚度',
  `work_state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工作状态',
  `color` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '颜色',
  `content` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '没用',
  `task_id` bigint(20) NULL DEFAULT NULL COMMENT '没用',
  `raw_id` bigint(20) NULL DEFAULT NULL COMMENT '任务编号',
  `finsh_time` datetime NULL DEFAULT NULL COMMENT '完成时间',
  `flag` int(11) NOT NULL COMMENT '标记',
  `groups` bigint(20) NOT NULL COMMENT '线路',
  `Update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `task_id`(`task_id`) USING BTREE,
  INDEX `raw_id`(`raw_id`) USING BTREE,
  CONSTRAINT `gmms_pattern_task_interrupt_ibfk_1` FOREIGN KEY (`raw_id`) REFERENCES `gmms_raw_file` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_peak_value
-- ----------------------------
DROP TABLE IF EXISTS `gmms_peak_value`;
CREATE TABLE `gmms_peak_value`  (
  `id` int(11) NOT NULL,
  `peak_value` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '峰值',
  `groups` int(11) NULL DEFAULT NULL COMMENT '线路',
  `type` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '类型',
  `work_procedure` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工序',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_preprocessing_glass
-- ----------------------------
DROP TABLE IF EXISTS `gmms_preprocessing_glass`;
CREATE TABLE `gmms_preprocessing_glass`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `glass_id` bigint(20) NOT NULL COMMENT '玻璃编号',
  `actual_length` double(20, 1) NULL DEFAULT NULL COMMENT '测量长度',
  `actual_width` double(20, 1) NULL DEFAULT NULL COMMENT '测量宽度',
  `length` double(10, 1) NOT NULL COMMENT '实际长度',
  `width` double(10, 1) NOT NULL COMMENT '实际宽度',
  `statu` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '状态',
  `color` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '颜色',
  `thickness` double(20, 0) NULL DEFAULT NULL COMMENT '厚度',
  `flag` bigint(20) NULL DEFAULT NULL COMMENT '标记',
  `groups` int(11) NULL DEFAULT NULL COMMENT '线路',
  `marking` int(11) NULL DEFAULT NULL COMMENT '打标模式',
  `minwidths` double(10, 1) NULL DEFAULT NULL,
  `tk` int(11) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `glass_id`(`glass_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_print_log
-- ----------------------------
DROP TABLE IF EXISTS `gmms_print_log`;
CREATE TABLE `gmms_print_log`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `liuchengka` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `time_stamp` datetime NULL DEFAULT NULL,
  `piece_count` int(255) NULL DEFAULT NULL,
  `piece_area` double NULL DEFAULT NULL,
  `line` int(11) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_prod_line_state
-- ----------------------------
DROP TABLE IF EXISTS `gmms_prod_line_state`;
CREATE TABLE `gmms_prod_line_state`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `line` int(11) NULL DEFAULT NULL COMMENT '线路',
  `state` tinyint(1) NULL DEFAULT NULL COMMENT '生产状态',
  `standalonestate` tinyint(1) NULL DEFAULT NULL COMMENT '单机状态',
  `gongwei_mode` int(11) NULL DEFAULT NULL COMMENT '切割工位模式：1单工位/2双工位',
  `lipianlong_mode` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '理片笼模式：1单片/2双片模式',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_rank
-- ----------------------------
DROP TABLE IF EXISTS `gmms_rank`;
CREATE TABLE `gmms_rank`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `glass_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '玻璃编号',
  `pieces` int(11) NULL DEFAULT NULL COMMENT '片数',
  `width` double NULL DEFAULT NULL COMMENT '宽度',
  `height` double NULL DEFAULT NULL COMMENT '高度',
  `thickness` double NULL DEFAULT NULL COMMENT '厚度',
  `color` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '颜色',
  `endtime` timestamp NULL DEFAULT NULL COMMENT '结束时间',
  `manufacturer` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '没用',
  `type` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '类型',
  `createtime` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '创建时间',
  `flag` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '标记',
  `shelf_rack_id` bigint(20) NULL DEFAULT NULL COMMENT '仓储架子',
  `batch_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '批次号',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `shelf_rank_id`(`shelf_rack_id`) USING BTREE,
  CONSTRAINT `gmms_rank_ibfk_1` FOREIGN KEY (`shelf_rack_id`) REFERENCES `gmms_shelf_rack` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 5834 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_rank_2
-- ----------------------------
DROP TABLE IF EXISTS `gmms_rank_2`;
CREATE TABLE `gmms_rank_2`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `glass_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '玻璃编号',
  `pieces` int(11) NULL DEFAULT NULL COMMENT '片数',
  `width` double NULL DEFAULT NULL COMMENT '宽度',
  `height` double NULL DEFAULT NULL COMMENT '高度',
  `thickness` double NULL DEFAULT NULL COMMENT '厚度',
  `color` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '颜色',
  `endtime` timestamp NULL DEFAULT NULL COMMENT '结束时间',
  `manufacturer` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '没用',
  `type` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '类型',
  `createtime` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `flag` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '标记',
  `shelf_rack_id` bigint(20) NULL DEFAULT NULL COMMENT '仓储架子',
  `batch_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '批次号',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `shelf_rank_id`(`shelf_rack_id`) USING BTREE,
  CONSTRAINT `gmms_rank_2_ibfk_1` FOREIGN KEY (`shelf_rack_id`) REFERENCES `gmms_shelf_rack` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_raw_file
-- ----------------------------
DROP TABLE IF EXISTS `gmms_raw_file`;
CREATE TABLE `gmms_raw_file`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `file_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工程号',
  `file_url` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '没用',
  `state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '状态',
  `operator` bigint(20) NULL DEFAULT NULL COMMENT '线路',
  `width` double NOT NULL COMMENT '宽度',
  `height` double NOT NULL COMMENT '高度',
  `thickness` double NOT NULL COMMENT '厚度',
  `count` int(11) NOT NULL COMMENT '数量',
  `create_time` datetime NULL DEFAULT NULL COMMENT '创建时间',
  `receive_time` datetime NULL DEFAULT NULL COMMENT '完成时间',
  `galssmeasure` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '0' COMMENT '小片面积',
  `patternmeasure` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '0' COMMENT '原片面积',
  `cutting_rate` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '0' COMMENT '切裁率',
  `Update_time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `file_name`(`file_name`) USING BTREE,
  INDEX `create_time`(`create_time`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 10540 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_raw_file_2
-- ----------------------------
DROP TABLE IF EXISTS `gmms_raw_file_2`;
CREATE TABLE `gmms_raw_file_2`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `file_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工程号',
  `state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '状态',
  `operator` bigint(20) NULL DEFAULT NULL COMMENT '线路',
  `create_time` datetime NULL DEFAULT NULL COMMENT '创建时间',
  `receive_time` datetime NULL DEFAULT NULL COMMENT '完成时间',
  `jiakong` int(11) NOT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 815 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_raw_package
-- ----------------------------
DROP TABLE IF EXISTS `gmms_raw_package`;
CREATE TABLE `gmms_raw_package`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `raw_type` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '玻璃类型',
  `raw_width` double NULL DEFAULT NULL COMMENT '宽度',
  `raw_height` double NULL DEFAULT NULL COMMENT '高度',
  `raw_thickness` double NULL DEFAULT NULL COMMENT '厚度',
  `state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '状态',
  `color` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '颜色',
  `pieces` int(11) NULL DEFAULT NULL COMMENT '总数量',
  `left_pieces` int(11) NULL DEFAULT NULL COMMENT '剩余数量',
  `qrcode` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '物料编码',
  `create_time` datetime NULL DEFAULT NULL COMMENT '创建时间',
  `production_time` datetime NULL DEFAULT NULL COMMENT '生产日期',
  `batch_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '批次号',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 7264 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_shelf_rack
-- ----------------------------
DROP TABLE IF EXISTS `gmms_shelf_rack`;
CREATE TABLE `gmms_shelf_rack`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `number` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '架子编号',
  `state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '状态',
  `raw_package_id1` bigint(20) NULL DEFAULT NULL COMMENT '包号编号',
  `create_time` datetime NULL DEFAULT NULL COMMENT '创建时间',
  `mod_time` datetime NULL DEFAULT NULL COMMENT '修改时间',
  `falg` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '禁用启用',
  `color_falg` int(11) NOT NULL COMMENT '翻片标记',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `raw_package_id1`(`raw_package_id1`) USING BTREE,
  CONSTRAINT `gmms_shelf_rack_ibfk_1` FOREIGN KEY (`raw_package_id1`) REFERENCES `gmms_raw_package` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 72 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_shelf_rack_2
-- ----------------------------
DROP TABLE IF EXISTS `gmms_shelf_rack_2`;
CREATE TABLE `gmms_shelf_rack_2`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `number` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `processcard_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `create_time` datetime NULL DEFAULT NULL,
  `mod_time` datetime NULL DEFAULT NULL,
  `falg` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `orientation` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `sign` int(11) NOT NULL,
  `jiakong` int(11) NOT NULL,
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `raw_package_id1`(`processcard_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 72 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_shelf_task
-- ----------------------------
DROP TABLE IF EXISTS `gmms_shelf_task`;
CREATE TABLE `gmms_shelf_task`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `task_id` bigint(20) NULL DEFAULT 0 COMMENT '任务编号',
  `task_type` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '任务类型',
  `task_state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工作状态',
  `shelf_rack_id` bigint(20) NOT NULL COMMENT '仓储架号',
  `load_rack` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '上片位编号',
  `file_id` bigint(20) NULL DEFAULT NULL COMMENT '没用',
  `groups` bigint(20) NULL DEFAULT NULL COMMENT '线路',
  `start_time` datetime NULL DEFAULT NULL COMMENT '创建时间',
  `finsh_time` datetime NULL DEFAULT NULL COMMENT '完成时间',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `file_id`(`file_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 95108 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_shelf_task_2
-- ----------------------------
DROP TABLE IF EXISTS `gmms_shelf_task_2`;
CREATE TABLE `gmms_shelf_task_2`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `task_type` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `task_state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `shelf_rack_id` bigint(20) NULL DEFAULT NULL,
  `load_rack` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `groups` bigint(20) NULL DEFAULT NULL,
  `start_time` datetime NULL DEFAULT NULL,
  `finsh_time` datetime NULL DEFAULT NULL,
  `line` bigint(20) NOT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 130995 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_shelf_tasks2
-- ----------------------------
DROP TABLE IF EXISTS `gmms_shelf_tasks2`;
CREATE TABLE `gmms_shelf_tasks2`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `groups` bigint(20) NULL DEFAULT NULL COMMENT '线路',
  `tasktype` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '任务类型',
  `taskphase` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工作阶段',
  `taskstatus` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '任务状态',
  `starttime` datetime NULL DEFAULT NULL COMMENT '生成时间',
  `endtime` datetime NULL DEFAULT NULL COMMENT '结束时间',
  `updatetime` datetime NULL DEFAULT NULL COMMENT '修改时间',
  `beginaddress` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '起始地',
  `endaddress` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '目的地',
  `cardlocation` bigint(20) NULL DEFAULT NULL COMMENT '小车上的位置',
  `rackid` bigint(20) NULL DEFAULT NULL COMMENT '工位id',
  `endgroups` bigint(20) NULL DEFAULT NULL COMMENT '任务优先级(\r\n吊装移库400，\r\n中空旋转（旋转退库）300，\r\n中空移库200，\r\n移库100\r\n)',
  `isrepeal` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '正常' COMMENT '任务标记（撤销,正常,暂停）',
  `isout` bigint(20) NULL DEFAULT 0 COMMENT '是否下车,0上车1下车',
  `finshtype1` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '上车完成手动或自动',
  `finshtype2` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '下车完成手动或自动',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 71643 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_slicecage
-- ----------------------------
DROP TABLE IF EXISTS `gmms_slicecage`;
CREATE TABLE `gmms_slicecage`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `slicecage_rank` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '架号',
  `state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '状态',
  `type_id` bigint(20) NOT NULL COMMENT '类型编号',
  `count` int(11) NOT NULL DEFAULT 0 COMMENT '数量',
  `flag` int(20) NOT NULL DEFAULT 0 COMMENT '0/1',
  `gourp` bigint(20) NULL DEFAULT NULL COMMENT '线路',
  `processcard_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '流程卡号',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `glass_id`(`type_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 161 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_slicecage_details
-- ----------------------------
DROP TABLE IF EXISTS `gmms_slicecage_details`;
CREATE TABLE `gmms_slicecage_details`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `processcard_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `groups` int(11) NULL DEFAULT NULL,
  `time` datetime NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 41 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_slicecage_house_full
-- ----------------------------
DROP TABLE IF EXISTS `gmms_slicecage_house_full`;
CREATE TABLE `gmms_slicecage_house_full`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `processcard_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `groups` bigint(20) NULL DEFAULT NULL,
  `time` datetime NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_slicecage_pattern
-- ----------------------------
DROP TABLE IF EXISTS `gmms_slicecage_pattern`;
CREATE TABLE `gmms_slicecage_pattern`  (
  `id` int(11) NOT NULL,
  `var` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '笼子状态（进出存）',
  `groups` int(11) NULL DEFAULT NULL COMMENT '线路',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_slicecage_tempering
-- ----------------------------
DROP TABLE IF EXISTS `gmms_slicecage_tempering`;
CREATE TABLE `gmms_slicecage_tempering`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `slicecage_rank` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '架号',
  `state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '状态',
  `type_id` bigint(20) NOT NULL COMMENT '类型编号',
  `count` int(11) NOT NULL DEFAULT 0 COMMENT '数量',
  `flag` int(20) NOT NULL DEFAULT 0 COMMENT '0/1',
  `gourp` bigint(20) NULL DEFAULT NULL COMMENT '线路',
  `processcard_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '流程卡号',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `glass_id`(`type_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 81 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_task
-- ----------------------------
DROP TABLE IF EXISTS `gmms_task`;
CREATE TABLE `gmms_task`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `title` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `description` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `user_id` bigint(20) NOT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 6 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_tempere_layout
-- ----------------------------
DROP TABLE IF EXISTS `gmms_tempere_layout`;
CREATE TABLE `gmms_tempere_layout`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `work_id` int(11) NOT NULL COMMENT '所属钢化炉编号',
  `layout_number` int(11) NOT NULL COMMENT '炉号',
  `insert_time` datetime NOT NULL,
  `layout_rate` double NULL DEFAULT NULL COMMENT '装载率',
  `glass_number` int(11) NULL DEFAULT NULL COMMENT '从1 开始',
  `x` int(11) NULL DEFAULT NULL,
  `y` int(11) NULL DEFAULT NULL,
  `width` int(11) NULL DEFAULT NULL,
  `height` int(11) NULL DEFAULT NULL,
  `rotate` bit(1) NULL DEFAULT NULL,
  `row` int(11) NULL DEFAULT NULL,
  `column` int(11) NULL DEFAULT NULL,
  `glass_id` bigint(20) NULL DEFAULT NULL,
  `erp_glass_id` bigint(20) NULL DEFAULT NULL,
  `head_info` text CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT '附加信息头 一般只有第一片玻璃有该字段',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 236561 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_timer
-- ----------------------------
DROP TABLE IF EXISTS `gmms_timer`;
CREATE TABLE `gmms_timer`  (
  `timer_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `time_stamp` datetime NULL DEFAULT NULL,
  `time_out` datetime NULL DEFAULT NULL,
  PRIMARY KEY (`timer_name`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_user
-- ----------------------------
DROP TABLE IF EXISTS `gmms_user`;
CREATE TABLE `gmms_user`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `login_name` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '登录名',
  `name` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '用户名',
  `password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '密码',
  `salt` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '编译文',
  `roles` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '角色',
  `groups` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '线路',
  `register_date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '修改时间',
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE INDEX `login_name`(`login_name`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 43 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_user_current
-- ----------------------------
DROP TABLE IF EXISTS `gmms_user_current`;
CREATE TABLE `gmms_user_current`  (
  `Id` int(11) NOT NULL AUTO_INCREMENT,
  `gongyi` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工艺',
  `chanxian` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '产线',
  `banzu` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '班组',
  `intRemark` int(11) NULL DEFAULT NULL COMMENT '预留列，使用请标明用途',
  `strRemark` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '预留列，使用请标明用途',
  `isPaused` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '是否暂停设备（用于切换班组）',
  PRIMARY KEY (`Id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 12 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_user_details
-- ----------------------------
DROP TABLE IF EXISTS `gmms_user_details`;
CREATE TABLE `gmms_user_details`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `groups` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_var
-- ----------------------------
DROP TABLE IF EXISTS `gmms_var`;
CREATE TABLE `gmms_var`  (
  `id` int(11) NOT NULL,
  `varname` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '理片笼保存的类型',
  `value` int(11) NULL DEFAULT NULL COMMENT '保存的数据',
  `groups` int(11) NULL DEFAULT NULL COMMENT '线路',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_var_table
-- ----------------------------
DROP TABLE IF EXISTS `gmms_var_table`;
CREATE TABLE `gmms_var_table`  (
  `ID` int(11) NOT NULL,
  `var_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `var_value` decimal(10, 0) NULL DEFAULT 0,
  PRIMARY KEY (`ID`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_workmanship
-- ----------------------------
DROP TABLE IF EXISTS `gmms_workmanship`;
CREATE TABLE `gmms_workmanship`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `processcard_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '流程卡',
  `specifications` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '规格',
  `cuttingcount` int(11) NULL DEFAULT NULL COMMENT '切割',
  `edgingcount` int(11) NULL DEFAULT NULL COMMENT '磨边',
  `temperingcount` int(11) NULL DEFAULT NULL COMMENT '钢化',
  `hollowcount` int(11) NULL DEFAULT NULL COMMENT '中空',
  `processcard_count` int(11) NULL DEFAULT NULL COMMENT '总数',
  `starttime` datetime NULL DEFAULT NULL COMMENT '创建时间',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 120850 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for gmms_yuliao_slicecage
-- ----------------------------
DROP TABLE IF EXISTS `gmms_yuliao_slicecage`;
CREATE TABLE `gmms_yuliao_slicecage`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `slicecage_number` int(11) NULL DEFAULT NULL,
  `slicecage_group` int(11) NULL DEFAULT NULL,
  `slicecage_status` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `ban` int(11) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 203 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gmms_yuliao_task
-- ----------------------------
DROP TABLE IF EXISTS `gmms_yuliao_task`;
CREATE TABLE `gmms_yuliao_task`  (
  `yuliao_id` bigint(20) NOT NULL,
  `pattern_task_id` bigint(11) NULL DEFAULT NULL,
  `state` int(255) NULL DEFAULT NULL,
  `lib_id` bigint(20) NULL DEFAULT NULL,
  `chanxian` int(11) NULL DEFAULT NULL,
  PRIMARY KEY (`yuliao_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for gongcheng_raw
-- ----------------------------
DROP TABLE IF EXISTS `gongcheng_raw`;
CREATE TABLE `gongcheng_raw`  (
  `gc_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `order_index` int(11) NOT NULL,
  `color` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `thickness` double(11, 0) NOT NULL,
  `width` int(11) NOT NULL,
  `height` int(11) NOT NULL,
  `count` int(11) NOT NULL,
  `state` int(11) NOT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 43 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for hn_user
-- ----------------------------
DROP TABLE IF EXISTS `hn_user`;
CREATE TABLE `hn_user`  (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `phone` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `idcard` varchar(18) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `update_time` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `add_time` bigint(13) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 49702 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for io_log
-- ----------------------------
DROP TABLE IF EXISTS `io_log`;
CREATE TABLE `io_log`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `device_id` int(11) NULL DEFAULT NULL,
  `begin_time` datetime NULL DEFAULT NULL,
  `end_time` datetime NULL DEFAULT NULL,
  `read_message` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `send_message` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 479352 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for liuchengka_output
-- ----------------------------
DROP TABLE IF EXISTS `liuchengka_output`;
CREATE TABLE `liuchengka_output`  (
  `liuchengka` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `guige` int(11) NULL DEFAULT NULL,
  `state` int(11) NULL DEFAULT NULL,
  `gongcheng` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `maxxuhao` int(11) NULL DEFAULT NULL,
  `currentxuhao` int(11) NULL DEFAULT NULL,
  `width` double NULL DEFAULT NULL,
  `height` double NULL DEFAULT NULL,
  `suo` int(11) NULL DEFAULT NULL,
  PRIMARY KEY (`liuchengka`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for log_debug
-- ----------------------------
DROP TABLE IF EXISTS `log_debug`;
CREATE TABLE `log_debug`  (
  `id` bigint(20) NOT NULL,
  `txt` text CHARACTER SET utf8 COLLATE utf8_general_ci NULL,
  `dt` datetime NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for memory_var_table
-- ----------------------------
DROP TABLE IF EXISTS `memory_var_table`;
CREATE TABLE `memory_var_table`  (
  `var_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `update_time` datetime NULL DEFAULT NULL,
  `var_value` text CHARACTER SET utf8 COLLATE utf8_general_ci NULL,
  PRIMARY KEY (`var_name`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT STORAGE MEMORY;

-- ----------------------------
-- Table structure for names5
-- ----------------------------
DROP TABLE IF EXISTS `names5`;
CREATE TABLE `names5`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name1` datetime NULL DEFAULT NULL,
  `name2` datetime NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 638603 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for namess
-- ----------------------------
DROP TABLE IF EXISTS `namess`;
CREATE TABLE `namess`  (
  `id` int(11) NOT NULL,
  `name1` int(11) NULL DEFAULT NULL COMMENT '出片条件数量',
  `name2` int(11) NULL DEFAULT NULL,
  `groups` int(11) NULL DEFAULT NULL COMMENT '线路',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for run_view
-- ----------------------------
DROP TABLE IF EXISTS `run_view`;
CREATE TABLE `run_view`  (
  `ID` int(11) NOT NULL,
  `t` datetime NULL DEFAULT NULL,
  `tms` int(255) NULL DEFAULT NULL,
  `msg` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`ID`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for supplement_liuchengkafua
-- ----------------------------
DROP TABLE IF EXISTS `supplement_liuchengkafua`;
CREATE TABLE `supplement_liuchengkafua`  (
  `id` bigint(20) UNSIGNED NOT NULL DEFAULT 0,
  `liuchengkahao` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `xuhao` int(11) NOT NULL,
  `luojiashunxu` int(11) NOT NULL,
  `loucengbianhao` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `danpianmingcheng` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `pianbiaoji` varchar(5) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `gongyiliucheng` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `mobiandengji` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `qiepian` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `mobianleixing` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `ganghua` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `jiajiao` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `zhongkong` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `baozhuang` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `mobianbiaoji` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `kuan` decimal(7, 2) NOT NULL,
  `gao` decimal(7, 2) NOT NULL,
  `shuliang` decimal(9, 6) NOT NULL,
  `mianji` decimal(12, 6) NULL DEFAULT NULL,
  `xingzhuang` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `beizhu` varchar(600) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `gyqrxh` int(11) NULL DEFAULT NULL,
  `houdu` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `yanse` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `qiangdu` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `weizhi` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `lowe` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `pbzt` int(11) NULL DEFAULT NULL,
  `bpsl` int(11) NULL DEFAULT NULL,
  `jianlitime` datetime NULL DEFAULT NULL,
  `xiugaitime` datetime NULL DEFAULT NULL,
  `yiyouhuashuliang` decimal(9, 6) NULL DEFAULT 0.000000,
  `chanpinbianhao` varchar(16) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `chanpinmingcheng` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `kucunshuliang` int(11) NULL DEFAULT NULL,
  `danjuzhuangtai` int(11) NULL DEFAULT NULL,
  `yirushuliang` int(11) NULL DEFAULT NULL,
  `bupianbianhao` varchar(15) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `tianFenID` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `yipian` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `wanmian` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `dakong` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `waque` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `qiejiao` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `zixu` int(11) NULL DEFAULT NULL,
  `ordrID2` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `jiahao` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '架号',
  `state` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '未领取',
  `gongcheng_id` bigint(20) NULL DEFAULT NULL,
  `xiaoshoudanhao` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '销售单号',
  `posungongxu` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '破损工序',
  `liuchengkahaos` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for supplement_liuchengkazhu
-- ----------------------------
DROP TABLE IF EXISTS `supplement_liuchengkazhu`;
CREATE TABLE `supplement_liuchengkazhu`  (
  `id` bigint(20) NOT NULL,
  `xiaoshoudanhao` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `shengchandingdanhao` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `liuchengkahao` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `kehubianma` varchar(15) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `kehumingcheng` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `xiangmumingcheng` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `chanpinbianhao` varchar(16) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `chanpinmingcheng` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `pianbiaoji` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `fenjiayuangonghao` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `fenjiayuan` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `fenjiashijian` datetime NOT NULL,
  `gyqrxh` int(11) NULL DEFAULT NULL,
  `beizhu` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `baozhuangfangshi` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `banjin` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `danpian` varchar(255) CHARACTER SET utf32 COLLATE utf32_general_ci NULL DEFAULT NULL,
  `jiangewu` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `kuaisuchaxun` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `pbzt` int(11) NULL DEFAULT NULL,
  `jianlitime` datetime NULL DEFAULT NULL,
  `xiugaitime` datetime NULL DEFAULT NULL,
  `youhuazhuangtai` int(11) NULL DEFAULT NULL,
  `jiagongyaoqiu` varchar(500) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `gylc` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `qiejiao` int(11) NULL DEFAULT NULL,
  `yipian` int(11) NULL DEFAULT NULL,
  `tianFenID` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `shengchanID` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `projectNo` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '工程编号',
  `projectID` int(11) NULL DEFAULT NULL COMMENT '工程ID',
  `No` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `dyzt` int(11) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;

-- ----------------------------
-- Table structure for y_Len_CeLiang
-- ----------------------------
DROP TABLE IF EXISTS `y_Len_CeLiang`;
CREATE TABLE `y_Len_CeLiang`  (
  `rank` int(11) NULL DEFAULT NULL COMMENT '序号',
  `Fx_Content` varchar(2000) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '冷加工测量台执行内容',
  `minpattern` varchar(2000) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '第几张版图',
  `Mode_State` int(255) NULL DEFAULT NULL COMMENT '0，关闭  1 选择    只能有一个是1  多个1时默认为第一个',
  `groups` int(11) NULL DEFAULT NULL COMMENT '线路'
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for y_mode_type
-- ----------------------------
DROP TABLE IF EXISTS `y_mode_type`;
CREATE TABLE `y_mode_type`  (
  `groups` bigint(20) NULL DEFAULT NULL COMMENT '线路',
  `mode_type` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '模式类型',
  `machine_type` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '设备类型',
  `number` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '编号'
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Table structure for y_pro_timer
-- ----------------------------
DROP TABLE IF EXISTS `y_pro_timer`;
CREATE TABLE `y_pro_timer`  (
  `id` int(11) NOT NULL,
  `pro_timer` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '定时器名称',
  `is_enable` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '是否开启',
  `is_run` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '是否在运行   runing',
  `run_begin_time` datetime NULL DEFAULT NULL COMMENT '上次运行开始时间',
  `run_end_time` datetime NULL DEFAULT NULL COMMENT '上次运行结束时间',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- View structure for chanliang
-- ----------------------------
DROP VIEW IF EXISTS `chanliang`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `chanliang` AS select sum(`gmms_glass_log`.`area`) AS `area`,`gmms_glass_log`.`work_name` AS `work_name`,`gmms_glass_log`.`line` AS `line`,count(0) AS `total`,count((case when (`gmms_glass_log`.`status` = 100) then 1 else NULL end)) AS `ok_count`,count((case when (`gmms_glass_log`.`status` = 101) then 1 else NULL end)) AS `broken_count`,sum((case when (`gmms_glass_log`.`status` = 100) then `gmms_glass_log`.`area` else 0 end)) AS `ok_area`,sum((case when (`gmms_glass_log`.`status` = 101) then `gmms_glass_log`.`area` else 0 end)) AS `broken_area` from `gmms_glass_log` group by `gmms_glass_log`.`work_name`,`gmms_glass_log`.`line`;

-- ----------------------------
-- View structure for gmms_dongzuo_table
-- ----------------------------
DROP VIEW IF EXISTS `gmms_dongzuo_table`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `gmms_dongzuo_table` AS select `a`.`id` AS `id`,`a`.`id` AS `number`,`b`.`mode` AS `mode`,`a`.`note` AS `note`,`a`.`da_biao_mo_ban` AS `da_biao_mo_ban`,`b`.`qing_xi_hou_zhua` AS `qing_xi_hou_zhua`,`b`.`gang_qian_zhua` AS `gang_qian_zhua`,`b`.`gang_qian_bu_zhuan` AS `gang_qian_bu_zhuan`,`b`.`gang_qian_zhuan` AS `gang_qian_zhuan`,`b`.`gang_hou_bu_zhuan` AS `gang_hou_bu_zhuan`,`b`.`gang_hou_zhuan` AS `gang_hou_zhuan`,`b`.`gang_hou_zhua` AS `gang_hou_zhua`,`b`.`zhong_kong_fang` AS `zhong_kong_fang` from (`gmms_biao_table` `a` left join `gmms_dongzuo` `b` on((`a`.`dongzuo_id` = `b`.`number`)));

-- ----------------------------
-- View structure for horizontal
-- ----------------------------
DROP VIEW IF EXISTS `horizontal`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `horizontal` AS select `kgt`.`processcard_id` AS `processcard_id`,count(0) AS `count1` from ((`gmms_raw_file` `krf` left join `gmms_galss_task` `kgt` on(((`krf`.`file_name` = `kgt`.`engineering_id`) and (`krf`.`state` = '已领取') and (`krf`.`operator` = 1)))) left join `gmms_horizontal` `kghl` on((`kgt`.`slicecage_rank` = `kghl`.`id`))) where ((`kgt`.`work_state` = '已进库') and (`kgt`.`work_procedure` = '理片') and (`kghl`.`flag` = 0)) group by `kgt`.`processcard_id`;

-- ----------------------------
-- View structure for online_glass_view
-- ----------------------------
DROP VIEW IF EXISTS `online_glass_view`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `online_glass_view` AS select `gmms_galss_task`.`id` AS `id`,`gmms_galss_task`.`edg_width` AS `edg_width`,`gmms_galss_task`.`edg_height` AS `edg_height`,`gmms_galss_task`.`thickness` AS `thickness`,`gmms_galss_task`.`color` AS `color`,`gmms_galss_task`.`processcard_id` AS `processcard_id`,`gmms_galss_task`.`work_procedure` AS `work_procedure`,`gmms_galss_task`.`work_state` AS `work_state`,`gmms_galss_task`.`out_rank` AS `out_rank`,`gmms_galss_task`.`rank` AS `rank`,`gmms_galss_task`.`stop_position` AS `stop_position` from `gmms_galss_task` where ((((`gmms_galss_task`.`work_procedure` = '理片') and (`gmms_galss_task`.`work_state` = '已出库')) or (`gmms_galss_task`.`work_procedure` = '复合') or (`gmms_galss_task`.`work_procedure` = '磨边') or ((`gmms_galss_task`.`work_procedure` = '下片') and (`gmms_galss_task`.`work_state` <> '已完成'))) and (`gmms_galss_task`.`work_state` <> '破损')) order by `gmms_galss_task`.`out_rank` desc;

-- ----------------------------
-- View structure for processcard
-- ----------------------------
DROP VIEW IF EXISTS `processcard`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `processcard` AS select `rf`.`file_name` AS `工程号`,`rf`.`operator` AS `线路`,`gt`.`processcard_id` AS `流程卡`,`gt`.`drop_frame` AS `架号`,count(0) AS `总数`,count(0) AS `切割数`,(select count(0) from `gmms_galss_task` where ((`gmms_galss_task`.`processcard_id` = `gt`.`processcard_id`) and (`gmms_galss_task`.`work_state` = '已完成') and (`gmms_galss_task`.`work_procedure` = '下片'))) AS `下架数`,if((`rf`.`operator` = 1),ifnull(`gr1`.`count1`,0),ifnull(`gr2`.`count2`,0)) AS `库内数` from (((`gmms_raw_file` `rf` left join `gmms_galss_task` `gt` on(((`rf`.`file_name` = `gt`.`engineering_id`) and (`rf`.`state` = '已领取')))) left join `horizontal` `gr1` on((`gt`.`processcard_id` = `gr1`.`processcard_id`))) left join `slicecage` `gr2` on((`gt`.`processcard_id` = `gr2`.`processcard_id`))) where (`gt`.`engineering_id` is not null) group by `gt`.`processcard_id`;

-- ----------------------------
-- View structure for processcard1
-- ----------------------------
DROP VIEW IF EXISTS `processcard1`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `processcard1` AS select `gmms_galss_task`.`processcard_id` AS `流程卡`,`gmms_galss_task`.`engineering_id` AS `工程号`,count(`gmms_galss_task`.`id`) AS `总数`,count((case when (`gmms_galss_task`.`work_procedure_number` >= 100) then 1 end)) AS `切割数`,count((case when (`gmms_galss_task`.`work_procedure_number` = 500) then 1 end)) AS `下架数`,count((case when ((`gmms_galss_task`.`slicecage_rank` is not null) and (`gmms_galss_task`.`work_state` = '已进库') and (`gmms_galss_task`.`slicecage_rank` > 0)) then 1 end)) AS `库内数`,`gmms_galss_task`.`drop_frame` AS `架号` from (`gmms_galss_task` join `gmms_horizontal` on((`gmms_galss_task`.`slicecage_rank` = `gmms_horizontal`.`id`))) group by `gmms_galss_task`.`processcard_id`,`gmms_galss_task`.`engineering_id`;

-- ----------------------------
-- View structure for processcard_ex
-- ----------------------------
DROP VIEW IF EXISTS `processcard_ex`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `processcard_ex` AS select `a`.`流程卡` AS `流程卡`,`a`.`工程号` AS `工程号`,`a`.`架号` AS `架号`,`a`.`总数` AS `总数`,`a`.`切割数` AS `切割数`,`a`.`下架数` AS `下架数`,`a`.`库内数` AS `库内数`,`b`.`id` AS `下片位置` from (`processcard` `a` left join `gmms_drop_frame` `b` on((`a`.`流程卡` = `b`.`processcard_id`)));

-- ----------------------------
-- View structure for processcard_ex1
-- ----------------------------
DROP VIEW IF EXISTS `processcard_ex1`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `processcard_ex1` AS select `a`.`流程卡` AS `流程卡`,`a`.`工程号` AS `工程号`,`a`.`架号` AS `架号`,`a`.`总数` AS `总数`,`a`.`切割数` AS `切割数`,`a`.`下架数` AS `下架数`,`a`.`库内数` AS `库内数`,`b`.`id` AS `下片位置` from (`processcard1` `a` left join `gmms_drop_frame` `b` on((`a`.`流程卡` = `b`.`processcard_id`)));

-- ----------------------------
-- View structure for rf_qiecailv_lck
-- ----------------------------
DROP VIEW IF EXISTS `rf_qiecailv_lck`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `rf_qiecailv_lck` AS select NULL AS `序号`,`g`.`engineering_id` AS `工程号`,`g`.`processcard_id` AS `流程卡号`,`g`.`thickness` AS `厚度`,`g`.`color` AS `膜系`,round((sum((`g`.`width` * `g`.`height`)) / 1000000),6) AS `面积`,count(`g`.`id`) AS `数量`,`f`.`cutting_rate` AS `切裁率`,`g`.`start_time` AS `开始时间` from (`gmms_galss_task_buffer` `g` left join `gmms_raw_file` `f` on((`g`.`engineering_id` = `f`.`file_name`))) where (`f`.`create_time` > (curdate() - interval 1 year)) group by `g`.`processcard_id` order by `g`.`engineering_id` desc,`g`.`processcard_id`;

-- ----------------------------
-- View structure for select
-- ----------------------------
DROP VIEW IF EXISTS `select`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `select` AS select `gmms_galss_task`.`processcard_id` AS `流程卡`,`gmms_galss_task`.`engineering_id` AS `工程号`,count(`gmms_galss_task`.`id`) AS `总数`,count((case when (`gmms_galss_task`.`work_procedure_number` >= 100) then 1 end)) AS `切割数`,count((case when (`gmms_galss_task`.`work_procedure_number` = 500) then 1 end)) AS `下架数`,count((case when ((`gmms_galss_task`.`slicecage_rank` is not null) and (`gmms_galss_task`.`work_state` = '已进库')) then 1 end)) AS `库内数`,`gmms_galss_task`.`drop_frame` AS `架号` from `gmms_galss_task` group by `gmms_galss_task`.`processcard_id`,`gmms_galss_task`.`engineering_id`;

-- ----------------------------
-- View structure for slicecage
-- ----------------------------
DROP VIEW IF EXISTS `slicecage`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `slicecage` AS select `kgt`.`processcard_id` AS `processcard_id`,count(0) AS `count2` from ((`gmms_raw_file` `krf` left join `gmms_galss_task` `kgt` on(((`krf`.`file_name` = `kgt`.`engineering_id`) and (`krf`.`state` = '已领取') and ((`krf`.`operator` = 2) or (`krf`.`operator` = 3))))) left join `gmms_slicecage` `kgs` on((`kgt`.`slicecage_rank` = `kgs`.`slicecage_rank`))) where ((`kgt`.`work_state` = '已进库') and (`kgt`.`work_procedure` = '理片') and (`kgs`.`gourp` = `krf`.`operator`) and (`kgs`.`flag` = 0)) group by `kgt`.`processcard_id`;

-- ----------------------------
-- View structure for slicecage_content
-- ----------------------------
DROP VIEW IF EXISTS `slicecage_content`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `slicecage_content` AS select `b`.`id` AS `格子`,`b`.`state` AS `格子状态`,`b`.`type_id` AS `玻璃类型ID`,`a`.`processcard_id` AS `流程卡`,`a`.`drop_frame` AS `架号`,`a`.`width` AS `宽度`,`a`.`height` AS `高度`,`a`.`work_procedure` AS `工序`,`a`.`work_state` AS `工序状态` from (`gmms_galss_task` `a` join `gmms_slicecage` `b` on((`a`.`slicecage_rank` = `b`.`id`))) order by `b`.`id`,`a`.`rank`;

-- ----------------------------
-- View structure for slicecage_view
-- ----------------------------
DROP VIEW IF EXISTS `slicecage_view`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `slicecage_view` AS select `b`.`slicecage_rank` AS `格子`,`b`.`state` AS `格子状态`,`b`.`type_id` AS `玻璃类型ID`,`b`.`count` AS `count`,`a`.`processcard_id` AS `流程卡`,`a`.`drop_frame` AS `架号`,`a`.`width` AS `宽度`,`a`.`height` AS `高度`,`a`.`work_procedure` AS `工序`,`a`.`work_state` AS `工序状态`,`b`.`gourp` AS `groups` from (`gmms_galss_task` `a` join `gmms_slicecage` `b` on(((`a`.`slicecage_rank` = `b`.`slicecage_rank`) and (`a`.`processcard_id` = `b`.`processcard_id`)))) order by `b`.`id`,`a`.`rank`;

-- ----------------------------
-- View structure for spq111
-- ----------------------------
DROP VIEW IF EXISTS `spq111`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `spq111` AS select `dr`.`id` AS `id`,`dr`.`stops` AS `stops`,`dr`.`processcard_id` AS `processcard_id`,`dr`.`state` AS `state`,(select count(0) from `gmms_galss_task_2` where (`gmms_galss_task_2`.`processcard_id` = `dr`.`processcard_id`)) AS `zongshu`,(select count(0) from `gmms_galss_task_2` where ((`gmms_galss_task_2`.`processcard_id` = `dr`.`processcard_id`) and (`gmms_galss_task_2`.`work_state` = '破损'))) AS `posunshu`,(select count(0) from `gmms_galss_task_2` where ((`gmms_galss_task_2`.`processcard_id` = `dr`.`processcard_id`) and (`gmms_galss_task_2`.`work_state` <> '未优化'))) AS `yiyouhua`,(select count(0) from `gmms_galss_task_2` where ((`gmms_galss_task_2`.`processcard_id` = `dr`.`processcard_id`) and (`gmms_galss_task_2`.`work_procedure` <> '钢化上片') and (`gmms_galss_task_2`.`work_state` <> '破损'))) AS `wanchengshu`,((select count(0) from `gmms_galss_task_2` where (`gmms_galss_task_2`.`processcard_id` = `dr`.`processcard_id`)) - (select count(0) from `gmms_galss_task_2` where ((`gmms_galss_task_2`.`processcard_id` = `dr`.`processcard_id`) and (`gmms_galss_task_2`.`work_procedure` <> '钢化上片') and (`gmms_galss_task_2`.`work_state` <> '破损')))) AS `shengyushu`,`dr`.`surplus_count` AS `shangpianshengyushu`,`dr`.`noodles` AS `noodles`,`dr`.`flag` AS `flag`,`dr`.`groups` AS `groups`,(select `gmms_liuchengka`.`Occupied` from `gmms_liuchengka` where (`gmms_liuchengka`.`liuchengka` = `dr`.`processcard_id`)) AS `weizhi` from `gmms_drop_frame_2` `dr`;

-- ----------------------------
-- View structure for tempereworkgalsss1
-- ----------------------------
DROP VIEW IF EXISTS `tempereworkgalsss1`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `tempereworkgalsss1` AS select `temperelayout`.`id` AS `id`,`temperelayout`.`work_id` AS `work_id`,`temperelayout`.`layout_number` AS `layout_number`,`temperelayout`.`insert_time` AS `insert_time`,`temperelayout`.`layout_rate` AS `layout_rate`,`temperelayout`.`glass_number` AS `glass_number`,`temperelayout`.`x` AS `x`,`temperelayout`.`y` AS `y`,`temperelayout`.`width` AS `width`,`temperelayout`.`height` AS `height`,`temperelayout`.`rotate` AS `rotate`,`temperelayout`.`row` AS `row`,`temperelayout`.`column` AS `column`,`temperelayout`.`glass_id` AS `glass_id`,`temperelayout`.`erp_glass_id` AS `erp_glass_id`,`temperelayout`.`head_info` AS `head_info`,`glasstask`.`work_procedure` AS `work_procedure`,`glasstask`.`work_state` AS `work_state` from (`gmms_galss_task_2` `glasstask` join `gmms_tempere_layout` `temperelayout` on((`glasstask`.`id` = `temperelayout`.`glass_id`))) where (`glasstask`.`furnace` = (select `gmms_galss_task_2`.`furnace` from `gmms_galss_task_2` where ((`gmms_galss_task_2`.`work_state` = '已完成') and (`gmms_galss_task_2`.`work_procedure` = '钢化') and (`gmms_galss_task_2`.`groups` = 1)) order by `gmms_galss_task_2`.`furnace` limit 1));

-- ----------------------------
-- View structure for tempereworkgalsss2
-- ----------------------------
DROP VIEW IF EXISTS `tempereworkgalsss2`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `tempereworkgalsss2` AS select `temperelayout`.`id` AS `id`,`temperelayout`.`work_id` AS `work_id`,`temperelayout`.`layout_number` AS `layout_number`,`temperelayout`.`insert_time` AS `insert_time`,`temperelayout`.`layout_rate` AS `layout_rate`,`temperelayout`.`glass_number` AS `glass_number`,`temperelayout`.`x` AS `x`,`temperelayout`.`y` AS `y`,`temperelayout`.`width` AS `width`,`temperelayout`.`height` AS `height`,`temperelayout`.`rotate` AS `rotate`,`temperelayout`.`row` AS `row`,`temperelayout`.`column` AS `column`,`temperelayout`.`glass_id` AS `glass_id`,`temperelayout`.`erp_glass_id` AS `erp_glass_id`,`temperelayout`.`head_info` AS `head_info`,`glasstask`.`work_procedure` AS `work_procedure`,`glasstask`.`work_state` AS `work_state` from (`gmms_galss_task_2` `glasstask` join `gmms_tempere_layout` `temperelayout` on((`glasstask`.`id` = `temperelayout`.`glass_id`))) where (`glasstask`.`furnace` = (select `gmms_galss_task_2`.`furnace` from `gmms_galss_task_2` where (((`gmms_galss_task_2`.`work_state` = '已完成') or (`gmms_galss_task_2`.`work_state` = '正在工作')) and (`gmms_galss_task_2`.`work_procedure` = '钢化') and (`gmms_galss_task_2`.`groups` = 2)) order by `gmms_galss_task_2`.`furnace` limit 1));

-- ----------------------------
-- View structure for tempere_task_glass
-- ----------------------------
DROP VIEW IF EXISTS `tempere_task_glass`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `tempere_task_glass` AS select `a`.`id` AS `id`,`a`.`processcard_id` AS `processcard`,`b`.`stops` AS `position`,`b`.`flag` AS `front`,`a`.`width` AS `width`,`a`.`height` AS `height`,`a`.`stop_rank` AS `glass_idx`,`a`.`thickness` AS `thickness`,`a`.`color` AS `color`,`b`.`groups` AS `work_id`,0 AS `layout_mode`,(select `gmms_galss_task_buffer`.`w` from `gmms_galss_task_buffer` where (`gmms_galss_task_buffer`.`id` = `a`.`id`)) AS `w`,(select `gmms_galss_task_buffer`.`h` from `gmms_galss_task_buffer` where (`gmms_galss_task_buffer`.`id` = `a`.`id`)) AS `h` from (`gmms_galss_task_2` `a` join `gmms_drop_frame_2` `b` on((`a`.`processcard_id` = `b`.`processcard_id`))) where ((`a`.`work_procedure` = '钢化上片') and (`a`.`work_state` = '未优化') and (`b`.`flag` = 1));

-- ----------------------------
-- View structure for tempere_task_glass_copy1
-- ----------------------------
DROP VIEW IF EXISTS `tempere_task_glass_copy1`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `tempere_task_glass_copy1` AS select `a`.`id` AS `id`,`a`.`processcard_id` AS `processcard`,`b`.`stops` AS `position`,`b`.`flag` AS `front`,`c`.`w` AS `width`,`c`.`h` AS `height`,`a`.`stop_rank` AS `glass_idx`,`a`.`thickness` AS `thickness`,`a`.`color` AS `color`,`b`.`groups` AS `work_id`,0 AS `layout_mode` from ((`gmms_galss_task_2` `a` join `gmms_drop_frame_2` `b` on((`a`.`processcard_id` = `b`.`processcard_id`))) join `gmms_galss_task_buffer` `c` on((`a`.`id` = `c`.`id`))) where ((`a`.`work_procedure` = '钢化上片') and (`a`.`work_state` = '未优化') and (`b`.`flag` = 1));

-- ----------------------------
-- View structure for tempere_task_glass_copy2
-- ----------------------------
DROP VIEW IF EXISTS `tempere_task_glass_copy2`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `tempere_task_glass_copy2` AS select `a`.`id` AS `id`,`a`.`processcard_id` AS `processcard`,`b`.`stops` AS `position`,`b`.`flag` AS `front`,`a`.`width` AS `width`,`a`.`height` AS `height`,`a`.`stop_rank` AS `glass_idx`,`a`.`thickness` AS `thickness`,`a`.`color` AS `color`,`b`.`groups` AS `work_id`,0 AS `layout_mode`,`a`.`height` AS `h`,`a`.`width` AS `w` from (`gmms_galss_task_2` `a` join `gmms_drop_frame_2` `b` on((`a`.`processcard_id` = `b`.`processcard_id`))) where ((`a`.`work_procedure` = '钢化上片') and (`a`.`work_state` = '未优化') and (`b`.`flag` = 1));

-- ----------------------------
-- View structure for tempere_task_glass_copy3
-- ----------------------------
DROP VIEW IF EXISTS `tempere_task_glass_copy3`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `tempere_task_glass_copy3` AS select `a`.`id` AS `id`,`a`.`processcard_id` AS `processcard`,`b`.`stops` AS `position`,`b`.`flag` AS `front`,`c`.`w` AS `width`,`c`.`h` AS `height`,`a`.`stop_rank` AS `glass_idx`,`a`.`thickness` AS `thickness`,`a`.`color` AS `color`,`b`.`groups` AS `work_id`,0 AS `layout_mode`,`c`.`h` AS `h`,`c`.`w` AS `w` from ((`gmms_galss_task_2` `a` join `gmms_drop_frame_2` `b` on((`a`.`processcard_id` = `b`.`processcard_id`))) join `gmms_galss_task_buffer` `c` on((`a`.`id` = `c`.`id`))) where ((`a`.`work_procedure` = '钢化上片') and (`a`.`work_state` = '未优化') and (`b`.`flag` = 1));

-- ----------------------------
-- View structure for tempere_task_glass_copy4
-- ----------------------------
DROP VIEW IF EXISTS `tempere_task_glass_copy4`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `tempere_task_glass_copy4` AS select `a`.`id` AS `id`,`a`.`processcard_id` AS `processcard`,`b`.`stops` AS `position`,`b`.`flag` AS `front`,`a`.`width` AS `width`,`a`.`height` AS `height`,`a`.`stop_rank` AS `glass_idx`,`a`.`thickness` AS `thickness`,`a`.`color` AS `color`,`b`.`groups` AS `work_id`,0 AS `layout_mode`,`a`.`height` AS `w`,`a`.`width` AS `h` from (`gmms_galss_task_2` `a` join `gmms_drop_frame_2` `b` on((`a`.`processcard_id` = `b`.`processcard_id`))) where ((`a`.`work_procedure` = '钢化上片') and (`a`.`work_state` = '未优化') and (`b`.`flag` = 1));

-- ----------------------------
-- View structure for v_cc_cpinfo
-- ----------------------------
DROP VIEW IF EXISTS `v_cc_cpinfo`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `v_cc_cpinfo` AS select NULL AS `序号`,`lk`.`parent_liuchengka` AS `中空流程卡号`,NULL AS `总数/库内数`,(select group_concat(`gmms_liuchengka`.`liuchengka` separator ',') from `gmms_liuchengka` where ((`gmms_liuchengka`.`parent_liuchengka` = `lk`.`parent_liuchengka`) and `gmms_liuchengka`.`liuchengka` in (select `gmms_shelf_rack_2`.`processcard_id` from `gmms_shelf_rack_2`))) AS `库内的流程卡`,NULL AS `未到位流程卡`,(select group_concat(`gmms_liuchengka`.`liuchengka` separator ',') from `gmms_liuchengka` where (`gmms_liuchengka`.`parent_liuchengka` = `lk`.`parent_liuchengka`)) AS `包含的流程卡` from (`gmms_liuchengka` `lk` left join `gmms_shelf_rack_2` `sr` on((`lk`.`liuchengka` = `sr`.`processcard_id`))) where (`sr`.`processcard_id` is not null) group by `lk`.`parent_liuchengka`;

-- ----------------------------
-- View structure for v_cc_liaojia
-- ----------------------------
DROP VIEW IF EXISTS `v_cc_liaojia`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `v_cc_liaojia` AS select `sr`.`id` AS `id`,`sr`.`state` AS `state`,`sr`.`processcard_id` AS `processcard_id`,`sr`.`orientation` AS `orientation`,`sr`.`falg` AS `falg` from `gmms_shelf_rack_2` `sr` where ((`sr`.`id` < 42) and (not(`sr`.`id` in (select `gmms_drop_frame_5`.`rank` from `gmms_drop_frame_5` where ((`gmms_drop_frame_5`.`groups_` <> 100) and (`gmms_drop_frame_5`.`rank` is not null))))));

-- ----------------------------
-- View structure for v_cipo
-- ----------------------------
DROP VIEW IF EXISTS `v_cipo`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `v_cipo` AS select NULL AS `xh`,`ep`.`glass_id` AS `glass_id`,`ep`.`processcard_id` AS `processcard_id`,`gtb`.`edg_width` AS `width`,`gtb`.`edg_height` AS `height`,concat(`gtb`.`thickness`,'  ',`gtb`.`color`) AS `moxi`,1 AS `shuliang`,ifnull(`gs`.`work_procedure`,' ') AS `work_procedure`,`ep`.`pierced_type` AS `pierced_type`,`ep`.`pierced_cause` AS `pierced_cause`,`ep`.`process` AS `process`,`ep`.`team_name` AS `team_name`,`ep`.`create_time` AS `create_time`,`ep`.`is_report` AS `is_report` from ((`e_pierceds` `ep` left join `gmms_galss_task_2` `gs` on((`ep`.`glass_id` = `gs`.`id`))) left join `gmms_galss_task_buffer` `gtb` on((`gtb`.`id` = `ep`.`glass_id`)));

-- ----------------------------
-- View structure for v_completedtasks
-- ----------------------------
DROP VIEW IF EXISTS `v_completedtasks`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `v_completedtasks` AS select `gmms_raw_file`.`id` AS `id`,`gmms_raw_file`.`file_name` AS `file_name`,`gmms_raw_file`.`file_url` AS `file_url`,`gmms_raw_file`.`state` AS `state`,`gmms_raw_file`.`operator` AS `operator`,`gmms_raw_file`.`width` AS `width`,`gmms_raw_file`.`height` AS `height`,`gmms_raw_file`.`thickness` AS `thickness`,`gmms_raw_file`.`count` AS `count`,`gmms_raw_file`.`create_time` AS `create_time`,`gmms_raw_file`.`receive_time` AS `receive_time`,`gmms_raw_file`.`galssmeasure` AS `galssmeasure`,`gmms_raw_file`.`patternmeasure` AS `patternmeasure`,`gmms_raw_file`.`cutting_rate` AS `cutting_rate`,`gmms_raw_file`.`Update_time` AS `Update_time` from `gmms_raw_file` where ((`gmms_raw_file`.`state` like '%完成%') and (`gmms_raw_file`.`receive_time` > (curdate() - interval 7 day))) order by `gmms_raw_file`.`receive_time` desc,`gmms_raw_file`.`id` desc;

-- ----------------------------
-- View structure for v_ghh_online_glass
-- ----------------------------
DROP VIEW IF EXISTS `v_ghh_online_glass`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `v_ghh_online_glass` AS select NULL AS `NULL`,`ggt`.`galss_id` AS `galss_id`,`ggt`.`edg_width` AS `edg_width`,`ggt`.`edg_height` AS `edg_height`,concat(`ggt`.`thickness`,'mm ',`ggt`.`color`) AS `moxi`,`ggt`.`processcard_id` AS `processcard_id`,`ggt`.`work_procedure` AS `work_procedure`,`ggt`.`work_state` AS `work_state`,`ggt`.`drop_frame` AS `drop_frame`,`ggt`.`orders` AS `orders`,`ggt`.`grouporder` AS `grouporder`,`ggt`.`furnace` AS `furnace`,`ggt`.`groups` AS `groups`,`ggt`.`fine_throwing` AS `fine_throwing`,`ggt`.`id` AS `id` from `gmms_galss_task_2` `ggt` where ((`ggt`.`orders` is not null) and (((`ggt`.`work_procedure` = '钢化') and (`ggt`.`work_state` <> '等待中')) or (`ggt`.`work_procedure` = '钢化后摆片') or (`ggt`.`work_procedure` = '钢化后复合') or ((`ggt`.`work_procedure` = '钢化后下片') and (`ggt`.`work_state` <> '已完成'))) and (`ggt`.`work_state` <> '破损')) order by `ggt`.`furnace`,`ggt`.`grouporder`,`ggt`.`orders`;

-- ----------------------------
-- View structure for v_ghq_liuchengka
-- ----------------------------
DROP VIEW IF EXISTS `v_ghq_liuchengka`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `v_ghq_liuchengka` AS select NULL AS `NULL`,`l`.`liuchengka` AS `liuchengka`,`b`.`ceng` AS `ceng`,`b`.`houdu` AS `houdu`,`b`.`moxi` AS `moxi`,`b`.`mianji` AS `mianji`,`b`.`shuliang` AS `shuliang`,`b`.`Create_time` AS `Create_time` from (`gmms_liuchengka` `l` left join `gmms_liuchengka_buffer` `b` on((`l`.`liuchengka` = `b`.`liuchengkahaoceng`))) where ((`l`.`current_status` = '已完成') and (`l`.`current_work_position` = '创建') and (`l`.`gy_gang` = 1) and (`l`.`piece_count` > 10) and (`l`.`minArea` > 0.7) and (`l`.`isgang` = '冷加工下片完成')) group by `l`.`liuchengka` order by `b`.`Create_time`;

-- ----------------------------
-- View structure for v_ghq_online_glass
-- ----------------------------
DROP VIEW IF EXISTS `v_ghq_online_glass`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `v_ghq_online_glass` AS select NULL AS `NULL`,`gh`.`galss_id` AS `galss_id`,`gh`.`edg_width` AS `edg_width`,`gh`.`edg_height` AS `edg_height`,concat(`gh`.`thickness`,'mm ',`gh`.`color`) AS `moxi`,`gh`.`processcard_id` AS `processcard_id`,`gh`.`work_procedure` AS `work_procedure`,`gh`.`work_state` AS `work_state`,`gh`.`drop_frame` AS `drop_frame`,`gh`.`orders` AS `orders`,`gh`.`grouporder` AS `grouporder`,`gh`.`furnace` AS `furnace`,`gh`.`groups` AS `groups`,`gh`.`fine_throwing` AS `fine_throwing`,`gh`.`id` AS `id` from `gmms_galss_task_2` `gh` where ((`gh`.`orders` is not null) and ((`gh`.`work_procedure` = '钢化上片') or (`gh`.`work_procedure` = '钢化前复合') or (`gh`.`work_procedure` = '钢化前摆片') or (`gh`.`work_procedure` = '钢化')) and (`gh`.`work_state` <> '破损')) order by `gh`.`furnace`,`gh`.`orders`;

-- ----------------------------
-- View structure for v_gh_shangpianwei
-- ----------------------------
DROP VIEW IF EXISTS `v_gh_shangpianwei`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `v_gh_shangpianwei` AS select `f`.`id` AS `id`,`f`.`stops` AS `stops`,`f`.`processcard_id` AS `processcard_id`,`f`.`state` AS `state`,`f`.`processcard_count` AS `zongshu`,(select count(1) from `gmms_galss_task_2` `t2` where ((`t2`.`work_state` = '破损') and (`t2`.`processcard_id` = `f`.`processcard_id`))) AS `posunshu`,(select count(1) from `gmms_galss_task_2` `t2` where ((`t2`.`work_state` = '已优化') and (`t2`.`processcard_id` = `f`.`processcard_id`))) AS `yiyouhua`,NULL AS `wanchengshu`,(select count(1) from `gmms_galss_task_2` `t3` where ((`t3`.`work_state` <> '破损') and (`t3`.`work_procedure` = '钢化上片') and (`t3`.`processcard_id` = `f`.`processcard_id`))) AS `shengyushu`,`f`.`surplus_count` AS `shangpianshengyushu`,`f`.`noodles` AS `noodles`,`f`.`flag` AS `flag`,`f`.`groups` AS `groups`,(select `l`.`Occupied` from `gmms_liuchengka` `l` where (`l`.`liuchengka` = `f`.`processcard_id`)) AS `weizhi` from `gmms_drop_frame_2` `f` group by `f`.`id`,`f`.`stops`,`f`.`processcard_id`,`f`.`state`,`f`.`noodles`,`f`.`flag`,`f`.`groups`;

-- ----------------------------
-- View structure for v_gh_shangpianwei_copy1
-- ----------------------------
DROP VIEW IF EXISTS `v_gh_shangpianwei_copy1`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `v_gh_shangpianwei_copy1` AS select `f`.`id` AS `id`,`f`.`stops` AS `stops`,`f`.`processcard_id` AS `processcard_id`,`f`.`state` AS `state`,(select count(1) from `gmms_galss_task_2` `t2` where (`t2`.`processcard_id` = `f`.`processcard_id`)) AS `zongshu`,(select count(1) from `gmms_galss_task_2` `t2` where ((`t2`.`work_state` = '破损') and (`t2`.`processcard_id` = `f`.`processcard_id`))) AS `posunshu`,(select count(1) from `gmms_galss_task_2` `t2` where ((`t2`.`work_state` = '已优化') and (`t2`.`processcard_id` = `f`.`processcard_id`))) AS `yiyouhua`,(select count(1) from `gmms_galss_task_2` `t2` where ((`t2`.`work_state` = '已完成') and (`t2`.`processcard_id` = `f`.`processcard_id`))) AS `wanchengshu`,NULL AS `shengyushu`,`f`.`noodles` AS `noodles`,`f`.`flag` AS `flag`,`f`.`groups` AS `groups` from (`gmms_drop_frame_2` `f` left join `gmms_galss_task_2` `t` on(((`f`.`processcard_id` = `t`.`processcard_id`) and (`f`.`engineering_id` = `t`.`engineering_id`))));

-- ----------------------------
-- View structure for v_gh_shangpianwei_copy2
-- ----------------------------
DROP VIEW IF EXISTS `v_gh_shangpianwei_copy2`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `v_gh_shangpianwei_copy2` AS select `f`.`id` AS `id`,`f`.`stops` AS `stops`,`f`.`processcard_id` AS `processcard_id`,`f`.`state` AS `state`,(select count(1) from `gmms_galss_task_2` `t2` where (`t2`.`processcard_id` = `f`.`processcard_id`)) AS `zongshu`,(select count(1) from `gmms_galss_task_2` `t2` where ((`t2`.`work_state` = '破损') and (`t2`.`processcard_id` = `f`.`processcard_id`))) AS `posunshu`,(select count(1) from `gmms_galss_task_2` `t2` where ((`t2`.`work_state` = '已优化') and (`t2`.`processcard_id` = `f`.`processcard_id`))) AS `yiyouhua`,(select count(1) from `gmms_galss_task_2` `t2` where ((`t2`.`work_state` = '已完成') and (`t2`.`processcard_id` = `f`.`processcard_id`))) AS `wanchengshu`,NULL AS `shengyushu`,`f`.`noodles` AS `noodles`,`f`.`flag` AS `flag`,`f`.`groups` AS `groups` from (`gmms_drop_frame_2` `f` left join `gmms_galss_task_2` `t` on((`f`.`processcard_id` = `t`.`processcard_id`))) group by `f`.`id`,`f`.`stops`,`f`.`processcard_id`,`f`.`state`,`f`.`noodles`,`f`.`flag`,`f`.`groups`;

-- ----------------------------
-- View structure for v_gh_shangpianwei_copy3
-- ----------------------------
DROP VIEW IF EXISTS `v_gh_shangpianwei_copy3`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `v_gh_shangpianwei_copy3` AS select `f`.`id` AS `id`,`f`.`stops` AS `stops`,`f`.`processcard_id` AS `processcard_id`,`f`.`state` AS `state`,`f`.`processcard_count` AS `zongshu`,(select count(1) from `gmms_galss_task_2` `t2` where ((`t2`.`work_state` = '破损') and (`t2`.`processcard_id` = `f`.`processcard_id`))) AS `posunshu`,(select count(1) from `gmms_galss_task_2` `t2` where ((`t2`.`work_state` = '已优化') and (`t2`.`processcard_id` = `f`.`processcard_id`))) AS `yiyouhua`,(select count(1) from `gmms_galss_task_2` `t2` where ((`t2`.`work_state` = '已完成') and (`t2`.`processcard_id` = `f`.`processcard_id`))) AS `wanchengshu`,NULL AS `shengyushu`,`f`.`surplus_count` AS `shangpianshengyushu`,`f`.`noodles` AS `noodles`,`f`.`flag` AS `flag`,`f`.`groups` AS `groups`,(select `l`.`Occupied` from `gmms_liuchengka` `l` where (`l`.`liuchengka` = `f`.`processcard_id`)) AS `weizhi` from (`gmms_drop_frame_2` `f` left join `gmms_galss_task_2` `t` on((`f`.`processcard_id` = `t`.`processcard_id`))) group by `f`.`id`,`f`.`stops`,`f`.`processcard_id`,`f`.`state`,`f`.`noodles`,`f`.`flag`,`f`.`groups`;

-- ----------------------------
-- View structure for v_gh_shangpianwei_copy4_12
-- ----------------------------
DROP VIEW IF EXISTS `v_gh_shangpianwei_copy4_12`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `v_gh_shangpianwei_copy4_12` AS select `f`.`id` AS `id`,`f`.`stops` AS `stops`,`f`.`processcard_id` AS `processcard_id`,`f`.`state` AS `state`,`f`.`processcard_count` AS `zongshu`,(select count(1) from `gmms_galss_task_2` `t2` where ((`t2`.`work_state` = '破损') and (`t2`.`processcard_id` = `f`.`processcard_id`))) AS `posunshu`,(select count(1) from `gmms_galss_task_2` `t2` where ((`t2`.`work_state` = '已优化') and (`t2`.`processcard_id` = `f`.`processcard_id`))) AS `yiyouhua`,NULL AS `wanchengshu`,(select count(1) from `gmms_galss_task_2` `t3` where ((`t3`.`work_state` <> '已完成') and (`t3`.`work_procedure` = '钢化上片') and (`t3`.`processcard_id` = `f`.`processcard_id`))) AS `shengyushu`,`f`.`surplus_count` AS `shangpianshengyushu`,`f`.`noodles` AS `noodles`,`f`.`flag` AS `flag`,`f`.`groups` AS `groups`,(select `l`.`Occupied` from `gmms_liuchengka` `l` where (`l`.`liuchengka` = `f`.`processcard_id`)) AS `weizhi` from (`gmms_drop_frame_2` `f` left join `gmms_galss_task_2` `t` on((`f`.`processcard_id` = `t`.`processcard_id`))) group by `f`.`id`,`f`.`stops`,`f`.`processcard_id`,`f`.`state`,`f`.`noodles`,`f`.`flag`,`f`.`groups`;

-- ----------------------------
-- View structure for v_gh_xiapianwei
-- ----------------------------
DROP VIEW IF EXISTS `v_gh_xiapianwei`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `v_gh_xiapianwei` AS select `d`.`id` AS `Id`,`d`.`drop_groups` AS `groups`,`d`.`stops` AS `stops`,`d`.`rank` AS `rank`,`s`.`id` AS `rId`,`s`.`state` AS `state`,(select distinct `g`.`gc_number` from `gmms_galss_task_2` `g` where (`g`.`processcard_id` = `s`.`processcard_id`)) AS `gongchenghao`,`s`.`processcard_id` AS `processcard_id`,(select count(`g`.`id`) from `gmms_galss_task_2` `g` where ((`g`.`processcard_id` = `s`.`processcard_id`) and (`g`.`work_procedure` = '钢化后下片') and (`g`.`work_state` = '已完成'))) AS `wanchengshu`,(select count(`g`.`id`) from `gmms_galss_task_2` `g` where ((`g`.`processcard_id` = `s`.`processcard_id`) and (`g`.`work_state` = '破损'))) AS `posunshu`,(select count(`g`.`id`) from `gmms_galss_task_2` `g` where (`g`.`processcard_id` = `s`.`processcard_id`)) AS `zongshu`,NULL AS `weiwanchegnshu`,`d`.`orientation` AS `orientation`,`d`.`stop_rank` AS `stop_rank`,`d`.`sign` AS `sign`,`d`.`flag` AS `flag`,`d`.`groups_` AS `groups_` from (`gmms_drop_frame_5` `d` left join `gmms_shelf_rack_2` `s` on((`d`.`rank` = `s`.`id`))) where (`d`.`groups_` = 1);

-- ----------------------------
-- View structure for v_liuchengkareportform
-- ----------------------------
DROP VIEW IF EXISTS `v_liuchengkareportform`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `v_liuchengkareportform` AS select `g`.`id` AS `id`,`g`.`engineering_id` AS `gongchenghao`,`g`.`processcard_id` AS `liuchengkahao`,`g`.`thickness` AS `thinckness`,`g`.`color` AS `color`,sum((`g`.`width` * `g`.`height`)) AS `mianji`,count(`g`.`id`) AS `shuliang`,`g`.`start_time` AS `start_time` from `gmms_galss_task_buffer` `g` group by `g`.`processcard_id` order by `g`.`engineering_id` desc,`g`.`processcard_id`;

-- ----------------------------
-- View structure for v_rf_qiecailv
-- ----------------------------
DROP VIEW IF EXISTS `v_rf_qiecailv`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `v_rf_qiecailv` AS select NULL AS `序号`,`f`.`file_name` AS `工程号`,`f`.`state` AS `状态`,`f`.`operator` AS `产线`,(select `gmms_galss_task_buffer`.`color` from `gmms_galss_task_buffer` where (`gmms_galss_task_buffer`.`engineering_id` = `f`.`file_name`) limit 0,1) AS `膜系`,(select `gmms_galss_task_buffer`.`thickness` from `gmms_galss_task_buffer` where (`gmms_galss_task_buffer`.`engineering_id` = `f`.`file_name`) limit 0,1) AS `厚度`,(select count(1) from `gmms_galss_task_buffer` `b` where ((`b`.`id` > 500000) and (`b`.`engineering_id` = `f`.`file_name`))) AS `小片数量`,`f`.`galssmeasure` AS `小片面积`,`f`.`patternmeasure` AS `原片面积`,`f`.`cutting_rate` AS `切裁率`,`f`.`create_time` AS `创建时间`,`f`.`receive_time` AS `完成时间` from `gmms_raw_file` `f` where ((`f`.`state` like '%完成') and (`f`.`file_name` like 'P%') and (`f`.`create_time` > '2022-10-01')) order by `f`.`receive_time` desc;

-- ----------------------------
-- View structure for v_rf_qiecailv_lck
-- ----------------------------
DROP VIEW IF EXISTS `v_rf_qiecailv_lck`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `v_rf_qiecailv_lck` AS select NULL AS `序号`,`g`.`engineering_id` AS `工程号`,`g`.`processcard_id` AS `流程卡号`,`g`.`thickness` AS `厚度`,`g`.`color` AS `膜系`,round((sum((`g`.`width` * `g`.`height`)) / 1000000),6) AS `面积`,count(`g`.`id`) AS `数量`,`f`.`cutting_rate` AS `切裁率`,`g`.`start_time` AS `开始时间` from (`gmms_galss_task_buffer` `g` left join `gmms_raw_file` `f` on((`g`.`engineering_id` = `f`.`file_name`))) where (`g`.`start_time` > (curdate() - interval 1 year)) group by `g`.`processcard_id` order by `g`.`engineering_id` desc,`g`.`processcard_id`;

-- ----------------------------
-- View structure for v_zklp_shangpianwei
-- ----------------------------
DROP VIEW IF EXISTS `v_zklp_shangpianwei`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `v_zklp_shangpianwei` AS select `d`.`id` AS `Id`,`d`.`drop_groups` AS `groups`,`d`.`stops` AS `stops`,`d`.`rank` AS `rank`,`d`.`flag` AS `flag`,`d`.`sign` AS `sign`,`s`.`processcard_id` AS `processcard_id`,(select `t`.`piece_count` from `y_shelfrackcardinfo` `t` where (`t`.`liuchengka` = `s`.`processcard_id`)) AS `zongshu`,(select count(`g`.`id`) from `gmms_galss_task_shelf` `g` where (`g`.`processcard_id` = `s`.`processcard_id`)) AS `kuneishu`,(select count(`g`.`id`) from `gmms_galss_task_shelf` `g` where ((`g`.`processcard_id` = `s`.`processcard_id`) and (`g`.`work_state` = '破损'))) AS `posunshu`,NULL AS `quepianshu`,NULL AS `yishangpian`,(select count(`g`.`id`) from `gmms_galss_task_shelf` `g` where ((`g`.`processcard_id` = `s`.`processcard_id`) and (`g`.`work_procedure` = '钢化后下片') and (`g`.`work_state` = '已完成'))) AS `jiazishengyushu`,`d`.`orientation` AS `orientation`,`d`.`stop_rank` AS `stop_rank` from (`gmms_drop_frame_5` `d` left join `gmms_shelf_rack_2` `s` on((`d`.`rank` = `s`.`id`))) where (`d`.`groups_` = 3);

-- ----------------------------
-- View structure for v_zk_shangpianwei
-- ----------------------------
DROP VIEW IF EXISTS `v_zk_shangpianwei`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `v_zk_shangpianwei` AS select `d`.`id` AS `Id`,`d`.`drop_groups` AS `groups`,`d`.`stops` AS `stops`,`d`.`rank` AS `rank`,`d`.`flag` AS `flag`,`d`.`sign` AS `sign`,`s`.`processcard_id` AS `processcard_id`,(select `t`.`piece_count` from `y_shelfrackcardinfo` `t` where (`t`.`liuchengka` = `s`.`processcard_id`)) AS `zongshu`,(select count(`g`.`id`) from `gmms_galss_task_shelf` `g` where ((`g`.`processcard_id` = `s`.`processcard_id`) and (`g`.`stop_position` > 0))) AS `kuneishu`,((select `t`.`piece_count` from `y_shelfrackcardinfo` `t` where (`t`.`liuchengka` = `s`.`processcard_id`)) - (select count(`g`.`id`) from `gmms_galss_task_shelf` `g` where ((`g`.`processcard_id` = `s`.`processcard_id`) and (`g`.`stop_position` > 0)))) AS `quepianshu`,(select count(`g`.`id`) from `gmms_galss_task_shelf` `g` where ((`g`.`processcard_id` = `s`.`processcard_id`) and (`g`.`work_procedure` = '中空上片') and (`g`.`work_state` = '已完成'))) AS `wanchengshu`,NULL AS `shengyushu`,`d`.`orientation` AS `orientation`,`d`.`stop_rank` AS `stop_rank` from (`gmms_drop_frame_5` `d` left join `gmms_shelf_rack_2` `s` on((`d`.`rank` = `s`.`id`))) where (`d`.`groups_` = 4);

-- ----------------------------
-- View structure for y_howllorslicetasks
-- ----------------------------
DROP VIEW IF EXISTS `y_howllorslicetasks`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `y_howllorslicetasks` AS select `y_howllowgroupby`.`main_liuchengka` AS `main_liuchengka`,sum(`y_howllowgroupby`.`piececount`) AS `sumpiececount`,sum(`y_howllowgroupby`.`storagecounts`) AS `sumstoragecount`,(sum(`y_howllowgroupby`.`piececount`) = sum(`y_howllowgroupby`.`storagecounts`)) AS `isprice`,count(0) AS `ishowllorslice` from `y_howllowgroupby` group by `y_howllowgroupby`.`main_liuchengka` having (sum(`y_howllowgroupby`.`piececount`) = sum(`y_howllowgroupby`.`storagecounts`));

-- ----------------------------
-- View structure for y_howllowgroupby
-- ----------------------------
DROP VIEW IF EXISTS `y_howllowgroupby`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `y_howllowgroupby` AS select `y_shelfrackcardinfo`.`id` AS `id`,`y_shelfrackcardinfo`.`orientation` AS `orientation`,`y_shelfrackcardinfo`.`falg` AS `falg`,`y_shelfrackcardinfo`.`state` AS `state`,`y_shelfrackcardinfo`.`liuchengka` AS `liuchengka`,`y_shelfrackcardinfo`.`main_liuchengka` AS `main_liuchengka`,`y_shelfrackcardinfo`.`cardrank` AS `cardrank`,count(`y_shelfrackcardinfo`.`liuchengka`) AS `cardcount`,sum(`y_shelfrackcardinfo`.`piece_count`) AS `piececount`,sum(`y_shelfrackcardinfo`.`storagecount`) AS `storagecounts` from `y_shelfrackcardinfo` group by `y_shelfrackcardinfo`.`main_liuchengka`,`y_shelfrackcardinfo`.`groupwidth`,`y_shelfrackcardinfo`.`groupheight`;

-- ----------------------------
-- View structure for y_liuchengka
-- ----------------------------
DROP VIEW IF EXISTS `y_liuchengka`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `y_liuchengka` AS select `lk`.`parent_liuchengka` AS `parent_liuchengka`,`lk`.`liuchengka` AS `liuchengka`,`ypl`.`zongshu` AS `zongshu`,`lk`.`piece_count` AS `piece_count`,`sr`.`id` AS `id`,`sr`.`state` AS `state`,`sr`.`falg` AS `falg`,`sr`.`orientation` AS `orientation` from ((`y_parent_liuchengka` `ypl` left join `gmms_liuchengka` `lk` on((`ypl`.`zkliuchengka` = `lk`.`parent_liuchengka`))) left join `gmms_shelf_rack_2` `sr` on((`lk`.`liuchengka` = `sr`.`processcard_id`))) where (`lk`.`parent_liuchengka` is not null);

-- ----------------------------
-- View structure for y_liuchengka_copy1
-- ----------------------------
DROP VIEW IF EXISTS `y_liuchengka_copy1`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `y_liuchengka_copy1` AS select `lk`.`parent_liuchengka` AS `parent_liuchengka`,`lk`.`liuchengka` AS `liuchengka`,`lk`.`piece_count` AS `piece_count` from (`gmms_liuchengka` `lk` left join `gmms_shelf_rack_2` `sr` on((`lk`.`liuchengka` = `sr`.`processcard_id`))) where (`sr`.`processcard_id` is not null);

-- ----------------------------
-- View structure for y_parent_liuchengka
-- ----------------------------
DROP VIEW IF EXISTS `y_parent_liuchengka`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `y_parent_liuchengka` AS select NULL AS `xuhao`,`lk`.`parent_liuchengka` AS `zkliuchengka`,(select group_concat(`gmms_liuchengka`.`liuchengka` separator ',') from `gmms_liuchengka` where (`gmms_liuchengka`.`parent_liuchengka` = `lk`.`parent_liuchengka`)) AS `liuchengkas`,(select sum(`gmms_liuchengka`.`piece_count`) from `gmms_liuchengka` where (`gmms_liuchengka`.`parent_liuchengka` = `lk`.`parent_liuchengka`)) AS `zongshu`,(select count(`gmms_galss_task_shelf`.`id`) from `gmms_galss_task_shelf` where ((`gmms_galss_task_shelf`.`gc_number` = `lk`.`parent_liuchengka`) and (`gmms_galss_task_shelf`.`work_procedure` = '钢化后下片') and (`gmms_galss_task_shelf`.`work_state` = '已完成'))) AS `kuneishu` from (`gmms_liuchengka` `lk` left join `gmms_shelf_rack_2` `sr` on((`lk`.`liuchengka` = `sr`.`processcard_id`))) where (`sr`.`processcard_id` is not null) group by `lk`.`parent_liuchengka`;

-- ----------------------------
-- View structure for y_parent_liuchengka_copy1
-- ----------------------------
DROP VIEW IF EXISTS `y_parent_liuchengka_copy1`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `y_parent_liuchengka_copy1` AS select `lk`.`parent_liuchengka` AS `parent_liuchengka`,sum(`lk`.`piece_count`) AS `parent_count` from (`gmms_liuchengka` `lk` left join `gmms_shelf_rack_2` `sr` on((`lk`.`liuchengka` = `sr`.`processcard_id`))) where (`sr`.`processcard_id` is not null) group by `lk`.`parent_liuchengka`;

-- ----------------------------
-- View structure for y_shelfgalsss
-- ----------------------------
DROP VIEW IF EXISTS `y_shelfgalsss`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `y_shelfgalsss` AS select NULL AS `xuhao`,`yl`.`parent_liuchengka` AS `parent_liuchengka`,`yl`.`liuchengka` AS `liuchengka`,`yl`.`zongshu` AS `zongshu`,`yl`.`piece_count` AS `piece_count`,`yl`.`id` AS `id`,`yl`.`state` AS `state`,`yl`.`falg` AS `falg`,`yl`.`orientation` AS `orientation`,`gb`.`id` AS `taskid`,`gs`.`id` AS `shelftaskid`,`gs`.`stop_rank` AS `stop_rank`,`gs`.`stop_position` AS `stop_position`,`gb`.`width` AS `width`,`gb`.`height` AS `height`,`gb`.`thickness` AS `thickness`,`gb`.`color` AS `color`,`gs`.`work_state` AS `work_state`,`gs`.`work_procedure` AS `work_procedure` from ((`y_liuchengka` `yl` left join `gmms_galss_task_buffer` `gb` on((`yl`.`liuchengka` = `gb`.`processcard_id`))) left join `gmms_galss_task_shelf` `gs` on((`gb`.`id` = `gs`.`id`))) where (`gb`.`processcard_id` is not null);

-- ----------------------------
-- View structure for y_shelfprocesscard
-- ----------------------------
DROP VIEW IF EXISTS `y_shelfprocesscard`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `y_shelfprocesscard` AS select `y_shelfrack`.`processcard` AS `processcard` from `y_shelfrack` where (`y_shelfrack`.`processcard` is not null) group by `y_shelfrack`.`processcard`;

-- ----------------------------
-- View structure for y_shelfrack
-- ----------------------------
DROP VIEW IF EXISTS `y_shelfrack`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `y_shelfrack` AS select `sr`.`id` AS `id`,`sr`.`state` AS `state`,left(`sr`.`processcard_id`,(locate('/',`sr`.`processcard_id`) - 1)) AS `processcard`,substr(`sr`.`processcard_id`,(locate('/',`sr`.`processcard_id`) + 1)) AS `cardrank`,`gt`.`processcard_id` AS `processcard_id`,group_concat(`gt`.`edg_speed2` order by `gt`.`stop_rank` ASC separator ',') AS `grouprank`,group_concat(`gt`.`width` order by `gt`.`stop_rank` ASC separator ',') AS `groupwidth`,group_concat(`gt`.`height` order by `gt`.`stop_rank` ASC separator ',') AS `groupheight`,`sr`.`orientation` AS `orientation`,`sr`.`sign` AS `sign`,`sr`.`falg` AS `falg`,count(0) AS `storagecount` from (`gmms_shelf_rack_2` `sr` join `gmms_galss_task_shelf` `gt` on((`sr`.`processcard_id` = `gt`.`processcard_id`))) group by `gt`.`processcard_id`;

-- ----------------------------
-- View structure for y_shelfrackcardinfo
-- ----------------------------
DROP VIEW IF EXISTS `y_shelfrackcardinfo`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `y_shelfrackcardinfo` AS select `gl`.`liuchengka` AS `liuchengka`,`gl`.`main_liuchengka` AS `main_liuchengka`,`ysr`.`id` AS `id`,`ysr`.`state` AS `state`,`ysr`.`processcard` AS `processcard`,`ysr`.`cardrank` AS `cardrank`,`ysr`.`processcard_id` AS `processcard_id`,`ysr`.`grouprank` AS `grouprank`,`ysr`.`groupwidth` AS `groupwidth`,`ysr`.`groupheight` AS `groupheight`,`ysr`.`orientation` AS `orientation`,`ysr`.`sign` AS `sign`,`ysr`.`falg` AS `falg`,`ysr`.`storagecount` AS `storagecount`,`gl`.`gy_gang` AS `gy_gang`,`gl`.`gy_jiajiao` AS `gy_jiajiao`,`gl`.`gy_zhongkong` AS `gy_zhongkong`,`gl`.`piece_count` AS `piece_count`,`gliu`.`child_count` AS `child_count` from (((`gmms_liuchengka` `gl` join `y_shelfprocesscard` `ysp` on((`gl`.`main_liuchengka` = `ysp`.`processcard`))) left join `y_shelfrack` `ysr` on((`gl`.`liuchengka` = `ysr`.`processcard_id`))) left join `gmms_liuchengka` `gliu` on((`gl`.`main_liuchengka` = `gliu`.`main_liuchengka`))) where ((`gl`.`is_single` <> 0) and (`gliu`.`is_single` = 0));

-- ----------------------------
-- View structure for y_slicetasks
-- ----------------------------
DROP VIEW IF EXISTS `y_slicetasks`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `y_slicetasks` AS select `yhg`.`id` AS `id`,`yhg`.`orientation` AS `orientation`,`yhg`.`falg` AS `falg`,`yhg`.`state` AS `state`,`yhg`.`liuchengka` AS `liuchengka`,`yhg`.`main_liuchengka` AS `main_liuchengka`,`yhg`.`cardrank` AS `cardrank`,`yhg`.`cardcount` AS `cardcount`,`yhg`.`piececount` AS `piececount`,`yhg`.`storagecounts` AS `storagecounts`,`yh`.`sumpiececount` AS `sumpiececount`,`yh`.`sumstoragecount` AS `sumstoragecount`,`yh`.`isprice` AS `isprice`,`yh`.`ishowllorslice` AS `ishowllorslice` from (`y_howllorslicetasks` `yh` join `y_howllowgroupby` `yhg` on((`yh`.`main_liuchengka` = `yhg`.`main_liuchengka`))) where ((`yh`.`ishowllorslice` > 1) and (`yhg`.`cardcount` = 1)) order by `yhg`.`main_liuchengka`,`yhg`.`cardcount`,`yhg`.`cardrank` desc;

-- ----------------------------
-- View structure for y_slicetasks_copy1
-- ----------------------------
DROP VIEW IF EXISTS `y_slicetasks_copy1`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `y_slicetasks_copy1` AS select `yhg`.`id` AS `id`,`yhg`.`orientation` AS `orientation`,`yhg`.`falg` AS `falg`,`yhg`.`state` AS `state`,`yhg`.`liuchengka` AS `liuchengka`,`yhg`.`main_liuchengka` AS `main_liuchengka`,`yhg`.`cardrank` AS `cardrank`,`yhg`.`cardcount` AS `cardcount`,`yhg`.`piececount` AS `piececount`,`yhg`.`storagecounts` AS `storagecounts`,`yh`.`sumpiececount` AS `sumpiececount`,`yh`.`sumstoragecount` AS `sumstoragecount`,`yh`.`isprice` AS `isprice`,`yh`.`ishowllorslice` AS `ishowllorslice` from (`y_howllorslicetasks` `yh` join `y_howllowgroupby` `yhg` on((`yh`.`main_liuchengka` = `yhg`.`main_liuchengka`))) where (`yh`.`ishowllorslice` > 1) order by `yhg`.`main_liuchengka`,`yhg`.`cardcount`,`yhg`.`cardrank` desc;

-- ----------------------------
-- View structure for y_station_shelf
-- ----------------------------
DROP VIEW IF EXISTS `y_station_shelf`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `y_station_shelf` AS select `dr`.`id` AS `id`,`dr`.`groups_` AS `groups_`,`dr`.`drop_type` AS `drop_type`,`dr`.`drop_groups` AS `drop_groups`,`dr`.`stops` AS `stops`,`dr`.`stop_rank` AS `stop_rank`,`dr`.`orientation` AS `orientation`,`dr`.`rank` AS `rank`,`dr`.`sign` AS `sign`,`dr`.`flag` AS `flag`,`dr`.`all_groups` AS `all_groups`,`dr`.`position` AS `position`,`dr`.`rankposition` AS `rankposition`,`sr`.`processcard_id` AS `processcard_id`,`sr`.`orientation` AS `RankOrientation`,`lk`.`current_work_position` AS `current_work_position`,`lk`.`current_status` AS `current_status`,`lk`.`getdatatime` AS `getdatatime`,`lk`.`getgroups` AS `getgroups`,(select count(`gmms_galss_task_shelf`.`id`) from `gmms_galss_task_shelf` where ((`gmms_galss_task_shelf`.`processcard_id` = `sr`.`processcard_id`) and (`gmms_galss_task_shelf`.`work_state` <> '破损') and ((`gmms_galss_task_shelf`.`work_procedure` <> '中空上片') or (`gmms_galss_task_shelf`.`work_state` <> '已完成')))) AS `processcardcount` from ((`gmms_drop_frame_5` `dr` left join `gmms_shelf_rack_2` `sr` on((`dr`.`rank` = `sr`.`id`))) left join `gmms_liuchengka` `lk` on((`sr`.`processcard_id` = `lk`.`liuchengka`))) where (`dr`.`groups_` = 100);

-- ----------------------------
-- View structure for 冷加工产量时间
-- ----------------------------
DROP VIEW IF EXISTS `冷加工产量时间`;
CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `冷加工产量时间` AS select `rf`.`file_name` AS `工程号`,`rf`.`state` AS `状态`,`rf`.`operator` AS `线路`,min(`gs`.`finsh_time`) AS `第一片落架时间`,max(`gs`.`finsh_time`) AS `最后一片落架时间`,timediff(max(`gs`.`finsh_time`),min(`gs`.`finsh_time`)) AS `总用时`,(select count(0) from `gmms_galss_task_buffer` where (`gmms_galss_task_buffer`.`engineering_id` = `rf`.`file_name`)) AS `总数` from (`gmms_raw_file` `rf` left join `gmms_galss_task_stock` `gs` on((`rf`.`file_name` = `gs`.`engineering_id`))) where ((`gs`.`engineering_id` is not null) and (`rf`.`state` = '已完成') and (`rf`.`create_time` > (now() - interval 14 day))) group by `gs`.`engineering_id`;

-- ----------------------------
-- Procedure structure for 111
-- ----------------------------
DROP PROCEDURE IF EXISTS `111`;
delimiter ;;
CREATE PROCEDURE `111`()
BEGIN
DECLARE t_error INTEGER DEFAULT 0;
DECLARE c INTEGER DEFAULT 0;
DECLARE y int DEFAULT 0;
DECLARE processcardid2 varchar(255);
	DECLARE    CONTINUE HANDLER FOR SQLEXCEPTION SET t_error = 1;
	

--  DECLARE _rank int;
--  DECLARE  _id bigint;
--  DECLARE  done int ;
--  DECLARE  g  varchar(30);
--  declare cur CURSOR for   
--     select rank,id,engineering_id from gmms_pattern_task_buffer;
--   # 游标中的内容执行完后将done设置为1  
--   DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;  
--       open cur;
--       a: LOOP
-- 		      
--  					IF done=1 THEN    
--  						LEAVE a;  
-- 						END IF;
-- 				  FETCH cur into  _rank ,_id,g;
-- 				 					UPDATE gmms_galss_task_buffer set pattern_id=_id where gc_stockid=_rank and engineering_id=g;
-- 					
--  					
--  		 END LOOP a;	
-- 		 close cur;


-- 	select GROUP_CONCAT(gtf.edg_speed2)  from (select edg_speed2 from gmms_galss_task_shelf where processcard_id='NG22091607A10/3' and stop_rank<=20 order by stop_rank) as gtf;
-- 	select GROUP_CONCAT(gtf.edg_speed2)  from (select edg_speed2 from gmms_galss_task_shelf where processcard_id='NG22091607A10/2' and stop_rank<=20 order by stop_rank) as gtf;		
-- 						
-- 				if  position("P" in 'm23022416')>0 then
-- 					select 1;
-- 				else
-- 					select 2;
-- 				end if;		

if 1=1 then
select 1;
end if;
-- 						
-- #中空任务
-- select  count(*) into @count1 from 
-- (select count(*) as ccc,a.parent_liuchengka,b.child_count from gmms_liuchengka  a inner join gmms_liuchengka b on a.parent_liuchengka=b.liuchengka inner join  gmms_shelf_rack_2 c on 
-- a.liuchengka=c.processcard_id where c.sign=2 and b.gongyi='中空' group by a.parent_liuchengka ,b.child_count) aa
-- where aa.child_count=aa.ccc;
-- while c<@count1 do
-- 	select  aa.parent_liuchengka into @parent_liuchengka from 
-- (select count(*) as ccc,a.parent_liuchengka,b.child_count from gmms_liuchengka  a inner join gmms_liuchengka b on a.parent_liuchengka=b.liuchengka inner join  gmms_shelf_rack_2 c on 
-- a.liuchengka=c.processcard_id where c.sign=2 and b.gongyi='中空' group by a.parent_liuchengka ,b.child_count) aa
-- where aa.child_count=aa.ccc limit c,1;
-- 	if @parent_liuchengka is not null then
-- 		select count(*) into @count2 from gmms_raw_file_2 where file_name=@parent_liuchengka;
-- 		if @count2=0 then
-- 			select count(*) into @count8 from gmms_shelf_rack_2 where processcard_id like CONCAT('%',@parent_liuchengka,'%') and orientation='A';
-- 			select count(*) into @count9 from gmms_shelf_rack_2 where processcard_id like CONCAT('%',@parent_liuchengka,'%') and orientation='B';
-- 			if @count8!=3 or @count9!=3 then
-- 				insert into gmms_raw_file_2 (file_name,state,operator,create_time) values(@parent_liuchengka,'未领取',0,NOW());
-- 			else
-- 				select number into @number from gmms_shelf_rack_2 where processcard_id like CONCAT('%',@parent_liuchengka,'%') order by number limit 0,1;
-- 				select noodles into @noodles from gmms_drop_frame_2 where groups=20 and flag=1;
-- 				select count(*) into @count10 from gmms_shelf_task_2 where shelf_rack_id=@number and load_rack=@noodles and task_type='从仓位到旋转位' and groups=0 and line=3 and (task_state='正在工作' or task_state='等待中');
-- 				if @count10=0 and @number is not null then
-- 					insert into gmms_shelf_task_2 (task_type,task_state,shelf_rack_id,load_rack,groups,start_time,line) values('从仓位到旋转位','等待中',@number,@noodles,0,now(),3);
-- 				end if;
-- 			end if;
-- 			
-- 		end if;
-- 	end if;
-- 	
-- set c=c+1;
-- end while;


-- #钢化后的完成
-- insert into E_record_hollow  
-- select gs.processcard_id,gs.id,gs.edg_width,gs.edg_height,gs.thickness,gs.color,gs.stop_rank,gs.finsh_time,'工序','线路' from gmms_galss_task_shelf as gs left join E_Pierceds as ep on gs.id=gs.id
-- where ep.id is null;
-- 
-- #钢化后的破损
-- insert into E_Pierceds  
-- select gs.processcard_id,'玻璃类型',1,'刺破类型','刺破原因','是否报工',gs.id,NOW(),'工序','线路' from gmms_galss_task_shelf as gs left join E_Pierceds as ep on gs.id=gs.id
-- where ep.id is null;
-- 
-- #钢化破损
-- insert into E_Pierceds  
-- select gs.processcard_id,'玻璃类型',1,'刺破类型','刺破原因','是否报工',gs.id,NOW(),'工序','线路' from gmms_galss_task_2 as gs left join E_Pierceds as ep on gs.id=gs.id
-- where ep.id is null;






END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for aaaaqqq
-- ----------------------------
DROP PROCEDURE IF EXISTS `aaaaqqq`;
delimiter ;;
CREATE PROCEDURE `aaaaqqq`()
BEGIN
	#Routine body goes here...
		set @count=(select count(*) from gmms_shelf_task_2 
		where task_state='已下车' and shelf_rack_id=10);
		select @count;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for Accelerate
-- ----------------------------
DROP PROCEDURE IF EXISTS `Accelerate`;
delimiter ;;
CREATE PROCEDURE `Accelerate`(in sendMessageHex varchar(255),in machineids BIGINT,out messagedate varchar(255))
BEGIN
	#加速台存储过程
	#找到当前机器标志位
	#查询机器逻辑表中对应的逻辑进行对应的步骤
	DECLARE machinedisposeids int DEFAULT 0;
	DECLARE dates varchar(255);
	DECLARE taskplc varchar(255);
	DECLARE taskmes varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	set messagedate=messagedates;
	
	select machine_type_id,work_id into @mtid,@work_groups from gmms_machine where id=machineids;
	
	select speed2 into @speed2 from gmms_edg_speed where groups=@work_groups;
	
	
	select mf.flag into @请求标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='请求字';
	
	select mf.flag into @发送标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='发送字';
	
	set taskplc = substring(sendMessageHex,(cast(@请求标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskmes = substring(sendMessageHex,(cast(@发送标志位 as UNSIGNED INTEGER)*4)+19,4);
	
	select taskplc;
	select taskmes;
	
	#请求逻辑查询
	select count(*),md.id into @请求数量,@请求逻辑编号 from gmms_machine_dispose md join gmms_machine_type mt on md.machine_type_id=mt.id where md.machine_type_id=@mtid  and md.plc_font_type='请求字' and md.mes_font_type='发送字' and md.plc_font_value=taskplc and md.mes_font_value=taskmes; 
	#判断逻辑是否存在
	if  @请求数量=1 then
		
		#判断请求逻辑是否为空
		if @请求逻辑编号!=0 then
			set machinedisposeids=@请求逻辑编号;
			
		end if;
		
		#查询当前执行逻辑进行判断
		select logic,return_value,mes_font_value into @ExecutionLogic,@returnvalue,@mes_font_value from gmms_machine_dispose where id=machinedisposeids;
		if @ExecutionLogic='任务请求' then
				call AccelerateSend(sendMessageHex,machineids,@returnvalue,@messagedate);
				set messagedate=@messagedate;
 		elseif @ExecutionLogic='任务清空' then
				set dates=CONCAT(@returnvalue,"00000000000000000000");
				select dates;
				call message(dates,'000a',@date);
				set messagedate=@date;
		
		end if;
		else
				call intTo2ByteHex10_16_4(@speed2,@speed2s);
				call message(@speed2s,'0011',@messagedate);
				set messagedate=@messagedate;
	end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for AccelerateSend
-- ----------------------------
DROP PROCEDURE IF EXISTS `AccelerateSend`;
delimiter ;;
CREATE PROCEDURE `AccelerateSend`(in sendMessageHex varchar(255),in machineids BIGINT,in returnvalue varchar(255),out messagedate varchar(255))
BEGIN
	#加速台请求存储过程
	#找到当前对应的工序的玻璃信息
	#序号，高度，宽度，厚度发送给plc
	DECLARE number int DEFAULT 0;
	DECLARE out_ranks int;
	DECLARE widths int;
	DECLARE heigths int;
	DECLARE thicknesss int;
	DECLARE drop_frames int;
	DECLARE dates varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	set messagedate=messagedates;
	
	select machine_type_id,work_id into @mtid,@work_groups from gmms_machine where id=machineids;
	
	select file_name into @engineering_id from gmms_raw_file where operator=@work_groups and state='已领取';
	
	select speed2 into @speed2 from gmms_edg_speed where groups=@work_groups;
	
	select @work_groups;
	select count(*) into @count from gmms_galss_task where work_procedure='复合' and work_state='待复合' and engineering_id=@engineering_id;

	if @count>0 then
-- 		select count(*) into @number from gmms_galss_task where work_procedure='复合' and work_state='已复合' and drop_frame=@drop_frame;
		select out_rank,width,height,thickness,drop_frame,processcard_id into @out_rank,@width,@heigth,@thickness,@drop_frame,@processcard_id from gmms_galss_task where work_procedure='复合' and work_state='待复合' and engineering_id=@engineering_id  order by out_rank limit 1;

		select finsh_count into @counts from gmms_drop_frame where drop_frame=@drop_frame and processcard_id=@processcard_id and groups=@work_groups;
					select @count;
		update gmms_galss_task set work_state='正在工作' where out_rank=@out_rank and work_procedure='复合' and engineering_id=@engineering_id;
 		set number=@counts+1;
		set out_ranks=cast(@out_rank as UNSIGNED INTEGER);
		set widths=cast(@width as UNSIGNED INTEGER);
		set heigths=cast(@heigth as UNSIGNED INTEGER);
		set thicknesss=cast(@thickness as UNSIGNED INTEGER);
 		set drop_frames=cast(number as UNSIGNED INTEGER);
-- 		select ranks;
-- 		select heigths;
-- 		select widths;
-- 		select thicknesss;
-- 		select drop_frames;
		call intTo2ByteHex10_16_4(@speed2,@speed2s);
		call intTo2ByteHex10_16_4(out_ranks,@out_ranks);
		call intTo2ByteHex10_16_4(heigths,@heigth);
		call intTo2ByteHex10_16_4(widths,@width);
		call intTo2ByteHex10_16_4(thicknesss,@thickness);
 		call intTo2ByteHex10_16_4(drop_frames,@drop_frame);
		select @out_ranks;
		select @heigth;
		select @width;
		select @thickness;
		select @drop_frame;
		if machineids=32 then
		set dates=CONCAT(returnvalue,@out_ranks,@width,@heigth,@thickness,'0001','0000',@speed2s);
		else
		set dates=CONCAT(returnvalue,@out_ranks,@heigth,@width,@thickness,'00000000',@speed2s);
		end if;
		
		select dates;
		call message(dates,'000a',@date);
		set messagedate=@date;
	end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for ceshi
-- ----------------------------
DROP PROCEDURE IF EXISTS `ceshi`;
delimiter ;;
CREATE PROCEDURE `ceshi`()
BEGIN
	#Routine body goes here...
	#1.查找是否掉线
	#2.查找请求交互
	#3.查找完成交互
	#4.查找正在工作的ID 信息
	DECLARE values1 varchar(255) DEFAULT '';
	DECLARE groups int DEFAULT 1;#线路
	DECLARE flags int DEFAULT 1;#冷加工(1)，热加工(2),中空(3)
	DECLARE work_pro varchar(255) DEFAULT '复合';#工作阶段
	DECLARE work_st varchar(255)  DEFAULT '正在工作';#工作状态
	DECLARE messages varchar(255) DEFAULT '';
	set groups=1;
	set flags=1; 
	set work_pro='待测量';
	set work_st='正在工作';
	select id,galss_id,work_procedure,work_state,width,height,thickness,stop_rank,orders,marking_flag,distance from gmms_galss_task_2 where processcard_id='NG22102603A04/2' and work_state!='未优化' order by orders;
	select message into messages from gmms_machine_message where machine_id=22;#6.19.28.29.30
	select substring(messages,1+18,4) as 请求,
	       substring(messages,41+18,4) as 发送,
				 substring(messages,21+18,4) as 完成,
				 substring(messages,81+18,4) as 确认; 
  if flags=1 then
		select * from gmms_galss_task where engineering_id=(select file_name from gmms_raw_file where operator=groups and state='已领取') and work_state=work_st; 
		select * from gmms_galss_task where engineering_id=(select file_name from gmms_raw_file where operator=groups and state='已领取') and work_procedure=				work_pro and 	work_state=work_st;
		select * from gmms_galss_task where engineering_id=(select file_name from gmms_raw_file where operator=groups and state='已领取') and work_procedure=				work_pro and 	work_state='等待中';
	elseif flags=2 then
		select * from gmms_galss_task_2 where work_state=work_st;
		select * from gmms_galss_task_2 where work_procedure=work_pro and work_state=work_st;
	else
		select * from gmms_raw_file_2 where work_state=work_st;
	end if;
  
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for ceshi_copy1
-- ----------------------------
DROP PROCEDURE IF EXISTS `ceshi_copy1`;
delimiter ;;
CREATE PROCEDURE `ceshi_copy1`()
BEGIN
	#ATAB 对此表的  单子删除重新添加
	DECLARE deletenum int DEFAULT 0;#删除数
	
	DECLARE count int DEFAULT 0;#删除数
	
	DECLARE tkcount int DEFAULT 0;#删除数
	DECLARE lckh varchar(255) DEFAULT '';
	
	select count(*) into deletenum from ATAB;
  if deletenum>0 then
		
		WHILE deletenum>count DO
			select gg into lckh from ATAB limit count,1;
			#是否已经开始任务
			select count(*) into tkcount from gmms_liuchengka where current_work_position!='创建' and main_liuchengka=lckh and is_single=1;
			if tkcount=0 then
				delete from gmms_liuchengka where main_liuchengka=lckh;
				call liuchengka_load_rel(lckh);
			end if;
			
	  
			set count=count+1;
			set tkcount=0;
			set lckh='';
		END WHILE;

		
	end if;
	
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for Compound
-- ----------------------------
DROP PROCEDURE IF EXISTS `Compound`;
delimiter ;;
CREATE PROCEDURE `Compound`(in sendMessageHex varchar(255),in machineids BIGINT,out messagedate varchar(255))
BEGIN
	#复合台存储过程
	#找到当前机器标志位
	#查询机器逻辑表中对应的逻辑进行对应的步骤
	DECLARE machinedisposeids int DEFAULT 0;
	DECLARE dates varchar(255);
	DECLARE taskplc varchar(255);
	DECLARE taskmes varchar(255);
	DECLARE taskplc1 varchar(255);
	DECLARE taskmes1 varchar(255);
	DECLARE send_rankid BIGINT;
	DECLARE rankids varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	set messagedate=messagedates;
	
	select machine_type_id,work_id into @mtid,@work_groups from gmms_machine where id=machineids;
	
	
	select mf.flag into @请求标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='请求字';
	
	select mf.flag into @发送标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='发送字';
	
	set send_rankid = cast(@请求标志位 as UNSIGNED INTEGER)+1;
	set taskplc = substring(sendMessageHex,(cast(@请求标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskmes = substring(sendMessageHex,(cast(@发送标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskplc1 = substring(sendMessageHex,(cast(4 as UNSIGNED INTEGER)*4)+19,4);
	set taskmes1 = substring(sendMessageHex,(cast(20 as UNSIGNED INTEGER)*4)+19,4);
-- 	 if machineids=67 then
-- 					insert into aaaa(ddd) VALUES(99);
-- 			 end if;
	
	set rankids = conv(substring(sendMessageHex,(cast(send_rankid as UNSIGNED INTEGER)*4)+19,4),16,10);
	SELECT rankids;
	select taskplc;
	select taskmes;
	select taskplc1;
	select taskmes1;
	
	#请求逻辑查询
	select count(*),md.id into @请求数量,@请求逻辑编号 from gmms_machine_dispose md join gmms_machine_type mt on md.machine_type_id=mt.id where md.machine_type_id=@mtid  and md.plc_font_type='请求字' and md.mes_font_type='发送字' and md.plc_font_value=taskplc and md.mes_font_value=taskmes;
	select @mtid; 
	select taskplc;
	select taskmes;
	#判断逻辑是否存在
	if  @请求数量=1 then
		
	select 1;
		#判断请求逻辑是否为空
		if @请求逻辑编号!=0 then
			set machinedisposeids=@请求逻辑编号;
			
		end if;
		
		#查询当前执行逻辑进行判断
		select logic,return_value,mes_font_value into @ExecutionLogic,@returnvalue,@mes_font_value from gmms_machine_dispose where id=machinedisposeids;
		select @ExecutionLogic;
		if @ExecutionLogic='任务请求' and rankids>0 then
				call CompoundSend(sendMessageHex,machineids,1,rankids,@returnvalue,@messagedate);
				set messagedate=@messagedate;
		elseif @ExecutionLogic='匹配失败' and rankids>0 then
				call CompoundSend(sendMessageHex,machineids,2,rankids,@returnvalue,@messagedate);
				set messagedate=@messagedate;
 		elseif @ExecutionLogic='任务清空' then
				set dates=CONCAT(@returnvalue,"0000000000000000");
				select dates;
				call message(dates,'000a',@date);
				set messagedate=@date;
		else
				set messagedate='';
		end if;
		
	elseif taskplc1='0001' and taskmes1='0000' then
			set @Fhcount=null;
			select count(*) into @Fhcount from gmms_galss_task_2 where  work_procedure='钢化后复合'  and (work_state='正在工作' or work_state='复合失败') and groups=@work_groups;
			if (machineids=43 or machineids=67) and @Fhcount=0 then
			select machineids;
			select rankids;
			select @work_groups;
				select count(*),edg_width,edg_height,thickness,angle,marking_flag 
				into @counts,@width,@height,@thickness,@angle,@marking_flag from gmms_galss_task_2 
				where galss_id=rankids and work_state='待复合' and work_procedure='钢化后复合' and groups=@work_groups;
				select @counts;
				if @counts=1 then
					#此处修改					
					if @angle=1 then 
							select gang_hou_zhuan into @z_angle from gmms_dongzuo_table where gmms_dongzuo_table.number=@marking_flag;
					else
							select gang_hou_bu_zhuan into @z_angle from gmms_dongzuo_table where gmms_dongzuo_table.number=@marking_flag;
					end if;
	
					call intTo2ByteHex10_16_4(@width,@widths);
					call intTo2ByteHex10_16_4(@height,@heights);
					call intTo2ByteHex10_16_4(@thickness,@thicknesss);
					call intTo2ByteHex10_16_4(@z_angle,@angles);
					
					if @heights is not null and @heights!='' and 
						 @widths is not null and @widths!='' and
						 @thicknesss is not null and @thicknesss!='' and
						 @angles is not null and @angles!='' 	
					then
						 if @angle=1 or @angle=3 then
								set dates=CONCAT(@heights,@widths,@thicknesss,@angles,'000000000001');
						 else
								set dates=CONCAT(@widths,@heights,@thicknesss,@angles,'000000000001');
						 end if;
								call message(dates,'000e',@date);
								set messagedate=@date;	
								update gmms_galss_task_2 set work_state='正在工作' 
								where  galss_id=rankids and work_state='待复合' and work_procedure='钢化后复合' and groups=@work_groups;
							  insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,@work_groups,NOW(),'发送任务',rankids,messagedate);	
					end if;	
					
						-- 原程序开始
-- 					update gmms_galss_task_2 set work_state='正在工作' where  galss_id=rankids and work_state='待复合' and work_procedure='钢化后复合' and groups=@work_groups;
-- 					call intTo2ByteHex10_16_4(@width,@widths);
-- 					call intTo2ByteHex10_16_4(@height,@heights);
-- 					call intTo2ByteHex10_16_4(@thickness,@thicknesss);
-- 					
-- 					#此处修改
-- 					
-- 					if @angle=1 then 
-- 						select gang_hou_zhuan into @z_angle from gmms_dongzuo_table where gmms_dongzuo_table.number=@marking_flag;
-- 				  else
-- 					    	select gang_hou_bu_zhuan into @z_angle from gmms_dongzuo_table where gmms_dongzuo_table.number=@marking_flag;
-- 					end if;
-- 					
-- 					#########
-- 					
-- 					call intTo2ByteHex10_16_4(@z_angle,@angles);
-- 					if @angle=1 or @angle=3 then
-- 						set dates=CONCAT(@heights,@widths,@thicknesss,@angles,'000000000001');
-- 					else
-- 						set dates=CONCAT(@widths,@heights,@thicknesss,@angles,'000000000001');
-- 					end if;
-- 					
-- 					call message(dates,'000e',@date);
-- 					set messagedate=@date;
--          原程序结束
				end if;
			end if;
		elseif taskplc1='0000' and taskmes1='0001' then
				set dates=CONCAT("0000000000000000000000000000");
				call message(dates,'000e',@date);
				set messagedate=@date;
	end if;
	if (machineids=64 or machineids=39) and (messagedate is null or messagedate='') then
		set @fuheid=null;
		select gt.id into @fuheid from gmms_machine as gm LEFT JOIN gmms_galss_task_2 as gt on gm.remarks=gt.id
		where gm.id=machineids and gt.id is not null and gt.work_state='复合失败';
		if @fuheid is not null then
			set dates=CONCAT('0000',"0000000000000000");
			call message(dates,'000a',@date);
			set messagedate=@date;
		else
			update gmms_machine set remarks=null	where id=machineids;
		end if;
	end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for Compoundmanual
-- ----------------------------
DROP PROCEDURE IF EXISTS `Compoundmanual`;
delimiter ;;
CREATE PROCEDURE `Compoundmanual`(in machineids BIGINT,in returnvalue varchar(255),out messagedate varchar(255))
BEGIN
	#手动复合存储过程（备用）
	DECLARE number int DEFAULT 0;
	DECLARE dates varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	set messagedate=messagedates;
	
	select machine_type_id,work_id into @mtid,@work_groups from gmms_machine where id=machineids;
	
	select file_name into @engineering_id from gmms_raw_file where operator=@work_groups and state='已领取';
	
	select flag into @flag from gmms_preprocessing_glass where groups=@work_groups;
	select @flag;
	
	if @flag>0 then
	
		select width,height,galss_id,thickness,drop_frame into @width,@heigth,@galss_id,@thickness,@drop_frame from gmms_galss_task where id=@flag and engineering_id=@engineering_id;
		select finsh_count into @counts from gmms_drop_frame where drop_frame=@drop_frame and groups=@work_groups;
		
		set number=@counts+1;	
		update gmms_galss_task set work_state='等待中',work_procedure='下片' where id=@flag and engineering_id=@engineering_id;
		
		update gmms_preprocessing_glass set statu='复合成功',glass_id=@galss_id,length=@width,width=@heigth,flag=0 where groups=@work_groups;
		
		select rank into @rank from gmms_galss_task where id=@flag and engineering_id=@engineering_id;
		
		select @rank;
		
		call intTo2ByteHex10_16_4(@rank,@ranks);
		call intTo2ByteHex10_16_4(@width,@widths);
		call intTo2ByteHex10_16_4(@heigth,@heigths);
		call intTo2ByteHex10_16_4(@thickness,@thicknesss);
		call intTo2ByteHex10_16_4(number,@number);
		select @ranks;
		set dates=CONCAT(returnvalue,@ranks,@widths,@heigths,@thicknesss,@number);
		select dates;
		call message(dates,'0014',@date);
		set messagedate=@date;
-- 	elseif 	@flag=2 then
-- 		update gmms_preprocessing_glass set statu='匹配失败',flag=0 where id=1;
	end if;
				
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for Compounds
-- ----------------------------
DROP PROCEDURE IF EXISTS `Compounds`;
delimiter ;;
CREATE PROCEDURE `Compounds`()
BEGIN
	#Routine body goes here...
	#复合台程序
	
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for CompoundSend
-- ----------------------------
DROP PROCEDURE IF EXISTS `CompoundSend`;
delimiter ;;
CREATE PROCEDURE `CompoundSend`(in sendMessageHex varchar(255),in machineids BIGINT,in flags int,in rankid BIGINT,in returnvalue varchar(255),out messagedate varchar(255))
BEGIN
	#复合台请求存储过程
	#flag=1为复合成功，flag=2为复合失败
	#复合失败后界面上点击匹配成功，找到复合台机器表的备注字段绑定的玻璃编号
	#大于零则为绑定了玻璃编号，-1则为失败手动拿走，varchar的-1转换为int并不是-1
	DECLARE number int DEFAULT 0;
	DECLARE remarkss int;
	DECLARE ranks int;
	DECLARE widths int;
	DECLARE heigths int;
	DECLARE thicknesss int;
	DECLARE drop_frames int;
	DECLARE dates varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	
	DECLARE maxw int DEFAULT 0;
	DECLARE maxh int DEFAULT 0;
	DECLARE minw int DEFAULT 0;
	DECLARE minh int DEFAULT 0;
	DECLARE kaobian int DEFAULT 0;
	set messagedate=messagedates;
	
	select machine_type_id,work_id into @mtid,@work_groups from gmms_machine where id=machineids;
 
	

	if  (machineids=11 or machineids=21 or machineids=33) then
		select file_name into @engineering_id from gmms_raw_file where operator=@work_groups and state='已领取';
	
		select count(*) into @count from gmms_galss_task where work_procedure='复合' and (work_state='正在工作' or work_state='复合失败' ) and engineering_id=@engineering_id;
		set @count=1;
		if @count>0 then
		
		if flags=1 then
			
			if machineids=33 then
				#  一号线的特殊处理 33 
				select out_rank,width,height,thickness,drop_frame,processcard_id,stop_position into @rank,@width,@heigth,@thickness,@drop_frame,@processcard_id,kaobian from gmms_galss_task 
				where work_procedure='复合' and work_state='正在工作' and engineering_id=@engineering_id order by out_rank limit 1;
				#得到最小的 和最大的玻璃数据  判断往哪里靠边
				#select xiapianbiaoji into kaobian from gmms_liuchengka where liuchengka=@processcard_id;
				if kaobian<13 and kaobian>0 then
					update gmms_galss_task set work_state='等待中',work_procedure='下片' where out_rank=rankid and engineering_id=@engineering_id;
					set returnvalue=CONCAT(returnvalue,"0000");
					call message(returnvalue,'000a',@date);
					set messagedate=@date;
					insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,"",NOW(),'发送复合确认任务0',IFNULL(kaobian,0),messagedate);
				elseif kaobian>12 and kaobian>0 then
					update gmms_galss_task set work_state='等待中',work_procedure='下片' where out_rank=rankid and engineering_id=@engineering_id;
					set returnvalue=CONCAT(returnvalue,"0001");
					call message(returnvalue,'000a',@date);
					set messagedate=@date;
					insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,"",NOW(),'发送复合确认任务1',IFNULL(kaobian,0),messagedate);
				end if;
			else
				select out_rank,width,height,thickness,drop_frame,processcard_id into @rank,@width,@heigth,@thickness,@drop_frame,@processcard_id from gmms_galss_task where work_procedure='复合' and work_state='正在工作' and engineering_id=@engineering_id order by out_rank limit 1;
				select @rank;
				
				update gmms_galss_task set work_state='等待中',work_procedure='下片' where out_rank=rankid and engineering_id=@engineering_id;
				call message(returnvalue,'000a',@date);
				set messagedate=@date;
			end if;
		
	#复合失败	
		elseif flags=2 then
			set @flags=null;
			update gmms_galss_task set work_state='复合失败' where out_rank=rankid and work_procedure='复合' and engineering_id=@engineering_id;
			select remarks into @flags from gmms_machine where id=machineids;
			set remarkss=cast(@flags as UNSIGNED INTEGER);
			select remarkss;
			#手动匹配，判断是否绑定玻璃信息
			if remarkss>0 and remarkss<100000000 then
				if machineids=333 then
					select width,height,galss_id,thickness,drop_frame into @width,@heigth,@galss_id,@thickness,@drop_frame from gmms_galss_task where id=remarkss and engineering_id=@engineering_id;
					select finsh_count into @counts from gmms_drop_frame where drop_frame=@drop_frame and processcard_id=@processcard_id and groups=@work_groups;
		
					set number=@counts+1;	
					update gmms_galss_task set work_state='已完成',work_procedure='下片',finsh_time=now(),work_procedure_number=500,stop_rank=number where id=remarkss and engineering_id=@engineering_id;
					update gmms_machine set remarks=null where id=machineids;
				else
					select stop_position into kaobian from gmms_galss_task 
					where work_procedure='复合' and id=remarkss order by out_rank limit 1;
					update gmms_galss_task set work_state='等待中',work_procedure='下片' where id=remarkss and engineering_id=@engineering_id;
					update gmms_machine set remarks=null where id=machineids;
				end if;
				
		
		
-- 				select rank into @rank from gmms_galss_task where id=remarkss;
-- 		
-- 				call intTo2ByteHex10_16_4(@rank,@ranks);
-- 				call intTo2ByteHex10_16_4(@width,@widths);
-- 				call intTo2ByteHex10_16_4(@heigth,@heigths);
-- 				call intTo2ByteHex10_16_4(@thickness,@thicknesss);
-- 				call intTo2ByteHex10_16_4(number,@number);
-- 				select @ranks;
-- 				set dates=CONCAT(returnvalue,@ranks,@widths,@heigths,@thicknesss,@number);
-- 				select dates;
				
					if kaobian<13 and kaobian>0 then
						set returnvalue=CONCAT(returnvalue,"0000");
						call message(returnvalue,'000a',@date);
						set messagedate=@date;
						insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,"",NOW(),'发送复合手动确认任务0',IFNULL(kaobian,0),messagedate);
					elseif kaobian>12 and kaobian>0 then
						set returnvalue=CONCAT(returnvalue,"0001");
						call message(returnvalue,'000a',@date);
						set messagedate=@date;
						insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,"",NOW(),'发送复合手动确认任务1',IFNULL(kaobian,0),messagedate);
					end if;
			#人工拿走
			elseif remarkss>100000000 then
				update gmms_galss_task set work_state='破损' where work_state='复合失败' and engineering_id=@engineering_id;
				update gmms_machine set remarks='' where id=machineids;
				call message('0002','000a',@date);
				set messagedate=@date;
			else
				set messagedate='';
			end if;
		end if;	
		end if;
	
	#钢化前复合台
	#select count(*) into @a from gmms_galss_task_2 where work_procedure='钢化前复合' and (work_state='正在工作' or work_state='复合失败' ) and groups=@work_groups;
	elseif machineids=39 or machineids=43 or machineids=64 or machineids=67 then
		if machineids=39 or machineids=64 then
			if flags=1 then
				update gmms_galss_task_2 set work_state='等待中',work_procedure='钢化前摆片' where  work_state='正在工作' and work_procedure='钢化前复合' and groups=@work_groups and galss_id=rankid;
					
					call intTo2ByteHex10_16_4(rankid,@ranks);
					set dates=CONCAT(@returnvalue,@ranks);
					call message(dates,'000a',@date);
					set messagedate=@date;
				
					insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,@work_groups,NOW(),'复合成功',rankid,messagedate);
			#复合失败	
			elseif flags=2 then
				if (select id from aaaa where MachineName=machineids order by data1 desc limit 1)=rankid then
					insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,@work_groups,NOW(),'复合失败',rankid,messagedate);
				end if;			
				set @flags=null;
				select rankid;
				update gmms_galss_task_2 set work_state='复合失败' where  work_procedure='钢化前复合' and groups=@work_groups and galss_id=rankid;
				select remarks into @flags from gmms_machine where id=machineids;
				set remarkss=cast(@flags as UNSIGNED INTEGER);
				select remarkss;
				#手动匹配，判断是否绑定玻璃信息
				
				if remarkss>0 and remarkss<100000000 then
					select width,height,galss_id,thickness,drop_frame into @width,@heigth,@galss_id,@thickness,@drop_frame from gmms_galss_task_2 where id=remarkss ;
					#11.17修改 加 and work_state='复合失败' and work_procedure='钢化前复合'
					update gmms_galss_task_2 set work_state='等待中',work_procedure='钢化前摆片' 
					where id=remarkss and work_state='复合失败' and work_procedure='钢化前复合' and groups=@work_groups;
					update gmms_machine set remarks=null	where id=machineids;
					
					call intTo2ByteHex10_16_4(rankid,@ranks);
					set dates=CONCAT(@returnvalue,@ranks);
					call message(dates,'000a',@date);
					set messagedate=@date;
				#人工拿走
				elseif remarkss>100000000 then
					#11.17修改 加 and work_procedure='钢化前复合'
					update gmms_galss_task_2 set work_state='破损' where work_state='复合失败' and galss_id=rankid and groups=@work_groups and work_procedure='钢化前复合';
					update gmms_machine set remarks='' where id=machineids;
					call intTo2ByteHex10_16_4(rankid,@ranks);
					set dates=CONCAT('0002',@ranks);
					call message(dates,'000a',@date);
					set messagedate=@date;
				else
					set messagedate='';
				end if;
			end if;	
		
		
		#钢化后复合台
		elseif machineids=43 or machineids=67 then
			if flags=1 then					
-- 	原程序update gmms_galss_task_2 set work_state='等待中',work_procedure='钢化后下片' where  work_state='正在工作' and work_procedure='钢化后复合' and groups=@work_groups ORDER BY fine_throwing limit 1;
					if rankid is not null then
						update gmms_galss_task_2 set work_state='等待中',work_procedure='钢化后下片' 
						where  work_state='正在工作' and work_procedure='钢化后复合' and groups=@work_groups and galss_id=rankid ORDER BY fine_throwing limit 1;
					end if;
-- 					if rankid is not null then
-- 						update gmms_galss_task_2 set work_state='等待中',work_procedure='钢化后下片' where  work_state='正在工作' and work_procedure='钢化后复合' and groups=@work_groups  ORDER BY fine_throwing limit 1;
-- 					else
-- 						update gmms_galss_task_2 set work_state='等待中',work_procedure='钢化后下片' where  work_state='正在工作' and work_procedure='钢化后复合' and groups=@work_groups and galss_id=rankid ORDER BY fine_throwing limit 1;
-- 					end if;
					
					call intTo2ByteHex10_16_4(rankid,@ranks);
					set dates=CONCAT(@returnvalue,@ranks);
					call message(dates,'000a',@date);
					set messagedate=@date;
				  insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,@work_groups,NOW(),'复合成功',rankid,messagedate);	

			#复合失败	
			elseif flags=2 then
				set @flags=null;
				select rankid;
				select count(*) into @shibai2 from gmms_galss_task_2 
				where work_state='复合失败' and  work_procedure='钢化后复合' and groups=@work_groups and galss_id=rankid;
				select remarks into @flags from gmms_machine where id=machineids;
				if @shibai2<1 then
				#添加线路区分修改  
					update gmms_galss_task_2 set work_state='复合失败' where work_state='正在工作' and  work_procedure='钢化后复合' and groups=@work_groups and galss_id=rankid;
				  
				  insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,@work_groups,NOW(),'复合失败',rankid,messagedate);
				end if;
				
				set remarkss=cast(@flags as UNSIGNED INTEGER);
				select remarkss;
				#手动匹配，判断是否绑定玻璃信息
				if remarkss>0 and remarkss<100000000 then
					select width,height,galss_id,thickness,drop_frame into @width,@heigth,@galss_id,@thickness,@drop_frame from gmms_galss_task_2 where id=remarkss ;
					update gmms_machine set remarks=null	where id=machineids;
					#11.17添加  and work_state='正在工作' and  work_procedure='钢化后复合'
					update gmms_galss_task_2 set work_state='等待中',work_procedure='钢化后下片' where id=remarkss and work_state='复合失败' and  work_procedure='钢化后复合';
								
					call intTo2ByteHex10_16_4(rankid,@ranks);
					set dates=CONCAT(@returnvalue,@ranks);
					call message(dates,'000a',@date);
					set messagedate=@date;
					insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,@work_groups,NOW(),'手动匹配',rankid,messagedate);
				#人工拿走
				elseif remarkss>100000000 then
				#11.17添加  and work_state='正在工作' and  work_procedure='钢化后复合'
					update gmms_galss_task_2 set work_state='破损' where work_state='复合失败' and galss_id=rankid and groups=@work_groups  and  work_procedure='钢化后复合';
					update gmms_machine set remarks='' where id=machineids;
					call intTo2ByteHex10_16_4(rankid,@ranks);
					set dates=CONCAT('0002',@ranks);
					call message(dates,'000a',@date);
					set messagedate=@date;
					insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,@work_groups,NOW(),'拿走破损',rankid,messagedate);
				else
					set messagedate='';
				end if;
			end if;	
			end if;
	end if;
	
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for create_empty_raw_file
-- ----------------------------
DROP PROCEDURE IF EXISTS `create_empty_raw_file`;
delimiter ;;
CREATE PROCEDURE `create_empty_raw_file`(in type varchar(30),out id bigint,out gongcheng varchar(30))
BEGIN
	#Routine body goes here...
	DECLARE  gc varchar(30);
  DECLARE last varchar(30);
	DECLARE tmp int;
	  set gc=concat("MB",DATE_FORMAT(now(),"%y%m%d"),'%');
	 	set last=null;
		SELECT file_name into last from  gmms_raw_file where file_name like gc order by file_name DESC LIMIT 1; 
			
		 if last is not null then 
			  set @c=	substring(last,9);
				set tmp=cast(@c as UNSIGNED);
			  set tmp=tmp+1;
				set @ch=cast(tmp as char);
				set @chl=length(@ch);
				select @chl,@ch,tmp,@c;
				if @chl=1 then
				  set gc=concat("MB",DATE_FORMAT(now(),"%y%m%d"),'00',@ch);
				end if;
				if @chl=2 then  
				  set gc=concat("MB",DATE_FORMAT(now(),"%y%m%d"),'0',@ch);
				end if;
				if @chl=3 then
				  set gc=concat("MB",DATE_FORMAT(now(),"%y%m%d"),@ch);
				end if;
				
		 else 
		   set gc=concat("MB",DATE_FORMAT(now(),"%y%m%d"),'001');

		 end if;
		 insert into gmms_raw_file (file_name,state,operator,create_time) 
			values (gc,'已下发',0,now());
		 set gongcheng=gc;
		 SET id=LAST_INSERT_ID();

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for create_gongchengorder
-- ----------------------------
DROP PROCEDURE IF EXISTS `create_gongchengorder`;
delimiter ;;
CREATE PROCEDURE `create_gongchengorder`(in gongcheng varchar(50))
BEGIN
	#Routine body goes here...
		 INSERT into gmms_raw_file(thickness,color,width,height,count)   (SELECT thickness,color,width,height,count from gongcheng_raw where gc_name=gongcheng and state=0);
		 UPDATE gongcheng_raw SET state=1 where gc_name=gongcheng;
		
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for create_log_records
-- ----------------------------
DROP PROCEDURE IF EXISTS `create_log_records`;
delimiter ;;
CREATE PROCEDURE `create_log_records`(in gongcheng varchar(50),in line int)
BEGIN
--      call	create_log_records_pattern(gongcheng,line);
-- 		 call	create_log_records_segment(gongcheng,line,"切磨线");
-- 		 call	create_log_records_segment(gongcheng,null,"钢化线");
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for create_log_records_pattern
-- ----------------------------
DROP PROCEDURE IF EXISTS `create_log_records_pattern`;
delimiter ;;
CREATE PROCEDURE `create_log_records_pattern`(in gongcheng varchar(50),in _line int)
BEGIN
	#Routine body goes here...
		declare done int default 0;
	declare w double;
	declare h double;
	DECLARE gid BIGINT;
	declare _work_segment varchar(50);
	DECLARE cr_work_name varchar(50);
  declare cur cursor for
		  	SELECT id,raw_width,raw_height from gmms_pattern_task_buffer where engineering_id=gongcheng;
	declare cur1 cursor for
		  	SELECT work_name from gmms_glass_log_config where segment=_work_segment group by work_name;

				DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
	set _work_segment="原片仓储";
	set  @zid=null;
	SELECT id into @zid  from gmms_glass_log_main  where gongchenghao=gongcheng;
   if @zid is null then 
	 	INSERT INTO gmms_glass_log_main (
				segment
				,gongchenghao
				,create_time
				,line
				,state
				) values (_work_segment,gongcheng,now(),_line,0);
			SET @zid=LAST_INSERT_ID();
	 end if;
	 
	 SET @tmp=0;
	 select count(*) into @tmp from gmms_glass_log where main_id=@zid and segment_name=_work_segment;
	 set done=0;
	if @tmp=0 then 
			
				open cur;
			  loop1:loop 
					FETCH cur into gid,w,h;
					if done=1 then 
									set @cccc=@cccc+1000;
					   LEAVE loop1;
					 end if;
				 set @tmp=w*h/1000/1000;
				 
				 INSERT INTO gmms_glass_log (
main_id
,glass_id
,segment_name
,work_name
,line
,begin_time
,end_time
,`status`
,parent_id
,area
)
values (@zid,gid, _work_segment,null,_line,null,null,0,null,@tmp);
				end loop loop1;
				close cur;
	set done=0;
	open cur1;
	
	 loop2:loop 
					FETCH cur1 into cr_work_name;
					if done=1 then 
					   LEAVE loop2;
					 end if;
						set @crw=cr_work_name;
						set @csg=_work_segment;
				 INSERT INTO gmms_glass_log (
main_id
,glass_id
,segment_name
,work_name
,line
,begin_time
,end_time
,`status`
,parent_id
,area)
select 
main_id
,glass_id
,@csg
,@crw
,line
,null
,null
,0
,id
,area
 from  gmms_glass_log where main_id=@zid and parent_id is null and segment_name=_work_segment;
	end loop loop2;
	close cur1;
	end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for create_log_records_segment
-- ----------------------------
DROP PROCEDURE IF EXISTS `create_log_records_segment`;
delimiter ;;
CREATE PROCEDURE `create_log_records_segment`(in gongcheng varchar(50),in _line int,in _work_segment varchar(50))
BEGIN
	#Routine body goes here...
	declare done int default 0;
	declare w double;
	declare h double;
	DECLARE gid BIGINT;
	DECLARE cr_work_name varchar(50);
  declare cur cursor for
		  	SELECT id,edg_width,edg_height from gmms_galss_task_buffer where engineering_id=gongcheng;
	declare cur1 cursor for
		  	SELECT work_name from gmms_glass_log_config where segment=_work_segment group by work_name;

				DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
			
	set  @zid=null;
	SELECT id into @zid  from gmms_glass_log_main  where gongchenghao=gongcheng;
   if @zid is null then 
	 	INSERT INTO gmms_glass_log_main (
				segment
				,gongchenghao
				,create_time
				,line
				,state
				) values (_work_segment,gongcheng,now(),_line,0);
			SET @zid=LAST_INSERT_ID();
	 end if;
	 
	 SET @tmp=0;
	 select count(*) into @tmp from gmms_glass_log where main_id=@zid and segment_name=_work_segment;
	 set done=0;
	if @tmp=0 then 
			
				open cur;
			  loop1:loop 
					FETCH cur into gid,w,h;
					if done=1 then 
									set @cccc=@cccc+1000;
					   LEAVE loop1;
					 end if;
				 set @tmp=w*h/1000/1000;
								
				 INSERT INTO gmms_glass_log (
main_id
,glass_id
,segment_name
,work_name
,line
,begin_time
,end_time
,`status`
,parent_id
,area
)
values (@zid,gid, _work_segment,null,_line,null,null,0,null,@tmp);
				end loop loop1;
				close cur;
	set done=0;
	open cur1;
	
	 loop2:loop 
					FETCH cur1 into cr_work_name;
					if done=1 then 
					   LEAVE loop2;
					 end if;
						set @crw=cr_work_name;
						set @csg=_work_segment;
				 INSERT INTO gmms_glass_log (
main_id
,glass_id
,segment_name
,work_name
,line
,begin_time
,end_time
,`status`
,parent_id
,area)
select 
main_id
,glass_id
,@csg
,@crw
,line
,null
,null
,0
,id
,area
 from  gmms_glass_log where main_id=@zid and parent_id is null and segment_name=_work_segment;
	end loop loop2;
	close cur1;
	end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for deleteshujujilu
-- ----------------------------
DROP PROCEDURE IF EXISTS `deleteshujujilu`;
delimiter ;;
CREATE PROCEDURE `deleteshujujilu`()
BEGIN

delete from gmms_identify_error_details where  times<now() - interval 14 day;
 
delete from gmms_edg_perimeter where start_time<now() - interval 90 day;
 
delete from gmms_shelf_task where task_state='已完成' and finsh_time<now() - interval 14 day;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for DoEvent
-- ----------------------------
DROP PROCEDURE IF EXISTS `DoEvent`;
delimiter ;;
CREATE PROCEDURE `DoEvent`(in iswrite int,in id int,in msg varchar(255))
BEGIN
--  SELECT 'DDDD',id;
	#Routine body goes here...
		if id=10 then 
		  if iswrite=0 then
					call record_event("加速台请求",1,msg,id,0,0,false);
			 else 
					call record_event("加速台回复",101,msg,id,10,0,true);
			 end if;
		end if;
		if id=11 then 
			if iswrite=0 then
					call record_event("复核台请求",1,1msg,id,0,0,false);
			 else 
					call record_event("复合台回复",101,msg,id,10,0,true);
			 end if;
		end if;

		if id=17 then 
		
			if iswrite=0 then
				SELECT 17;
					call record_event("下片台请求",1,msg,17,0,0,false);
					call record_event("下片台请求",2,msg,17,6,6,false);
			 else 
					call record_event("下片台汇报",101,msg,17,10,0,true);
					call record_event("下片台确认",102,msg,17,24,0,true);
			 end if;
		end if;
		
		
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for dongzuo_get_number
-- ----------------------------
DROP PROCEDURE IF EXISTS `dongzuo_get_number`;
delimiter ;;
CREATE PROCEDURE `dongzuo_get_number`(glass_id bigint,line int,out _number int)
BEGIN
	#Routine body goes here...
	#Routine body goes here...
		declare m_f   int DEFAULT NULL;
		declare cl  varchar(50) default null;
		declare md varchar(10) default null;
		set _number=null;
		SELECT color,marking_flag into cl ,m_f from gmms_galss_task_buffer where id=glass_id;
	
		#SELECT m_f,cl;
		set md="";
		if m_f is not null then 
			if cl='白玻' or cl='超白' or cl='C' then 
			  set  md="0";
			 else
			  set  md="1";
			end if;
		
		  if line=1 then 
			   set md=concat(md,"1");
				 
			else 
			 set md=concat(md,"0");
			end if;
		
		if m_f=1 then 
		     set md=CONCAT(md,"1");
			 else		 
			 if m_f<>0 then 
			   set md=CONCAT(md,"2");
			 end if;
		 end if;
	 
		 
			 
			 
			 
			# select md;
			 select number into _number from gmms_dongzuo_table where `mode`=md;
			 
			 	if 	 m_f=0 then 

		    set _number= 15;
		end if;
			 
			 if m_f>=10 then 
		
			set _number=m_f;
			end if;
		end if;
		
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for dongzuo_get_number_copy1
-- ----------------------------
DROP PROCEDURE IF EXISTS `dongzuo_get_number_copy1`;
delimiter ;;
CREATE PROCEDURE `dongzuo_get_number_copy1`(cl varchar(50),m_f int ,_line int,out _number int)
BEGIN
	#Routine body goes here...
	#Routine body goes here...
     
		declare db int;
		declare shu int;
		declare biao int;
		declare le int;
		set db=0;
		set shu=0;
		set biao=0;
		set le=0;
		set _number=null;
		
		if _line<>1 then 
		   set _line=0;
		end if;
			if cl='白玻' or cl='超白' or cl='C' or cl='c' or cl='C（DB）'  then 
			  set  le=0;
			 else
			  set  le=1;
			  set	_line=0;
			end if;
		
		
		set @bit1=m_f mod 2;
		set @bit2=(m_f div 2) mod 2;
		set @bit3=(m_f div 4) mod 2;
		if @bit3=1 then 
			 set db=1;
			 	set shu=@bit1;
		 else 
		     if @bit2=1 then 
				    set db=2;
						set shu=@bit1;
					else	
							#不打标
							set _line=0;
							set shu=0;
							set db=0;
				 end if;
		end if;

		
		if le=0 then 
				if db=2 then 
				   set db=3;
				end if;
		else
			 if db=1 then 
			     set db=2;
			 else 
			     set db=0;
			 end if;
		end if;
		
		if db=0 then 
		   set _line=0;
			 set shu=0;
			 set db=0;
		end if;
		
		
	 if m_f>20 then 
			set _number=m_f;
	else
		if (m_f is null or m_f<0 or m_f>7) AND false  then 
        set _number=16;	 
	  else
		   #low-e不钢化
		  if le=1 and @bit3=0 then 
				set _number=31;
			else
				select id into _number from gmms_biao_table where biao_type=db and fangxiang_type=shu and line=_line and color_type=le  and id<33;
				 end if;
	   end if;
	 end if;
		
		#select * from gmms_dongzuo_table_1 where number=_number;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for dongzuo_get_number_ex
-- ----------------------------
DROP PROCEDURE IF EXISTS `dongzuo_get_number_ex`;
delimiter ;;
CREATE PROCEDURE `dongzuo_get_number_ex`(cl varchar(50),m_f int,line int,out _number int)
BEGIN
	#Routine body goes here...
	#Routine body goes here...
	
		declare md varchar(10) default null;
		set _number=null;

		#SELECT m_f,cl;
		set md="";
	
		if m_f is not null then 
			if cl='白玻' or cl='超白' or cl='C' then 
			  set  md="0";
			 else
			  set  md="1";
			end if;
	
		  if line=1 then 
			   set md=concat(md,"1");
				 
			else 
			 set md=concat(md,"0");
			end if;
			
		if m_f=1 then 
		     set md=CONCAT(md,"1");
			 else		 
			 if m_f<>0 then 
			   set md=CONCAT(md,"2");
			 end if;
		 end if;
	 
		 
	
			 

			 select number into _number from gmms_dongzuo_table where `mode`=md;
			 
			 	if 	 m_f=0 then 

		    set _number= 15;
		end if;
			 
			 if m_f>=10 then 
		
			set _number=m_f;
			end if;
		end if;
		
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for dongzuo_update_marking_flag
-- ----------------------------
DROP PROCEDURE IF EXISTS `dongzuo_update_marking_flag`;
delimiter ;;
CREATE PROCEDURE `dongzuo_update_marking_flag`(gc varchar(50),in _line int)
BEGIN
  # 用于更新动作标记
	#Routine body goes here...
	DECLARE cid long ;
	declare cmk int;
	DECLARE done1 int;
	DECLARE cl varchar(100);
	DECLARE cur CURSOR FOR

	SELECT id,marking_flag,color FROM gmms_galss_task_buffer where  engineering_id=gc;
	DECLARE CONTINUE HANDLER FOR NOT FOUND SET done1=1;
	set done1=0;
	open cur;
	set @kkk=0;
	loop1 :loop
	  set cmk=null;
		FETCH cur into cid,cmk,cl;
		if done1=1 then 
		 LEAVE loop1;
	 end if;
	 

		IF cmk is null then 
				update gmms_galss_task set marking_flag=15 where id=cid;
				update gmms_galss_task_buffer set marking_flag_ex=15 where id=cid;
		elseif cmk >32 then -- 打标混排新增-2023.07.07
				update gmms_galss_task set marking_flag=cmk where id=cid;
				update gmms_galss_task_buffer set marking_flag_ex=cmk where id=cid;
		else
		  call dongzuo_get_number_copy1(cl,cmk,_line,@kkkk);
			#call dongzuo_get_number_ex(cl,cmk,_line,@kkkk);
			if @kkkk is null then 
			set @kkkk=15;
			end if;
			update gmms_galss_task set marking_flag=@kkkk where id=cid;
		  update gmms_galss_task_buffer set marking_flag_ex=@kkkk where id=cid;
		end if;
	end loop loop1;
	close cur;
	
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for d_dapingxianshi1
-- ----------------------------
DROP PROCEDURE IF EXISTS `d_dapingxianshi1`;
delimiter ;;
CREATE PROCEDURE `d_dapingxianshi1`()
BEGIN
	#Routine body goes here...

	#原片仓储，半成品仓储架子

	select count(*) into @shelf_rack1 from gmms_shelf_rack where state='空闲';
	select count(*) into @shelf_rack2 from gmms_shelf_rack where state='使用中' or state='正在使用';
	
	select count(*) into @shelf_rack3 from gmms_shelf_rack_2 where processcard_id is null;
	select count(*) into @shelf_rack4 from gmms_shelf_rack_2 where processcard_id is not null;

select @shelf_rack2,@shelf_rack1,@shelf_rack4,@shelf_rack3;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for d_dapingxianshi13
-- ----------------------------
DROP PROCEDURE IF EXISTS `d_dapingxianshi13`;
delimiter ;;
CREATE PROCEDURE `d_dapingxianshi13`()
BEGIN
	#Routine body goes here...
	DECLARE i int DEFAULT 0;
	DECLARE shijian1 varchar(255);
	DECLARE shijian2 varchar(255);
	DECLARE shijian3 varchar(255);

drop table if EXISTS  tabletmp_table1;

create temporary table tabletmp_table1 (riqi VARCHAR(255) NOT NULL);
while i<14 do
	set shijian2= subdate(date_format( now(), '%Y-%m-%d' ),i);
	insert into tabletmp_table1(riqi) values(shijian2);
	set shijian1=CONCAT('%',@shijian,'%');

	set i=i+1;
end while;
set shijian3=subdate(date_format( now(), '%Y-%m-%d' ),14);


-- select ifnull(max(tag5)-min(tag5),0),ifnull(max(tag6)-min(tag6),0),ifnull(max(tag7)-min(tag7),0),ifnull(max(tag4)-min(tag4),0),ifnull(max(tag9)-min(tag9),0),ifnull(max(tag20)-min(tag20),0),ifnull(max(tag19)-min(tag19),0),ifnull(max(tag13)-min(tag13),0) from tabletmp_table1 tt left join  gmms_data.dianliang_view dl  on tt.riqi=date(dl.mydatetime) and dl.mydatetime>shijian3 group by tt.riqi;
select max(tag5)-min(tag5),max(tag6)-min(tag6),max(tag7)-min(tag7),max(tag4)-min(tag4),max(tag9)-min(tag9),max(tag20)-min(tag20),max(tag19)-min(tag19),max(tag13)-min(tag13) from tabletmp_table1 tt left join  gmms_data.dianliang_view dl  on tt.riqi=date(dl.mydatetime) and dl.mydatetime>shijian3 group by tt.riqi;


END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for d_dapingxianshi14
-- ----------------------------
DROP PROCEDURE IF EXISTS `d_dapingxianshi14`;
delimiter ;;
CREATE PROCEDURE `d_dapingxianshi14`()
BEGIN
	#Routine body goes here...
	#半圆
	DECLARE i int DEFAULT 0;
	DECLARE shijian1 varchar(255);
	DECLARE shijian2 varchar(255);
	DECLARE shijian3 varchar(255);
	DECLARE posunfengzhi varchar(255);
	DECLARE posunshiji varchar(255);
	
	DECLARE dainhaofengzhi varchar(255);
	DECLARE dainhaoshiji varchar(255);
	
	DECLARE chanliangfengzhi varchar(255);
	DECLARE chanliangshiji varchar(255);

set shijian1= date_format( now(), '%Y-%m-%d' );
set shijian2= subdate(date_format( now(), '%Y-%m-%d' ),14);


select sum(peak_value*14) into posunfengzhi from gmms_peak_value where type='破损';

select sum(peak_value*14) into dainhaofengzhi from gmms_peak_value where type='电耗';

select sum(peak_value*14) into chanliangfengzhi from gmms_peak_value where type='产量';


select ifnull(round(sum(gt.width*gt.height/1000000),2),0) into posunshiji from e_pierceds ep left join gmms_galss_task_buffer gt on ep.glass_id=gt.id  where  create_time>=shijian2 and create_time<shijian1;


select ifnull(max(tag5+tag6+tag7+tag4+tag9+tag20+tag2+tag13)-min(tag5+tag6+tag7+tag4+tag9+tag20+tag2+tag13),0) into dainhaoshiji from gmms_data.dianliang_view dl where dl.mydatetime >=shijian2 and dl.mydatetime<shijian1;

select ifnull(round(sum((width*height)/1000000),2),0) into @chanliangshiji1 from gmms_galss_task_stock where finsh_time>=shijian2 and finsh_time<shijian1;
select ifnull(round(sum((width*height)/1000000),2),0) into @chanliangshiji2 from E_record_tempering where create_time>=shijian2 and create_time<shijian1;
select ifnull(round(sum((width*height)/1000000),2),0) into @chanliangshiji3 from E_record_hollow where create_time>=shijian2 and create_time<shijian1;

set @zongchangliang =@chanliangshiji1+@chanliangshiji2+@chanliangshiji3;


select posunfengzhi,posunshiji,dainhaofengzhi,dainhaoshiji,chanliangfengzhi,@zongchangliang;

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for d_dapingxianshi15
-- ----------------------------
DROP PROCEDURE IF EXISTS `d_dapingxianshi15`;
delimiter ;;
CREATE PROCEDURE `d_dapingxianshi15`()
BEGIN
	#Routine body goes here...
	#Routine body goes here...
		#冷加工总产量图

#Routine body goes here...
	DECLARE i int DEFAULT 0;
	DECLARE shijian1 varchar(255);
	DECLARE shijian2 varchar(255);
	DECLARE shijian3 varchar(255);


drop table if EXISTS  tabletmp_table2;
create temporary table tabletmp_table2 (riqi VARCHAR(255) NOT NULL);
while i<14 do
	set shijian2= subdate(date_format( now(), '%Y-%m-%d' ),i);
	insert into tabletmp_table2(riqi) values(shijian2);
	set shijian1=CONCAT('%',@shijian,'%');

	set i=i+1;
end while;
set shijian3=subdate(date_format( now(), '%Y-%m-%d' ),14);


select sum(ifnull((round(gc.width*gc.height/1000000,2)*gc.pieces),0)) as qiege from 
(select ss.mobian,ss.ganghua,ss.riqi,sum(ifnull(round(erh.width*erh.height/1000000,2),0)) as zhongkong from
(select aa.riqi,c as mobian,sum(ifnull(round(ert.width*ert.height/1000000,2),0)) as ganghua  from
(select riqi,sum(ifnull(round(gr.width*gr.height/1000000,2),0)) as c
from tabletmp_table2 tt 
left join gmms_galss_task_stock gr on tt.riqi=date(gr.finsh_time) and gr.finsh_time>shijian3 left join gmms_raw_file rf on rf.file_name=gr.engineering_id and rf.create_time>shijian3 group by tt.riqi) as aa 
left join E_record_tempering ert on aa.riqi=date(ert.create_time) and ert.create_time>shijian3 group by aa.riqi) ss 
left join E_record_hollow erh on ss.riqi=date(erh.create_time) and erh.create_time>shijian3 group by ss.riqi) jj
left join gmms_cutmanage gc on jj.riqi=date(gc.statrtime) and gc.statrtime>shijian3 group by jj.riqi;





END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for d_dapingxianshi15_1
-- ----------------------------
DROP PROCEDURE IF EXISTS `d_dapingxianshi15_1`;
delimiter ;;
CREATE PROCEDURE `d_dapingxianshi15_1`()
BEGIN
	#Routine body goes here...
	#Routine body goes here...
		#冷加工总产量图

#Routine body goes here...
	DECLARE i int DEFAULT 0;
	DECLARE shijian1 varchar(255);
	DECLARE shijian2 varchar(255);
	DECLARE shijian3 varchar(255);


drop table if EXISTS  tabletmp_table2;
create temporary table tabletmp_table2 (riqi VARCHAR(255) NOT NULL);
while i<14 do
	set shijian2= subdate(date_format( now(), '%Y-%m-%d' ),i);
	insert into tabletmp_table2(riqi) values(shijian2);
	set shijian1=CONCAT('%',@shijian,'%');

	set i=i+1;
end while;
set shijian3=subdate(date_format( now(), '%Y-%m-%d' ),14);


select sum(ifnull(gc.pieces,0)) as qiege from 
(select ss.mobian,ss.ganghua,ss.riqi,if(erh.glass_id is null,0,count(*)) as zhongkong from
(select aa.riqi,c as mobian,if(ert.glass_id is null,0,count(*)) as ganghua  from
(select riqi,if(gr.id is null,0,count(*)) as c
from tabletmp_table2 tt 
left join gmms_galss_task_stock gr on tt.riqi=date(gr.finsh_time) and gr.finsh_time>shijian3 left join gmms_raw_file rf on rf.file_name=gr.engineering_id and rf.create_time>shijian3 group by tt.riqi) as aa 
left join E_record_tempering ert on aa.riqi=date(ert.create_time) and ert.create_time>shijian3 group by aa.riqi) ss 
left join E_record_hollow erh on ss.riqi=date(erh.create_time) and erh.create_time>shijian3 group by ss.riqi) jj
left join gmms_cutmanage gc on jj.riqi=date(gc.statrtime) and gc.statrtime>shijian3 group by jj.riqi;





END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for d_dapingxianshi2
-- ----------------------------
DROP PROCEDURE IF EXISTS `d_dapingxianshi2`;
delimiter ;;
CREATE PROCEDURE `d_dapingxianshi2`()
BEGIN
	#Routine body goes here...
	#原片仓储调拨记录
	DECLARE i int DEFAULT 0;
	DECLARE shijian1 varchar(255);
	DECLARE shijian2 varchar(255);

drop table if EXISTS  tabletmp_table;

create temporary table tabletmp_table (riqi VARCHAR(255) NOT NULL);
while i<14 do
	set shijian2= subdate(date_format( now(), '%Y-%m-%d' ),i);
	insert into tabletmp_table(riqi) values(shijian2);
	set shijian1=CONCAT('%',@shijian,'%');

	set i=i+1;
end while;
select sum(case when type="进库" then round((width*height)/1000000,2) else 0 end),round(concat('-',sum(case when type="退库" then width*height/1000000  else 0 end)),2) from tabletmp_table tt left join gmms_rank gr on tt.riqi=date(gr.createtime) group by tt.riqi;


-- select sum(case when type="进库" then 1 else 0 end),round(concat('-',sum(case when type="退库" then 1 else 0 end)),2) from tabletmp_table tt left join gmms_rank gr on tt.riqi=date(gr.createtime) group by tt.riqi;

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for d_dapingxianshi3
-- ----------------------------
DROP PROCEDURE IF EXISTS `d_dapingxianshi3`;
delimiter ;;
CREATE PROCEDURE `d_dapingxianshi3`()
BEGIN
	#Routine body goes here...
	#破损数据
	
		#Routine body goes here...
	#破损数据
	DECLARE shijian1 varchar(255);
	DECLARE shijian2 varchar(255);
	
	set shijian1=CONCAT('%',date_format( now(), '%Y-%m-%d' ),'%');

select CAST(sum(galssmeasure) as UNSIGNED) into @daijiagong1 from gmms_raw_file where state='已下发' and file_name like '%P%';
-- select ifnull(CAST(sum(galssmeasure) as UNSIGNED),0) into @yijiagong1 from  gmms_raw_file where state='已完成' and operator=1 and file_name like '%P%' and receive_time like shijian1; 
-- select ifnull(CAST(sum(galssmeasure) as UNSIGNED),0) into @yijiagong2 from  gmms_raw_file where state='已完成' and operator=2 and file_name like '%P%' and receive_time like shijian1; 
-- select ifnull(CAST(sum(galssmeasure) as UNSIGNED),0) into @yijiagong3 from  gmms_raw_file where state='已完成' and operator=3 and file_name like '%P%' and receive_time like shijian1; 

select ifnull(round(sum(case when operator=1 then (gt.width*gt.height)/1000000  else 0 end),2),0),ifnull(round(sum(case when operator=2 then (gt.width*gt.height)/1000000 else 0 end),2),0),ifnull(round(sum(case when operator=3 then (gt.width*gt.height)/1000000 else 0 end),2),0) into @yijiagong1,@yijiagong2,@yijiagong3 from gmms_galss_task_stock gt inner join gmms_raw_file rf on gt.engineering_id=rf.file_name and gt.finsh_time like shijian1;

select ifnull(CAST(sum((gt.edg_width*gt.edg_height)/1000000) as UNSIGNED),0) into @qiegeposun1 from e_pierceds ep left join gmms_galss_task_buffer gt on ep.glass_id=gt.id where line=1 and create_time like shijian1 and (process='切割' or process='磨边');
select ifnull(CAST(sum((gt.edg_width*gt.edg_height)/1000000) as UNSIGNED),0)  into @qiegeposun2 from e_pierceds ep left join gmms_galss_task_buffer gt on ep.glass_id=gt.id where line=2 and create_time like shijian1 and (process='切割' or process='磨边');
select ifnull(CAST(sum((gt.edg_width*gt.edg_height)/1000000) as UNSIGNED),0)  into @qiegeposun3 from e_pierceds ep left join gmms_galss_task_buffer gt on ep.glass_id=gt.id where line=3 and create_time like shijian1 and (process='切割' or process='磨边');
select ifnull(CAST(sum((gt.edg_width*gt.edg_height)/1000000) as UNSIGNED),0)  into @ganghuaposun1 from e_pierceds ep left join gmms_galss_task_buffer gt on ep.glass_id=gt.id where line=1 and create_time like shijian1 and process='钢化';
select ifnull(CAST(sum((gt.edg_width*gt.edg_height)/1000000) as UNSIGNED),0)  into @ganghuaposun2 from e_pierceds ep left join gmms_galss_task_buffer gt on ep.glass_id=gt.id where line=2 and create_time like shijian1 and process='钢化';
select ifnull(CAST(sum((gt.edg_width*gt.edg_height)/1000000) as UNSIGNED),0)  into @zhongkongposun1 from e_pierceds ep left join gmms_galss_task_buffer gt on ep.glass_id=gt.id where line=1 and create_time like shijian1 and process='中空';
select ifnull(CAST(sum((gt.edg_width*gt.edg_height)/1000000) as UNSIGNED),0)  into @zhongkongposun2 from e_pierceds ep left join gmms_galss_task_buffer gt on ep.glass_id=gt.id where line=2 and create_time like shijian1 and process='中空';
select ifnull(CAST(sum((gt.edg_width*gt.edg_height)/1000000) as UNSIGNED),0)  into @zhongkongposun3 from e_pierceds ep left join gmms_galss_task_buffer gt on ep.glass_id=gt.id where line=3 and create_time like shijian1 and process='中空';
select ifnull(CAST(sum((edg_width*edg_height)/1000000) as UNSIGNED),0) into @daijiagong2 from gmms_galss_task_stock where fine_throwing=0 and marking_flag in('11','12','13','14','15','16','17','18');

select CAST(sum((edg_width*edg_height)/1000000) as UNSIGNED) into @daijiagong3 from gmms_galss_task_shelf where work_procedure='钢化后下片';

select ifnull(round(sum((width*height)/1000000),2),0) into @yijiagong4 from E_record_tempering where line=1 and create_time like shijian1;
select ifnull(round(sum((width*height)/1000000),2),0) into @yijiagong5 from  E_record_tempering where line=2 and create_time like shijian1;
select ifnull(round(sum((width*height)/1000000),2),0)  into @yijiagong6 from  E_record_hollow where line=1 and create_time like shijian1;
select ifnull(round(sum((width*height)/1000000),2),0) into @yijiagong7 from  E_record_hollow where line=2 and create_time like shijian1;
select ifnull(round(sum((width*height)/1000000),2),0) into @yijiagong8 from  E_record_hollow where line=3 and create_time like shijian1;


select ifnull(sum(case when operator=1 then 1 else 0 end),0),ifnull(sum(case when operator=2 then 1 else 0 end),0),ifnull(sum(case when operator=3 then 1 else 0 end),0) into @jiagongshu1,@jiagongshu2,@jiagongshu3 from gmms_galss_task_stock gt inner join gmms_raw_file rf on gt.engineering_id=rf.file_name and gt.finsh_time like shijian1;

select ifnull(sum(case when line=1 then 1 else 0 end),0),ifnull(sum(case when line=2 then 1 else 0 end),0) into @jiagongshu4,@jiagongshu5 from E_record_tempering where create_time like shijian1;

select ifnull(sum(case when line=1 then 1 else 0 end),0),ifnull(sum(case when line=2 then 1 else 0 end),0),ifnull(sum(case when line=3 then 1 else 0 end),0) into @jiagongshu6,@jiagongshu7,@jiagongshu8 from E_record_hollow where create_time like shijian1;


-- select sum(tag5),sum(tag6),sum(tag7),sum(tag4),sum(tag9),sum(tag20),sum(tag19),sum(tag13) into @haodian1,@haodian2,@haodian3,@haodian4,@haodian5,@haodian6,@haodian7,@haodian8 from   gmms_data.dianliang_view dl order by myid desc limit 0,1;


select ifnull(max(tag5)-min(tag5),0),ifnull(max(tag6)-min(tag6),0),ifnull(max(tag7)-min(tag7),0),ifnull(max(tag4)-min(tag4),0),ifnull(max(tag9)-min(tag9),0),ifnull(max(tag20)-min(tag20),0),ifnull(max(tag19)-min(tag19),0),ifnull(max(tag13)-min(tag13),0) into @haodian1,@haodian2,@haodian3,@haodian4,@haodian5,@haodian6,@haodian7,@haodian8 from   gmms_data.dianliang_view dl where mydatetime like shijian1  ;



select @daijiagong1,@yijiagong1,@yijiagong2,@yijiagong3,@qiegeposun1,@qiegeposun2,@qiegeposun3,@ganghuaposun1,@ganghuaposun2,@zhongkongposun1,@zhongkongposun2,@zhongkongposun3,@daijiagong2,@daijiagong3,@yijiagong4,@yijiagong5,@yijiagong6,@yijiagong7,@yijiagong8,@haodian1,@haodian2,@haodian3,@haodian4,@haodian5,@haodian6,@haodian7,@haodian8,@jiagongshu1,
@jiagongshu2,@jiagongshu3,@jiagongshu4,@jiagongshu5,@jiagongshu6,@jiagongshu7,@jiagongshu8,ifnull(round(@haodian1/@yijiagong1,2),0),ifnull(round(@haodian2/@yijiagong2,2),0),ifnull(round(@haodian3/@yijiagong3,2),0),ifnull(round(@haodian4/@yijiagong4,2),0),ifnull(round(@haodian5/@yijiagong5,2),0),ifnull(round(@haodian6/@yijiagong6,2),0),ifnull(round(@haodian7/@yijiagong7,2),0),ifnull(round(@haodian8/@yijiagong8,2),0);


-- select CAST(sum(galssmeasure) as UNSIGNED) into @daijiagong1 from gmms_raw_file where state='已下发' and file_name like '%P%';
-- select CAST(sum(galssmeasure) as UNSIGNED) into @yijiagong1 from  gmms_raw_file where state='已完成' and operator=1 and file_name like '%P%'; 
-- select CAST(sum(galssmeasure) as UNSIGNED) into @yijiagong2 from  gmms_raw_file where state='已完成' and operator=2 and file_name like '%P%'; 
-- select CAST(sum(galssmeasure) as UNSIGNED) into @yijiagong3 from  gmms_raw_file where state='已完成' and operator=3 and file_name like '%P%'; 
-- select ifnull(CAST(sum((gt.edg_width*gt.edg_height)/1000000) as UNSIGNED),0) into @qiegeposun1 from e_pierceds ep left join gmms_galss_task_buffer gt on ep.glass_id=gt.id where line=1 and (process='切割' or process='磨边');
-- select ifnull(CAST(sum((gt.edg_width*gt.edg_height)/1000000) as UNSIGNED),0)  into @qiegeposun2 from e_pierceds ep left join gmms_galss_task_buffer gt on ep.glass_id=gt.id where line=2 and (process='切割' or process='磨边');
-- select ifnull(CAST(sum((gt.edg_width*gt.edg_height)/1000000) as UNSIGNED),0)  into @qiegeposun3 from e_pierceds ep left join gmms_galss_task_buffer gt on ep.glass_id=gt.id where line=3 and (process='切割' or process='磨边');
-- select ifnull(CAST(sum((gt.edg_width*gt.edg_height)/1000000) as UNSIGNED),0)  into @ganghuaposun1 from e_pierceds ep left join gmms_galss_task_buffer gt on ep.glass_id=gt.id where line=1 and process='钢化';
-- select ifnull(CAST(sum((gt.edg_width*gt.edg_height)/1000000) as UNSIGNED),0)  into @ganghuaposun2 from e_pierceds ep left join gmms_galss_task_buffer gt on ep.glass_id=gt.id where line=2 and process='钢化';
-- select ifnull(CAST(sum((gt.edg_width*gt.edg_height)/1000000) as UNSIGNED),0)  into @zhongkongposun1 from e_pierceds ep left join gmms_galss_task_buffer gt on ep.glass_id=gt.id where line=1 and process='中空';
-- select ifnull(CAST(sum((gt.edg_width*gt.edg_height)/1000000) as UNSIGNED),0)  into @zhongkongposun2 from e_pierceds ep left join gmms_galss_task_buffer gt on ep.glass_id=gt.id where line=2 and process='中空';
-- select ifnull(CAST(sum((gt.edg_width*gt.edg_height)/1000000) as UNSIGNED),0)  into @zhongkongposun3 from e_pierceds ep left join gmms_galss_task_buffer gt on ep.glass_id=gt.id where line=3 and process='中空';
-- select ifnull(CAST(sum((edg_width*edg_height)/1000000) as UNSIGNED),0) into @daijiagong2 from gmms_galss_task_stock where fine_throwing=0 and marking_flag in('11','12','13','14','15','16','17','18');
-- 
-- select ifnull(CAST(sum((edg_width*edg_height)/1000000) as UNSIGNED),0) into @daijiagong3 from gmms_galss_task_shelf where work_procedure='钢化后下片';
-- 
-- select ifnull(round(sum((width*height)/1000000),2),0) into @yijiagong4 from E_record_tempering where line=1;
-- select ifnull(round(sum((width*height)/1000000),2),0) into @yijiagong5 from  E_record_tempering where line=2;
-- select ifnull(round(sum((width*height)/1000000),2),0)  into @yijiagong6 from  E_record_hollow where line=1;
-- select ifnull(round(sum((width*height)/1000000),2),0) into @yijiagong7 from  E_record_hollow where line=2;
-- select ifnull(round(sum((width*height)/1000000),2),0) into @yijiagong8 from  E_record_hollow where line=3;
-- 
-- select round(sum(tag5)),round(sum(tag6)),round(sum(tag7)),round(sum(tag4)),round(sum(tag9)),round(sum(tag20)),round(sum(tag19)),round(sum(tag13)) into @haodian1,@haodian2,@haodian3,@haodian4,@haodian5,@haodian6,@haodian7,@haodian8 from   gmms_data.dianliang_view dl ;
-- 
-- select @daijiagong1,@yijiagong1,@yijiagong2,@yijiagong3,@qiegeposun1,@qiegeposun2,@qiegeposun3,@ganghuaposun1,@ganghuaposun2,@zhongkongposun1,@zhongkongposun2,@zhongkongposun3,@daijiagong2,@daijiagong3,@yijiagong4,@yijiagong5,@yijiagong6,@yijiagong7,@yijiagong8,@haodian1,@haodian2,@haodian3,@haodian4,@haodian5,@haodian6,@haodian7,@haodian8;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for d_dapingxianshi4
-- ----------------------------
DROP PROCEDURE IF EXISTS `d_dapingxianshi4`;
delimiter ;;
CREATE PROCEDURE `d_dapingxianshi4`()
BEGIN
	#Routine body goes here...
	#生产线冷加工完工折线图
	DECLARE i int DEFAULT 10;
	DECLARE shijian1 varchar(255);
	DECLARE shijian2 varchar(255);
	DECLARE shijian3 varchar(255);

drop table if EXISTS  tabletmp_table1;
drop table if EXISTS  tabletmp_table2;

create temporary table tabletmp_table1 (riqi VARCHAR(255) NOT NULL);
create temporary table tabletmp_table2 (riqi VARCHAR(255) NOT NULL);
while i<=60 do
	set shijian2= DATE_ADD(now(), INTERVAL -i MINUTE);
	insert into tabletmp_table1(riqi) values(shijian2);
	insert into tabletmp_table2(riqi) values(shijian2);
	set shijian1=CONCAT('%',@shijian,'%');

	set i=i+10;
end while;

set shijian3=DATE_ADD(now(), INTERVAL -70 MINUTE);

-- select sum(case when operator=1 then round((gr.width*gr.height)/1000000,2) else 0 end) as bbb,
-- sum(case when operator=2 then round((gr.width*gr.height)/1000000,2) else 0 end) as sss,
-- sum(case when operator=3 then round((gr.width*gr.height)/1000000,2) else 0 end) as aaa
-- from tabletmp_table1 tt left join  gmms_galss_task_stock   gr on finsh_time>shijian3 and left(tt.riqi,15)=left(gr.finsh_time,15) 
-- left join gmms_raw_file grf on gr.engineering_id=grf.file_name group by left(tt.riqi,15);


select qiege1,qiege2,qiege3,ganghua1,ganghua2,sum(case when ert.line=1 then round((ert.width*ert.height)/1000000 ,2) else 0 end) as zhongkong1,sum(case when ert.line=2 then round((ert.width*ert.height)/1000000 ,2) else 0 end) as zhongkong2,sum(case when ert.line=3 then round((ert.width*ert.height)/1000000 ,2) else 0 end) as zhongkong3 from
(select ss.riqi,bbb as qiege1,sss as qiege2,aaa as qiege3,
sum(case when erp.line=1 then round((erp.width*erp.height)/1000000,2)  else 0 end) as ganghua1,
sum(case when erp.line=2 then round((erp.width*erp.height)/1000000,2)  else 0 end) as ganghua2  from 
(select riqi,sum(case when operator=1 then round((gr.width*gr.height)/1000000,2) else 0 end) as bbb,
sum(case when operator=2 then round((gr.width*gr.height)/1000000,2) else 0 end) as sss,
sum(case when operator=3 then round((gr.width*gr.height)/1000000,2) else 0 end) as aaa


from tabletmp_table1 tt left join  gmms_galss_task_stock   gr on left(tt.riqi,15)=left(gr.finsh_time,15) and finsh_time>shijian3

left join gmms_raw_file grf on gr.engineering_id=grf.file_name group by left(tt.riqi,15)) as ss left join E_record_tempering erp on left(ss.riqi,15)=left(erp.create_time,15) and create_time>shijian3
group by left(ss.riqi,15)) as gg left join E_record_hollow ert on left(gg.riqi,15)=left(ert.create_time,15) and create_time>shijian3 group by left(gg.riqi,15);
-- 
-- 
-- 
-- 
-- 
-- select qiege1,qiege2,qiege3,ganghua1,ganghua2,sum(case when ert.line=1 then 1 else 0 end) as zhongkong1,sum(case when ert.line=2 then 1 else 0 end) as zhongkong2,sum(case when ert.line=3 then 1 else 0 end) as zhongkong3 from
-- (select ss.riqi,bbb as qiege1,sss as qiege2,aaa as qiege3,
-- sum(case when erp.line=1 then 1  else 0 end) as ganghua1,
-- sum(case when erp.line=2 then 1  else 0 end) as ganghua2  from 
-- (select riqi,sum(case when operator=1 then 1 else 0 end) as bbb,
-- sum(case when operator=2 then 1 else 0 end) as sss,
-- sum(case when operator=3 then 1 else 0 end) as aaa
-- 
-- 
-- from tabletmp_table1 tt left join  gmms_galss_task_stock   gr on left(tt.riqi,15)=left(gr.finsh_time,15) and finsh_time>shijian3
-- 
-- left join gmms_raw_file grf on gr.engineering_id=grf.file_name group by left(tt.riqi,15)) as ss left join E_record_tempering erp on left(ss.riqi,15)=left(erp.create_time,15) and create_time>shijian3
-- group by left(ss.riqi,15)) as gg left join E_record_hollow ert on left(gg.riqi,15)=left(ert.create_time,15) and create_time>shijian3 group by left(gg.riqi,15);











-- select riqi,sum(case when ert.line=3 then round((ert.width*ert.width)/1000000,2)  else 0 end)
-- 
-- 
-- from tabletmp_table1 tt left join E_record_hollow ert on left(tt.riqi,15)=left(ert.create_time,15) and create_time>@shijian2 group by left(tt.riqi,15);


-- select riqi,sum(case when operator=1 then 1 else 0 end),
-- sum(case when operator=2 then 1 else 0 end) ,
-- sum(case when operator=3 then 1 else 0 end) 
-- 
-- 
-- from tabletmp_table1 tt left join  gmms_galss_task_stock   gr on left(tt.riqi,15)=left(gr.finsh_time,15) and finsh_time>DATE_ADD('2023-04-10 10:49:48', INTERVAL -70 MINUTE)
-- 
-- left join gmms_raw_file grf on gr.engineering_id=grf.file_name group by left(tt.riqi,15);

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for d_dapingxianshi5
-- ----------------------------
DROP PROCEDURE IF EXISTS `d_dapingxianshi5`;
delimiter ;;
CREATE PROCEDURE `d_dapingxianshi5`()
BEGIN
	#Routine body goes here...
		#冷加工总产量图

#Routine body goes here...
	DECLARE i int DEFAULT 0;
	DECLARE shijian1 varchar(255);
	DECLARE shijian2 varchar(255);
	DECLARE shijian3 varchar(255);


drop table if EXISTS  tabletmp_table2;
create temporary table tabletmp_table2 (riqi VARCHAR(255) NOT NULL);
while i<14 do
	set shijian2= subdate(date_format( now(), '%Y-%m-%d' ),i);
	insert into tabletmp_table2(riqi) values(shijian2);
	set shijian1=CONCAT('%',@shijian,'%');

	set i=i+1;
end while;
set shijian3=subdate(date_format( now(), '%Y-%m-%d' ),14);



select qiege1,qiege2,qiege3,ganghua1,ganghua2,sum(case when erh.line=1 then round(erh.width*erh.height/1000000,2) else 0 end) as zhongkong1,sum(case when erh.line=2 then round(erh.width*erh.height/1000000,2) else 0 end) as zhongkong2,sum(case when erh.line=3 then round(erh.width*erh.height/1000000,2) else 0 end) as zhongkong3 from
(select aa.riqi,a as qiege1,b as qiege2,c as qiege3,sum(case when ert.line=1 then round(ert.width*ert.height/1000000,2) else 0 end) as ganghua1,sum(case when ert.line=2 then round(ert.width*ert.height/1000000,2) else 0 end) as ganghua2  from
(select riqi,sum(case when operator=1 then round(gr.width*gr.height/1000000,2) else 0 end) as a,sum(case when operator=2 then round(gr.width*gr.height/1000000,2) else 0 end) as b,sum(case when operator=3 then round(gr.width*gr.height/1000000,2) else 0 end) as c
from tabletmp_table2 tt left join gmms_galss_task_stock gr on tt.riqi=date(gr.finsh_time) and gr.finsh_time>shijian3 left join gmms_raw_file rf on rf.file_name=gr.engineering_id and rf.create_time>shijian3 group by tt.riqi) as aa 
left join E_record_tempering ert on aa.riqi=date(ert.create_time) and ert.create_time>shijian3 group by aa.riqi) ss 
left join E_record_hollow erh on ss.riqi=date(erh.create_time) and erh.create_time>shijian3 group by ss.riqi;



-- select qiege1,qiege2,qiege3,ganghua1,ganghua2,sum(case when erh.line=1 then 1 else 0 end) as zhongkong1,sum(case when erh.line=2 then 1 else 0 end) as zhongkong2,sum(case when erh.line=3 then 1 else 0 end) as zhongkong3 from
-- (select aa.riqi,a as qiege1,b as qiege2,c as qiege3,sum(case when ert.line=1 then 1 else 0 end) as ganghua1,sum(case when ert.line=2 then 1 else 0 end) as ganghua2  from
-- (select riqi,sum(case when operator=1 then 1 else 0 end) as a,sum(case when operator=2 then 1 else 0 end) as b,sum(case when operator=3 then 1 else 0 end) as c
-- from tabletmp_table2 tt left join gmms_galss_task_stock gr on tt.riqi=date(gr.finsh_time) and gr.finsh_time>shijian3 left join gmms_raw_file rf on rf.file_name=gr.engineering_id and rf.create_time>shijian3 group by tt.riqi) as aa 
-- left join E_record_tempering ert on aa.riqi=date(ert.create_time) and ert.create_time>shijian3 group by aa.riqi) ss 
-- left join E_record_hollow erh on ss.riqi=date(erh.create_time) and erh.create_time>shijian3 group by ss.riqi;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for d_dapingxianshi5_1
-- ----------------------------
DROP PROCEDURE IF EXISTS `d_dapingxianshi5_1`;
delimiter ;;
CREATE PROCEDURE `d_dapingxianshi5_1`()
BEGIN
	#	#冷加工总产量图

#Routine body goes here...
	DECLARE i int DEFAULT 0;
	DECLARE shijian1 varchar(255);
	DECLARE shijian2 varchar(255);
	DECLARE shijian3 varchar(255);


drop table if EXISTS  tabletmp_table2;
create temporary table tabletmp_table2 (riqi VARCHAR(255) NOT NULL);
while i<=14 do
	set shijian2= subdate(date_format( now(), '%Y-%m-%d' ),i);
	insert into tabletmp_table2(riqi) values(shijian2);
	set shijian1=CONCAT('%',@shijian,'%');

	set i=i+1;
end while;
set shijian3=subdate(date_format( now(), '%Y-%m-%d' ),14);


-- select qiege1,qiege2,qiege3,ganghua1,ganghua2,sum(case when erh.line=1 then round(erh.width*erh.height/1000000,2) else 0 end) as zhongkong1,sum(case when erh.line=2 then round(erh.width*erh.height/1000000,2) else 0 end) as zhongkong2,sum(case when erh.line=3 then round(erh.width*erh.height/1000000,2) else 0 end) as zhongkong3 from
-- (select aa.riqi,a as qiege1,b as qiege2,c as qiege3,sum(case when ert.line=1 then round(ert.width*ert.height/1000000,2) else 0 end) as ganghua1,sum(case when ert.line=2 then round(ert.width*ert.height/1000000,2) else 0 end) as ganghua2  from
-- (select riqi,sum(case when operator=1 then round(gr.width*gr.height/1000000,2) else 0 end) as a,sum(case when operator=2 then round(gr.width*gr.height/1000000,2) else 0 end) as b,sum(case when operator=3 then round(gr.width*gr.height/1000000,2) else 0 end) as c
-- from tabletmp_table2 tt left join gmms_galss_task_stock gr on tt.riqi=date(gr.finsh_time) and gr.finsh_time>shijian3 left join gmms_raw_file rf on rf.file_name=gr.engineering_id and rf.create_time>@shijian3 group by tt.riqi) as aa 
-- left join E_record_tempering ert on aa.riqi=date(ert.create_time) and ert.create_time>shijian3 group by aa.riqi) ss 
-- left join E_record_hollow erh on ss.riqi=date(erh.create_time) and erh.create_time>shijian3 group by ss.riqi;





select qiege1,qiege2,qiege3,ganghua1,ganghua2,sum(case when erh.line=1 then 1 else 0 end) as zhongkong1,sum(case when erh.line=2 then 1 else 0 end) as zhongkong2,sum(case when erh.line=3 then 1 else 0 end) as zhongkong3 from
(select aa.riqi,a as qiege1,b as qiege2,c as qiege3,sum(case when ert.line=1 then 1 else 0 end) as ganghua1,sum(case when ert.line=2 then 1 else 0 end) as ganghua2  from
(select riqi,sum(case when operator=1 then 1 else 0 end) as a,sum(case when operator=2 then 1 else 0 end) as b,sum(case when operator=3 then 1 else 0 end) as c
from tabletmp_table2 tt left join gmms_galss_task_stock gr on tt.riqi=date(gr.finsh_time) and gr.finsh_time>shijian3 left join gmms_raw_file rf on rf.file_name=gr.engineering_id and rf.create_time>shijian3 group by tt.riqi) as aa 
left join E_record_tempering ert on aa.riqi=date(ert.create_time) and ert.create_time>shijian3 group by aa.riqi) ss 
left join E_record_hollow erh on ss.riqi=date(erh.create_time) and erh.create_time>shijian3 group by ss.riqi;

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for d_dapingxianshi6
-- ----------------------------
DROP PROCEDURE IF EXISTS `d_dapingxianshi6`;
delimiter ;;
CREATE PROCEDURE `d_dapingxianshi6`()
BEGIN
	#Routine body goes here...
	#总破损统计图

#Routine body goes here...
	DECLARE i int DEFAULT 0;
	DECLARE shijian1 varchar(255);
	DECLARE shijian2 varchar(255);
	DECLARE shijian3 varchar(255);

drop table if EXISTS  tabletmp_table3;

create temporary table tabletmp_table3 (riqi VARCHAR(255) NOT NULL);
while i<14 do
	set shijian2= subdate(date_format( now(), '%Y-%m-%d' ),i);
	insert into tabletmp_table3(riqi) values(shijian2);
	set shijian1=CONCAT('%',@shijian,'%');

	set i=i+1;
end while;
set shijian3=subdate(date_format( now(), '%Y-%m-%d' ),14);

select sum(case when line=1 and (process='切割' or process='磨边') then round(gt.width*gt.height/1000000,2) else 0 end)
,sum(case when line=2 and (process='切割' or process='磨边') then round(gt.width*gt.height/1000000,2) else 0 end)
,sum(case when line=3 and (process='切割' or process='磨边') then round(gt.width*gt.height/1000000,2) else 0 end)
,sum(case when line=1 and process='钢化'  then round(gt.width*gt.height/1000000,2) else 0 end)
,sum(case when line=2 and process='钢化'  then round(gt.width*gt.height/1000000,2) else 0 end)
,sum(case when line=1 and process='中空'  then round(gt.width*gt.height/1000000,2) else 0 end)
,sum(case when line=2 and process='中空'  then round(gt.width*gt.height/1000000,2) else 0 end)
,sum(case when line=3 and process='中空'  then round(gt.width*gt.height/1000000,2) else 0 end)
from tabletmp_table3 tt left join e_pierceds gr on tt.riqi=date(gr.create_time) and gr.create_time>shijian3 left join gmms_galss_task_buffer gt on gr.glass_id=gt.id  group by tt.riqi;

-- select sum(case when line=1 and (process='切割' or process='磨边') then 1 else 0 end)
-- ,sum(case when line=2 and (process='切割' or process='磨边') then 1 else 0 end)
-- ,sum(case when line=3 and (process='切割' or process='磨边') then 1 else 0 end)
-- ,sum(case when line=1 and process='钢化'  then 1 else 0 end)
-- ,sum(case when line=2 and process='钢化'  then 1 else 0 end)
-- ,sum(case when line=1 and process='中空'  then 1 else 0 end)
-- ,sum(case when line=2 and process='中空'  then 1 else 0 end)
-- ,sum(case when line=3 and process='中空'  then 1 else 0 end)
-- from tabletmp_table3 tt left join e_pierceds gr on tt.riqi=date(gr.create_time) and gr.create_time>shijian3  group by tt.riqi;

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for d_dapingxianshi6_1
-- ----------------------------
DROP PROCEDURE IF EXISTS `d_dapingxianshi6_1`;
delimiter ;;
CREATE PROCEDURE `d_dapingxianshi6_1`()
BEGIN
		#总破损统计图

#Routine body goes here...
	DECLARE i int DEFAULT 0;
	DECLARE shijian1 varchar(255);
	DECLARE shijian2 varchar(255);
	DECLARE shijian3 varchar(255);

drop table if EXISTS  tabletmp_table3;

create temporary table tabletmp_table3 (riqi VARCHAR(255) NOT NULL);
while i<=14 do
	set shijian2= subdate(date_format(now(), '%Y-%m-%d' ),i);
	insert into tabletmp_table3(riqi) values(shijian2);
	set shijian1=CONCAT('%',@shijian,'%');

	set i=i+1;
end while;
set shijian3=subdate(date_format( now(), '%Y-%m-%d' ),14);

-- select sum(case when line=1 and (process='切割' or process='磨边') then round(gt.width*gt.height/1000000,2) else 0 end)
-- ,sum(case when line=2 and (process='切割' or process='磨边') then round(gt.width*gt.height/1000000,2) else 0 end)
-- ,sum(case when line=3 and (process='切割' or process='磨边') then round(gt.width*gt.height/1000000,2) else 0 end)
-- ,sum(case when line=1 and process='钢化'  then round(gt.width*gt.height/1000000,2) else 0 end)
-- ,sum(case when line=2 and process='钢化'  then round(gt.width*gt.height/1000000,2) else 0 end)
-- ,sum(case when line=1 and process='中空'  then round(gt.width*gt.height/1000000,2) else 0 end)
-- ,sum(case when line=2 and process='中空'  then round(gt.width*gt.height/1000000,2) else 0 end)
-- ,sum(case when line=3 and process='中空'  then round(gt.width*gt.height/1000000,2) else 0 end)
-- from tabletmp_table3 tt left join e_pierceds gr on tt.riqi=date(gr.create_time) and gr.create_time>shijian3 left join gmms_galss_task_buffer gt on gr.glass_id=gt.id  group by tt.riqi;

select sum(case when line=1 and (process='切割' or process='磨边') then 1 else 0 end)
,sum(case when line=2 and (process='切割' or process='磨边') then 1 else 0 end)
,sum(case when line=3 and (process='切割' or process='磨边') then 1 else 0 end)
,sum(case when line=1 and process='钢化'  then 1 else 0 end)
,sum(case when line=2 and process='钢化'  then 1 else 0 end)
,sum(case when line=1 and process='中空'  then 1 else 0 end)
,sum(case when line=2 and process='中空'  then 1 else 0 end)
,sum(case when line=3 and process='中空'  then 1 else 0 end)
from tabletmp_table3 tt left join e_pierceds gr on tt.riqi=date(gr.create_time) and gr.create_time>shijian3  group by tt.riqi;

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for d_dapingxianshi7
-- ----------------------------
DROP PROCEDURE IF EXISTS `d_dapingxianshi7`;
delimiter ;;
CREATE PROCEDURE `d_dapingxianshi7`()
BEGIN
	#Routine body goes here...
	#Routine body goes here...
	#生产线破损折线图
	DECLARE i int DEFAULT 10;
	DECLARE shijian1 varchar(255);
	DECLARE shijian2 varchar(255);
	DECLARE shijian3 varchar(255);

drop table if EXISTS  tabletmp_table4;

create temporary table tabletmp_table4 (riqi VARCHAR(255) NOT NULL);
while i<=60 do
	set shijian2= DATE_ADD(now(), INTERVAL -i MINUTE);
	insert into tabletmp_table4(riqi) values(shijian2);
	set shijian1=CONCAT('%',@shijian,'%');

	set i=i+10;
end while;
set shijian3=DATE_ADD(now(), INTERVAL -70 MINUTE);

select sum(case when line=1 and (process='切割' or process='磨边') then round(gt.width*gt.height/1000000,2) else 0 end)
,sum(case when line=2 and (process='切割' or process='磨边') then round(gt.width*gt.height/1000000,2) else 0 end)
,sum(case when line=3 and (process='切割' or process='磨边') then round(gt.width*gt.height/1000000,2) else 0 end)
,sum(case when line=1 and process='钢化'  then round(gt.width*gt.height/1000000,2) else 0 end)
,sum(case when line=2 and process='钢化'  then round(gt.width*gt.height/1000000,2) else 0 end)
,sum(case when line=1 and process='中空'  then round(gt.width*gt.height/1000000,2) else 0 end)
,sum(case when line=2 and process='中空'  then round(gt.width*gt.height/1000000,2) else 0 end)
,sum(case when line=3 and process='中空'  then round(gt.width*gt.height/1000000,2) else 0 end)
from tabletmp_table4 tt left join e_pierceds gr on left(tt.riqi,15)=left(gr.create_time,15) and gr.create_time>shijian3 left join gmms_galss_task_buffer gt on gr.glass_id=gt.id

group by left(tt.riqi,15);


-- select sum(case when line=1 and (process='切割' or process='磨边') then 1 else 0 end)
-- ,sum(case when line=2 and (process='切割' or process='磨边') then 1 else 0 end)
-- ,sum(case when line=3 and (process='切割' or process='磨边') then 1 else 0 end)
-- ,sum(case when line=1 and process='钢化'  then 1 else 0 end)
-- ,sum(case when line=2 and process='钢化'  then 1 else 0 end)
-- ,sum(case when line=1 and process='中空'  then 1 else 0 end)
-- ,sum(case when line=2 and process='中空'  then 1 else 0 end)
-- ,sum(case when line=3 and process='中空'  then 1 else 0 end)
-- from tabletmp_table4 tt left join e_pierceds gr on left(tt.riqi,15)=left(gr.create_time,15) and gr.create_time>shijian3
-- 
-- group by left(tt.riqi,15);

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for d_dapingxianshi8
-- ----------------------------
DROP PROCEDURE IF EXISTS `d_dapingxianshi8`;
delimiter ;;
CREATE PROCEDURE `d_dapingxianshi8`()
BEGIN
	#Routine body goes here...
	#生产线冷加工完工折线图
	DECLARE i int DEFAULT 0;
	DECLARE shijian1 varchar(255);
	DECLARE shijian2 varchar(255);
	DECLARE shijian3 varchar(255);

drop table if EXISTS  tabletmp_table1;
drop table if EXISTS  tabletmp_table2;

create temporary table tabletmp_table1 (riqi VARCHAR(255) NOT NULL);
create temporary table tabletmp_table2 (riqi VARCHAR(255) NOT NULL);
while i<14 do
	set shijian2= subdate(date_format( now(), '%Y-%m-%d' ),i);
	insert into tabletmp_table1(riqi) values(shijian2);
	insert into tabletmp_table2(riqi) values(shijian2);
	set shijian1=CONCAT('%',@shijian,'%');

	set i=i+1;
end while;
set shijian3=subdate(date_format( now(), '%Y-%m-%d' ),14);


select biao1.aaa+biao2.bbb,if(biao1.bbb+biao2.aaa!=0,concat('-',biao1.bbb+biao2.aaa),biao1.bbb+biao2.aaa) from
(select riqi,aaa,ifnull(sum(round(erh.width*erh.height/1000000,2)),0)  as bbb from 
(select riqi,ifnull(sum(round(ert.width*ert.height/1000000,2)),0)  as aaa


from tabletmp_table1 tt left join  E_record_tempering   ert on tt.riqi=date(ert.create_time) and ert.create_time>shijian3 group by tt.riqi) as gg 
left join E_record_hollow erh on gg.riqi=date(erh.create_time) and erh.create_time>shijian3 group by gg.riqi) as biao1


inner join 


(select riqi,sum(case when type='进库' then round(gr.width*gr.height/1000000,2) else 0 end) as bbb ,sum(case when type='退库' then round(gr.width*gr.height/1000000,2) else 0 end) as aaa


from tabletmp_table2 tt left join  gmms_rank_2   gr on tt.riqi=date(gr.createtime) and createtime>shijian3 group by tt.riqi) as biao2
on biao1.riqi=biao2.riqi;



-- select biao1.riqi,biao1.bbb+biao2.bbb,biao1.aaa+biao2.aaa from
-- (select riqi,aaa,sum(case when groups is not null then 1 else 0 end) as bbb from 
-- (select riqi,sum(case when groups is not null then 1 else 0 end)  as aaa
-- 
-- 
-- from tabletmp_table1 tt left join  E_record_tempering   ert on tt.riqi=date(ert.create_time) and ert.create_time>@shijian2 group by tt.riqi) as gg 
-- left join E_record_hollow erh on gg.riqi=date(erh.create_time) and erh.create_time>shijian3 group by gg.riqi) as biao1
-- 
-- 
-- inner join 
-- 
-- 
-- (select riqi,sum(case when type='进库' then 1 else 0 end) as bbb ,sum(case when type='退库' then 1 else 0 end) as aaa
-- 
-- 
-- from tabletmp_table2 tt left join  gmms_rank_2   gr on tt.riqi=date(gr.createtime) and createtime>shijian3 group by tt.riqi) as biao2
-- on biao1.riqi=biao2.riqi;


END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for d_dapingxianshi9
-- ----------------------------
DROP PROCEDURE IF EXISTS `d_dapingxianshi9`;
delimiter ;;
CREATE PROCEDURE `d_dapingxianshi9`()
BEGIN
	#Routine body goes here...
	#右上角
	DECLARE i int DEFAULT 0;
	DECLARE shijian1 varchar(255);
	DECLARE shijian2 varchar(255);
	DECLARE shijian3 varchar(255);
	DECLARE shijian4 varchar(255);



drop table if EXISTS  tabletmp_table4;
create temporary table tabletmp_table4 (riqi VARCHAR(255) NOT NULL);
insert into tabletmp_table4(riqi) values('切割一线'),('切割二线'),('切割三线'),('钢化一线'),('钢化二线'),('中空一线'),('中空二线'),('中空三线');


set shijian4=DATE_ADD(curdate(), INTERVAL -1 HOUR);

 
set @db='select tt.riqi,qqq.* from tabletmp_table4 tt left join (select if(rf.operator!=1 and rf.operator!=2,"切割三线",if(rf.operator!=1,"切割二线","切割一线")) as aaa';
set @db1='select if(ert.line!=2,"钢化一线","钢化二线") as aaa';
set @db2='select if(erh.line!=1 and erh.line!=2,"中空三线",if(erh.line!=1,"中空二线","中空一线")) as aaa' ;
 
 
 while i<1440 do
	set shijian2= DATE_ADD(curdate(), INTERVAL +i MINUTE);
	
	set shijian3=DATE_ADD(shijian2, INTERVAL +20 MINUTE);

	set @db=concat(@db,',ifnull(if(round(sum(case when left(gtc.finsh_time,16)<"',left(shijian3,16),'" and left(gtc.finsh_time,16)>="',left(shijian2,16),'"   then 1 else 0 end)/20*100,2)>100,100,round(sum(case when left(gtc.finsh_time,16)<"',left(shijian3,16),'" and left(gtc.finsh_time,16)>="',left(shijian2,16),'"   then 1 else 0 end)/20*100,2)),0)');
	
	set @db1=concat(@db1,',ifnull(if(round(sum(case when left(ert.create_time,16)<"',left(shijian2,16),'" and left(ert.create_time,13)>="',left(shijian2,16),'"   then 1 else 0 end)/20*100,2)>100,100,round(sum(case when left(ert.create_time,16)<"',left(shijian3,16),'" and left(ert.create_time,16)>="',left(shijian2,16),'"   then 1 else 0 end)/20*100,2)),0)');
	
	set @db2=concat(@db2,',if(round(sum(case when left(erh.create_time,16)<"',left(shijian2,16),'" and left(erh.create_time,16)>="',left(shijian2,16),'"   then 1 else 0 end)/20*100,2)>100,100,round(sum(case when left(erh.create_time,16)<"',left(shijian3,16),'" and left(erh.create_time,16)>="',left(shijian2,16),'"   then 1 else 0 end)/20*100,2))');
	set i=i+20;
 end while;
 
 
set @db=concat(@db,'from gmms_galss_task_stock gtc left join gmms_raw_file rf on rf.file_name=gtc.engineering_id and finsh_time>"',shijian4,'"   where gtc.finsh_time>"',shijian4,'"and  rf.operator is not null group by rf.operator');
set @db1=concat(@db1,'from E_record_tempering ert  where ert.create_time>"',shijian4,'"and  ert.line is not null group by ert.line ');
set @db2=concat(@db2,'from E_record_hollow erh  where erh.create_time>"',shijian4,'"and  erh.line is not null group by erh.line ) as qqq on tt.riqi=qqq.aaa ');




set @dbmax=concat(@db,' union all ',@db1,' union all ',@db2);



prepare danp from @dbmax;
EXECUTE danp;
DEALLOCATE PREPARE danp;
 

	
	
	
	
	
	
-- 	DECLARE i int DEFAULT 336;
-- 	DECLARE shijian1 varchar(255);
-- 	DECLARE shijian2 varchar(255);
-- 	DECLARE shijian3 varchar(255);
-- 	DECLARE shijian4 varchar(255);
-- 
-- drop table if EXISTS  tabletmp_table4;
-- create temporary table tabletmp_table4 (riqi VARCHAR(255) NOT NULL);
-- insert into tabletmp_table4(riqi) values('切割一线'),('切割二线'),('切割三线'),('钢化一线'),('钢化二线'),('中空一线'),('中空二线'),('中空三线');
-- 
-- 
-- set shijian4=DATE_ADD(now(), INTERVAL -337 HOUR);
--  
-- set @db='select tt.riqi,qqq.* from tabletmp_table4 tt left join (select if(rf.operator!=1 and rf.operator!=2,"切割三线",if(rf.operator!=1,"切割二线","切割一线")) as aaa';
-- set @db1='select if(ert.line!=2,"钢化一线","钢化二线") as aaa';
-- set @db2='select if(erh.line!=1 and erh.line!=2,"中空三线",if(erh.line!=1,"中空二线","中空一线")) as aaa' ;
--  
--  
--  while i>=0 do
-- 	set shijian2= DATE_ADD(now(), INTERVAL -i HOUR);
-- 	
-- 	set shijian3=DATE_ADD(shijian2, INTERVAL -4 HOUR);
-- 	set @db=concat(@db,',ifnull(if(round(sum(case when left(gtc.finsh_time,13)<"',left(shijian2,13),'" and left(gtc.finsh_time,13)>="',left(shijian3,13),'"   then 1 else 0 end)/200*100,2)>100,100,round(sum(case when left(gtc.finsh_time,13)<"',left(shijian2,13),'" and left(gtc.finsh_time,13)>="',left(shijian3,13),'"   then 1 else 0 end)/200*100,2)),0)');
-- 	
-- 	set @db1=concat(@db1,',ifnull(if(round(sum(case when left(ert.create_time,13)<"',left(shijian2,13),'" and left(ert.create_time,13)>="',left(shijian3,13),'"   then 1 else 0 end)/200*100,2)>100,100,round(sum(case when left(ert.create_time,13)<"',left(shijian2,13),'" and left(ert.create_time,13)>="',left(shijian3,13),'"   then 1 else 0 end)/200*100,2)),0)');
-- 	
-- 	set @db2=concat(@db2,',if(round(sum(case when left(erh.create_time,13)<"',left(shijian2,13),'" and left(erh.create_time,13)>="',left(shijian3,13),'"   then 1 else 0 end)/200*100,2)>100,100,round(sum(case when left(erh.create_time,13)<"',left(shijian2,13),'" and left(erh.create_time,13)>="',left(shijian3,13),'"   then 1 else 0 end)/200*100,2))');
-- 	set i=i-4;
--  end while;
--  
--  
-- set @db=concat(@db,'from gmms_galss_task_stock gtc left join gmms_raw_file rf on rf.file_name=gtc.engineering_id and finsh_time>"',shijian4,'"   where gtc.finsh_time>"',shijian4,'"and  rf.operator is not null group by rf.operator');
-- set @db1=concat(@db1,'from E_record_tempering ert  where ert.create_time>"',shijian4,'"and  ert.line is not null group by ert.line ');
-- set @db2=concat(@db2,'from E_record_hollow erh  where erh.create_time>"',shijian4,'"and  erh.line is not null group by erh.line ) as qqq on tt.riqi=qqq.aaa ');
-- 
-- 
-- 
-- 
-- set @dbmax=concat(@db,' union all ',@db1,' union all ',@db2);
-- 
-- 
-- 
-- prepare danp from @dbmax;
-- EXECUTE danp;
-- DEALLOCATE PREPARE danp;

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for d_dapingxianshi9_copy1
-- ----------------------------
DROP PROCEDURE IF EXISTS `d_dapingxianshi9_copy1`;
delimiter ;;
CREATE PROCEDURE `d_dapingxianshi9_copy1`()
BEGIN
	#Routine body goes here...
	#右上角
	DECLARE i int DEFAULT 1440;
	DECLARE shijian1 varchar(255);
	DECLARE shijian2 varchar(255);
	DECLARE shijian3 varchar(255);
	DECLARE shijian4 varchar(255);

drop table if EXISTS  tabletmp_table4;
create temporary table tabletmp_table4 (riqi VARCHAR(255) NOT NULL);
insert into tabletmp_table4(riqi) values('切割一线'),('切割二线'),('切割三线'),('钢化一线'),('钢化二线'),('中空一线'),('中空二线'),('中空三线');


set shijian4=DATE_ADD('2023-06-19 11:10:18', INTERVAL -25 HOUR);
 
set @db='select tt.riqi,qqq.* from tabletmp_table4 tt left join (select if(rf.operator!=1 and rf.operator!=2,"切割三线",if(rf.operator!=1,"切割二线","切割一线")) as aaa';
set @db1='select if(ert.line!=2,"钢化一线","钢化二线") as aaa';
set @db2='select if(erh.line!=1 and erh.line!=2,"中空三线",if(erh.line!=1,"中空二线","中空一线")) as aaa' ;
 
 
 while i>0 do
	set shijian2= DATE_ADD('2023-06-19 11:10:18', INTERVAL -i MINUTE);
	
	set shijian3=DATE_ADD(shijian2, INTERVAL -20 MINUTE);

	set @db=concat(@db,',ifnull(if(round(sum(case when left(gtc.finsh_time,13)<"',left(shijian2,16),'" and left(gtc.finsh_time,13)>="',left(shijian3,16),'"   then 1 else 0 end)/20*100,2)>100,100,round(sum(case when left(gtc.finsh_time,13)<"',left(shijian2,16),'" and left(gtc.finsh_time,13)>="',left(shijian3,16),'"   then 1 else 0 end)/20*100,2)),0)');
	
	set @db1=concat(@db1,',ifnull(if(round(sum(case when left(ert.create_time,13)<"',left(shijian2,16),'" and left(ert.create_time,13)>="',left(shijian3,16),'"   then 1 else 0 end)/20*100,2)>100,100,round(sum(case when left(ert.create_time,13)<"',left(shijian2,16),'" and left(ert.create_time,13)>="',left(shijian3,16),'"   then 1 else 0 end)/20*100,2)),0)');
	
	set @db2=concat(@db2,',if(round(sum(case when left(erh.create_time,13)<"',left(shijian2,16),'" and left(erh.create_time,13)>="',left(shijian3,16),'"   then 1 else 0 end)/20*100,2)>100,100,round(sum(case when left(erh.create_time,13)<"',left(shijian2,16),'" and left(erh.create_time,13)>="',left(shijian3,16),'"   then 1 else 0 end)/20*100,2))');
	set i=i-20;
 end while;
 
 
set @db=concat(@db,'from gmms_galss_task_stock gtc left join gmms_raw_file rf on rf.file_name=gtc.engineering_id and finsh_time>"',shijian4,'"   where gtc.finsh_time>"',shijian4,'"and  rf.operator is not null group by rf.operator');
set @db1=concat(@db1,'from E_record_tempering ert  where ert.create_time>"',shijian4,'"and  ert.line is not null group by ert.line ');
set @db2=concat(@db2,'from E_record_hollow erh  where erh.create_time>"',shijian4,'"and  erh.line is not null group by erh.line ) as qqq on tt.riqi=qqq.aaa ');




set @dbmax=concat(@db,' union all ',@db1,' union all ',@db2);



prepare danp from @dbmax;
EXECUTE danp;
DEALLOCATE PREPARE danp;

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for Edg
-- ----------------------------
DROP PROCEDURE IF EXISTS `Edg`;
delimiter ;;
CREATE PROCEDURE `Edg`(in sendMessageHex varchar(255),in machineids BIGINT,out messagedate varchar(255))
BEGIN
	#磨边机存储过程
	#找到当前机器标志位
	#查询机器逻辑表中对应的逻辑进行对应的步骤
	DECLARE machinedisposeids int DEFAULT 0;
	DECLARE dates varchar(255);
	DECLARE findplc varchar(255);
	DECLARE findmes varchar(255);
	DECLARE taskplc varchar(255);
	DECLARE taskmes varchar(255);
	DECLARE posunplc varchar(255);
	DECLARE posunmes varchar(255);
	DECLARE speed1s int;
	DECLARE speed2s int;
	DECLARE messagedates varchar(255) DEFAULT '';
	set messagedate=messagedates;
	
	select machine_type_id into @mtid from gmms_machine where id=machineids;
	
	select machine_type_id,work_id into @mtid,@work_groups from gmms_machine where id=machineids;
	
	
	
	select mf.flag into @完成标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='完成字';
	
	select mf.flag into @请求标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='请求字';
	
	select mf.flag into @发送标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='发送字';
	
	select mf.flag into @完成确认标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='完成确认字';
	
	select mf.flag into @破损标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='破损字';
	
	select mf.flag into @破损确认标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='破损确认字';
	
	set findplc = substring(sendMessageHex,(cast(@完成标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskplc = substring(sendMessageHex,(cast(@请求标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskmes = substring(sendMessageHex,(cast(@发送标志位 as UNSIGNED INTEGER)*4)+19,4);
	set findmes = substring(sendMessageHex,(cast(@完成确认标志位 as UNSIGNED INTEGER)*4)+19,4);
	set posunplc = substring(sendMessageHex,(cast(@破损标志位 as UNSIGNED INTEGER)*4)+19,4);
	set posunmes = substring(sendMessageHex,(cast(@破损确认标志位 as UNSIGNED INTEGER)*4)+19,4);
	set speed1s = conv(substring(sendMessageHex,(cast(45 as UNSIGNED INTEGER)*4)+19,4),16,10);
	set speed2s = conv(substring(sendMessageHex,(cast(48 as UNSIGNED INTEGER)*4)+19,4),16,10);
	select taskplc;
	select taskmes;
	select findplc;
	select findmes;
	select posunplc;
	select posunmes;
	select @mtid;
	
	if machineids=28 then
		update gmms_edg_speed set speed1=speed1s,speed2=speed2s where groups=@work_groups;
	elseif machineids=6 or machineids=29 then
		update gmms_edg_speed set speed1=speed1s where groups=@work_groups;
 elseif machineids=30 or machineids=19 then
		update gmms_edg_speed set speed2=speed1s where groups=@work_groups;
	end if;
	
	#完成逻辑查询
	select count(*),md.id into @完成数量,@完成逻辑编号 from gmms_machine_dispose md join gmms_machine_type mt on md.machine_type_id=mt.id where md.machine_type_id=@mtid  and md.plc_font_type='完成字' and md.mes_font_type='完成确认字' and md.plc_font_value=findplc and md.mes_font_value=findmes;
	
	#请求逻辑查询
	select count(*),md.id into @请求数量,@请求逻辑编号 from gmms_machine_dispose md join gmms_machine_type mt on md.machine_type_id=mt.id where md.machine_type_id=@mtid  and md.plc_font_type='请求字' and md.mes_font_type='发送字' and md.plc_font_value=taskplc and md.mes_font_value=taskmes;
	
	#破损逻辑查询
	select count(*),md.id into @破损数量,@破损逻辑编号 from gmms_machine_dispose md join gmms_machine_type mt on md.machine_type_id=mt.id where md.machine_type_id=@mtid  and md.plc_font_type='破损字' and md.mes_font_type='破损确认字' and md.plc_font_value=posunplc and md.mes_font_value=posunmes;
	
	#判断逻辑是否存在
	select @完成数量;
	select @请求数量;
	select @完成逻辑编号;
	select machineids;
	if @完成数量=1 or @请求数量=1 or @破损数量=1 then
		if machineids = 6 or machineids = 29 or machineids = 28 then
			if @请求逻辑编号!=0 then
				set machinedisposeids=@请求逻辑编号;
			elseif @完成逻辑编号!=0 then
				set machinedisposeids=@完成逻辑编号;
			elseif @破损逻辑编号!=0 then
				set machinedisposeids=@破损逻辑编号;
			end if;
		elseif machineids = 19 or machineids = 30  then
			if (@完成逻辑编号!=0 && machineids=19) or (@完成逻辑编号!=0 && machineids=30)  then
				set machinedisposeids=@完成逻辑编号;
			
-- 		#判断破损逻辑是否为空
 		elseif @破损逻辑编号!=0 then
 			set machinedisposeids=@破损逻辑编号;
			
		#判断请求逻辑是否为空
			elseif @请求逻辑编号!=0 then
				set machinedisposeids=@请求逻辑编号;
			
			end if;
		end if;
	select machinedisposeids;
		
		#查询当前执行逻辑进行判断
		select logic,return_value into @ExecutionLogic,@returnvalue from gmms_machine_dispose where id=machinedisposeids;
		select @ExecutionLogic;
		if @ExecutionLogic='任务完成' then
				call 	EdgFinsh(sendMessageHex,machineids,machinedisposeids,1,@messagedate);
				set messagedate=@messagedate;
 		elseif @ExecutionLogic='完成破损' then
				call EdgFinsh(sendMessageHex,machineids,machinedisposeids,2,@messagedate);
				set messagedate=@messagedate;
 		elseif @ExecutionLogic='完成清空' then		
				if machineids=28 then
					call message(@returnvalue,'0014',@date);
					set messagedate=@date;
				else
					call message(@returnvalue,'1784',@date);
					set messagedate=@date;
				end if;
				
		elseif @ExecutionLogic='破损清空' then		
				if machineids=28 then
					call message(@returnvalue,'0016',@date);
					set messagedate=@date;
				else
					call message(@returnvalue,'1786',@date);
					set messagedate=@date;
				end if;
		elseif @ExecutionLogic='任务请求' then
			if machineids=6 or  machineids=29 or  machineids=28 then
				call EdgSend(machineids,machinedisposeids,@messagedate);
				set messagedate=@messagedate;
			end if;
 		elseif @ExecutionLogic='任务清空' then
				if machineids=28 then
					set dates=CONCAT(@returnvalue,"0000000000000000");
					call message(dates,'000a',@date);
					set messagedate=@date;
				else
					set dates=CONCAT(@returnvalue,"0000000000000000");
					call message(dates,'177a',@date);
					set messagedate=@date;
				end if;
				
		end if;
	end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for EdgFinsh
-- ----------------------------
DROP PROCEDURE IF EXISTS `EdgFinsh`;
delimiter ;;
CREATE PROCEDURE `EdgFinsh`(in sendMessageHex varchar(255),in machineids BIGINT,in machinedisposeids BIGINT,in flag int,out messagedate varchar(255))
BEGIN
	#磨边机完成存储过程
	#任务完成以及任务破损，flag=1为完成，flag=2为破损
	DECLARE rank3 int ;
	DECLARE rank4 varchar(255);
	DECLARE dates varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	set messagedate=messagedates;
	
	select machine_type_id,work_id into @mtid,@work_groups from gmms_machine where id=machineids;
	
	select file_name into @engineering_id from gmms_raw_file where operator=@work_groups and state='已领取';
	
	select return_value into @returnvalue from gmms_machine_dispose where id=machinedisposeids;
	
	
	select mf.flag into @完成标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='完成字';
	
	select mf.flag into @破损标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='破损字';
	
	#完成
if machineids=6 or machineids=29 then
select flag;
	if flag=1 then 
		call message('0001','1784',@date);
		set messagedate=@date;
		
	#破损
	elseif flag=2 then
		set rank4 = substring(sendMessageHex,(cast(@破损标志位+1 as UNSIGNED INTEGER)*4)+19,4);
		select rank4;
		set rank3=conv(rank4,16,10);
		select rank3;
		
		update gmms_galss_task set work_state='破损' where galss_id=rank3 and work_state='正在工作' and engineering_id=@engineering_id;
		
		call message(@returnvalue,'1786',@date);
		set messagedate=@date;
	end if;
		
elseif machineids=19 or machineids=30 then
	if flag=1 then 
-- 	 if machineids=19 then
-- 		insert into aaaa set ddd="111222";
-- 		end if;
		set rank4 = substring(sendMessageHex,(cast(@完成标志位+1 as UNSIGNED INTEGER)*4)+19,4);
		
		set rank3=conv(rank4,16,10);
		
		select width,height into @width,@height from gmms_galss_task where galss_id=rank3 and work_state='正在工作' and engineering_id=@engineering_id;
		insert into gmms_edg_perimeter set width=@width,height=@height,start_time=NOW(),groups=@work_groups;
		
		update gmms_galss_task set work_state='待复合',work_procedure='复合' where galss_id=rank3 and work_state='正在工作' and engineering_id=@engineering_id;

		call message(@returnvalue,'1784',@date);
		set messagedate=@date;
		
	#破损
	elseif flag=2 then
	
		set rank4 = substring(sendMessageHex,(cast(@破损标志位+1 as UNSIGNED INTEGER)*4)+19,4);
		select rank4;
		set rank3=conv(rank4,16,10);
		
		update gmms_galss_task set work_state='破损' where galss_id=rank3 and work_state='正在工作' and work_procedure='磨边' and engineering_id=@engineering_id;
		
		call message(@returnvalue,'1786',@date);
		set messagedate=@date;
	
	end if;
elseif machineids=28 then
	if flag=1 then 
		
		set rank4 = substring(sendMessageHex,(cast(@完成标志位+1 as UNSIGNED INTEGER)*4)+19,4);
		
		set rank3=conv(rank4,16,10);
		
		select width,height into @width,@height from gmms_galss_task where galss_id=rank3 and work_state='正在工作' and engineering_id=@engineering_id;
		insert into gmms_edg_perimeter set width=@width,height=@height,start_time=NOW(),groups=@work_groups;
		
		update gmms_galss_task set work_state='待复合',work_procedure='复合' where galss_id=rank3 and work_state='正在工作' and engineering_id=@engineering_id;

		call message(@returnvalue,'0014',@date);
		set messagedate=@date;
		
	#破损
	elseif flag=2 then
	
		set rank4 = substring(sendMessageHex,(cast(@破损标志位+1 as UNSIGNED INTEGER)*4)+19,4);
		select rank4;
		set rank3=conv(rank4,16,10);
		
		update gmms_galss_task set work_state='破损' where galss_id=rank3 and work_state='正在工作' and work_procedure='磨边' and engineering_id=@engineering_id;
		
		call message(@returnvalue,'0016',@date);
		set messagedate=@date;
	
	end if;
end if;
	
	
	
	
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for EdgSend
-- ----------------------------
DROP PROCEDURE IF EXISTS `EdgSend`;
delimiter ;;
CREATE PROCEDURE `EdgSend`(in machineids BIGINT,in machinedisposeids BIGINT,out messagedate varchar(255))
BEGIN
	#磨边机请求存储过程
	#找到当前的一条玻璃信息，把需要的数据发送过去
	#发送数据：序号，宽度，高度，厚度，类型，磨边速度，磨边大小
	DECLARE width1 int;
	DECLARE heigth1 int;
	DECLARE rank1 int;
	DECLARE thickness1 int;
	DECLARE chamfer_size1 int;
	DECLARE galss_type1 int;
	DECLARE fine_throwing1 int;
	DECLARE edg_speed11 int;
	DECLARE edg_speed21 int;
	DECLARE dates varchar(255);
	DECLARE booldate varchar(255);
	DECLARE booldate1 varchar(255);
	DECLARE booldate2 varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	
	DECLARE jiID BIGINT DEFAULT 0;
	DECLARE thistype varchar(255);
	DECLARE lasttype varchar(255) DEFAULT '';
	DECLARE lastRecordsid BIGINT DEFAULT 0;
	DECLARE lastRecordsidW BIGINT DEFAULT 0;
	DECLARE lastRecordsidH BIGINT DEFAULT 0;
	DECLARE lastW int DEFAULT 0;
	DECLARE lastH int DEFAULT 0;
	
	
	set messagedate=messagedates;
	
	select machine_type_id,work_id into @mtid,@work_groups from gmms_machine where id=machineids;
	
	select file_name into @engineering_id from gmms_raw_file where operator=@work_groups and state='已领取';
	
	select return_value into @returnvalue from gmms_machine_dispose where id=machinedisposeids;
	
	select id,galss_id,edg_width,edg_height,thickness,chamfer_size,galss_type,fine_throwing,edg_speed1,edg_speed2 into jiID,@rank,@width,@heigth,@thickness,@chamfer_size,@galss_type,@fine_throwing,@edg_speed1,@edg_speed2 from gmms_galss_task where work_state='等待中' and work_procedure='磨边' and engineering_id=@engineering_id order by out_rank limit 1;
	
	
	select @rank;
	if @rank is not null then
	
 	update gmms_galss_task set work_state='正在工作' where galss_id=@rank and engineering_id=@engineering_id;
	#update gmms_galss_task set work_state='待复合',work_procedure='复合' where galss_id=@rank;
	  
	set thistype=CONCAT(IFNULL(@width,''),'*',IFNULL(@heigth,''));
	select SizeText,Recordsid,Width,Height,RecordsidW,RecordsidH into lasttype,lastRecordsid,lastW,lastH,lastRecordsidW,lastRecordsidH from Y_Edg_Statistics 
	where Line=@work_groups order by Recordsid desc limit 1;
	if lasttype is null then
		set lastRecordsid=1;
		set lastRecordsidW=1;
		set lastRecordsidH=1;
	else
		if lasttype!=thistype then
			set lastRecordsid=lastRecordsid+1;
		end if;
		if lastW!=@width then
			set lastRecordsidW=lastRecordsidW+1;
		end if;
		if lastH!=@heigth then
			set lastRecordsidH=lastRecordsidH+1;
		end if;
	end if;
	
	
	
	set rank1=cast(@rank as UNSIGNED INTEGER);
	select rank1;
	set width1=cast(@width as UNSIGNED INTEGER)*10;
	set heigth1=cast(@heigth as UNSIGNED INTEGER)*10;
	set thickness1=cast(@thickness as UNSIGNED INTEGER)*10;
-- 	set chamfer_size1=cast(@chamfer_size as UNSIGNED INTEGER);
-- 	set galss_type1=@galss_type;
-- 	set fine_throwing1=@fine_throwing;
-- 	set edg_speed11=cast(@edg_speed1 as UNSIGNED INTEGER);
-- 	set edg_speed21=cast(@edg_speed2 as UNSIGNED INTEGER);
-- 	
-- 	set booldate1=CONCAT(galss_type1,'11',fine_throwing1,'0000');
-- 	set booldate2=CONCAT("00000000");
	
	#转换进制
	call intTo2ByteHex10_16_4(rank1,@ranks);
	call intTo2ByteHex10_16_4(width1,@widths);
	call intTo2ByteHex10_16_4(heigth1,@heigths);
	call intTo2ByteHex10_16_4(thickness1,@thicknesss);
	select @ranks;
	select @widths;
	select @heigths;
	select @thicknesss;
-- 	call intTo2ByteHex2_16(booldate1,@booldate1);
-- 	call intTo2ByteHex2_16(booldate2,@booldate2);
-- 	call intTo2ByteHex10_16_4(chamfer_size1,@chamfer_sizes);
-- 	call intTo2ByteHex10_16_4(edg_speed11,@edg_speed1s);
-- 	call intTo2ByteHex10_16_4(edg_speed21,@edg_speed2s);
-- 	
-- 	set booldate=CONCAT(@booldate2,@booldate1);


	
	
	INSERT INTO `gmms`.`Y_Edg_Statistics`(`Glassid`, `SendTime`, `Width`, `Height`, `Thickess`, `SizeText`, `SendText`, `Recordsid`, `Line`,RecordsidW,RecordsidH) 
	select id,NOW(),edg_width,edg_height,thickness,CONCAT(IFNULL(edg_width,''),'*',IFNULL(edg_height,'')),CONCAT(@returnvalue,@ranks,@heigths,@widths,@thicknesss),lastRecordsid,@work_groups,lastRecordsidW,lastRecordsidH from gmms_galss_task 
	where id=jiID;

	if machineids=28 then
		set dates=CONCAT(@returnvalue,@ranks,@heigths,@widths,@thicknesss);
		select dates;
		call message(dates,'000a',@date); 
		set messagedate=@date;
	elseif machineids=29 then
		set dates=CONCAT(@returnvalue,@ranks,@heigths,@widths,@thicknesss);
		select dates;
		call message(dates,'177a',@date); 
		set messagedate=@date;
	else
		set dates=CONCAT(@returnvalue,@ranks,@widths,@heigths,@thicknesss);
		select dates;
		call message(dates,'177a',@date); 
		set messagedate=@date;
	end if;
	end if;
	
	
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for GalssTask
-- ----------------------------
DROP PROCEDURE IF EXISTS `GalssTask`;
delimiter ;;
CREATE PROCEDURE `GalssTask`(in rawFileid BIGINT,in line BIGINT,out dates int)
BEGIN
	#Routine body goes here...
	declare t_err int default 0;
  DECLARE t_step int;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION,SQLWARNING,NOT FOUND set t_err=1;
	
	set dates=0;
	set t_step=0;
	set t_err=0;
	set @file_name=null;
	select rawFileid;
	select id,file_name,state into @id,@file_name,@z_state from gmms_raw_file where id=rawFileid;
	select @z_state;
	if @z_state<>'已下发' and @z_state<>'中断' then
			set t_err=2;
	elseif @z_state='中断' then
			set t_err=10;
  else 
		START TRANSACTION;
	end if;
	
	
	if t_err=0 then 
	select @file_name;
		set t_step=1;

		insert into gmms_pattern_task (id,rank,raw_type,raw_height,raw_width,raw_thickness,work_state,color,content,task_id,raw_id,flag,groups) select ptf.id,ptf.rank,ptf.raw_type,ptf.raw_height,ptf.raw_width,ptf.raw_thickness,ptf.work_state,ptf.color,ptf.content,ptf.task_id,rf.id,ptf.flag,rf.operator from gmms_pattern_task_buffer ptf join gmms_raw_file rf on ptf.engineering_id=rf.file_name where engineering_id=@file_name;

		
	elseif t_err=10 then
		
		select count(*) into @count1 from gmms_galss_task where engineering_id=@file_name;
		select count(*) into @count2 from gmms_pattern_task where raw_id=@id and groups=line;
		select @count1,@count2;
		if @count1<>0 or @count2<>0 then
			delete from gmms_galss_task where engineering_id=@file_name;
			delete from gmms_pattern_task where raw_id=@id and groups=line;
		end if;
		
		insert into gmms_galss_task select * from gmms_galss_task_interrupt where engineering_id=@file_name;
		select count(*) from gmms_pattern_task_interrupt where raw_id=@id and groups=line;
		insert into gmms_pattern_task select * from gmms_pattern_task_interrupt where raw_id=@id and groups=line;
		set dates=100;
	end if;

	
	if t_err=0 then 
	set t_step=2;
 	#insert into gmms_galss_task select * from gmms_galss_task_buffer where engineering_id=@file_name;
	select @file_name;
	insert into gmms_galss_task (id,rank,type_id,galss_id,pattern_id,width,height,thickness,color,edg_width,edg_height,chamfer_size,galss_type,edg_speed1,edg_speed2,fine_throwing,work_state,work_procedure,slicecage_rank,drop_frame,slicecage_flag,out_rank,start_time,finsh_time,stop_position,processcard_id,engineering_id,broken_state,stop_rank,work_procedure_number,gc_number,gc_width,gc_height,gc_stockid,marking_flag) select id,rank,type_id,MOD(galss_id,10000),pattern_id,width,height,thickness,color,edg_width,edg_height,chamfer_size,galss_type,edg_speed1,edg_speed2,fine_throwing,work_state,work_procedure,slicecage_rank,drop_frame,slicecage_flag,out_rank,start_time,finsh_time,stop_position,processcard_id,engineering_id,broken_state,stop_rank,work_procedure_number,gc_number,gc_width,gc_height,gc_stockid,marking_flag from gmms_galss_task_buffer where engineering_id=@file_name;
	
	select t_err,t_step,0;
	call output_create(@file_name);
	select t_err,t_step,1;
	end if;
	if t_err=0 then 
				set t_step=3;
		   call dongzuo_update_marking_flag(@file_name,line);
	end if;
	if t_err=0 then 	
			set t_step=4;
	  	 update gmms_raw_file set `state`='已领取',operator=line,create_time=now() where id= rawFileid;			 
	end if;
	if t_err=0 then 			
			set dates=100;
			COMMIT; 
	end if;
	
	if t_err=1 then
	   ROLLBACK;
	end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for GalssTaskAdd
-- ----------------------------
DROP PROCEDURE IF EXISTS `GalssTaskAdd`;
delimiter ;;
CREATE PROCEDURE `GalssTaskAdd`(in width1 double,in height1 double,in thickness1 double,in pices int,in groups1 BIGINT,in dropframe varchar(255),in grinding double,in processcardid varchar(255),in engineeringid varchar(255),in marking varchar(255),out dates int)
BEGIN
	#Routine body goes here...   1622   978  6   3   3   35 A2 1.4 NG2023920933    MD2023920933
	DECLARE s int DEFAULT 1;
	DECLARE begins int DEFAULT 0;
	select count(*),id into @count,@id from gmms_raw_file where file_name=engineeringid and state='已领取';
	select count(*) into @count2 from gmms_raw_file where operator=groups1 and state='已领取';
	select number into @markings from gmms_dongzuo_table where note=marking;
	
	set begins=(groups1-1)*5000;
	set s=begins+1;
	if @count>0 then
		select id into @id1 from gmms_pattern_task where groups=groups1 limit 0,1;
		select max(type_id),max(id) into @type_id,@maxid from gmms_galss_task where engineering_id=engineeringid;
		set s=@maxid+1;
		while s<=(pices+@maxid) do
			insert into gmms_galss_task (id,rank,type_id,galss_id,pattern_id,width,height,thickness,color,edg_width,edg_height,galss_type,work_state,work_procedure,drop_frame,start_time,processcard_id,engineering_id,work_procedure_number,gc_height,gc_width,gc_number,fine_throwing,gc_stockid,marking_flag) values (s,0,@type_id+1,s,@id1,width1+(grinding/2),height1+(grinding/2),thickness1,'白玻',width1,height1,1,'待识别','测量',dropframe,now(),processcardid,engineeringid,100,0,0,0,0,0,@markings);
		set s=s+1;
		end while;
		set dates=1;
	else
		if @count2=0 then
			insert into gmms_raw_file (file_name,state,operator,create_time) values (engineeringid,'已领取',groups1,NOW());
			select id into @ids from gmms_raw_file where file_name=engineeringid and state='已领取';
			select max(id) into @maxid from gmms_pattern_task;
			insert into gmms_pattern_task (id,rank,raw_type,raw_width,raw_height,raw_thickness,work_state,color,task_id,raw_id,finsh_time,flag,groups) values (groups1,0,0,0,0,0,'已完成','0',0,@ids,NOW(),0,groups1);
			
			select id into @id1 from gmms_pattern_task where groups=groups1 limit 0,1;
			while s<=(pices+begins) do
				insert into gmms_galss_task (id,rank,type_id,galss_id,pattern_id,width,height,thickness,color,edg_width,edg_height,galss_type,work_state,work_procedure,drop_frame,start_time,processcard_id,engineering_id,work_procedure_number,gc_height,gc_width,gc_number,fine_throwing,gc_stockid,marking_flag) values (s,0,1,s,@id1,width1+(grinding/2),height1+(grinding/2),thickness1,'白玻',width1,height1,1,'待识别','测量',dropframe,now(),processcardid,engineeringid,100,0,0,0,0,0,@markings);
			set s=s+1;
			end while;
			set dates=1;
		end if;
	end if;
	
	

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for GalssTaskHollow
-- ----------------------------
DROP PROCEDURE IF EXISTS `GalssTaskHollow`;
delimiter ;;
CREATE PROCEDURE `GalssTaskHollow`(in rawFileid BIGINT,in line BIGINT,out dates int)
BEGIN
	#Routine body goes here...
	DECLARE g int DEFAULT 0;
	DECLARE counts int DEFAULT 1;
	
select file_name into @liuchengka from gmms_raw_file_2 where id=rawFileid;

select count(*) into counts from gmms_raw_file_2 where id=rawFileid and state='已领取';

if counts=0 then


select count(count1) into @count2 from (select count(*) as count1 from gmms_galss_task_shelf where  processcard_id like CONCAT('%',@liuchengka,'%') group by processcard_id ) aa;
select @count2;
while g<@count2 do
	select g;
	select processcard_id into @processcard_id from gmms_galss_task_shelf where  processcard_id like CONCAT('%',@liuchengka,'%')  group by  processcard_id  limit g,1;
	select @processcard_id;
	select dr.id,sr.number,dr.stop_rank into @id,@number,@stop_rank from gmms_shelf_rack_2 sr join gmms_drop_frame_4 dr on sr.orientation=dr.orientation where dr.groups=line and flag=0 and dr.state='空闲' and sr.processcard_id=@processcard_id order by dr.id limit 0,1;
	select @number;
	if @number is not null then
		select count(*) into @count3 from gmms_shelf_task_2 where shelf_rack_id=@number and load_rack=@stop_rank and task_state!='已下车';
		if @count3=0 then
			insert into gmms_shelf_task_2 (task_type,task_state,shelf_rack_id,load_rack,groups,start_time,line) values('从仓位到中空位','等待上车',@number,@stop_rank,1,now(),1);
		end if;
	end if;
	set g=g+1;
end while;
set dates=1;
update gmms_raw_file_2 set state='已领取',operator=line where id=rawFileid;

end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for GalssTaskHollowJia
-- ----------------------------
DROP PROCEDURE IF EXISTS `GalssTaskHollowJia`;
delimiter ;;
CREATE PROCEDURE `GalssTaskHollowJia`(in rawFileid BIGINT,out dates int)
BEGIN
	#Routine body goes here...
	DECLARE g int DEFAULT 0;
	
select file_name into @liuchengka from gmms_raw_file_2 where id=rawFileid;

select count(*) into @count from gmms_raw_file_2 where id=rawFileid and state='已领取';
select @count;
if @count=0 then

select liuchengka into @liuchengka1 from gmms_liuchengka_copy2 where parent_liuchengka=@liuchengka limit 0,1;

select liuchengka into @liuchengka2 from gmms_liuchengka_copy2 where parent_liuchengka=@liuchengka limit 1,1;

select number into @number1 from gmms_shelf_rack_2 where processcard_id=@liuchengka1;
select number into @number2 from gmms_shelf_rack_2 where processcard_id=@liuchengka2;
select @liuchengka;
select @number1;
	if @number1 is not null and @number2 is not null then
-- 			insert into gmms_shelf_task_2 (task_type,task_state,shelf_rack_id,load_rack,groups,start_time) values('从仓位到吊装位','等待中',@number1,99,0,now());
-- 			insert into gmms_shelf_task_2 (task_type,task_state,shelf_rack_id,load_rack,groups,start_time) values('从吊装位到仓位','等待中',@number1,99,0,now());
-- 			insert into gmms_shelf_task_2 (task_type,task_state,shelf_rack_id,load_rack,groups,start_time) values('从仓位到吊装位','等待中',@number2,99,0,now());
-- 			insert into gmms_shelf_task_2 (task_type,task_state,shelf_rack_id,load_rack,groups,start_time) values('从吊装位到仓位','等待中',@number2,99,0,now());
			update gmms_raw_file_2 set state='已领取' where id=rawFileid;
	end if;

set dates=1;

end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for GalssTaskShelfAdd
-- ----------------------------
DROP PROCEDURE IF EXISTS `GalssTaskShelfAdd`;
delimiter ;;
CREATE PROCEDURE `GalssTaskShelfAdd`(in processcardid VARCHAR(254),out dates int)
BEGIN
	#Routine body goes here...

	insert into gmms_galss_task_shelf select * from gmms_galss_task_details where processcard_id=processcardid;
	update gmms_galss_task_shelf set work_procedure='钢化后下片' where processcard_id=processcardid;

set dates=1;


END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for GalssTaskStock
-- ----------------------------
DROP PROCEDURE IF EXISTS `GalssTaskStock`;
delimiter ;;
CREATE PROCEDURE `GalssTaskStock`(in processcardid VARCHAR(255),in groups1 BIGINT,out dates int)
BEGIN
	
	
	#Routine body goes here...
	#update aaaa set data1= now(),data2=null where ddd='1';
--  	insert into gmms_galss_task_2 (id,rank,type_id,galss_id,pattern_id,width,height,thickness,color,edg_width,edg_height,chamfer_size,	galss_type,edg_speed1,edg_speed2,fine_throwing,work_state,work_procedure,slicecage_rank,drop_frame,slicecage_flag,out_rank,start_time,finsh_time,stop_position,processcard_id,engineering_id,broken_state,stop_rank,work_procedure_number,gc_number,gc_width,gc_height,gc_stockid,marking_flag) select * from gmms_galss_task_stock where processcard_id=processcardid;
-- 	
-- 	update gmms_galss_task_2 set groups=groups1,work_state='等待中',work_procedure='钢化上片',galss_id=0 where processcard_id=processcardid;
-- set dates=1;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for getId
-- ----------------------------
DROP PROCEDURE IF EXISTS `getId`;
delimiter ;;
CREATE PROCEDURE `getId`(in str text,in idx int,out backid varchar(30))
BEGIN
	#Routine body goes here...
	 set @str1=SUBSTRING_INDEX(str,',',idx+1);
	 set	@str2=SUBSTRING_INDEX(str,',',idx);
	  if length(@str1)=length(@str2) then 
		 set	backid=null;
		else
		  set backid=SUBSTRING_INDEX(@str1,',',-1);
		end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for get_baobiao_chanliang
-- ----------------------------
DROP PROCEDURE IF EXISTS `get_baobiao_chanliang`;
delimiter ;;
CREATE PROCEDURE `get_baobiao_chanliang`()
BEGIN
	#Routine body goes here...

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for get_device_status
-- ----------------------------
DROP PROCEDURE IF EXISTS `get_device_status`;
delimiter ;;
CREATE PROCEDURE `get_device_status`(in  dev_id int,in flag int,out _work_name varchar(50),out _work_status varchar(50),out _islast bit)
BEGIN
	#dev_id      设备的ID号
  #flag					获取的状态标志 0  获取上一个工艺段的完成状态。  1 获取当前工艺段的开始状态    2  获取当前工艺段的完成状态
	#_work_name    获取到的工序名
	#_work_status   获取到的状态名
	#_islast        如果当前状态是最后一个工艺段的完成状态，这里会返回true 1

	#Routine body goes here...
		DECLARE idx int default(null);
		DECLARE _name varchar(50) default(null);
		set _islast=false;
		set _work_name=null;
		set _work_status=null;
		if flag= 1 then 
	
				SELECT work_name,begin_status into _work_name,_work_status from gmms_device where id=dev_id;
			end if;
			
			if flag= 2 then 
	
				SELECT work_name,end_status,is_last into _work_name,_work_status,_islast from gmms_device where id=dev_id;
			end if;
			
		if flag=0 then 
			SELECT device_index,line_name into idx,_name from gmms_device where id=dev_id;
			if idx>1 then 
					SELECT work_name,end_status into _work_name,_work_status from gmms_device where device_index=idx-1 and line_name=_name;
			end if;
			
			if idx=0 then 	
							SELECT work_name,begin_status into _work_name,_work_status from gmms_device where id=dev_id;
			end if;
		
		end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for get_next_tempere_number
-- ----------------------------
DROP PROCEDURE IF EXISTS `get_next_tempere_number`;
delimiter ;;
CREATE PROCEDURE `get_next_tempere_number`(in _work_id int,out _number int)
BEGIN
	#Routine body goes here...
   SET _number=0;
	 SELECT layout_number into _number from gmms_tempere_layout where work_id=_work_id order by layout_number desc LIMIT 1;
	 set  _number=_number+1;
END
;;
delimiter ;

-- ----------------------------
-- Function structure for get_str
-- ----------------------------
DROP FUNCTION IF EXISTS `get_str`;
delimiter ;;
CREATE FUNCTION `get_str`(_data text,_idx int)
 RETURNS varchar(200) CHARSET utf8
BEGIN
	#Routine body goes here...
  declare pos int;
	declare pos1 int;
	set pos=1;
	
	while _idx>-1 do 
		set pos1=LOCATE(',',_data,pos);
		if pos1>0 then 
			if _idx=0  then
				RETURN SUBSTR(_data FROM pos FOR (pos1-pos));
			else
				set pos=pos1+1;
			end if;
		else
			if _idx=0 then
				RETURN SUBSTR(_data FROM pos FOR (LENGTH(_data)-pos+1));
			else
			  return null;
			end if;
		end if;
			set _idx=_idx-1;	
	end while;
	
	RETURN null;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for get_view_ping_chanliang
-- ----------------------------
DROP PROCEDURE IF EXISTS `get_view_ping_chanliang`;
delimiter ;;
CREATE PROCEDURE `get_view_ping_chanliang`(seg varchar(50),	
_line int,			
_from datetime, 
_to  datetime,   
out result int,
out msg varchar(50))
BEGIN
set result=0;
set @a=TIMESTAMPDIFF(MINUTE,_from,_to);
	#Routine body goes here...
select a.work_name,(case when b.b is null then '0/0' else b.b end) as bb ,
(case when b.d is null then '0/0' else b.d end) as dd,
(case when b.c is null then 0 else b.c end) as cc
  from 
(SELECT work_name from gmms_glass_log_config where segment=seg group by work_name order by work_number) 
a
LEFT JOIN
(
SELECT work_name,CONVERT(concat(count(*),'/', round(sum(area),2)),CHAR) as b, '0/0' as d, round(count(*)/TIMESTAMPDIFF(MINUTE,min(end_time),max(end_time)),1) as  c   FROM gmms_glass_log where end_time>=_from and end_time<=_to and `status`=100 and segment_name=seg  and line=_line and parent_id is not null and freeze=false group by work_name 
) b on a.work_name=b.work_name;
set result=200;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for get_view_ping_kucun
-- ----------------------------
DROP PROCEDURE IF EXISTS `get_view_ping_kucun`;
delimiter ;;
CREATE PROCEDURE `get_view_ping_kucun`(in flag int,out result int,out msg varchar(50))
BEGIN
	#Routine body goes here...
	if flag=0 or flag=1 then
				select  b.number,a.color,a.raw_width,a.raw_height,a.left_pieces,CAST(a.create_time as date) as create_time from gmms_raw_package a RIGHT join gmms_shelf_rack b on b.raw_package_id1=a.id order by convert(b.number,DECIMAL(10,0));
	end if;
	

if flag=0 or  flag=2 then 
					SELECT a.liuchengka,b.color
	,count(a.liuchengka) as count 
	from gmms_liuchengka a inner join gmms_galss_task_details b on a.liuchengka=b.processcard_id 
	where a.current_work_position='切磨线'  and a.current_status='下架'
	group by a.liuchengka,b.color;
end if;


if flag=0 or flag=3 then 
					SELECT a.liuchengka,b.color
	,count(a.liuchengka) as count 
	from gmms_liuchengka a inner join gmms_galss_task_details b on a.liuchengka=b.processcard_id 
	where a.current_work_position='钢化后'  and a.current_status='下架'
	group by a.liuchengka,b.color;
end if;
end
;;
delimiter ;

-- ----------------------------
-- Procedure structure for GlassTask_furnace
-- ----------------------------
DROP PROCEDURE IF EXISTS `GlassTask_furnace`;
delimiter ;;
CREATE PROCEDURE `GlassTask_furnace`(in RId BIGINT,in line int,out back_Num int,out back_Msg varchar(255))
BEGIN
	#逻辑流程：
	#1.查询规则
	#2.更新规则

	#生成钢化前玻璃上片规则

	Declare w varchar(255);#最大宽
	Declare l int;#最大高
	Declare x int;#x轴间隔
	Declare y int;#y轴间隔
	Declare r int;#记录受影响行数

	select width into w from gmms_furnace_config where Id = RId;
	select length into l from gmms_furnace_config where Id = RId;
	select xinterval into x from gmms_furnace_config where Id = RId;
	select yinterval into y from gmms_furnace_config where Id = RId;
	set back_num = 0;
	set back_Msg = "查询失败";
	
	update gmms_furnace_config set line = null where line = line;
	update gmms_furnace_config set line = line where Id = RId;
	set back_num = 2;
	set back_Msg = "规则表更新失败";
	
	update gmms_furnace set width = w,length = l,xinterval = x,yinterval = y where Id = line;
	set r =  FOUND_ROWS();
	if r=1 
		then
		set back_num = 1;
		set back_Msg = "成功";
	else 
		set back_num = 3;
		set back_Msg = "更新失败";
	end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for GlassTask_ganghuaqian
-- ----------------------------
DROP PROCEDURE IF EXISTS `GlassTask_ganghuaqian`;
delimiter ;;
CREATE PROCEDURE `GlassTask_ganghuaqian`(in pro_line BIGINT,out back_Num int,out back_Msg varchar(255))
BEGIN
	#生成钢化前玻璃上片任务
	Declare lck_num varchar(255);#记录流程卡号
	DECLARE sum_count int default 0;#记录累加的记录数
	DECLARE mlck_sum int default 0;#某流程卡数据总数
	DECLARE drop_num int default 0;#有流程卡的架位数量
	declare ks_time DateTime default now();#存储过程调用时间
	#定义游标
	Declare lck_cursor cursor for select processcard_id from gmms_drop_frame_2 where groups = pro_line and processcard_id is not null and processcard_id <>'' order by Id desc;
	
	select count(*) into drop_num from gmms_drop_frame_2 where groups = pro_line and processcard_id is not null and processcard_id <>'' order by Id desc;
	select drop_num;
	set back_Num = 0;#记录
	#打开游标
	Open lck_cursor;
	set back_Num = 6;#记录
	#重复
	REPEAT
	set back_Num = 7;#记录
		#使用游标---从游标中获取数据
		FETCH lck_cursor INTO lck_num;
		
		Update gmms_liuchengka set Occupied = 1000,isgang='已钢化' where liuchengka = lck_num;
		
		set back_Num = 2;
		#流程卡玻璃任务总数
		Select count(*) into mlck_sum from gmms_galss_task_2 where processcard_id = lck_num;
		if mlck_sum =0
			then
				SET sum_count = sum_count + 1;
				set back_Num = 3;
				#插入玻璃任务数据
				insert into gmms_galss_task_2 (id,rank,type_id,galss_id,pattern_id,width,height,thickness,color,edg_width,edg_height,chamfer_size,galss_type,edg_speed1,edg_speed2,fine_throwing,work_state,work_procedure,slicecage_rank,drop_frame,slicecage_flag,out_rank,start_time,finsh_time,stop_position,processcard_id,engineering_id,broken_state,stop_rank,work_procedure_number,gc_number,gc_width,gc_height,gc_stockid,marking_flag) select * from gmms_galss_task_stock where processcard_id=lck_num;
				
				set back_Num = 4;
				
				#更新任务数据
				update gmms_galss_task_2 set groups=pro_line,work_state='未优化',work_procedure='钢化上片',galss_id=0,start_time= ks_time	where processcard_id=lck_num;
				
				set back_Num = 5;
				
		else
			SET sum_count = sum_count + 1;
		end if;

	#退出重复条件
		UNTIL sum_count=drop_num
	END REPEAT;
	#关闭游标
	CLOSE lck_cursor;
	
	set back_Num=1;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for GlassTask_ganghuaqian_copy1
-- ----------------------------
DROP PROCEDURE IF EXISTS `GlassTask_ganghuaqian_copy1`;
delimiter ;;
CREATE PROCEDURE `GlassTask_ganghuaqian_copy1`(in pro_line BIGINT,out back_Num int,out back_Msg varchar(255))
BEGIN
	#生成钢化前玻璃上片任务
	Declare lck_num varchar(255);#记录流程卡号
	DECLARE sum_count int default 0;#记录累加的记录数
	DECLARE mlck_sum int default 0;#某流程卡数据总数
	DECLARE drop_num int default 0;#有流程卡的架位数量
	#定义游标
	Declare lck_cursor cursor for select processcard_id from gmms_drop_frame_2 where groups = pro_line and processcard_id is not null and processcard_id <>'' order by Id desc;
	
	select count(*) into drop_num from gmms_drop_frame_2 where groups = pro_line and processcard_id is not null and processcard_id <>'' order by Id desc;
	select drop_num;
	set back_Num = 0;#记录
	#打开游标
	Open lck_cursor;
	set back_Num = 6;#记录
	#重复
	REPEAT
	set back_Num = 7;#记录
		#使用游标---从游标中获取数据
		FETCH lck_cursor INTO lck_num;
		set back_Num = 2;
		Select count(*) into mlck_sum from gmms_galss_task_2 where processcard_id = lck_num;
		if mlck_sum =0
			then
				SET sum_count = sum_count + 1;
				set back_Num = 3;
				#插入玻璃任务数据
				insert into gmms_galss_task_2 (id,rank,type_id,galss_id,pattern_id,width,height,thickness,color,edg_width,edg_height,chamfer_size,galss_type,edg_speed1,edg_speed2,fine_throwing,work_state,work_procedure,slicecage_rank,drop_frame,slicecage_flag,out_rank,start_time,finsh_time,stop_position,processcard_id,engineering_id,broken_state,stop_rank,work_procedure_number,gc_number,gc_width,gc_height,gc_stockid,marking_flag) select * from gmms_galss_task_stock where processcard_id=lck_num;
				
				set back_Num = 4;
				
				#更新任务数据
				update gmms_galss_task_2 set groups=pro_line,work_state='未优化',work_procedure='钢化上片',galss_id=0	where processcard_id=lck_num;
				
				set back_Num = 5;
				
		else
			SET sum_count = sum_count + 1;
		end if;

	#退出重复条件
		UNTIL sum_count=drop_num
	END REPEAT;
	#关闭游标
	CLOSE lck_cursor;
	
	set back_Num=1;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for GlassTask_ghq_BindRack
-- ----------------------------
DROP PROCEDURE IF EXISTS `GlassTask_ghq_BindRack`;
delimiter ;;
CREATE PROCEDURE `GlassTask_ghq_BindRack`(in RId BIGINT,in lckNo varchar(50),out back_Num int,out back_Msg varchar(255))
BEGIN
	#逻辑流程：
	#1.判断流程卡是否已经绑定
	#2.判断流程卡小片是否已经完成冷加工部分
	#3.
	#生成钢化前玻璃上片任务

	Declare old_lck varchar(255);#旧流程卡号
	Declare lck_c int;#流程卡数量
	Declare lck_xc int;#流程卡小片数量

	#查询当前料架是否已经有流程卡
	select processcard_id into old_lck from gmms_drop_frame_2 where Id = RId;
	select old_lck;
	if old_lck is null
	then
			select count(*) into lck_c from gmms_drop_frame_2 where processcard_id = lckNo;
			select lck_c;
			if lck_c = 0
			then 
				select count(*) into lck_xc from gmms_galss_task_stock 
				where id>(select min(id) from (select * from gmms_galss_task_stock order by id desc limit 50000) as tbmin) and processcard_id = lckNo;
				if lck_xc = 0 then 
					select count(*) into lck_xc from gmms_galss_task_buffer 
					where id>(select min(id) from (select * from gmms_galss_task_stock order by id desc limit 50000) as tbmin) and processcard_id = lckNo;
				end if;
				
				if lck_xc = 0
				then 
					set back_Num=4;
					set back_Msg = "绑定失败：当前流程卡还在冷加工工艺段或输入错误,暂时无法绑定！";
				else	
					 Update gmms_drop_frame_2 set processcard_Id = lckNo ,state = '使用中',processcard_count = lck_xc,surplus_count= lck_xc where id= RId;
-- 			  Update gmms_drop_frame_2 set processcard_Id = lckNo ,state = '使用中',processcard_count = (SELECT count(1) FROM gmms_galss_task_stock WHERE processcard_id = lckNo),surplus_count= (SELECT count(1) 	FROM gmms_galss_task_stock WHERE processcard_id = lckNo) where id= RId;
				Update gmms_liuchengka set Occupied = RId where liuchengka = lckNo;
				set back_Num=1;
				set back_Msg = "绑定成功：流程卡绑定成功！";
				end if;
			else 
				set back_num = 2;
				set back_Msg = "绑定失败：当前流程卡已经被其他工位绑定,无法再次绑定！";
			end if;
			
	else 
			set back_num = 3;
			set back_Msg = "绑定失败：当前上片位已绑定流程卡,无法再次绑定！";
	end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for glass_log_new
-- ----------------------------
DROP PROCEDURE IF EXISTS `glass_log_new`;
delimiter ;;
CREATE PROCEDURE `glass_log_new`(in gid bigint,in work_segment varchar(50))
BEGIN
	DECLARE cr_work_name varchar(50);
	DECLARE done int default(0);
	#Routine body goes here...
	declare cur1 cursor for
		  	SELECT work_name from gmms_glass_log_config where segment=work_segment group by work_name;
				DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
	
	  SELECT main_id,line,area into @p1,@p2,@p3 from gmms_glass_log where glass_id=gid and parent_id is null and segment_name=work_segment LIMIT 1;
		
		
			 INSERT INTO gmms_glass_log (
main_id
,glass_id
,segment_name
,work_name
,line
,begin_time
,end_time
,`status`
,parent_id
,area)
values (@p1,gid,work_segment,null,@p2,null,null,0,null,@p3);
SET @zid=LAST_INSERT_ID();

open cur1;
loop1: loop
		FETCH  cur1 into cr_work_name;
			if done=1 then 
				LEAVE loop1;
			end if;
					 INSERT INTO gmms_glass_log (
main_id
,glass_id
,segment_name
,work_name
,line
,begin_time
,end_time
,`status`
,parent_id
,area)
values (@p1,gid,work_segment,cr_work_name,@p2,null,null,0,@zid,@p3);	
end loop loop1;
close cur1;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for HollowSlicecage
-- ----------------------------
DROP PROCEDURE IF EXISTS `HollowSlicecage`;
delimiter ;;
CREATE PROCEDURE `HollowSlicecage`(in sendMessageHex varchar(255),in machineids BIGINT,out messagedate varchar(255))
BEGIN
	#理片笼存储过程
	#找到当前机器标志位
	#查询机器逻辑表中对应的逻辑进行对应的步骤
	DECLARE machinedisposeids int DEFAULT 0;
	DECLARE dates varchar(255);
	DECLARE findplc varchar(255);
	DECLARE findmes varchar(255);
	DECLARE taskplc varchar(255);
	DECLARE taskmes varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	set messagedate=messagedates;


	select machine_type_id,remarks into @mtid,@remarks from gmms_machine where id=machineids;
	

	
	select mf.flag into @完成标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='完成字';
	
	select mf.flag into @请求标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='请求字';
	
	select mf.flag into @发送标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='发送字';
	
	select mf.flag into @完成确认标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='完成确认字';
	
	set findplc = substring(sendMessageHex,(cast(@完成标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskplc = substring(sendMessageHex,(cast(@请求标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskmes = substring(sendMessageHex,(cast(@发送标志位 as UNSIGNED INTEGER)*4)+19,4);
	set findmes = substring(sendMessageHex,(cast(@完成确认标志位 as UNSIGNED INTEGER)*4)+19,4);
	select taskplc;
	select taskmes;
	select findplc;
	select findmes;
	
	#完成逻辑查询
	select count(*),md.id into @完成数量,@完成逻辑编号 from gmms_machine_dispose md join gmms_machine_type mt on md.machine_type_id=mt.id where md.machine_type_id=@mtid  and md.plc_font_type='完成字' and md.mes_font_type='完成确认字' and md.plc_font_value=findplc and md.mes_font_value=findmes;
	
	#请求逻辑查询
	select count(*),md.id into @请求数量,@请求逻辑编号 from gmms_machine_dispose md join gmms_machine_type mt on md.machine_type_id=mt.id where md.machine_type_id=@mtid  and md.plc_font_type='请求字' and md.mes_font_type='发送字' and md.plc_font_value=taskplc and md.mes_font_value=taskmes;
	
	#判断逻辑是否存在
	if @完成数量=1 or @请求数量=1 then
	
		#判断完成逻辑是否为空
		if @完成逻辑编号!=0 then
			set machinedisposeids=@完成逻辑编号;
			
		#判断请求逻辑是否为空
		elseif @请求逻辑编号!=0 then
			set machinedisposeids=@请求逻辑编号;
		end if;
		#查询当前执行逻辑进行判断
		select logic,return_value into @ExecutionLogic,@returnvalue from gmms_machine_dispose where id=machinedisposeids;
		select @ExecutionLogic;
		if @ExecutionLogic='任务清空' then
				set dates=CONCAT(@returnvalue,"00000000000000000000");
				call message(dates,'000a',@date);
				set messagedate=@date;
		elseif @ExecutionLogic='完成清空' then
				call message(@returnvalue,'0014',@date);
				set messagedate=@date;
		elseif @ExecutionLogic='任务完成' then
				call HollowSlicecageFinsh(machineids,machinedisposeids,1,@messagedate);
				set messagedate=@messagedate;
 		elseif @ExecutionLogic='任务失败' then
				call HollowSlicecageFinsh(machineids,machinedisposeids,2,@messagedate);
				set messagedate=@messagedate;		
		elseif @ExecutionLogic='任务请求' then
				call HollowSlicecageSend(machineids,@returnvalue,1,@messagedate);
				set messagedate=@messagedate;
 		end if;
	end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for HollowSlicecageFinsh
-- ----------------------------
DROP PROCEDURE IF EXISTS `HollowSlicecageFinsh`;
delimiter ;;
CREATE PROCEDURE `HollowSlicecageFinsh`(in machineids BIGINT,in machinedisposeids BIGINT,in flag int,out messagedate varchar(255))
BEGIN
	#理片笼完成存储过程
	#判断完成，破损，直通
	DECLARE y int DEFAULT 0;
	DECLARE processcardid2 varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	DECLARE ischuku_ varchar(255) DEFAULT '';
	set messagedate=messagedates;
	
	select machine_type_id,work_id into @mtid,@work_groups from gmms_machine where id=machineids;
	
	
	select return_value into @returnvalue from gmms_machine_dispose where id=machinedisposeids;
	
	SELECT type_id,id,slicecage_rank,work_state,processcard_id into @type_id,@id,@slicecage_rank,@work_state,@processcard_id from gmms_galss_task_shelf 
	where work_procedure='理片' and (work_state='进库中' or work_state='出库中')  order by rank ASC limit 1;
	select ischu into ischuku_ from gmms_liuchengka where liuchengka=@processcard_id;
	#完成
	if flag=1 then
		if @work_state='出库中' then 
			#出库
			if ischuku_='出库' then
				update gmms_slicecage_tempering as st LEFT JOIN gmms_galss_task_shelf as gs on st.id=gs.slicecage_rank
				set gs.work_state='已出库',gs.finsh_time=now(),gs.slicecage_rank=null,
				st.state='空闲',st.count=0,st.type_id=null,st.processcard_id=null 
				where gs.slicecage_rank is not null and gs.id=@id;
			else
				update gmms_slicecage_tempering as st LEFT JOIN gmms_galss_task_shelf as gs on st.id=gs.slicecage_rank
				set gs.work_state='已出库',gs.finsh_time=now(),gs.slicecage_rank=null,gs.stop_rank=stop_position,
				st.state='空闲',st.count=0,st.type_id=null,st.processcard_id=null 
				where gs.slicecage_rank is not null and gs.id=@id;
			end if;		
		else 
			#进库
			update gmms_slicecage_tempering as st LEFT JOIN gmms_galss_task_shelf as gs on st.id=gs.slicecage_rank
			set gs.work_state='已进库',st.state='使用中',st.count=1 
			where gs.id=@id;
		end if;
		
 		call message(@returnvalue,'0014',@date);
 		set messagedate=@date;
		insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,"",NOW(),'收到完成',"",messagedate);
	#破损
	elseif flag=2 then
		if @work_state='出库中' then 
			#出库
			update gmms_slicecage_tempering as st LEFT JOIN gmms_galss_task_shelf as gs on st.id=gs.slicecage_rank
			set gs.work_state='已进库',st.state='使用中'
			where gs.slicecage_rank is not null and gs.id=@id;
		elseif @work_state='进库中' then 
			#进库
			update gmms_slicecage_tempering as st LEFT JOIN gmms_galss_task_shelf as gs on st.id=gs.slicecage_rank
			set gs.work_state='等待中',gs.finsh_time=now(),gs.slicecage_rank=null,
			st.state='空闲',st.count=0,st.type_id=null,st.processcard_id=null 
			where gs.id=@id;			
		end if;
-- 		UPDATE gmms_galss_task_shelf SET work_state='破损',slicecage_rank=null 
-- 		where id=@id;
		call message(@returnvalue,'0014',@date);
		set messagedate=@date;
		insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,"",NOW(),'收到失败',"",messagedate);

	end if;
	

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for HollowSlicecageSend
-- ----------------------------
DROP PROCEDURE IF EXISTS `HollowSlicecageSend`;
delimiter ;;
CREATE PROCEDURE `HollowSlicecageSend`(in machineids BIGINT,in returnvalue varchar(255),in flags int,out messagedate varchar(255))
BEGIN
	#理片笼请求存储过程
	#判断是进片还是出片还是进出片请求
	#发送数据：玻璃编号，理片笼架子
	DECLARE a int DEFAULT 0;
	DECLARE dates varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	DECLARE processcardid varchar(255) DEFAULT null;
	DECLARE waitingcount int DEFAULT 0;
	DECLARE notsliceagecount int DEFAULT 0;
	DECLARE nottaizi int DEFAULT 0;
	DECLARE Nullsliceagecount int DEFAULT 0;
	DECLARE rk int DEFAULT 0;
	DECLARE ischuku_ varchar(255) DEFAULT '';
	set messagedate=messagedates;
	
	SET @galss_id=NULL;
	select machine_type_id,work_id,remarks into @mtid,@work_groups,@remarks from gmms_machine where id=machineids;
	SET @lastpos=null;
	SELECT `value` into @lastpos from gmms_var where varname='last_pos' and groups=@work_groups;
		
	if flags=1 then
			#找到有任务的第一个工位,流程卡 默认B  A
			select lk.liuchengka,lk.ischu into processcardid,ischuku_ from gmms_drop_frame_5 as dr5 
			left join gmms_shelf_rack_2 as sr on dr5.rank=sr.id
			left join gmms_liuchengka as lk on sr.processcard_id=lk.liuchengka
			where groups_=3 and drop_groups=1 and dr5.sign=3 and sr.id is not null and lk.liuchengka is not null and 
			lk.current_status='正在工作' and lk.current_work_position='理片';
			
			#未配对的数量
			select count(*) into notsliceagecount from gmms_galss_task_shelf 
			where processcard_id=processcardid and (slicecage_rank<=0 or slicecage_rank is null) 
			and stop_position>0 and stop_position!=stop_rank and work_state!='破损';
			
			select count(*) into nottaizi from gmms_galss_task_shelf 
			where processcard_id=processcardid and (slicecage_rank<=0 or slicecage_rank is null) 
			and stop_position>0 and work_state!='破损' and work_procedure!='钢化后下片';
			
			SELECT count(*) into waitingcount from gmms_slicecage_tempering where state="等待中";
			select notsliceagecount,ischuku_,nottaizi;
			if notsliceagecount=0 or (ischuku_='出库' and nottaizi=0) then
				#出片
				if (ischuku_='出库' and nottaizi=0) then
					SELECT  b.id,a.slicecage_rank,a.count,a.type_id,b.galss_id,b.edg_width,b.edg_height 
					into @taskids,rk,@count,@type_id,@galss_id,@width,@height from gmms_slicecage_tempering a 
					left join gmms_galss_task_shelf b on a.slicecage_rank=b.slicecage_rank 
					where b.slicecage_rank is not null and a.processcard_id=processcardid and a.state='使用中' and a.flag=0 
					order by stop_rank asc limit 0,1;
				else
					SELECT  b.id,a.slicecage_rank,a.count,a.type_id,b.galss_id,b.edg_width,b.edg_height 
					into @taskids,rk,@count,@type_id,@galss_id,@width,@height from gmms_slicecage_tempering a 
					left join gmms_galss_task_shelf b on a.slicecage_rank=b.slicecage_rank 
					where b.slicecage_rank is not null and a.processcard_id=processcardid and a.state='使用中' and a.flag=0 
					order by stop_position asc limit 0,1;
				end if;
				
				if @galss_id is not null then					
					call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
					call intTo2ByteHex10_16_4(rk,@rks);
					call intTo2ByteHex10_16_4(@width,@widths);
					call intTo2ByteHex10_16_4(@height,@heights);
					set dates=CONCAT('0002',@galss_ids,@rks,@widths,@heights);
					call message(dates,'000a',@date);
					set messagedate=@date;					
					if messagedate is not null then
						update gmms_slicecage_tempering as st LEFT JOIN gmms_galss_task_shelf as gs on st.id=gs.slicecage_rank
						set gs.work_state='出库中',st.state='出库中'
						where gs.slicecage_rank is not null and gs.id=@taskids;
						UPDATE gmms_var SET Value=rk where varname='last_pos' and groups=@work_groups;
						insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,"",NOW(),'发送出库任务',"",messagedate);
					end if;
				end if;
			elseif waitingcount=0 then
				#提前移动
				select count(*) into Nullsliceagecount from gmms_slicecage_tempering 
				where state="空闲" and count=0 and flag=0 and gourp=@work_groups;
				SELECT id into rk from gmms_slicecage_tempering 
				where state="空闲" and count=0 and flag=0 and gourp=@work_groups  
				order by ABS(slicecage_rank-@lastpos)  limit 1;
				select rk;
				if rk>0 then
					if Nullsliceagecount=80 then
						UPDATE gmms_slicecage_tempering set state='等待中' where slicecage_rank=rk and gourp=@work_groups;
						UPDATE gmms_var set value=rk where varname='last_pos' and groups=@work_groups;
					else
						call intTo2ByteHex10_16_4(rk,@rks);
						set dates=CONCAT('0001','0000',@rks);
						call message(dates,'000a',@date);
						set messagedate=@date;
						if messagedate is not null then
							UPDATE gmms_slicecage_tempering set state='等待中' where slicecage_rank=rk and gourp=@work_groups;
							UPDATE gmms_var set value=rk where varname='last_pos' and groups=@work_groups;
							insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,"",NOW(),'发送提前移动任务',"",messagedate);
						end if;
					end if;				
				end if;
					
			elseif waitingcount=1 then
				select id,stop_rank,type_id,galss_id,processcard_id,width,height 
				into @id,@rankid,@type_id,@galss_id,processcardid,@width,@height from gmms_galss_task_shelf 
				where work_procedure='理片' and work_state='等待中' and processcard_id= processcardid order by stop_rank desc limit 1;
				#进库
				if @rankid is not null then			
					SET rk=0;
					SELECT id into rk from gmms_slicecage_tempering where state="等待中" limit 1;																	
					call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
					call intTo2ByteHex10_16_4(rk,@rks);
					set dates=CONCAT('0001',@galss_ids,@rks);
					call message(dates,'000a',@date);
					set messagedate=@date;
					if messagedate is not null then
						UPDATE gmms_galss_task_shelf set slicecage_rank=rk,work_procedure='理片',work_state='进库中' 
						WHERE id=@id and processcard_id=processcardid;
						UPDATE gmms_slicecage_tempering set state='进库中',count=1,type_id=@type_id,processcard_id=processcardid 
						where slicecage_rank=rk and gourp=@work_groups;
						insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,"",NOW(),'发送理片任务',IFNULL(@galss_id,0),messagedate);
					end if;

				end if;
			end if;			
	
	elseif flags=2 then
		select 1;
	end if;
		
select dates;
	
	
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for HollowTask
-- ----------------------------
DROP PROCEDURE IF EXISTS `HollowTask`;
delimiter ;;
CREATE PROCEDURE `HollowTask`()
BEGIN
DECLARE t_error INTEGER DEFAULT 0;
DECLARE c INTEGER DEFAULT 0;
DECLARE y int DEFAULT 0;
DECLARE processcardid2 varchar(255);

DECLARE _isSucess varchar(255);

DECLARE _Content varchar(255);
	DECLARE    CONTINUE HANDLER FOR SQLEXCEPTION SET t_error = 1;											
						
#中空任务
select  count(*) into @count1 from 
(select count(*) as ccc,a.parent_liuchengka,b.child_count from gmms_liuchengka  a 
inner join gmms_liuchengka b on a.parent_liuchengka=b.liuchengka inner 
join  gmms_shelf_rack_2 c on 
a.liuchengka=c.processcard_id 
where c.sign=2 and b.gongyi='中空' 
group by a.parent_liuchengka ,b.child_count) aa
where aa.child_count=aa.ccc;

while c<@count1 do
	select  aa.parent_liuchengka into @parent_liuchengka from 
(select count(*) as ccc,a.parent_liuchengka,b.child_count from gmms_liuchengka  a inner join gmms_liuchengka b on a.parent_liuchengka=b.liuchengka inner join  gmms_shelf_rack_2 c on 
a.liuchengka=c.processcard_id where c.sign=2 and b.gongyi='中空' group by a.parent_liuchengka ,b.child_count) aa
where aa.child_count=aa.ccc limit c,1;
	set @parent_liuchengka1= substring(@parent_liuchengka, 2, 13);
	if @parent_liuchengka1 is not null then
		select count(*) into @count2 from gmms_raw_file_2 where file_name=@parent_liuchengka1;
		if @count2=0 then
			select count(*) into @count8 from gmms_shelf_rack_2 where processcard_id like CONCAT('%',@parent_liuchengka1,'%') and orientation='A';
			select count(*) into @count9 from gmms_shelf_rack_2 where processcard_id like CONCAT('%',@parent_liuchengka1,'%') and orientation='B';
			if @count8!=3 or @count9!=3 then
				insert into gmms_raw_file_2 (file_name,state,operator,create_time) values(@parent_liuchengka1,'未领取',0,NOW());
			else
				select number into @number from gmms_shelf_rack_2 where processcard_id like CONCAT('%',@parent_liuchengka1,'%') order by number limit 0,1;
				select noodles into @noodles from gmms_drop_frame_2 where groups=20 and flag=1;
				select count(*) into @count10 from gmms_shelf_task_2 where shelf_rack_id=@number and load_rack=@noodles and task_type='从仓位到旋转位' and groups=0 and line=3 and (task_state='正在工作' or task_state='等待中');
				if @count10=0 and @number is not null then
					call y_TemperingAddShelftask(1,@number,@noodles,9, _isSucess, _Content);
				end if;
			end if;
			
		end if;
	end if;
	
set c=c+1;
end while;

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for Horizontal
-- ----------------------------
DROP PROCEDURE IF EXISTS `Horizontal`;
delimiter ;;
CREATE PROCEDURE `Horizontal`(in sendMessageHex varchar(255),in machineids BIGINT,out messagedate varchar(255),out msg varchar(255))
BEGIN
	#理片笼存储过程
	#找到当前机器标志位
	#查询机器逻辑表中对应的逻辑进行对应的步骤
	DECLARE machinedisposeids int DEFAULT 0;
	DECLARE dates varchar(255);
	DECLARE findplc1 varchar(255);
	DECLARE findmes1 varchar(255);
	DECLARE taskplc1 varchar(255);
	DECLARE taskmes1 varchar(255);
	DECLARE findplc2 varchar(255);
	DECLARE findmes2 varchar(255);
	DECLARE taskplc2 varchar(255);
	DECLARE taskmes2 varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	DECLARE texts varchar(255) DEFAULT '';
	DECLARE IsExistChu varchar(255);
	#2023/11/13   查看理片时间
	set @create_time1_=null;
	set @end_time1_=null;
	set @create_time1_=SYSDATE();
	
	
	
	set messagedate=messagedates;
	set @log_task="";
		SET msg:='Slicecage  查配置';	
	select machine_type_id,remarks into @mtid,@remarks from gmms_machine where id=machineids;
	

	
	select mf.flag into @进完成标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='进完成字';
	select mf.flag into @出完成标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='出完成字';
	
	select mf.flag into @进请求标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='进请求字';
	select mf.flag into @出请求标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='出请求字';
	
	select mf.flag into @进发送标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='进发送字';
	select mf.flag into @出发送标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='出发送字';
	
	select mf.flag into @进完成确认标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='进完成确认字';
	select mf.flag into @出完成确认标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='出完成确认字';
	
	set findplc1 = substring(sendMessageHex,(cast(@进完成标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskplc1 = substring(sendMessageHex,(cast(@进请求标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskmes1 = substring(sendMessageHex,(cast(@进发送标志位 as UNSIGNED INTEGER)*4)+19,4);
	set findmes1 = substring(sendMessageHex,(cast(@进完成确认标志位 as UNSIGNED INTEGER)*4)+19,4);
	
	set findplc2 = substring(sendMessageHex,(cast(@出完成标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskplc2 = substring(sendMessageHex,(cast(@出请求标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskmes2 = substring(sendMessageHex,(cast(@出发送标志位 as UNSIGNED INTEGER)*4)+19,4);
	set findmes2 = substring(sendMessageHex,(cast(@出完成确认标志位 as UNSIGNED INTEGER)*4)+19,4);
	-- 修改
	set IsExistChu = substring(sendMessageHex,(8*4)+19,4);  

	
-- 	select taskplc1;
-- 	select taskmes1;
-- 	select findplc1;
-- 	select findmes1;
-- 	
-- 	select taskplc2;
-- 	select taskmes2;
-- 	select findplc2;
-- 	select findmes2;
-- 	

	
	#完成逻辑查询
	select count(*),md.id into @进完成数量,@进完成逻辑编号 from gmms_machine_dispose md join gmms_machine_type mt on md.machine_type_id=mt.id where md.machine_type_id=@mtid  and md.plc_font_type='进完成字' and md.mes_font_type='进完成确认字' and md.plc_font_value=findplc1 and md.mes_font_value=findmes1;
	
	select count(*),md.id into @出完成数量,@出完成逻辑编号 from gmms_machine_dispose md join gmms_machine_type mt on md.machine_type_id=mt.id where md.machine_type_id=@mtid  and md.plc_font_type='出完成字' and md.mes_font_type='出完成确认字' and md.plc_font_value=findplc2 and md.mes_font_value=findmes2;
	
	#请求逻辑查询
	select count(*),md.id into @进请求数量,@进请求逻辑编号 from gmms_machine_dispose md join gmms_machine_type mt on md.machine_type_id=mt.id where md.machine_type_id=@mtid  and md.plc_font_type='进请求字' and md.mes_font_type='进发送字' and md.plc_font_value=taskplc1 and md.mes_font_value=taskmes1;
	
	select count(*),md.id into @出请求数量,@出请求逻辑编号 from gmms_machine_dispose md join gmms_machine_type mt on md.machine_type_id=mt.id where md.machine_type_id=@mtid  and md.plc_font_type='出请求字' and md.mes_font_type='出发送字' and md.plc_font_value=taskplc2 and md.mes_font_value=taskmes2;
	
	
	#判断逻辑是否存在
	if @进完成数量=1 or @出完成数量=1 or @进请求数量=1 or @出请求数量=1 then
	
		#判断完成逻辑是否为空
		if @进完成逻辑编号!=0 then
			set machinedisposeids=@进完成逻辑编号;
			
		elseif @出完成逻辑编号!=0 then
			set machinedisposeids=@出完成逻辑编号;
			
		#判断请求逻辑是否为空
		elseif @进请求逻辑编号!=0 then
			set machinedisposeids=@进请求逻辑编号;
			
		elseif @出请求逻辑编号!=0 then
			set machinedisposeids=@出请求逻辑编号;
			
		end if;
		select machinedisposeids;
		set @ExecutionLogic=null;
		select count(*) into @chuansong from gmms_galss_task  
		where work_state='传送中' and work_procedure='理片' and engineering_id=(select file_name from gmms_raw_file where operator=1 and state='已领取');
		
		select count(*) into @chuansong1 from gmms_horizontal as gh 
		left join gmms_galss_task as gt on gh.id =gt.slicecage_rank 
		where gh.processcard_id is not null and gh.gourp=1 and gt.work_state='传送中' and gt.work_procedure='理片' and gt.engineering_id=(select file_name from gmms_raw_file where operator=1 		and state='已领取');
		
		#查询当前执行逻辑进行判断
		select logic,return_value into @ExecutionLogic,@returnvalue from gmms_machine_dispose where id=machinedisposeids;
		select @ExecutionLogic;
		if taskmes2="0001" then
				set dates=CONCAT("0000","00000000000000000000000000000000");
				call message(dates,'000d',@date);
				set messagedate=@date;
		elseif @ExecutionLogic='出片完成' then
				call HorizontalFinsh(machineids,machinedisposeids,1,@messagedate);
				set messagedate=@messagedate;
		elseif @ExecutionLogic='进片完成' then
				call HorizontalFinsh(machineids,machinedisposeids,2,@messagedate);
				set messagedate=@messagedate;		
		elseif @ExecutionLogic='进片完成破损1' then
				call HorizontalFinsh(machineids,machinedisposeids,3,@messagedate);
				set messagedate=@messagedate;
		elseif @ExecutionLogic='进片完成破损2' then
				call HorizontalFinsh(machineids,machinedisposeids,4,@messagedate);
				set messagedate=@messagedate;
		elseif @ExecutionLogic='进片完成破损3' then
				call HorizontalFinsh(machineids,machinedisposeids,5,@messagedate);
				set messagedate=@messagedate;
 		elseif @ExecutionLogic='出片完成破损1' then
				call HorizontalFinsh(machineids,machinedisposeids,6,@messagedate);
				set messagedate=@messagedate;
		elseif @ExecutionLogic='出片完成破损2' then
				call HorizontalFinsh(machineids,machinedisposeids,7,@messagedate);
				set messagedate=@messagedate;
		elseif @ExecutionLogic='出片完成破损3' then
				call HorizontalFinsh(machineids,machinedisposeids,8,@messagedate);
				set messagedate=@messagedate;
		elseif @ExecutionLogic='进完成清空' then
				call message(@returnvalue,'0016',@date);
				set messagedate=@date;
 		elseif @ExecutionLogic='出完成清空' then
				call message(@returnvalue,'0018',@date);
				set messagedate=@date;
		elseif taskplc1='0001' and taskplc2='0001' then		
				call HorizontalSend(machineids,0001,IsExistChu,3,@messagedate,texts);
				set messagedate=@messagedate;
		elseif @ExecutionLogic='进片请求' then
				 call HorizontalSend(machineids,@returnvalue,IsExistChu,1,@messagedate,texts);
				set messagedate=@messagedate;
		elseif @ExecutionLogic='出片请求' then		
				 call HorizontalSend(machineids,@returnvalue,IsExistChu,2,@messagedate,texts);
				set messagedate=@messagedate;
		elseif @ExecutionLogic='出任务清空' then
				set dates=CONCAT(@returnvalue,"00000000000000000000000000000000");
				call message(dates,'000d',@date);
				set messagedate=@date;
 		elseif @ExecutionLogic='进任务清空' then
				set dates=CONCAT(@returnvalue,"00000000");
				call message(dates,'000a',@date);
				set messagedate=@date;
		end if;
	end if;
	
	
-- 		if taskmes2="0001" then
-- 				set dates=CONCAT("0000","00000000000000000000000000000000");
-- 				call message(dates,'000d',@date);
-- 				set messagedate=@date;
-- 		elseif @ExecutionLogic='进片完成' then
-- 				call HorizontalFinsh(machineids,machinedisposeids,2,@messagedate);
-- 				set messagedate=@messagedate;		
-- 		elseif @ExecutionLogic='进片完成破损1' then
-- 				call HorizontalFinsh(machineids,machinedisposeids,3,@messagedate);
-- 				set messagedate=@messagedate;
-- 		elseif @ExecutionLogic='进片完成破损2' then
-- 				call HorizontalFinsh(machineids,machinedisposeids,4,@messagedate);
-- 				set messagedate=@messagedate;
-- 		elseif @ExecutionLogic='进片完成破损3' then
-- 				call HorizontalFinsh(machineids,machinedisposeids,5,@messagedate);
-- 				set messagedate=@messagedate;
-- 		elseif @ExecutionLogic='进完成清空' then
-- 				call message(@returnvalue,'0016',@date);
-- 				set messagedate=@date;
--  		elseif @ExecutionLogic='进片请求' and taskplc2='0000' then
-- 				call HorizontalSend(machineids,@returnvalue,IsExistChu,1,@messagedate,texts);
-- 				set messagedate=@messagedate;
-- 		elseif @ExecutionLogic='进任务清空' then
-- 				set dates=CONCAT(@returnvalue,"00000000");
-- 				call message(dates,'000a',@date);
-- 				set messagedate=@date;
-- 				
--  		elseif @ExecutionLogic='出片完成' then
-- 				call HorizontalFinsh(machineids,machinedisposeids,1,@messagedate);
-- 				set messagedate=@messagedate;
-- 		elseif @ExecutionLogic='出片完成破损1' then
-- 				call HorizontalFinsh(machineids,machinedisposeids,6,@messagedate);
-- 				set messagedate=@messagedate;
-- 		elseif @ExecutionLogic='出片完成破损2' then
-- 				call HorizontalFinsh(machineids,machinedisposeids,7,@messagedate);
-- 				set messagedate=@messagedate;
-- 		elseif @ExecutionLogic='出片完成破损3' then
-- 				call HorizontalFinsh(machineids,machinedisposeids,8,@messagedate);
-- 				set messagedate=@messagedate;
-- 		elseif @ExecutionLogic='出完成清空' then
-- 				call message(@returnvalue,'0018',@date);
-- 				set messagedate=@date;
-- 		elseif taskplc1='0001' and taskplc2='0001' then		
-- 				call HorizontalSend(machineids,0001,IsExistChu,3,@messagedate,texts);
-- 				set messagedate=@messagedate;
-- 		elseif @ExecutionLogic='出片请求' then		
-- 				 call HorizontalSend(machineids,@returnvalue,IsExistChu,2,@messagedate,texts);
-- 				set messagedate=@messagedate;
-- 		elseif @ExecutionLogic='出任务清空' then
-- 				set dates=CONCAT(@returnvalue,"00000000000000000000000000000000");
-- 				call message(dates,'000d',@date);
-- 				set messagedate=@date;
--  		end if;
-- 	end if;
	
	
	set @end_time1_=SYSDATE();
	set texts=CONCAT(IFNULL(taskplc1,'-'),'   ',IFNULL(taskplc2,'-'),'   ',IFNULL(findplc1,'-'),'   ',IFNULL(findplc2,'-'),'   ',IFNULL(@ExecutionLogic,'-'),'   传送1:',IFNULL(@chuansong1,'-'),'   ','   传送2:',IFNULL(@chuansong,'-'),'   ',IFNULL(texts,'-'));
	insert into A_LengLipian_copy1(create_time,end_time,content) values (@create_time1_,@end_time1_,texts);
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for HorizontalFinsh
-- ----------------------------
DROP PROCEDURE IF EXISTS `HorizontalFinsh`;
delimiter ;;
CREATE PROCEDURE `HorizontalFinsh`(in machineids BIGINT,in machinedisposeids BIGINT,in flag int,out messagedate varchar(255))
BEGIN
	#理片笼完成存储过程
	#判断完成，破损，直通
	DECLARE dates varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	set messagedate=messagedates;
	
	select machine_type_id,work_id into @mtid,@work_groups from gmms_machine where id=machineids;
	
	select file_name into @engineering_id from gmms_raw_file where operator=@work_groups and state='已领取';
	
	select return_value into @returnvalue from gmms_machine_dispose where id=machinedisposeids;
	
	select galss_id into @galss_id from gmms_galss_task where work_state='出库中' and work_procedure='理片' and engineering_id=@engineering_id limit 1;
	select @returnvalue;
	
	SELECT type_id,id,slicecage_rank,work_state into @type_id,@id ,@slicecage_rank,@work_state from   gmms_galss_task where work_procedure='理片'  and (work_state='进库中'
 or work_state='出库中' or work_state='传送中') and engineering_id=@engineering_id order by rank ASC limit 1;
 
 
 select slicecage_rank into @slicecage_rank1 from gmms_horizontal where id=@slicecage_rank;
	if @id is not null then
	select flag;
	#出片完成
	if flag=1 then
		SELECT type_id,id,slicecage_rank,work_state into @type_id,@id ,@slicecage_rank,@work_state from   gmms_galss_task where work_procedure='理片' and (work_state='出库中' or work_state='传送中') and engineering_id=@engineering_id order by rank ASC limit 1;
		
		
		select slicecage_rank into @slicecage_rank1 from gmms_horizontal where id=@slicecage_rank;
				if @work_state='出库中' then 
					select count(*) into @counts from gmms_galss_task where work_procedure='理片' and work_state='出库中' and slicecage_rank=@slicecage_rank and engineering_id=@engineering_id;
					if @counts=1 then
						select max(out_rank) into @maxrank from gmms_galss_task where engineering_id=@engineering_id;
						UPDATE gmms_galss_task SET work_state='已出库',finsh_time=now(),slicecage_rank=null,out_rank=@maxrank+1  where slicecage_rank=@slicecage_rank and work_state='出库中' and engineering_id=@engineering_id;	
					else 
						select galss_id into @galss_id1 from gmms_galss_task where work_procedure='理片' and work_state='出库中' and engineering_id=@engineering_id order by rank limit 0,1;
						select galss_id into @galss_id2 from gmms_galss_task where work_procedure='理片' and work_state='出库中' and engineering_id=@engineering_id order by rank limit 1,1;
						select max(out_rank) into @maxrank from gmms_galss_task where engineering_id=@engineering_id;
						UPDATE gmms_galss_task SET work_state='已出库',finsh_time=now(),slicecage_rank=null,out_rank=@maxrank+1  where galss_id=@galss_id1 and work_state='出库中' and engineering_id=@engineering_id;
						UPDATE gmms_galss_task SET work_state='已出库',finsh_time=now(),slicecage_rank=null,out_rank=@maxrank+2  where galss_id=@galss_id2 and work_state='出库中' and engineering_id=@engineering_id;
					end if;	
					UPDATE gmms_horizontal SET state='空闲'  ,count=0,type_id=null,processcard_id=null where slicecage_rank=@slicecage_rank1 and gourp=2;
					
				elseif @work_state='传送中' then
						select id into @slicecage_rankid from gmms_horizontal where slicecage_rank=@slicecage_rank1 and gourp=2;
						UPDATE gmms_galss_task SET work_state='已进库',slicecage_rank=@slicecage_rankid  where work_state='传送中' and engineering_id=@engineering_id;
						UPDATE gmms_horizontal SET state='使用中'  where slicecage_rank=@slicecage_rank1 and gourp=2;
				elseif @work_state='直通' then
						select max(out_rank) into @maxrank from gmms_galss_task where engineering_id=@engineering_id;
						UPDATE gmms_galss_task SET work_state='已出库',out_rank=@maxrank+1  where work_state='直通' and engineering_id=@engineering_id;
				end if;
				
		
		call message(@returnvalue,'0018',@date);
		set messagedate=@date;

	#进片完成
	elseif flag=2 then
		if @id is not null then
		SELECT type_id,id,slicecage_rank,work_state into @type_id,@id ,@slicecage_rank,@work_state from   gmms_galss_task where work_procedure='理片' and (work_state='进库中' or work_state='传送中') and engineering_id=@engineering_id order by rank ASC limit 1;
		select slicecage_rank into @slicecage_rank1 from gmms_horizontal where id=@slicecage_rank;
			if @work_state='进库中' then
				SELECT count(*) into @count from gmms_galss_task WHERE  slicecage_rank=@slicecage_rank and engineering_id=@engineering_id;
				UPDATE gmms_galss_task SET work_state='已进库' where slicecage_rank=@slicecage_rank and work_state='进库中' and engineering_id=@engineering_id;
				UPDATE gmms_horizontal SET state='使用中' ,count=@count  where id=@slicecage_rank and gourp=1; 
			elseif @work_state='传送中' then
				UPDATE gmms_horizontal SET state='空闲'  ,count=0,type_id=null,processcard_id=null where slicecage_rank=@slicecage_rank1 and gourp=1;
			end if;		
		call message(@returnvalue,'0016',@date);
		set messagedate=@date;
		end if;
-- 	#传送完成
-- 	elseif flag=3 then
-- 		SELECT count(*) into @count from gmms_galss_task WHERE  slicecage_rank=@slicecage_rank and engineering_id=@engineering_id;
-- 		UPDATE gmms_galss_task SET work_state='已进库' where slicecage_rank=@slicecage_rank and work_state='传送中' and slicecage_rank=@slicecage_rank and engineering_id=@engineering_id;
-- 		UPDATE gmms_horizontal SET state='使用中'  ,count=@count  where slicecage_rank=@slicecage_rank1 and gourp=1;
-- 		UPDATE gmms_horizontal SET state='空闲'  ,count=0,type_id=null,processcard_id=null where slicecage_rank=@slicecage_rank1 and gourp=2;
-- 		set dates=CONCAT(@returnvalue,"0000",@returnvalue);
-- 		call message(dates,'0014',@date);
-- 		set messagedate=@date;
		
	#出片完成破损1
	elseif flag=6 then
		select slicecage_rank into @slicecage_rank from gmms_galss_task where work_state=@work_state and engineering_id=@engineering_id limit 1;
		UPDATE gmms_galss_task SET work_state='破损',broken_state=1,slicecage_rank=null  where type_id=@type_id and work_state=@work_state and engineering_id=@engineering_id limit 1;
		update gmms_horizontal set state='空闲',count=0,type_id=null,processcard_id=null where state=@work_state and slicecage_rank=@slicecage_rank1 and gourp=2;
		call message(@returnvalue,'0018',@date);
		set messagedate=@date;
		
	#出片完成破损2
	elseif flag=7 then
		select slicecage_rank into @slicecage_rank from gmms_galss_task where work_state=@work_state and engineering_id=@engineering_id limit 2;
		UPDATE gmms_galss_task SET work_state='破损',broken_state=1,slicecage_rank=null  where type_id=@type_id and work_state=@work_state and engineering_id=@engineering_id limit 2;
		update gmms_horizontal set state='空闲',count=0,type_id=null,processcard_id=null where state=@work_state and slicecage_rank=@slicecage_rank1 and gourp=2;
		call message(@returnvalue,'0018',@date);
		set messagedate=@date;
		
	#出片完成破损3
	elseif flag=8 then
		select slicecage_rank into @slicecage_rank from gmms_galss_task where work_state=@work_state and engineering_id=@engineering_id;
		UPDATE gmms_galss_task SET work_state='破损',broken_state=1,slicecage_rank=null  where type_id=@type_id and work_state=@work_state and engineering_id=@engineering_id;
		update gmms_horizontal set state='空闲',count=0,type_id=null,processcard_id=null where state=@work_state and slicecage_rank=@slicecage_rank1 and gourp=2;
		call message(@returnvalue,'0018',@date);
		set messagedate=@date;
		
		
	#进片完成破损1
	elseif flag=3 then
		select slicecage_rank into @slicecage_rank from gmms_galss_task where work_state=@work_state and engineering_id=@engineering_id limit 1;
		UPDATE gmms_galss_task SET work_state='破损',broken_state=1,slicecage_rank=null  where type_id=@type_id and work_state=@work_state and engineering_id=@engineering_id limit 1;
		update gmms_horizontal set state='空闲',count=0,type_id=null,processcard_id=null where state=@work_state and slicecage_rank=@slicecage_rank1 and gourp=1;
		call message(@returnvalue,'0016',@date);
		set messagedate=@date;
		
	#进片完成破损2
	elseif flag=4 then
		select slicecage_rank into @slicecage_rank from gmms_galss_task where work_state='传送中' and engineering_id=@engineering_id limit 2;
		UPDATE gmms_galss_task SET work_state='破损',broken_state=1,slicecage_rank=null  where type_id=@type_id and work_state='传送中' and engineering_id=@engineering_id limit 2;
		update gmms_horizontal set state='空闲',count=0,type_id=null,processcard_id=null where state='传送中' and slicecage_rank=@slicecage_rank1 and gourp=1;
		call message(@returnvalue,'0016',@date);
		set messagedate=@date;
		
	#进片完成破损3
	elseif flag=5 then
		select slicecage_rank into @slicecage_rank from gmms_galss_task where work_state='传送中' and engineering_id=@engineering_id;
		UPDATE gmms_galss_task SET work_state='破损',broken_state=1,slicecage_rank=null  where type_id=@type_id and work_state='传送中' and engineering_id=@engineering_id;
		update gmms_horizontal set state='空闲',count=0,type_id=null,processcard_id=null where state='传送中' and slicecage_rank=@slicecage_rank1 and gourp=1;
		call message(@returnvalue,'0016',@date);
		set messagedate=@date;

	end if;
	end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for HorizontalSend
-- ----------------------------
DROP PROCEDURE IF EXISTS `HorizontalSend`;
delimiter ;;
CREATE PROCEDURE `HorizontalSend`(in machineids BIGINT,in returnvalue varchar(255),in isexistChu varchar(255),in flags int,out messagedate varchar(255),out ResultText varchar(255))
BEGIN
	#理片笼请求存储过程
	#判断是进片还是出片还是进出片请求
	#发送数据：玻璃编号，理片笼架子
	DECLARE a int DEFAULT 0;
	DECLARE ranks int DEFAULT 0;
	DECLARE dates varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	DECLARE maxw int DEFAULT 0;
	DECLARE maxh int DEFAULT 0;
	DECLARE minw int DEFAULT 0;
	DECLARE minh int DEFAULT 0;
	DECLARE kaobian varchar(255) DEFAULT null;
	set @create_time_=null;
	set @end_time_=null;
	set @create_time_=SYSDATE();
	set messagedate=messagedates;
	

select machine_type_id,work_id into @mtid,@work_groups from gmms_machine where id=machineids;

select @work_groups;
	
select file_name into @engineering_id from gmms_raw_file where operator=@work_groups and state='已领取';

select @engineering_id;

select var into @var from gmms_slicecage_pattern where groups=@work_groups;

select @var;
set @counts6=0;
set @counts7=0;
set @counts8=0;
set @counts9=0;
set @counts10=0;
set @Lp_Mode=0;
SET @发送字=0;



select count(*) into @counts6 from gmms_galss_task  where work_state='直通' and work_procedure='理片' and engineering_id=@engineering_id;
select count(*) into @counts7 from gmms_galss_task  where work_state='进库中' and work_procedure='理片' and engineering_id=@engineering_id;
select count(*) into @counts8 from gmms_galss_task  where work_state='出库中' and work_procedure='理片' and engineering_id=@engineering_id;
select count(*) into @counts9 from gmms_galss_task  where work_state='传送中' and work_procedure='理片' and engineering_id=@engineering_id;

select count(*) into @counts10 from gmms_horizontal as gh 
left join gmms_galss_task as gt on gh.id =gt.slicecage_rank 
where gh.processcard_id is not null and gh.gourp=1 and gt.work_state='传送中' and gt.work_procedure='理片' and gt.engineering_id=@engineering_id;

set @conts=null;
set @conts=CONCAT(IFNULL(flags,'-'),',',IFNULL(isexistChu,'-'),' count6:',IFNULL(@counts6,'-'),' count7:',IFNULL(@counts7,'-'),' count8:',IFNULL(@counts8,'-'),' count9:',IFNULL(@counts9,'-'),' count10:',IFNULL(@counts10,'-'));

#进出片
if @var='1' then
		#传送
	if flags=3 then
		if a=0 and @counts8=0 and @counts9=0 and isexistChu='0000' then
			set @kuneishu=0;
			select 库内数 into @kuneishu from processcard1 where 工程号=@engineering_id and 库内数>0 order by 库内数 desc  limit 0,1;
			select @kuneishu;
			select @work_groups;
			select count(*) into @counts from gmms_horizontal where state='空闲' and gourp=2;
			
				if (@kuneishu>0 or @counts<=1) and @counts6=0 then
				#出片
				SET @processcardid=null;
				set @rkid=null;
				set @rk=null;
				set @galss_id=null;
				select `value` into @lastid from gmms_var where varname='last_out_glass_id'and groups=@work_groups;
				select `value` into @lastpos from gmms_var where varname='last_pos'and groups=@work_groups;
				if @lastid is not null then
					select @lastid;
					select @lastpos;
					select '进入';
					
					SELECT a.id,a.slicecage_rank,a.type_id,a.count,galss_id into @rkid,@rk,@type_id,@count,@galss_id from gmms_horizontal a 
					INNER JOIN gmms_galss_task b on a.id=b.slicecage_rank INNER JOIN gmms_drop_frame c on b.processcard_id=c.processcard_id 
					where a.state='使用中' and a.count>0 and a.type_id=@lastid and a.flag=0 and b.engineering_id=@engineering_id and  c.processcard_id is not null and a.gourp=2 and b.stop_position>0 
					order by a.count desc, ABS(a.slicecage_rank-@lastpos) asc LIMIT 1;
					select @rkid;
					if @rk is not null and @rk<>0 then
					select @rk;
						UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
							set @galss_ids=null;
							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
							select @galss_ids;
							call intTo2ByteHex10_16_4(@rk,@rks);
							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
							call message(dates,'000d',@date);
							set messagedate=@date;
							insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼出库1',@rk,@date);
							set @发送字=1;
							set a=1;
					end if;
				end if;
				if @发送字=0 then
					SELECT 流程卡 into @processcardid from processcard_ex where 下片位置 is not null and 库内数>0 and 总数-切割数=0 and 工程号=@engineering_id order by  总数-下架数  asc LIMIT 1;
					IF @processcardid is  null then
						select 2;
							#  后使用  未完全切完的
							SELECT 流程卡 into @processcardid from processcard_ex where 下片位置 is not null and 库内数>0 and 工程号=@engineering_id order by 总数-下架数  asc LIMIT 1;	
					end if;
					-- 看看空架 有没有 ，如果有  就找一个库能玻璃最多的下架

					IF @processcardid is null then 

						SET @drop_pos=null;
						
						select name1 into @name1 from namess where groups=@work_groups;
						SELECT 流程卡,架号,总数,工程号 into @processcardid,@drop_frame,@processcard_count,@engineering_ids from processcard_ex 
						where  (库内数>=@name1 or (库内数>0 and 总数<@name1)) and 工程号=@engineering_id order by  总数-库内数 LIMIT 1;
						
						#得到最小的 和最大的玻璃数据  判断往哪里靠边  2023-8-30
						select MAX(width),MAX(height),MIN(width),MIN(height) into maxw,maxh,minw,minh from gmms_galss_task where processcard_id=@processcardid;
						#if maxw<2400 and maxh<1900 and minw>599 and minh>349 then
						if maxw<2400 and maxh<1900 and minw>529 and minh>349 then
							set kaobian='机器人';
						else
							set kaobian='下片机';	
						end if;
						select kaobian;
						if kaobian='下片机' then
							select id into @drop_pos from gmms_drop_frame where state='空架' and groups=@work_groups and (stops=1 or stops=2) limit 1;
						elseif kaobian='机器人' then
							select id into @drop_pos from gmms_drop_frame where state='空架' and groups=@work_groups and (stops=3 or stops=4 or stops=5) limit 1;
						end if;
						set kaobian=null;
						if @processcardid is not null and @drop_pos  is not null then 						
							#  流程卡占架子
							select @processcardid;
							select @drop_pos;
							select count(*) into @counts2 from gmms_galss_task where processcard_id=@processcardid and engineering_id=@engineering_id;
							if @counts2>0 then 
								UPDATE gmms_drop_frame set state='使用中',drop_frame=@drop_frame,processcard_id=@processcardid,processcard_count=@processcard_count,engineering_id=@engineering_id where id=@drop_pos;
								INSERT into hn_user(update_time,add_time) VALUES(@processcardid,@work_groups);
								UPDATE gmms_galss_task set stop_position=@drop_pos where processcard_id=@processcardid and engineering_id=@engineering_id;				
							end if;
						else
							set @processcardid=null;
							set @drop_pos=null;
						end if;
					end if;
					if @processcardid is not null then 
							SELECT @processcardid;	
							#  选定大小和槽
							SELECT  a.id,a.slicecage_rank,a.count,a.type_id,galss_id into @rkid,@rk,@count,@type_id,@galss_id from gmms_horizontal a 
							inner join gmms_galss_task b on a.id=b.slicecage_rank  where a.processcard_id=@processcardid and a.state='使用中' and a.gourp=2 and a.flag=0 and engineering_id=@engineering_id 
							order by b.width*b.height desc ,a.count DESC  limit 1;
							select @rkid;
							select @count;
							select a;
							if @rk is not null and @rk<>0 then
							UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
							call intTo2ByteHex10_16_4(@rk,@rks);
							select @galss_ids,@rks,@date;
							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
							call message(dates,'000d',@date);
							set messagedate=@date;
							insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼出库2',@rk,@date);
							set a=1;
							end if;
						end if;
					
				end if;

				end if;
			
		end if;
		select a;
		if a=0 and @counts7=0 and @counts8=0 and @counts9=0 then
		
			set @rk=null;
			set @LastGalssid=null;
			set @ProcesscardA1=null;
			set @ProcesscardA2=null;
			
-- 			select slicecage_rank into @rk from gmms_horizontal  
-- 			where (gourp=1 and state='使用中' and count>0 and flag=0) or (gourp=2 and state='空闲')  group by slicecage_rank having count(*)=2 order by count desc limit 1;
			
			#2023-11-14  YZX更新
			select 
			(SELECT b.id from gmms_horizontal a 
			INNER JOIN gmms_galss_task b on a.id=b.slicecage_rank 
			INNER JOIN gmms_drop_frame c on b.processcard_id=c.processcard_id 
			where a.state='使用中' and a.count>0 and a.type_id=ls1.value and a.flag=0 and b.engineering_id=grf.file_name and  c.processcard_id is not null and a.gourp=1 and b.stop_position>0 
			order by a.count desc, ABS(a.slicecage_rank-ls2.value) asc LIMIT 1),
			(SELECT 流程卡 from processcard_ex where 下片位置 is not null and 库内数>0 and 工程号=grf.file_name order by 总数-下架数  asc LIMIT 1) ,
			(SELECT 流程卡 from processcard_ex where (库内数>=ns.name1 or (库内数>0 and 总数<ns.name2)) and 工程号=grf.file_name order by  总数-库内数 LIMIT 1)
			INTO @LastGalssid,@ProcesscardA1,@ProcesscardA2
			from gmms_machine as gm 
			LEFT JOIN gmms_raw_file as grf on gm.work_id=grf.operator and grf.state='已领取'
			LEFT JOIN gmms_var as ls1 on gm.work_id=ls1.groups and ls1.varname='last_out_glass_id'
			LEFT JOIN gmms_var as ls2 on gm.work_id=ls2.groups and ls2.varname='last_pos'
			LEFT JOIN namess as ns on gm.work_id=ns.groups
			where gm.id=27;
			
			if @LastGalssid is not null then
				select hl1.slicecage_rank into @rk from gmms_horizontal as hl1 
				left join gmms_horizontal as hl2 on hl1.slicecage_rank=hl2.slicecage_rank
				where hl1.gourp=1 and hl2.gourp=2 and hl1.state='使用中' and hl1.count>0 and hl1.flag=0 and hl2.gourp=2 and hl2.state='空闲' 
				and hl1.slicecage_rank=(select slicecage_rank from gmms_galss_task where id=@LastGalssid);
			elseif @ProcesscardA1 is not null then
				select hl1.slicecage_rank into @rk from gmms_horizontal as hl1 
				left join gmms_horizontal as hl2 on hl1.slicecage_rank=hl2.slicecage_rank
				where hl1.gourp=1 and hl2.gourp=2 and hl1.state='使用中' and hl1.count>0 and hl1.flag=0 and hl2.gourp=2 and hl2.state='空闲' 
				and hl1.processcard_id=@ProcesscardA1 order by hl1.count desc limit 1;
			elseif @ProcesscardA2 is not null then
				select hl1.slicecage_rank into @rk from gmms_horizontal as hl1 
				left join gmms_horizontal as hl2 on hl1.slicecage_rank=hl2.slicecage_rank
				where hl1.gourp=1 and hl2.gourp=2 and hl1.state='使用中' and hl1.count>0 and hl1.flag=0 and hl2.gourp=2 and hl2.state='空闲' 
				and hl1.processcard_id=@ProcesscardA2 order by hl1.count desc limit 1;
			end if;			
			
			if @rk is null then
				select slicecage_rank into @rk from gmms_horizontal  
				where (gourp=1 and state='使用中' and count>0 and flag=0) or (gourp=2 and state='空闲')  group by slicecage_rank having count(*)=2 order by count desc limit 1;
			end if;
			select @rk;
			
					
			if @rk is not null and @rk<>0 then
				select count(*) into @countstate from gmms_horizontal where  (state='出库中' or state='进库中');
				if @countstate=0 then
				UPDATE gmms_galss_task SET work_state='传送中' where slicecage_rank=@rk and engineering_id=@engineering_id;
				UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=1;
				select count,type_id,processcard_id into @count,@type_id,@processcard_id from gmms_horizontal where slicecage_rank=@rk and gourp=1;
				UPDATE gmms_horizontal SET state='进库中',count=@count,type_id=@type_id,processcard_id=@processcard_id where slicecage_rank=@rk and gourp=2;
				select count(*) into @counts from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id;
				select count(*) into @count1 from gmms_galss_task where work_state='直通' and work_procedure='理片' and engineering_id=@engineering_id;
				if @counts=1 then
					if @type_id <>0 and (@processcard_id is not null or @processcard_id<>'') then
						select galss_id into @galss_id1 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id;
						call intTo2ByteHex10_16_4(@galss_id1,@galss_ids1);
						call intTo2ByteHex10_16_4(@rk,@rks);
						set dates=CONCAT("00020000",@rks,"000200000000",@rks,"000000000000",@galss_ids1);
						call message(dates,'000a',@date);
						insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
							values (machineids,@work_groups,NOW(),'理片笼传送1',@rk,@date);
						set messagedate=@date;
						set a=1;
					end if;
				else 
					select galss_id into @galss_id1 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id limit 1;
					select galss_id into @galss_id2 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id limit 2;
					call intTo2ByteHex10_16_4(@galss_id1,@galss_ids1);
					call intTo2ByteHex10_16_4(@galss_id2,@galss_ids2);
					call intTo2ByteHex10_16_4(@rk,@rks);
					set dates=CONCAT("00020000",@rks,"000200000000",@rks,"000000000000",@galss_ids1,@galss_id2);
					call message(dates,'000a',@date);
					set a=1;
					set messagedate=@date;
					insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
							values (machineids,@work_groups,NOW(),'理片笼传送2',@rk,@date);
				end if;
				end if;
				
			elseif @count1>0 then
					set dates=CONCAT("000300000003","0003000000000003");
					call message(dates,'000a',@date);
					set messagedate=@date;
					set a=1;
			
			end if;
		end if;
		
		if a=0 and @counts7=0 and @counts10=0 then
				#进片
				set @type_id=0;
				set @processcardid=null;
				select rank,type_id,galss_id,processcard_id,width,height,slicecage_rank into @rankid,@type_id,@galss_id,@processcardid,@width,@height,@rk from gmms_galss_task where work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id order by rank limit 1;
				if @galss_id is not null and @rk<>0 and @rk is not null then
				UPDATE gmms_galss_task set work_state='进库中',finsh_time=now() WHERE rank=@rankid and work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id;
				UPDATE gmms_horizontal set state='进库中',count=1,type_id=@type_id,processcard_id=@processcardid where slicecage_rank=@rk and gourp=1;
				call intTo2ByteHex10_16_4(@rankid,@rankids);
				call intTo2ByteHex10_16_4(@rk,@rks);
				set dates=CONCAT("0001",@rankids,@rks);
				call message(dates,'000a',@date);
				set messagedate=@date;
				insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼进库1',@rk,@date);
				end if;
		end if;

		
	#出片
	elseif flags=2 and @counts6=0 and @counts8=0 and @counts9=0 and isexistChu='0000' then
				SET @processcardid=null;
				set @rkid=null;
				set @rk=null;
				set @galss_id=null;
				select `value` into @lastid from gmms_var where varname='last_out_glass_id'and groups=@work_groups;
				select `value` into @lastpos from gmms_var where varname='last_pos'and groups=@work_groups;
				if @lastid is not null then
					select @lastid;
					SELECT a.id,a.slicecage_rank,a.type_id,a.count,galss_id into @rkid,@rk,@type_id,@count,@galss_id from gmms_horizontal a 
					INNER JOIN gmms_galss_task b on a.id=b.slicecage_rank INNER JOIN gmms_drop_frame c on b.processcard_id=c.processcard_id 
					where a.state='使用中' and a.count>0 and a.type_id=@lastid and a.flag=0 and b.engineering_id=@engineering_id and  c.processcard_id is not null and a.gourp=2 and b.stop_position>0 
					order by a.count desc, ABS(a.slicecage_rank-@lastpos) asc LIMIT 1;
					if @rk is not null and @rk<>0 then
					select @rk;
						UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
							call intTo2ByteHex10_16_4(@rk,@rks);
							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
							call message(dates,'000d',@date);
							set messagedate=@date;
							insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼出库3',@rk,@date);
							set @发送字=1;
					end if;
				end if;
				if @发送字=0 then
					SELECT 流程卡 into @processcardid from processcard_ex where 下片位置 is not null and 库内数>0 and 总数-切割数=0 and 工程号=@engineering_id order by  总数-下架数  asc LIMIT 1;
					IF @processcardid is  null then
						select 2;
							#  后使用  未完全切完的
							SELECT 流程卡 into @processcardid from processcard_ex where 下片位置 is not null and 库内数>0 and 工程号=@engineering_id order by 总数-下架数  asc LIMIT 1;	
					end if;
					-- 看看空架 有没有 ，如果有  就找一个库能玻璃最多的下架
					IF @processcardid is null then 

						SET @drop_pos=null;
						SELECT 流程卡,架号,总数,工程号 into @processcardid,@drop_frame,@processcard_count,@engineering_ids from processcard_ex 
						where  (库内数>=@name1 or (库内数>0 and 总数<@name1)) and 工程号=@engineering_id order by  总数-库内数 LIMIT 1;
							
						#得到最小的 和最大的玻璃数据  判断往哪里靠边  2023-8-30
						select MAX(width),MAX(height),MIN(width),MIN(height) into maxw,maxh,minw,minh from gmms_galss_task where processcard_id=@processcardid;
						if maxw<2400 and maxh<1900 and minw>599 and minh>349 then
							set kaobian='机器人';
						else
							set kaobian='下片机';	
						end if;
						if kaobian='下片机' then
							select id into @drop_pos from gmms_drop_frame where state='空架' and groups=@work_groups and (stops=1 or stops=2) limit 1;
						elseif kaobian='机器人' then
							select id into @drop_pos from gmms_drop_frame where state='空架' and groups=@work_groups and (stops=3 or stops=4 or stops=5) limit 1;
						end if;
						set kaobian=null;
						
						if @processcardid is not null and @drop_pos  is not null then 
							select name1 into @name1 from namess where groups=@work_groups;
							#  流程卡占架子
							select count(*) into @counts2 from gmms_galss_task where processcard_id=@processcardid and engineering_id=@engineering_id;
							if @counts2>0 then 
								UPDATE gmms_drop_frame set state='使用中',drop_frame=@drop_frame,processcard_id=@processcardid,processcard_count=@processcard_count,engineering_id=@engineering_id where id=@drop_pos;
								INSERT into hn_user(update_time,add_time) VALUES(@processcardid,@work_groups);
								UPDATE gmms_galss_task set stop_position=@drop_pos where processcard_id=@processcardid and engineering_id=@engineering_id;				
							end if;
						else
							set @processcardid=null;
							set @drop_pos=null;
						end if;
					end if;
					if @processcardid is not null then 
							SELECT @processcardid;	
							#  选定大小和槽
							SELECT  a.id,a.slicecage_rank,a.count,a.type_id,galss_id into @rkid,@rk,@count,@type_id,@galss_id from gmms_horizontal a 
							inner join gmms_galss_task b on a.id=b.slicecage_rank  
							where a.processcard_id=@processcardid and a.state='使用中' and a.gourp=2 and a.flag=0 and engineering_id=@engineering_id 
							order by b.width*b.height desc ,a.count DESC  limit 1;
							select @count;
							if @rk is not null and @rk<>0 then
							UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
							call intTo2ByteHex10_16_4(@rk,@rks);
							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
							call message(dates,'000d',@date);
							set messagedate=@date;
							insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼出库4',@rk,@date);
							end if;
						end if;
					
				end if;

				
	#进片
	elseif flags=1 and @counts7=0 and @counts10=0 then
				set @conts=CONCAT(IFNULL(@conts,'-'),' ','进片');
				set @rk=null;
				set @type_id=0;
				set @processcardid=null;
				select rank,type_id,galss_id,processcard_id,width,height,slicecage_rank into @rankid,@type_id,@galss_id,@processcardid,@width,@height,@rk from gmms_galss_task where work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id order by rank limit 1;
				if @galss_id is not null and @rk<>0 and @rk is not null then
				UPDATE gmms_galss_task set work_state='进库中',finsh_time=now() WHERE rank=@rankid and work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id;
				UPDATE gmms_horizontal set state='进库中',count=1,type_id=@type_id,processcard_id=@processcardid where slicecage_rank=@rk and gourp=1;
				call intTo2ByteHex10_16_4(@rankid,@rankids);
				call intTo2ByteHex10_16_4(@rk,@rks);
				set dates=CONCAT("0001",@rankids,@rks);
				call message(dates,'000a',@date);
				set messagedate=@date;
				insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼进库2',@rk,@date);
				end if;
				set @conts=CONCAT(IFNULL(@conts,'-'),' ',IFNULL(@rk,'无任务'));
	end if;

#直通
-- elseif @var='2' then
-- 
-- 
#存笼
elseif @var='3' then
	if flags=3 and @counts7=0 then
	set @type_id=null;
	set @count=null;
		select slicecage_rank into @rk from gmms_horizontal  where (gourp=1 and state='使用中' and count>0 and flag=0) or (gourp=2 and state='空闲')  group by slicecage_rank having count(*)=2 order by count desc limit 1;
		select @rk;
		if @rk is not null and @counts8=0 and @counts9=0  then
				select count(*) into @countstate from gmms_horizontal where  (state='出库中' or state='进库中');
				if @countstate=0 then
  			UPDATE gmms_galss_task SET work_state='传送中' where slicecage_rank=@rk and engineering_id=@engineering_id;
  			UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=1;
 			  select count,type_id,processcard_id into @count,@type_id,@processcard_id from gmms_horizontal where slicecage_rank=@rk and gourp=1;
				
  			UPDATE gmms_horizontal SET state='进库中',count=@count,type_id=@type_id,processcard_id=@processcard_id where slicecage_rank=@rk and gourp=2;
			select count(*) into @counts from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id;
			select @counts;
			if @counts=1 then
				if @type_id <>0 and (@processcard_id is not null or @processcard_id<>'') then
					select galss_id into @galss_id1 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id;
					call intTo2ByteHex10_16_4(@galss_id1,@galss_ids1);
					call intTo2ByteHex10_16_4(@rk,@rks);
					set dates=CONCAT("00020000",@rks,"000200000000",@rks,"000000000000",@galss_ids1);
					call message(dates,'000a',@date);
					set messagedate=@date;
					insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼传送3',@rk,@date);
				end if;
			else 
			     	select @counts;
				end if;
			else 
				
				select galss_id into @galss_id1 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id limit 1;
				select galss_id into @galss_id2 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id limit 2;
				select @galss_id1,@galss_id2;
				call intTo2ByteHex10_16_4(@galss_id1,@galss_ids1);
				call intTo2ByteHex10_16_4(@galss_id2,@galss_ids2);
				call intTo2ByteHex10_16_4(@rk,@rks);
				set dates=CONCAT("00020000",@rks,"000200000000",@rks,"000000000000",@galss_ids1,@galss_id2);
				call message(dates,'000a',@date);
				set messagedate=@date;
				insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼传送4',@rk,@date);
			end if;
		elseif @counts10=0 then
			set @type_id=0;
			set @processcardid=null;
			select rank,type_id,galss_id,processcard_id,width,height,slicecage_rank into @rankid,@type_id,@galss_id,@processcardid,@width,@height,@rk from gmms_galss_task where work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id order by rank limit 1;
			if @galss_id is not null and @rk<>0 and @rk is not null then
				#UPDATE gmms_galss_task set slicecage_rank=@rk,work_procedure='理片',work_state='进库中' WHERE rank=@rankid and engineering_id=@engineering_id;
				UPDATE gmms_galss_task set work_state='进库中',finsh_time=now() WHERE rank=@rankid and work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id;
				UPDATE gmms_horizontal set state='进库中',count=1,type_id=@type_id,processcard_id=@processcardid where slicecage_rank=@rk and gourp=1;
				UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
				UPDATE gmms_var SET Value=@rankid where varname='last_out_glass_id' and groups=@work_groups;
				select @rankid,@rk;
				call intTo2ByteHex10_16_4(@rankid,@rankids);
				call intTo2ByteHex10_16_4(@rk,@rks);
				set dates=CONCAT("0001",@rankids,@rks);
				call message(dates,'000a',@date);
				set messagedate=@date;
				insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼进库3',@rk,@date);
			end if;

			
		end if;
		


				
	#进片
	elseif flags=1 and @counts7=0 and @counts10=0 then
				
		set @type_id=0;
				set @processcardid=null;
				select rank,type_id,galss_id,processcard_id,width,height,slicecage_rank into @rankid,@type_id,@galss_id,@processcardid,@width,@height,@rk from gmms_galss_task where work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id order by rank limit 1;
				if @galss_id is not null and @rk<>0 and @rk is not null then
				UPDATE gmms_galss_task set work_state='进库中',finsh_time=now() WHERE rank=@rankid and work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id;
				UPDATE gmms_horizontal set state='进库中',count=1,type_id=@type_id,processcard_id=@processcardid where slicecage_rank=@rk and gourp=1;
				call intTo2ByteHex10_16_4(@rankid,@rankids);
				call intTo2ByteHex10_16_4(@rk,@rks);
				UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
				UPDATE gmms_var SET Value=@rankid where varname='last_out_glass_id' and groups=@work_groups;
				set dates=CONCAT("0001",@rankids,@rks);
				call message(dates,'000a',@date);
				set messagedate=@date;
				
				insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼进库4',@rk,@date);
				end if;
				
	end if;
		
#出片
elseif @var='4' then

	if (flags=3 or flags=2) and @counts6=0 and @counts8=0 and @counts9=0 then
	
		SET @processcardid=null;
				set @rkid=null;
				set @rk=null;
				set @galss_id=null;
				select @lastid;
				select `value` into @lastid from gmms_var where varname='last_out_glass_id'and groups=@work_groups;
				select `value` into @lastpos from gmms_var where varname='last_pos'and groups=@work_groups;
				if @lastid is not null then
					select @lastid;
					SELECT a.id,a.slicecage_rank,a.type_id,a.count,galss_id into @rkid,@rk,@type_id,@count,@galss_id from gmms_horizontal a 
					INNER JOIN gmms_galss_task b on a.id=b.slicecage_rank INNER JOIN gmms_drop_frame c on b.processcard_id=c.processcard_id 
					where a.state='使用中' and a.count>0 and a.type_id=@lastid and a.flag=0 and b.engineering_id=@engineering_id and  c.processcard_id is not null and a.gourp=2 and b.stop_position>0 
					order by a.count desc, ABS(a.slicecage_rank-@lastpos) asc LIMIT 1;
					if @rk is not null and @rk<>0 then
					select @rk;
						UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
							call intTo2ByteHex10_16_4(@rk,@rks);
							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
							call message(dates,'000d',@date);
							set messagedate=@date;
							insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼出库5',@rk,@date);
							set @发送字=1;
					end if;
				end if;
				if @发送字=0 then
					SELECT 流程卡 into @processcardid from processcard_ex where 下片位置 is not null and 库内数>0 and 总数-切割数=0 and 工程号=@engineering_id order by  总数-下架数  asc LIMIT 1;
					IF @processcardid is  null then
						select 2;
							#  后使用  未完全切完的
							SELECT 流程卡 into @processcardid from processcard_ex where 下片位置 is not null and 库内数>0 and 工程号=@engineering_id order by 总数-下架数  asc LIMIT 1;	
					end if;
					-- 看看空架 有没有 ，如果有  就找一个库能玻璃最多的下架
					IF @processcardid is null then 

						SET @drop_pos=null;
						SELECT 流程卡,架号,总数,工程号 into @processcardid,@drop_frame,@processcard_count,@engineering_ids from processcard_ex 
						where  (库内数>=@name1 or (库内数>0 and 总数<@name1)) and 工程号=@engineering_id order by  总数-库内数 LIMIT 1;
						
						#得到最小的 和最大的玻璃数据  判断往哪里靠边  2023-8-30
						select MAX(width),MAX(height),MIN(width),MIN(height) into maxw,maxh,minw,minh from gmms_galss_task where processcard_id=@processcardid;
						if maxw<2400 and maxh<1900 and minw>599 and minh>349 then
							set kaobian='机器人';
						else
							set kaobian='下片机';	
						end if;
						if kaobian='下片机' then
							select id into @drop_pos from gmms_drop_frame where state='空架' and groups=@work_groups and (stops=1 or stops=2) limit 1;
						elseif kaobian='机器人' then
							select id into @drop_pos from gmms_drop_frame where state='空架' and groups=@work_groups and (stops=3 or stops=4 or stops=5) limit 1;
						end if;
						set kaobian=null;
						
						if @processcardid is not null and @drop_pos  is not null then 
							select name1 into @name1 from namess where groups=@work_groups;
							#  流程卡占架子
							select count(*) into @counts2 from gmms_galss_task where processcard_id=@processcardid and engineering_id=@engineering_id;
							if  @counts2>0 then 
								UPDATE gmms_drop_frame set state='使用中',drop_frame=@drop_frame,processcard_id=@processcardid,processcard_count=@processcard_count,engineering_id=@engineering_id where id=@drop_pos;
								INSERT into hn_user(update_time,add_time) VALUES(@processcardid,@work_groups);
								UPDATE gmms_galss_task set stop_position=@drop_pos where processcard_id=@processcardid and engineering_id=@engineering_id;				
							end if;			
						else
							set @processcardid=null;
							set @drop_pos=null;
						end if;
					end if;
					if @processcardid is not null then 
							SELECT @processcardid;	
							#  选定大小和槽
							SELECT  a.id,a.slicecage_rank,a.count,a.type_id,galss_id into @rkid,@rk,@count,@type_id,@galss_id from gmms_horizontal a inner join gmms_galss_task b on a.id=b.slicecage_rank  where a.processcard_id=@processcardid and a.state='使用中' and a.gourp=2 and a.flag=0 and engineering_id=@engineering_id order by b.width*b.height desc ,a.count DESC  limit 1;
							select @rkid;
							select @count;
							UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
							call intTo2ByteHex10_16_4(@rk,@rks);
							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
							call message(dates,'000d',@date);
							set messagedate=@date;
							insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼出库6',@rk,@date);
							set a=1;
						end if;
					
				end if;

	
	end if;
	
end if;
-- 
-- 	#传送
-- 	if flag=3 then
-- 		SELECT a.slicecage_rank,a.type_id,a.count into @rk,@type_id,@count from gmms_horizontal a INNER JOIN gmms_galss_task b on a.id=b.slicecage_rank where a.state='使用中' and a.count>0  and a.flag=0 and engineering_id=@engineering_id and a.gourp=1  order by a.count desc,a.slicecage_rank LIMIT 1;
-- 		select count(*) into @count1 from gmms_galss_task where work_state='直通' and work_procedure='理片';
-- 		select @rk;
-- 		if @rk is not null then
-- 			UPDATE gmms_galss_task SET work_state='传送中' where slicecage_rank=@rk and engineering_id=@engineering_id;
-- 			UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=1;
-- 			select count,type_id,processcard_id into @count,@type_id,@processcard_id from gmms_horizontal where slicecage_rank=@rk and gourp=1;
-- 			UPDATE gmms_horizontal SET state='进库中',count=@count,type_id=@type_id,processcard_id=@processcard_id where slicecage_rank=@rk and gourp=2;
-- 			select count(*) into @counts from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id;
-- 			if @counts=1 then
-- 				select galss_id into @galss_id1 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id;
-- 				call intTo2ByteHex10_16_4(@galss_id1,@galss_ids1);
-- 				call intTo2ByteHex10_16_4(@rk,@rks);
-- 				set dates=CONCAT("00020000",@rks,"000200000000",@rks,"000000000000",@galss_ids1);
-- 				call message(dates,'000a',@date);
-- 				set messagedate=@date;
-- 			else 
-- 				select galss_id into @galss_id1 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id limit 1;
-- 				select galss_id into @galss_id2 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id limit 2;
-- 				call intTo2ByteHex10_16_4(@galss_id1,@galss_ids1);
-- 				call intTo2ByteHex10_16_4(@galss_id2,@galss_ids2);
-- 				call intTo2ByteHex10_16_4(@rk,@rks);
-- 				set dates=CONCAT("00020000",@rks,"000200000000",@rks,"000000000000",@galss_ids1,@galss_id2);
-- 				call message(dates,'000a',@date);
-- 				set messagedate=@date;
-- 			end if;
-- 		elseif @count1>0 then
-- 				set dates=CONCAT("000300000003","0003000000000003");
-- 				call message(dates,'000a',@date);
-- 				set messagedate=@date;
-- 		else
-- 			select 库内数 into @kuneishu from processcard1 where 工程号=@engineering_id limit 1;
-- 			select @work_groups;
-- 			select count(*) into @counts from gmms_horizontal where state='空闲' and gourp=2;
-- 			if a=0 then
-- 				if @kuneishu>0 or @counts<=1 then
-- 				#出片
-- 				SET @processcardid=null;
-- 				set @rkid=null;
-- 				set @rk=null;
-- 				set @galss_id=null;
-- 				select `value` into @lastid from gmms_var where varname='last_out_glass_id'and groups=@work_groups;
-- 				select `value` into @lastpos from gmms_var where varname='last_pos'and groups=@work_groups;
-- 				if @lastid is not null then
-- 					select @lastid;
-- 					SELECT a.id,a.slicecage_rank,a.type_id,a.count,galss_id into @rkid,@rk,@type_id,@count,@galss_id from gmms_horizontal a INNER JOIN gmms_galss_task b on a.id=b.slicecage_rank where a.state='使用中' and a.count>0 and a.type_id=@lastid and a.flag=0 and engineering_id=@engineering_id and a.gourp=2 and b.stop_position>0 order by a.count desc, ABS(a.slicecage_rank-@lastpos) asc LIMIT 1;
-- 					if @rk is not null then
-- 					select @rk;
-- 						UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
-- 							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
-- 							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
-- 							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
-- 							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
-- 							call intTo2ByteHex10_16_4(@rk,@rks);
-- 							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
-- 							call message(dates,'000d',@date);
-- 							set messagedate=@date;
-- 							set @发送字=1;
-- 					end if;
-- 				end if;
-- 				if @发送字=0 then
-- 					SELECT 流程卡 into @processcardid from processcard_ex1 where 下片位置 is not null and 库内数>0 and 总数-切割数=0 and 工程号=@engineering_id order by  总数-下架数  asc LIMIT 1;
-- 					IF @processcardid is  null then
-- 						select 2;
-- 							#  后使用  未完全切完的
-- 							SELECT 流程卡 into @processcardid from processcard_ex1 where 下片位置 is not null and 库内数>0 and 工程号=@engineering_id order by 总数-下架数  asc LIMIT 1;	
-- 					end if;
-- 					-- 看看空架 有没有 ，如果有  就找一个库能玻璃最多的下架
-- 					IF @processcardid is null then 
-- 
-- 						SET @drop_pos=null;
-- 						SELECT id into @drop_pos from gmms_drop_frame where state='空架' and groups=@work_groups limit 1;
-- 						if @drop_pos  is not null then 
-- 							select name1 into @name1 from namess where groups=@work_groups;
-- 							SELECT 流程卡,架号,总数,工程号 into @processcardid,@drop_frame,@processcard_count,@engineering_ids from processcard_ex1 where  (库内数>=@name1 or (库内数>0 and 总数<@name1)) order by  库内数 DESC LIMIT 1;
-- 							#  流程卡占架子
-- 							if @processcardid is not null then 
-- 								UPDATE gmms_drop_frame set state='使用中',drop_frame=@drop_frame,processcard_id=@processcardid,processcard_count=@processcard_count,engineering_id=@engineering_ids where id=@drop_pos;
-- 								UPDATE gmms_galss_task set stop_position=@drop_pos where processcard_id=@processcardid and engineering_id=@engineering_id;				
-- 							end if;			
-- 						end if;
-- 					end if;
-- 					if @processcardid is not null then 
-- 							SELECT @processcardid;	
-- 							#  选定大小和槽
-- 							SELECT  a.id,a.slicecage_rank,a.count,a.type_id,galss_id into @rkid,@rk,@count,@type_id,@galss_id from gmms_horizontal a inner join gmms_galss_task b on a.type_id=b.type_id where a.processcard_id=@processcardid and a.state='使用中' and a.gourp=2 and a.flag=0 and engineering_id=@engineering_id order by b.width*b.height desc ,a.count DESC  limit 1;
-- 							select @count;
-- 							UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
-- 							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
-- 							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
-- 							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
-- 							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
-- 							call intTo2ByteHex10_16_4(@rk,@rks);
-- 							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
-- 							call message(dates,'000d',@date);
-- 							set messagedate=@date;
-- 							set a=1;
-- 						end if;
-- 					
-- 				end if;
-- -- 							SELECT  a.id,a.slicecage_rank,a.count,a.type_id,galss_id into @rkid,@rk,@count,@type_id,@galss_id from gmms_horizontal a inner join gmms_galss_task b on a.id=b.slicecage_rank where  a.state='使用中' and a.gourp=2 and a.flag=0 and engineering_id=@engineering_id order by b.width*b.height desc ,a.count DESC  limit 1;
-- -- 							select @count;
-- -- 							UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
-- -- 							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
-- -- 							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
-- -- 							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
-- -- 							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
-- -- 							call intTo2ByteHex10_16_4(@rk,@rks);
-- -- 							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
-- -- 							call message(dates,'000d',@date);
-- -- 							set messagedate=@date;
-- -- 							set a=1;
-- 				end if;
-- 			end if;
-- 			
-- 			if a=0 then
-- 				#进片
-- 				select rank,type_id,galss_id,processcard_id,width,height,slicecage_rank into @rankid,@type_id,@galss_id,@processcardid,@width,@height,@rk from gmms_galss_task where work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id order by rank limit 1;
-- 				UPDATE gmms_galss_task set slicecage_rank=@rk,work_procedure='理片',work_state='进库中' WHERE rank=@rankid and engineering_id=@engineering_id;
-- 				UPDATE gmms_horizontal set state='进库中',count=1,type_id=@type_id,processcard_id=@processcardid where slicecage_rank=@rk and gourp=1;
-- 				call intTo2ByteHex10_16_4(@rankid,@rankids);
-- 				call intTo2ByteHex10_16_4(@rk,@rks);
-- 				set dates=CONCAT("0001",@rankids,@rks);
-- 				call message(dates,'000a',@date);
-- 				set messagedate=@date;
-- 			end if;
-- 				
-- 	
-- 		end if;
-- 		
-- 	#出片
-- 	elseif flag=2 then
-- 				SET @processcardid=null;
-- 				set @rkid=null;
-- 				set @rk=null;
-- 				set @galss_id=null;
-- 				select `value` into @lastid from gmms_var where varname='last_out_glass_id'and groups=@work_groups;
-- 				select `value` into @lastpos from gmms_var where varname='last_pos'and groups=@work_groups;
-- 				if @lastid is not null then
-- 					select @lastid;
-- 					SELECT a.id,a.slicecage_rank,a.type_id,a.count,galss_id into @rkid,@rk,@type_id,@count,@galss_id from gmms_horizontal a INNER JOIN gmms_galss_task b on a.id=b.slicecage_rank where a.state='使用中' and a.count>0 and a.type_id=@lastid and a.flag=0 and engineering_id=@engineering_id and a.gourp=2 and b.stop_position>0 order by a.count desc, ABS(a.slicecage_rank-@lastpos) asc LIMIT 1;
-- 					if @rk is not null then
-- 					select @rk;
-- 						UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
-- 							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
-- 							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
-- 							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
-- 							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
-- 							call intTo2ByteHex10_16_4(@rk,@rks);
-- 							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
-- 							call message(dates,'000d',@date);
-- 							set messagedate=@date;
-- 							set @发送字=1;
-- 					end if;
-- 				end if;
-- 				if @发送字=0 then
-- 					SELECT 流程卡 into @processcardid from processcard_ex1 where 下片位置 is not null and 库内数>0 and 总数-切割数=0 and 工程号=@engineering_id order by  总数-下架数  asc LIMIT 1;
-- 					IF @processcardid is  null then
-- 						select 2;
-- 							#  后使用  未完全切完的
-- 							SELECT 流程卡 into @processcardid from processcard_ex1 where 下片位置 is not null and 库内数>0 and 工程号=@engineering_id order by 总数-下架数  asc LIMIT 1;	
-- 					end if;
-- 					-- 看看空架 有没有 ，如果有  就找一个库能玻璃最多的下架
-- 					IF @processcardid is null then 
-- 
-- 						SET @drop_pos=null;
-- 						SELECT id into @drop_pos from gmms_drop_frame where state='空架' and groups=@work_groups limit 1;
-- 						if @drop_pos  is not null then 
-- 							select name1 into @name1 from namess where groups=@work_groups;
-- 							SELECT 流程卡,架号,总数,工程号 into @processcardid,@drop_frame,@processcard_count,@engineering_ids from processcard_ex1 where  (库内数>=@name1 or (库内数>0 and 总数<@name1)) order by  库内数 DESC LIMIT 1;
-- 							#  流程卡占架子
-- 							if @processcardid is not null then 
-- 								UPDATE gmms_drop_frame set state='使用中',drop_frame=@drop_frame,processcard_id=@processcardid,processcard_count=@processcard_count,engineering_id=@engineering_ids where id=@drop_pos;
-- 								UPDATE gmms_galss_task set stop_position=@drop_pos where processcard_id=@processcardid and engineering_id=@engineering_id;				
-- 							end if;			
-- 						end if;
-- 					end if;
-- 					if @processcardid is not null then 
-- 							SELECT @processcardid;	
-- 							#  选定大小和槽
-- 							SELECT  a.id,a.slicecage_rank,a.count,a.type_id,galss_id into @rkid,@rk,@count,@type_id,@galss_id from gmms_horizontal a inner join gmms_galss_task b on a.type_id=b.type_id where a.processcard_id=@processcardid and a.state='使用中' and a.gourp=2 and a.flag=0 and engineering_id=@engineering_id order by b.width*b.height desc ,a.count DESC  limit 1;
-- 							select @count;
-- 							UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
-- 							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
-- 							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
-- 							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
-- 							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
-- 							call intTo2ByteHex10_16_4(@rk,@rks);
-- 							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
-- 							call message(dates,'000d',@date);
-- 							set messagedate=@date;
-- 						end if;
-- 					
-- 				end if;
-- -- 						SELECT  a.id,a.slicecage_rank,a.count,a.type_id,galss_id into @rkid,@rk,@count,@type_id,@galss_id from gmms_horizontal a inner join gmms_galss_task b on a.id=b.slicecage_rank where  a.state='使用中' and a.gourp=2 and a.flag=0 and engineering_id=@engineering_id order by b.width*b.height desc ,a.count DESC  limit 1;
-- -- 							select @count;
-- -- 							UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
-- -- 							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
-- -- 							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
-- -- 							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
-- -- 							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
-- -- 							call intTo2ByteHex10_16_4(@rk,@rks);
-- -- 							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
-- -- 							call message(dates,'000d',@date);
-- -- 							set messagedate=@date;
-- 				
-- 	#进片
-- 	elseif flag=1 then
-- 		select rank,type_id,galss_id,processcard_id,width,height,slicecage_rank into @rankid,@type_id,@galss_id,@processcardid,@width,@height,@rk from gmms_galss_task where work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id order by rank limit 1;
-- 		UPDATE gmms_galss_task set slicecage_rank=@rk,work_procedure='理片',work_state='进库中' WHERE rank=@rankid and engineering_id=@engineering_id;
-- 		UPDATE gmms_horizontal set state='进库中',count=1,type_id=@type_id,processcard_id=@processcardid where slicecage_rank=@rk and gourp=1;
-- 		call intTo2ByteHex10_16_4(@rankid,@rankids);
-- 		call intTo2ByteHex10_16_4(@rk,@rks);
-- 		set dates=CONCAT("0001",@rankids,@rks);
-- 		call message(dates,'000a',@date);
-- 		set messagedate=@date;
-- 	
-- 	end if;
		
	set @conts=CONCAT(IFNULL(@conts,'-'),'   ',IFNULL(messagedate,'-'));
	set @end_time_=SYSDATE();
	set ResultText=@conts;
	insert into A_LengLipian(create_time,end_time,content) values (@create_time_,@end_time_,@conts);
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for HorizontalSend_copy1
-- ----------------------------
DROP PROCEDURE IF EXISTS `HorizontalSend_copy1`;
delimiter ;;
CREATE PROCEDURE `HorizontalSend_copy1`(in machineids BIGINT,in returnvalue varchar(255),in flags int,out messagedate varchar(255))
BEGIN
	#理片笼请求存储过程
	#判断是进片还是出片还是进出片请求
	#发送数据：玻璃编号，理片笼架子
	DECLARE a int DEFAULT 0;
	DECLARE ranks int DEFAULT 0;
	DECLARE dates varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	DECLARE maxw int DEFAULT 0;
	DECLARE maxh int DEFAULT 0;
	DECLARE minw int DEFAULT 0;
	DECLARE minh int DEFAULT 0;
	DECLARE kaobian varchar(255) DEFAULT null;
	set messagedate=messagedates;
	

select machine_type_id,work_id into @mtid,@work_groups from gmms_machine where id=machineids;

select @work_groups;
	
select file_name into @engineering_id from gmms_raw_file where operator=@work_groups and state='已领取';

select @engineering_id;

select var into @var from gmms_slicecage_pattern where groups=@work_groups;

select @var;

SET @发送字=0;
select count(*) into @counts6 from gmms_galss_task  where work_state='直通' and work_procedure='理片' and engineering_id=@engineering_id;

#进出片
if @var='1' then
		#传送
	if flags=3 then
		if a=0 then
			set @kuneishu=0;
			select 库内数 into @kuneishu from processcard1 where 工程号=@engineering_id and 库内数>0 order by 库内数 desc  limit 0,1;
			select @kuneishu;
			select @work_groups;
			select count(*) into @counts from gmms_horizontal where state='空闲' and gourp=2;
			
				if (@kuneishu>0 or @counts<=1) and @counts6=0 then
				#出片
				SET @processcardid=null;
				set @rkid=null;
				set @rk=null;
				set @galss_id=null;
				select `value` into @lastid from gmms_var where varname='last_out_glass_id'and groups=@work_groups;
				select `value` into @lastpos from gmms_var where varname='last_pos'and groups=@work_groups;
				if @lastid is not null then
					select @lastid;
					SELECT a.id,a.slicecage_rank,a.type_id,a.count,galss_id into @rkid,@rk,@type_id,@count,@galss_id from gmms_horizontal a 
					INNER JOIN gmms_galss_task b on a.id=b.slicecage_rank INNER JOIN gmms_drop_frame c on b.processcard_id=c.processcard_id 
					where a.state='使用中' and a.count>0 and a.type_id=@lastid and a.flag=0 and b.engineering_id=@engineering_id and  c.processcard_id is not null and a.gourp=2 and b.stop_position>0 
					order by a.count desc, ABS(a.slicecage_rank-@lastpos) asc LIMIT 1;
					if @rk is not null and @rk<>0 then
					select @rk;
						UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
							set @galss_ids=null;
							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
							select @galss_ids;
							call intTo2ByteHex10_16_4(@rk,@rks);
							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
							call message(dates,'000d',@date);
							set messagedate=@date;
							insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼出库1',@rk,@date);
							set @发送字=1;
							set a=1;
					end if;
				end if;
				if @发送字=0 then
					SELECT 流程卡 into @processcardid from processcard_ex where 下片位置 is not null and 库内数>0 and 总数-切割数=0 and 工程号=@engineering_id order by  总数-下架数  asc LIMIT 1;
					IF @processcardid is  null then
						select 2;
							#  后使用  未完全切完的
							SELECT 流程卡 into @processcardid from processcard_ex where 下片位置 is not null and 库内数>0 and 工程号=@engineering_id order by 总数-下架数  asc LIMIT 1;	
					end if;
					-- 看看空架 有没有 ，如果有  就找一个库能玻璃最多的下架

					IF @processcardid is null then 

						SET @drop_pos=null;
						
-- 						#得到最小的 和最大的玻璃数据  判断往哪里靠边  2023-8-30
-- 						select MAX(width),MAX(height),MIN(width),MIN(height) into maxw,maxh,minw,minh from gmms_galss_task where processcard_id=@processcard_id;
-- 						select xiapianbiaoji into kaobian from gmms_liuchengka where liuchengka=@processcard_id;
-- 						if kaobian is null then
-- 							if maxw<2400 or maxh<1900 or minw>599 or minh>349 then
-- 								update gmms_liuchengka set xiapianbiaoji='下片机' where liuchengka=@processcard_id;
-- 							else
-- 								update gmms_liuchengka set xiapianbiaoji='机器人' where liuchengka=@processcard_id;
-- 							end if;
-- 						elseif kaobian='下片机' then
-- 							select id into @drop_pos from gmms_drop_frame where state='空架' and groups=@work_groups and (stops=1 or stops=2) limit 1;
-- 						elseif kaobian='机器人' then
-- 							select id into @drop_pos from gmms_drop_frame where state='空架' and groups=@work_groups and (stops=3 or stops=4 or stops=5) limit 1;
-- 						end if;
						
						SELECT id into @drop_pos from gmms_drop_frame where state='空架' and groups=@work_groups limit 1;
						if @drop_pos  is not null then 
							select name1 into @name1 from namess where groups=@work_groups;
							SELECT 流程卡,架号,总数,工程号 into @processcardid,@drop_frame,@processcard_count,@engineering_ids from processcard_ex where  (库内数>=@name1 or (库内数>0 and 总数<@name1)) and 工程号=@engineering_id order by  总数-库内数 LIMIT 1;
							#  流程卡占架子
							select @processcardid;
							select @drop_pos;
							select count(*) into @counts2 from gmms_galss_task where processcard_id=@processcardid and engineering_id=@engineering_id;
							if @processcardid is not null and @counts2>0 then 
								UPDATE gmms_drop_frame set state='使用中',drop_frame=@drop_frame,processcard_id=@processcardid,processcard_count=@processcard_count,engineering_id=@engineering_id where id=@drop_pos;
								INSERT into hn_user(update_time,add_time) VALUES(@processcardid,@work_groups);
								UPDATE gmms_galss_task set stop_position=@drop_pos where processcard_id=@processcardid and engineering_id=@engineering_id;				
							end if;			
						end if;
					end if;
					if @processcardid is not null then 
							SELECT @processcardid;	
							#  选定大小和槽
							SELECT  a.id,a.slicecage_rank,a.count,a.type_id,galss_id into @rkid,@rk,@count,@type_id,@galss_id from gmms_horizontal a inner join gmms_galss_task b on a.id=b.slicecage_rank  where a.processcard_id=@processcardid and a.state='使用中' and a.gourp=2 and a.flag=0 and engineering_id=@engineering_id order by b.width*b.height desc ,a.count DESC  limit 1;
							select @rkid;
							select @count;
							if @rk is not null and @rk<>0 then
							UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
							call intTo2ByteHex10_16_4(@rk,@rks);
							select @galss_ids,@rks,@date;
							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
							call message(dates,'000d',@date);
							set messagedate=@date;
							insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼出库2',@rk,@date);
							set a=1;
							end if;
						end if;
					
				end if;

				end if;
			
		end if;
		
		if a=0 then
		
				set @rk=null;
			select slicecage_rank into @rk from gmms_horizontal  
			where (gourp=1 and state='使用中' and count>0 and flag=0) or (gourp=2 and state='空闲')  group by slicecage_rank having count(*)=2 order by count desc limit 1;
			select @rk;
			if @rk is not null and @rk<>0 then
				select count(*) into @countstate from gmms_horizontal where  (state='出库中' or state='进库中');
				if @countstate=0 then
				UPDATE gmms_galss_task SET work_state='传送中' where slicecage_rank=@rk and engineering_id=@engineering_id;
				UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=1;
				select count,type_id,processcard_id into @count,@type_id,@processcard_id from gmms_horizontal where slicecage_rank=@rk and gourp=1;
				UPDATE gmms_horizontal SET state='进库中',count=@count,type_id=@type_id,processcard_id=@processcard_id where slicecage_rank=@rk and gourp=2;
				select count(*) into @counts from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id;
				select count(*) into @count1 from gmms_galss_task where work_state='直通' and work_procedure='理片' and engineering_id=@engineering_id;
				if @counts=1 then
					if @type_id <>0 and (@processcard_id is not null or @processcard_id<>'') then
						select galss_id into @galss_id1 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id;
						call intTo2ByteHex10_16_4(@galss_id1,@galss_ids1);
						call intTo2ByteHex10_16_4(@rk,@rks);
						set dates=CONCAT("00020000",@rks,"000200000000",@rks,"000000000000",@galss_ids1);
						call message(dates,'000a',@date);
						insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
							values (machineids,@work_groups,NOW(),'理片笼传送1',@rk,@date);
						set messagedate=@date;
						set a=1;
					end if;
				else 
					select galss_id into @galss_id1 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id limit 1;
					select galss_id into @galss_id2 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id limit 2;
					call intTo2ByteHex10_16_4(@galss_id1,@galss_ids1);
					call intTo2ByteHex10_16_4(@galss_id2,@galss_ids2);
					call intTo2ByteHex10_16_4(@rk,@rks);
					set dates=CONCAT("00020000",@rks,"000200000000",@rks,"000000000000",@galss_ids1,@galss_id2);
					call message(dates,'000a',@date);
					set a=1;
					set messagedate=@date;
					insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
							values (machineids,@work_groups,NOW(),'理片笼传送2',@rk,@date);
				end if;
				end if;
				
			elseif @count1>0 then
					set dates=CONCAT("000300000003","0003000000000003");
					call message(dates,'000a',@date);
					set messagedate=@date;
					set a=1;
			
			end if;
		end if;
		
			
			
		if a=0 then
				#进片
				set @type_id=0;
				set @processcardid=null;
				select rank,type_id,galss_id,processcard_id,width,height,slicecage_rank into @rankid,@type_id,@galss_id,@processcardid,@width,@height,@rk from gmms_galss_task where work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id order by rank limit 1;
				if @galss_id is not null and @rk<>0 and @rk is not null then
				UPDATE gmms_galss_task set work_state='进库中',finsh_time=now() WHERE rank=@rankid and work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id;
				UPDATE gmms_horizontal set state='进库中',count=1,type_id=@type_id,processcard_id=@processcardid where slicecage_rank=@rk and gourp=1;
				call intTo2ByteHex10_16_4(@rankid,@rankids);
				call intTo2ByteHex10_16_4(@rk,@rks);
				set dates=CONCAT("0001",@rankids,@rks);
				call message(dates,'000a',@date);
				set messagedate=@date;
				insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼进库1',@rk,@date);
				end if;
			end if;

		
	#出片
	elseif flags=2 and @counts6=0 then
				SET @processcardid=null;
				set @rkid=null;
				set @rk=null;
				set @galss_id=null;
				select `value` into @lastid from gmms_var where varname='last_out_glass_id'and groups=@work_groups;
				select `value` into @lastpos from gmms_var where varname='last_pos'and groups=@work_groups;
				if @lastid is not null then
					select @lastid;
					SELECT a.id,a.slicecage_rank,a.type_id,a.count,galss_id into @rkid,@rk,@type_id,@count,@galss_id from gmms_horizontal a 
					INNER JOIN gmms_galss_task b on a.id=b.slicecage_rank INNER JOIN gmms_drop_frame c on b.processcard_id=c.processcard_id 
					where a.state='使用中' and a.count>0 and a.type_id=@lastid and a.flag=0 and b.engineering_id=@engineering_id and  c.processcard_id is not null and a.gourp=2 and b.stop_position>0 
					order by a.count desc, ABS(a.slicecage_rank-@lastpos) asc LIMIT 1;
					if @rk is not null and @rk<>0 then
					select @rk;
						UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
							call intTo2ByteHex10_16_4(@rk,@rks);
							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
							call message(dates,'000d',@date);
							set messagedate=@date;
							insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼出库3',@rk,@date);
							set @发送字=1;
					end if;
				end if;
				if @发送字=0 then
					SELECT 流程卡 into @processcardid from processcard_ex where 下片位置 is not null and 库内数>0 and 总数-切割数=0 and 工程号=@engineering_id order by  总数-下架数  asc LIMIT 1;
					IF @processcardid is  null then
						select 2;
							#  后使用  未完全切完的
							SELECT 流程卡 into @processcardid from processcard_ex where 下片位置 is not null and 库内数>0 and 工程号=@engineering_id order by 总数-下架数  asc LIMIT 1;	
					end if;
					-- 看看空架 有没有 ，如果有  就找一个库能玻璃最多的下架
					IF @processcardid is null then 

						SET @drop_pos=null;
-- 						select min(edg_width),min(edg_height) into @minwidth,@minheight from gmms_galss_task where processcard_id=@processcardid and engineering_id=@engineering_id;
-- 						if @minwidth<=1200 or @minheight<=700 then
-- 							SELECT id into @drop_pos from gmms_drop_frame where state='空架' and groups=@work_groups and (stops=3 or stops=4 or stops=5) limit 1;
-- 						else
-- 							SELECT id into @drop_pos from gmms_drop_frame where state='空架' and groups=@work_groups and (stops=1 or stops=2) limit 1;
-- 						end if;
						SELECT id into @drop_pos from gmms_drop_frame where state='空架' and groups=@work_groups limit 1;
						if @drop_pos  is not null then 
							select name1 into @name1 from namess where groups=@work_groups;
							SELECT 流程卡,架号,总数,工程号 into @processcardid,@drop_frame,@processcard_count,@engineering_ids from processcard_ex where  (库内数>=@name1 or (库内数>0 and 总数<@name1)) and 工程号=@engineering_id order by  总数-库内数 LIMIT 1;
							#  流程卡占架子
							select count(*) into @counts2 from gmms_galss_task where processcard_id=@processcardid and engineering_id=@engineering_id;
							if @processcardid is not null and @counts2>0 then 
								UPDATE gmms_drop_frame set state='使用中',drop_frame=@drop_frame,processcard_id=@processcardid,processcard_count=@processcard_count,engineering_id=@engineering_id where id=@drop_pos;
								INSERT into hn_user(update_time,add_time) VALUES(@processcardid,@work_groups);
								UPDATE gmms_galss_task set stop_position=@drop_pos where processcard_id=@processcardid and engineering_id=@engineering_id;				
							end if;			
						end if;
					end if;
					if @processcardid is not null then 
							SELECT @processcardid;	
							#  选定大小和槽
							SELECT  a.id,a.slicecage_rank,a.count,a.type_id,galss_id into @rkid,@rk,@count,@type_id,@galss_id from gmms_horizontal a inner join gmms_galss_task b on a.id=b.slicecage_rank  where a.processcard_id=@processcardid and a.state='使用中' and a.gourp=2 and a.flag=0 and engineering_id=@engineering_id order by b.width*b.height desc ,a.count DESC  limit 1;
							select @count;
							if @rk is not null and @rk<>0 then
							UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
							call intTo2ByteHex10_16_4(@rk,@rks);
							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
							call message(dates,'000d',@date);
							set messagedate=@date;
							insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼出库4',@rk,@date);
							end if;
						end if;
					
				end if;

				
	#进片
	elseif flags=1 then
		set @rk=null;
		set @type_id=0;
				set @processcardid=null;
				select rank,type_id,galss_id,processcard_id,width,height,slicecage_rank into @rankid,@type_id,@galss_id,@processcardid,@width,@height,@rk from gmms_galss_task where work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id order by rank limit 1;
				if @galss_id is not null and @rk<>0 and @rk is not null then
				UPDATE gmms_galss_task set work_state='进库中',finsh_time=now() WHERE rank=@rankid and work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id;
				UPDATE gmms_horizontal set state='进库中',count=1,type_id=@type_id,processcard_id=@processcardid where slicecage_rank=@rk and gourp=1;
				call intTo2ByteHex10_16_4(@rankid,@rankids);
				call intTo2ByteHex10_16_4(@rk,@rks);
				set dates=CONCAT("0001",@rankids,@rks);
				call message(dates,'000a',@date);
				set messagedate=@date;
				insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼进库2',@rk,@date);
				end if;
	
	end if;

#直通
-- elseif @var='2' then
-- 
-- 
#存笼
elseif @var='3' then
	if flags=3 then
	set @type_id=null;
	set @count=null;
		select slicecage_rank into @rk from gmms_horizontal  where (gourp=1 and state='使用中' and count>0 and flag=0) or (gourp=2 and state='空闲')  group by slicecage_rank having count(*)=2 order by count desc limit 1;
		select @rk;
		if @rk is not null  then
				select count(*) into @countstate from gmms_horizontal where  (state='出库中' or state='进库中');
				if @countstate=0 then
  			UPDATE gmms_galss_task SET work_state='传送中' where slicecage_rank=@rk and engineering_id=@engineering_id;
  			UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=1;
 			  select count,type_id,processcard_id into @count,@type_id,@processcard_id from gmms_horizontal where slicecage_rank=@rk and gourp=1;
				
  			UPDATE gmms_horizontal SET state='进库中',count=@count,type_id=@type_id,processcard_id=@processcard_id where slicecage_rank=@rk and gourp=2;
			select count(*) into @counts from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id;
			select @counts;
			if @counts=1 then
				if @type_id <>0 and (@processcard_id is not null or @processcard_id<>'') then
					select galss_id into @galss_id1 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id;
					call intTo2ByteHex10_16_4(@galss_id1,@galss_ids1);
					call intTo2ByteHex10_16_4(@rk,@rks);
					set dates=CONCAT("00020000",@rks,"000200000000",@rks,"000000000000",@galss_ids1);
					call message(dates,'000a',@date);
					set messagedate=@date;
					insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼传送3',@rk,@date);
				end if;
			else 
			     	select @counts;
				end if;
			else 
				
				select galss_id into @galss_id1 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id limit 1;
				select galss_id into @galss_id2 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id limit 2;
				select @galss_id1,@galss_id2;
				call intTo2ByteHex10_16_4(@galss_id1,@galss_ids1);
				call intTo2ByteHex10_16_4(@galss_id2,@galss_ids2);
				call intTo2ByteHex10_16_4(@rk,@rks);
				set dates=CONCAT("00020000",@rks,"000200000000",@rks,"000000000000",@galss_ids1,@galss_id2);
				call message(dates,'000a',@date);
				set messagedate=@date;
				insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼传送4',@rk,@date);
			end if;
		else
			set @type_id=0;
			set @processcardid=null;
			select rank,type_id,galss_id,processcard_id,width,height,slicecage_rank into @rankid,@type_id,@galss_id,@processcardid,@width,@height,@rk from gmms_galss_task where work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id order by rank limit 1;
			if @galss_id is not null and @rk<>0 and @rk is not null then
				#UPDATE gmms_galss_task set slicecage_rank=@rk,work_procedure='理片',work_state='进库中' WHERE rank=@rankid and engineering_id=@engineering_id;
				UPDATE gmms_galss_task set work_state='进库中',finsh_time=now() WHERE rank=@rankid and work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id;
				UPDATE gmms_horizontal set state='进库中',count=1,type_id=@type_id,processcard_id=@processcardid where slicecage_rank=@rk and gourp=1;
				UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
				UPDATE gmms_var SET Value=@rankid where varname='last_out_glass_id' and groups=@work_groups;
				select @rankid,@rk;
				call intTo2ByteHex10_16_4(@rankid,@rankids);
				call intTo2ByteHex10_16_4(@rk,@rks);
				set dates=CONCAT("0001",@rankids,@rks);
				call message(dates,'000a',@date);
				set messagedate=@date;
				insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼进库3',@rk,@date);
			end if;

			
		end if;
		


				
	#进片
	elseif flags=1 then
		set @type_id=0;
				set @processcardid=null;
				select rank,type_id,galss_id,processcard_id,width,height,slicecage_rank into @rankid,@type_id,@galss_id,@processcardid,@width,@height,@rk from gmms_galss_task where work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id order by rank limit 1;
				if @galss_id is not null and @rk<>0 and @rk is not null then
				UPDATE gmms_galss_task set work_state='进库中',finsh_time=now() WHERE rank=@rankid and work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id;
				UPDATE gmms_horizontal set state='进库中',count=1,type_id=@type_id,processcard_id=@processcardid where slicecage_rank=@rk and gourp=1;
				call intTo2ByteHex10_16_4(@rankid,@rankids);
				call intTo2ByteHex10_16_4(@rk,@rks);
				UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
				UPDATE gmms_var SET Value=@rankid where varname='last_out_glass_id' and groups=@work_groups;
				set dates=CONCAT("0001",@rankids,@rks);
				call message(dates,'000a',@date);
				set messagedate=@date;
				insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼进库4',@rk,@date);
				end if;
	end if;
		
#出片
elseif @var='4' then

	if (flags=3 or flags=2) and @counts6=0 then
	
		SET @processcardid=null;
				set @rkid=null;
				set @rk=null;
				set @galss_id=null;
				select @lastid;
				select `value` into @lastid from gmms_var where varname='last_out_glass_id'and groups=@work_groups;
				select `value` into @lastpos from gmms_var where varname='last_pos'and groups=@work_groups;
				if @lastid is not null then
					select @lastid;
					SELECT a.id,a.slicecage_rank,a.type_id,a.count,galss_id into @rkid,@rk,@type_id,@count,@galss_id from gmms_horizontal a 
					INNER JOIN gmms_galss_task b on a.id=b.slicecage_rank INNER JOIN gmms_drop_frame c on b.processcard_id=c.processcard_id 
					where a.state='使用中' and a.count>0 and a.type_id=@lastid and a.flag=0 and b.engineering_id=@engineering_id and  c.processcard_id is not null and a.gourp=2 and b.stop_position>0 
					order by a.count desc, ABS(a.slicecage_rank-@lastpos) asc LIMIT 1;
					if @rk is not null and @rk<>0 then
					select @rk;
						UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
							call intTo2ByteHex10_16_4(@rk,@rks);
							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
							call message(dates,'000d',@date);
							set messagedate=@date;
							insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼出库5',@rk,@date);
							set @发送字=1;
					end if;
				end if;
				if @发送字=0 then
					SELECT 流程卡 into @processcardid from processcard_ex where 下片位置 is not null and 库内数>0 and 总数-切割数=0 and 工程号=@engineering_id order by  总数-下架数  asc LIMIT 1;
					IF @processcardid is  null then
						select 2;
							#  后使用  未完全切完的
							SELECT 流程卡 into @processcardid from processcard_ex where 下片位置 is not null and 库内数>0 and 工程号=@engineering_id order by 总数-下架数  asc LIMIT 1;	
					end if;
					-- 看看空架 有没有 ，如果有  就找一个库能玻璃最多的下架
					IF @processcardid is null then 

						SET @drop_pos=null;
-- 						select min(edg_width),min(edg_height) into @minwidth,@minheight from gmms_galss_task where processcard_id=@processcardid and engineering_id=@engineering_id;
-- 						if @minwidth<=1200 or @minheight<=700 then
-- 							SELECT id into @drop_pos from gmms_drop_frame where state='空架' and groups=@work_groups and (stops=3 or stops=4 or stops=5) limit 1;
-- 						else
-- 							SELECT id into @drop_pos from gmms_drop_frame where state='空架' and groups=@work_groups and (stops=1 or stops=2) limit 1;
-- 						end if;
						SELECT id into @drop_pos from gmms_drop_frame where state='空架' and groups=@work_groups limit 1;
						if @drop_pos  is not null then 
							select name1 into @name1 from namess where groups=@work_groups;
							SELECT 流程卡,架号,总数,工程号 into @processcardid,@drop_frame,@processcard_count,@engineering_ids from processcard_ex where  (库内数>=@name1 or (库内数>0 and 总数<@name1)) and 工程号=@engineering_id order by  总数-库内数 LIMIT 1;
							#  流程卡占架子
							select count(*) into @counts2 from gmms_galss_task where processcard_id=@processcardid and engineering_id=@engineering_id;
							if @processcardid is not null and @counts2>0 then 
								UPDATE gmms_drop_frame set state='使用中',drop_frame=@drop_frame,processcard_id=@processcardid,processcard_count=@processcard_count,engineering_id=@engineering_id where id=@drop_pos;
								INSERT into hn_user(update_time,add_time) VALUES(@processcardid,@work_groups);
								UPDATE gmms_galss_task set stop_position=@drop_pos where processcard_id=@processcardid and engineering_id=@engineering_id;				
							end if;			
						end if;
					end if;
					if @processcardid is not null then 
							SELECT @processcardid;	
							#  选定大小和槽
							SELECT  a.id,a.slicecage_rank,a.count,a.type_id,galss_id into @rkid,@rk,@count,@type_id,@galss_id from gmms_horizontal a inner join gmms_galss_task b on a.id=b.slicecage_rank  where a.processcard_id=@processcardid and a.state='使用中' and a.gourp=2 and a.flag=0 and engineering_id=@engineering_id order by b.width*b.height desc ,a.count DESC  limit 1;
							select @rkid;
							select @count;
							UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
							call intTo2ByteHex10_16_4(@rk,@rks);
							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
							call message(dates,'000d',@date);
							set messagedate=@date;
							insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼出库6',@rk,@date);
							set a=1;
						end if;
					
				end if;

	
	end if;
	
end if;
-- 
-- 	#传送
-- 	if flag=3 then
-- 		SELECT a.slicecage_rank,a.type_id,a.count into @rk,@type_id,@count from gmms_horizontal a INNER JOIN gmms_galss_task b on a.id=b.slicecage_rank where a.state='使用中' and a.count>0  and a.flag=0 and engineering_id=@engineering_id and a.gourp=1  order by a.count desc,a.slicecage_rank LIMIT 1;
-- 		select count(*) into @count1 from gmms_galss_task where work_state='直通' and work_procedure='理片';
-- 		select @rk;
-- 		if @rk is not null then
-- 			UPDATE gmms_galss_task SET work_state='传送中' where slicecage_rank=@rk and engineering_id=@engineering_id;
-- 			UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=1;
-- 			select count,type_id,processcard_id into @count,@type_id,@processcard_id from gmms_horizontal where slicecage_rank=@rk and gourp=1;
-- 			UPDATE gmms_horizontal SET state='进库中',count=@count,type_id=@type_id,processcard_id=@processcard_id where slicecage_rank=@rk and gourp=2;
-- 			select count(*) into @counts from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id;
-- 			if @counts=1 then
-- 				select galss_id into @galss_id1 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id;
-- 				call intTo2ByteHex10_16_4(@galss_id1,@galss_ids1);
-- 				call intTo2ByteHex10_16_4(@rk,@rks);
-- 				set dates=CONCAT("00020000",@rks,"000200000000",@rks,"000000000000",@galss_ids1);
-- 				call message(dates,'000a',@date);
-- 				set messagedate=@date;
-- 			else 
-- 				select galss_id into @galss_id1 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id limit 1;
-- 				select galss_id into @galss_id2 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id limit 2;
-- 				call intTo2ByteHex10_16_4(@galss_id1,@galss_ids1);
-- 				call intTo2ByteHex10_16_4(@galss_id2,@galss_ids2);
-- 				call intTo2ByteHex10_16_4(@rk,@rks);
-- 				set dates=CONCAT("00020000",@rks,"000200000000",@rks,"000000000000",@galss_ids1,@galss_id2);
-- 				call message(dates,'000a',@date);
-- 				set messagedate=@date;
-- 			end if;
-- 		elseif @count1>0 then
-- 				set dates=CONCAT("000300000003","0003000000000003");
-- 				call message(dates,'000a',@date);
-- 				set messagedate=@date;
-- 		else
-- 			select 库内数 into @kuneishu from processcard1 where 工程号=@engineering_id limit 1;
-- 			select @work_groups;
-- 			select count(*) into @counts from gmms_horizontal where state='空闲' and gourp=2;
-- 			if a=0 then
-- 				if @kuneishu>0 or @counts<=1 then
-- 				#出片
-- 				SET @processcardid=null;
-- 				set @rkid=null;
-- 				set @rk=null;
-- 				set @galss_id=null;
-- 				select `value` into @lastid from gmms_var where varname='last_out_glass_id'and groups=@work_groups;
-- 				select `value` into @lastpos from gmms_var where varname='last_pos'and groups=@work_groups;
-- 				if @lastid is not null then
-- 					select @lastid;
-- 					SELECT a.id,a.slicecage_rank,a.type_id,a.count,galss_id into @rkid,@rk,@type_id,@count,@galss_id from gmms_horizontal a INNER JOIN gmms_galss_task b on a.id=b.slicecage_rank where a.state='使用中' and a.count>0 and a.type_id=@lastid and a.flag=0 and engineering_id=@engineering_id and a.gourp=2 and b.stop_position>0 order by a.count desc, ABS(a.slicecage_rank-@lastpos) asc LIMIT 1;
-- 					if @rk is not null then
-- 					select @rk;
-- 						UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
-- 							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
-- 							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
-- 							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
-- 							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
-- 							call intTo2ByteHex10_16_4(@rk,@rks);
-- 							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
-- 							call message(dates,'000d',@date);
-- 							set messagedate=@date;
-- 							set @发送字=1;
-- 					end if;
-- 				end if;
-- 				if @发送字=0 then
-- 					SELECT 流程卡 into @processcardid from processcard_ex1 where 下片位置 is not null and 库内数>0 and 总数-切割数=0 and 工程号=@engineering_id order by  总数-下架数  asc LIMIT 1;
-- 					IF @processcardid is  null then
-- 						select 2;
-- 							#  后使用  未完全切完的
-- 							SELECT 流程卡 into @processcardid from processcard_ex1 where 下片位置 is not null and 库内数>0 and 工程号=@engineering_id order by 总数-下架数  asc LIMIT 1;	
-- 					end if;
-- 					-- 看看空架 有没有 ，如果有  就找一个库能玻璃最多的下架
-- 					IF @processcardid is null then 
-- 
-- 						SET @drop_pos=null;
-- 						SELECT id into @drop_pos from gmms_drop_frame where state='空架' and groups=@work_groups limit 1;
-- 						if @drop_pos  is not null then 
-- 							select name1 into @name1 from namess where groups=@work_groups;
-- 							SELECT 流程卡,架号,总数,工程号 into @processcardid,@drop_frame,@processcard_count,@engineering_ids from processcard_ex1 where  (库内数>=@name1 or (库内数>0 and 总数<@name1)) order by  库内数 DESC LIMIT 1;
-- 							#  流程卡占架子
-- 							if @processcardid is not null then 
-- 								UPDATE gmms_drop_frame set state='使用中',drop_frame=@drop_frame,processcard_id=@processcardid,processcard_count=@processcard_count,engineering_id=@engineering_ids where id=@drop_pos;
-- 								UPDATE gmms_galss_task set stop_position=@drop_pos where processcard_id=@processcardid and engineering_id=@engineering_id;				
-- 							end if;			
-- 						end if;
-- 					end if;
-- 					if @processcardid is not null then 
-- 							SELECT @processcardid;	
-- 							#  选定大小和槽
-- 							SELECT  a.id,a.slicecage_rank,a.count,a.type_id,galss_id into @rkid,@rk,@count,@type_id,@galss_id from gmms_horizontal a inner join gmms_galss_task b on a.type_id=b.type_id where a.processcard_id=@processcardid and a.state='使用中' and a.gourp=2 and a.flag=0 and engineering_id=@engineering_id order by b.width*b.height desc ,a.count DESC  limit 1;
-- 							select @count;
-- 							UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
-- 							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
-- 							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
-- 							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
-- 							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
-- 							call intTo2ByteHex10_16_4(@rk,@rks);
-- 							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
-- 							call message(dates,'000d',@date);
-- 							set messagedate=@date;
-- 							set a=1;
-- 						end if;
-- 					
-- 				end if;
-- -- 							SELECT  a.id,a.slicecage_rank,a.count,a.type_id,galss_id into @rkid,@rk,@count,@type_id,@galss_id from gmms_horizontal a inner join gmms_galss_task b on a.id=b.slicecage_rank where  a.state='使用中' and a.gourp=2 and a.flag=0 and engineering_id=@engineering_id order by b.width*b.height desc ,a.count DESC  limit 1;
-- -- 							select @count;
-- -- 							UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
-- -- 							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
-- -- 							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
-- -- 							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
-- -- 							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
-- -- 							call intTo2ByteHex10_16_4(@rk,@rks);
-- -- 							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
-- -- 							call message(dates,'000d',@date);
-- -- 							set messagedate=@date;
-- -- 							set a=1;
-- 				end if;
-- 			end if;
-- 			
-- 			if a=0 then
-- 				#进片
-- 				select rank,type_id,galss_id,processcard_id,width,height,slicecage_rank into @rankid,@type_id,@galss_id,@processcardid,@width,@height,@rk from gmms_galss_task where work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id order by rank limit 1;
-- 				UPDATE gmms_galss_task set slicecage_rank=@rk,work_procedure='理片',work_state='进库中' WHERE rank=@rankid and engineering_id=@engineering_id;
-- 				UPDATE gmms_horizontal set state='进库中',count=1,type_id=@type_id,processcard_id=@processcardid where slicecage_rank=@rk and gourp=1;
-- 				call intTo2ByteHex10_16_4(@rankid,@rankids);
-- 				call intTo2ByteHex10_16_4(@rk,@rks);
-- 				set dates=CONCAT("0001",@rankids,@rks);
-- 				call message(dates,'000a',@date);
-- 				set messagedate=@date;
-- 			end if;
-- 				
-- 	
-- 		end if;
-- 		
-- 	#出片
-- 	elseif flag=2 then
-- 				SET @processcardid=null;
-- 				set @rkid=null;
-- 				set @rk=null;
-- 				set @galss_id=null;
-- 				select `value` into @lastid from gmms_var where varname='last_out_glass_id'and groups=@work_groups;
-- 				select `value` into @lastpos from gmms_var where varname='last_pos'and groups=@work_groups;
-- 				if @lastid is not null then
-- 					select @lastid;
-- 					SELECT a.id,a.slicecage_rank,a.type_id,a.count,galss_id into @rkid,@rk,@type_id,@count,@galss_id from gmms_horizontal a INNER JOIN gmms_galss_task b on a.id=b.slicecage_rank where a.state='使用中' and a.count>0 and a.type_id=@lastid and a.flag=0 and engineering_id=@engineering_id and a.gourp=2 and b.stop_position>0 order by a.count desc, ABS(a.slicecage_rank-@lastpos) asc LIMIT 1;
-- 					if @rk is not null then
-- 					select @rk;
-- 						UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
-- 							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
-- 							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
-- 							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
-- 							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
-- 							call intTo2ByteHex10_16_4(@rk,@rks);
-- 							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
-- 							call message(dates,'000d',@date);
-- 							set messagedate=@date;
-- 							set @发送字=1;
-- 					end if;
-- 				end if;
-- 				if @发送字=0 then
-- 					SELECT 流程卡 into @processcardid from processcard_ex1 where 下片位置 is not null and 库内数>0 and 总数-切割数=0 and 工程号=@engineering_id order by  总数-下架数  asc LIMIT 1;
-- 					IF @processcardid is  null then
-- 						select 2;
-- 							#  后使用  未完全切完的
-- 							SELECT 流程卡 into @processcardid from processcard_ex1 where 下片位置 is not null and 库内数>0 and 工程号=@engineering_id order by 总数-下架数  asc LIMIT 1;	
-- 					end if;
-- 					-- 看看空架 有没有 ，如果有  就找一个库能玻璃最多的下架
-- 					IF @processcardid is null then 
-- 
-- 						SET @drop_pos=null;
-- 						SELECT id into @drop_pos from gmms_drop_frame where state='空架' and groups=@work_groups limit 1;
-- 						if @drop_pos  is not null then 
-- 							select name1 into @name1 from namess where groups=@work_groups;
-- 							SELECT 流程卡,架号,总数,工程号 into @processcardid,@drop_frame,@processcard_count,@engineering_ids from processcard_ex1 where  (库内数>=@name1 or (库内数>0 and 总数<@name1)) order by  库内数 DESC LIMIT 1;
-- 							#  流程卡占架子
-- 							if @processcardid is not null then 
-- 								UPDATE gmms_drop_frame set state='使用中',drop_frame=@drop_frame,processcard_id=@processcardid,processcard_count=@processcard_count,engineering_id=@engineering_ids where id=@drop_pos;
-- 								UPDATE gmms_galss_task set stop_position=@drop_pos where processcard_id=@processcardid and engineering_id=@engineering_id;				
-- 							end if;			
-- 						end if;
-- 					end if;
-- 					if @processcardid is not null then 
-- 							SELECT @processcardid;	
-- 							#  选定大小和槽
-- 							SELECT  a.id,a.slicecage_rank,a.count,a.type_id,galss_id into @rkid,@rk,@count,@type_id,@galss_id from gmms_horizontal a inner join gmms_galss_task b on a.type_id=b.type_id where a.processcard_id=@processcardid and a.state='使用中' and a.gourp=2 and a.flag=0 and engineering_id=@engineering_id order by b.width*b.height desc ,a.count DESC  limit 1;
-- 							select @count;
-- 							UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
-- 							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
-- 							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
-- 							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
-- 							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
-- 							call intTo2ByteHex10_16_4(@rk,@rks);
-- 							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
-- 							call message(dates,'000d',@date);
-- 							set messagedate=@date;
-- 						end if;
-- 					
-- 				end if;
-- -- 						SELECT  a.id,a.slicecage_rank,a.count,a.type_id,galss_id into @rkid,@rk,@count,@type_id,@galss_id from gmms_horizontal a inner join gmms_galss_task b on a.id=b.slicecage_rank where  a.state='使用中' and a.gourp=2 and a.flag=0 and engineering_id=@engineering_id order by b.width*b.height desc ,a.count DESC  limit 1;
-- -- 							select @count;
-- -- 							UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
-- -- 							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
-- -- 							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
-- -- 							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
-- -- 							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
-- -- 							call intTo2ByteHex10_16_4(@rk,@rks);
-- -- 							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
-- -- 							call message(dates,'000d',@date);
-- -- 							set messagedate=@date;
-- 				
-- 	#进片
-- 	elseif flag=1 then
-- 		select rank,type_id,galss_id,processcard_id,width,height,slicecage_rank into @rankid,@type_id,@galss_id,@processcardid,@width,@height,@rk from gmms_galss_task where work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id order by rank limit 1;
-- 		UPDATE gmms_galss_task set slicecage_rank=@rk,work_procedure='理片',work_state='进库中' WHERE rank=@rankid and engineering_id=@engineering_id;
-- 		UPDATE gmms_horizontal set state='进库中',count=1,type_id=@type_id,processcard_id=@processcardid where slicecage_rank=@rk and gourp=1;
-- 		call intTo2ByteHex10_16_4(@rankid,@rankids);
-- 		call intTo2ByteHex10_16_4(@rk,@rks);
-- 		set dates=CONCAT("0001",@rankids,@rks);
-- 		call message(dates,'000a',@date);
-- 		set messagedate=@date;
-- 	
-- 	end if;
		
	
	
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for HorizontalSend_copy2
-- ----------------------------
DROP PROCEDURE IF EXISTS `HorizontalSend_copy2`;
delimiter ;;
CREATE PROCEDURE `HorizontalSend_copy2`(in machineids BIGINT,in returnvalue varchar(255),in flags int,out messagedate varchar(255))
BEGIN
	#理片笼请求存储过程
	#判断是进片还是出片还是进出片请求
	#发送数据：玻璃编号，理片笼架子
	DECLARE a int DEFAULT 0;
	DECLARE ranks int DEFAULT 0;
	DECLARE dates varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	DECLARE maxw int DEFAULT 0;
	DECLARE maxh int DEFAULT 0;
	DECLARE minw int DEFAULT 0;
	DECLARE minh int DEFAULT 0;
	DECLARE kaobian varchar(255) DEFAULT null;
	set @create_time_=null;
	set @end_time_=null;
	set @create_time_=SYSDATE();
	set messagedate=messagedates;
	

select machine_type_id,work_id into @mtid,@work_groups from gmms_machine where id=machineids;

select @work_groups;
	
select file_name into @engineering_id from gmms_raw_file where operator=@work_groups and state='已领取';

select @engineering_id;

select var into @var from gmms_slicecage_pattern where groups=@work_groups;

select @var;
set @counts6=0;
set @counts7=0;
set @counts8=0;
set @counts9=0;
SET @发送字=0;
select count(*) into @counts6 from gmms_galss_task  where work_state='直通' and work_procedure='理片' and engineering_id=@engineering_id;
select count(*) into @counts7 from gmms_galss_task  where work_state='进库中' and work_procedure='理片' and engineering_id=@engineering_id;
select count(*) into @counts8 from gmms_galss_task  where work_state='出库中' and work_procedure='理片' and engineering_id=@engineering_id;
select count(*) into @counts9 from gmms_galss_task  where work_state='传送中' and work_procedure='理片' and engineering_id=@engineering_id;
#select count(*) into @counts10 from gmms_galss_task  where work_state='传送中' and work_procedure='理片' and engineering_id=@engineering_id;

#进出片
if @var='1' then
		#传送
	if flags=3 and @counts9=0 then
		if a=0 and @counts8=0 then
			set @kuneishu=0;
			select 库内数 into @kuneishu from processcard1 where 工程号=@engineering_id and 库内数>0 order by 库内数 desc  limit 0,1;
			select @kuneishu;
			select @work_groups;
			select count(*) into @counts from gmms_horizontal where state='空闲' and gourp=2;
			
				if (@kuneishu>0 or @counts<=1) and @counts6=0 then
				#出片
				SET @processcardid=null;
				set @rkid=null;
				set @rk=null;
				set @galss_id=null;
				select `value` into @lastid from gmms_var where varname='last_out_glass_id'and groups=@work_groups;
				select `value` into @lastpos from gmms_var where varname='last_pos'and groups=@work_groups;
				if @lastid is not null then
					select @lastid;
					select @lastpos;
					select '进入';
					
					SELECT a.id,a.slicecage_rank,a.type_id,a.count,galss_id into @rkid,@rk,@type_id,@count,@galss_id from gmms_horizontal a 
					INNER JOIN gmms_galss_task b on a.id=b.slicecage_rank INNER JOIN gmms_drop_frame c on b.processcard_id=c.processcard_id 
					where a.state='使用中' and a.count>0 and a.type_id=@lastid and a.flag=0 and b.engineering_id=@engineering_id and  c.processcard_id is not null and a.gourp=2 and b.stop_position>0 
					order by a.count desc, ABS(a.slicecage_rank-@lastpos) asc LIMIT 1;
					select @rkid;
					if @rk is not null and @rk<>0 then
					select @rk;
						UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
							set @galss_ids=null;
							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
							select @galss_ids;
							call intTo2ByteHex10_16_4(@rk,@rks);
							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
							call message(dates,'000d',@date);
							set messagedate=@date;
							insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼出库1',@rk,@date);
							set @发送字=1;
							set a=1;
					end if;
				end if;
				if @发送字=0 then
					SELECT 流程卡 into @processcardid from processcard_ex where 下片位置 is not null and 库内数>0 and 总数-切割数=0 and 工程号=@engineering_id order by  总数-下架数  asc LIMIT 1;
					IF @processcardid is  null then
						select 2;
							#  后使用  未完全切完的
							SELECT 流程卡 into @processcardid from processcard_ex where 下片位置 is not null and 库内数>0 and 工程号=@engineering_id order by 总数-下架数  asc LIMIT 1;	
					end if;
					-- 看看空架 有没有 ，如果有  就找一个库能玻璃最多的下架

					IF @processcardid is null then 

						SET @drop_pos=null;
						
						select name1 into @name1 from namess where groups=@work_groups;
						SELECT 流程卡,架号,总数,工程号 into @processcardid,@drop_frame,@processcard_count,@engineering_ids from processcard_ex 
						where  (库内数>=@name1 or (库内数>0 and 总数<@name1)) and 工程号=@engineering_id order by  总数-库内数 LIMIT 1;
						
						#得到最小的 和最大的玻璃数据  判断往哪里靠边  2023-8-30
						select MAX(width),MAX(height),MIN(width),MIN(height) into maxw,maxh,minw,minh from gmms_galss_task where processcard_id=@processcardid;
						#if maxw<2400 and maxh<1900 and minw>599 and minh>349 then
						if maxw<2400 and maxh<1900 and minw>529 and minh>349 then
							set kaobian='机器人';
						else
							set kaobian='下片机';	
						end if;
						select kaobian;
						if kaobian='下片机' then
							select id into @drop_pos from gmms_drop_frame where state='空架' and groups=@work_groups and (stops=1 or stops=2) limit 1;
						elseif kaobian='机器人' then
							select id into @drop_pos from gmms_drop_frame where state='空架' and groups=@work_groups and (stops=3 or stops=4 or stops=5) limit 1;
						end if;
						set kaobian=null;
						if @processcardid is not null and @drop_pos  is not null then 						
							#  流程卡占架子
							select @processcardid;
							select @drop_pos;
							select count(*) into @counts2 from gmms_galss_task where processcard_id=@processcardid and engineering_id=@engineering_id;
							if @counts2>0 then 
								UPDATE gmms_drop_frame set state='使用中',drop_frame=@drop_frame,processcard_id=@processcardid,processcard_count=@processcard_count,engineering_id=@engineering_id where id=@drop_pos;
								INSERT into hn_user(update_time,add_time) VALUES(@processcardid,@work_groups);
								UPDATE gmms_galss_task set stop_position=@drop_pos where processcard_id=@processcardid and engineering_id=@engineering_id;				
							end if;
						else
							set @processcardid=null;
							set @drop_pos=null;
						end if;
					end if;
					if @processcardid is not null then 
							SELECT @processcardid;	
							#  选定大小和槽
							SELECT  a.id,a.slicecage_rank,a.count,a.type_id,galss_id into @rkid,@rk,@count,@type_id,@galss_id from gmms_horizontal a 
							inner join gmms_galss_task b on a.id=b.slicecage_rank  where a.processcard_id=@processcardid and a.state='使用中' and a.gourp=2 and a.flag=0 and engineering_id=@engineering_id 
							order by b.width*b.height desc ,a.count DESC  limit 1;
							select @rkid;
							select @count;
							select a;
							if @rk is not null and @rk<>0 then
							UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
							call intTo2ByteHex10_16_4(@rk,@rks);
							select @galss_ids,@rks,@date;
							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
							call message(dates,'000d',@date);
							set messagedate=@date;
							insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼出库2',@rk,@date);
							set a=1;
							end if;
						end if;
					
				end if;

				end if;
			
		end if;
		select a;
		if a=0 and @counts7=0 and @counts8=0 then
		
			#set @rk=null;
			#select slicecage_rank into @rk from gmms_horizontal  
			#where (gourp=1 and state='使用中' and count>0 and flag=0) or (gourp=2 and state='空闲')  group by slicecage_rank having count(*)=2 order by count desc limit 1;
			
				#尝试代码开始
				SET @processcardid=null;
				set @rkid=null;
				set @rk=null;
				set @galss_id=null;
				select `value` into @lastid from gmms_var where varname='last_out_glass_id'and groups=@work_groups;
				select `value` into @lastpos from gmms_var where varname='last_pos'and groups=@work_groups;
				if @lastid is not null then
					select @lastid;
					select @lastpos;
					select '进入';
					
					SELECT a.id,a.slicecage_rank,a.type_id,a.count,galss_id into @rkid,@rk,@type_id,@count,@galss_id from gmms_horizontal a 
					INNER JOIN gmms_galss_task b on a.id=b.slicecage_rank INNER JOIN gmms_drop_frame c on b.processcard_id=c.processcard_id 
					where a.state='使用中' and a.count>0 and a.type_id=@lastid and a.flag=0 and b.engineering_id=@engineering_id and  c.processcard_id is not null and a.gourp=1 and b.stop_position>0 
					order by a.count desc, ABS(a.slicecage_rank-@lastpos) asc LIMIT 1;
				  
					select @rkid; 
					if @rk is null then
					 select slicecage_rank into @rk from gmms_horizontal  
			     where (gourp=1 and state='使用中' and count>0 and flag=0) or (gourp=2 and state='空闲')  group by slicecage_rank having count(*)=2 order by count desc limit 1;
					end if;
				end if;
			#尝试代码结束		
			
			
			select @rk;
			if @rk is not null and @rk<>0 then
				select count(*) into @countstate from gmms_horizontal where  (state='出库中' or state='进库中');
				if @countstate=0 then
				UPDATE gmms_galss_task SET work_state='传送中' where slicecage_rank=@rk and engineering_id=@engineering_id;
				UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=1;
				select count,type_id,processcard_id into @count,@type_id,@processcard_id from gmms_horizontal where slicecage_rank=@rk and gourp=1;
				UPDATE gmms_horizontal SET state='进库中',count=@count,type_id=@type_id,processcard_id=@processcard_id where slicecage_rank=@rk and gourp=2;
				select count(*) into @counts from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id;
				select count(*) into @count1 from gmms_galss_task where work_state='直通' and work_procedure='理片' and engineering_id=@engineering_id;
				if @counts=1 then
					if @type_id <>0 and (@processcard_id is not null or @processcard_id<>'') then
						select galss_id into @galss_id1 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id;
						call intTo2ByteHex10_16_4(@galss_id1,@galss_ids1);
						call intTo2ByteHex10_16_4(@rk,@rks);
						set dates=CONCAT("00020000",@rks,"000200000000",@rks,"000000000000",@galss_ids1);
						call message(dates,'000a',@date);
						insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
							values (machineids,@work_groups,NOW(),'理片笼传送1',@rk,@date);
						set messagedate=@date;
						set a=1;
					end if;
				else 
					select galss_id into @galss_id1 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id limit 1;
					select galss_id into @galss_id2 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id limit 2;
					call intTo2ByteHex10_16_4(@galss_id1,@galss_ids1);
					call intTo2ByteHex10_16_4(@galss_id2,@galss_ids2);
					call intTo2ByteHex10_16_4(@rk,@rks);
					set dates=CONCAT("00020000",@rks,"000200000000",@rks,"000000000000",@galss_ids1,@galss_id2);
					call message(dates,'000a',@date);
					set a=1;
					set messagedate=@date;
					insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
							values (machineids,@work_groups,NOW(),'理片笼传送2',@rk,@date);
				end if;
				end if;
				
			elseif @count1>0 then
					set dates=CONCAT("000300000003","0003000000000003");
					call message(dates,'000a',@date);
					set messagedate=@date;
					set a=1;
			
			end if;
		end if;
		
		if a=0 and @counts7=0 then
				#进片
				set @type_id=0;
				set @processcardid=null;
				select rank,type_id,galss_id,processcard_id,width,height,slicecage_rank into @rankid,@type_id,@galss_id,@processcardid,@width,@height,@rk from gmms_galss_task where work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id order by rank limit 1;
				if @galss_id is not null and @rk<>0 and @rk is not null then
				UPDATE gmms_galss_task set work_state='进库中',finsh_time=now() WHERE rank=@rankid and work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id;
				UPDATE gmms_horizontal set state='进库中',count=1,type_id=@type_id,processcard_id=@processcardid where slicecage_rank=@rk and gourp=1;
				call intTo2ByteHex10_16_4(@rankid,@rankids);
				call intTo2ByteHex10_16_4(@rk,@rks);
				set dates=CONCAT("0001",@rankids,@rks);
				call message(dates,'000a',@date);
				set messagedate=@date;
				insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼进库1',@rk,@date);
				end if;
			end if;

		
	#出片
	elseif flags=2 and @counts6=0 and @counts8=0 and @counts9=0 then
				SET @processcardid=null;
				set @rkid=null;
				set @rk=null;
				set @galss_id=null;
				select `value` into @lastid from gmms_var where varname='last_out_glass_id'and groups=@work_groups;
				select `value` into @lastpos from gmms_var where varname='last_pos'and groups=@work_groups;
				if @lastid is not null then
					select @lastid;
					SELECT a.id,a.slicecage_rank,a.type_id,a.count,galss_id into @rkid,@rk,@type_id,@count,@galss_id from gmms_horizontal a 
					INNER JOIN gmms_galss_task b on a.id=b.slicecage_rank INNER JOIN gmms_drop_frame c on b.processcard_id=c.processcard_id 
					where a.state='使用中' and a.count>0 and a.type_id=@lastid and a.flag=0 and b.engineering_id=@engineering_id and  c.processcard_id is not null and a.gourp=2 and b.stop_position>0 
					order by a.count desc, ABS(a.slicecage_rank-@lastpos) asc LIMIT 1;
					if @rk is not null and @rk<>0 then
					select @rk;
						UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
							call intTo2ByteHex10_16_4(@rk,@rks);
							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
							call message(dates,'000d',@date);
							set messagedate=@date;
							insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼出库3',@rk,@date);
							set @发送字=1;
					end if;
				end if;
				if @发送字=0 then
					SELECT 流程卡 into @processcardid from processcard_ex where 下片位置 is not null and 库内数>0 and 总数-切割数=0 and 工程号=@engineering_id order by  总数-下架数  asc LIMIT 1;
					IF @processcardid is  null then
						select 2;
							#  后使用  未完全切完的
							SELECT 流程卡 into @processcardid from processcard_ex where 下片位置 is not null and 库内数>0 and 工程号=@engineering_id order by 总数-下架数  asc LIMIT 1;	
					end if;
					-- 看看空架 有没有 ，如果有  就找一个库能玻璃最多的下架
					IF @processcardid is null then 

						SET @drop_pos=null;
						SELECT 流程卡,架号,总数,工程号 into @processcardid,@drop_frame,@processcard_count,@engineering_ids from processcard_ex 
						where  (库内数>=@name1 or (库内数>0 and 总数<@name1)) and 工程号=@engineering_id order by  总数-库内数 LIMIT 1;
							
						#得到最小的 和最大的玻璃数据  判断往哪里靠边  2023-8-30
						select MAX(width),MAX(height),MIN(width),MIN(height) into maxw,maxh,minw,minh from gmms_galss_task where processcard_id=@processcardid;
						if maxw<2400 and maxh<1900 and minw>599 and minh>349 then
							set kaobian='机器人';
						else
							set kaobian='下片机';	
						end if;
						if kaobian='下片机' then
							select id into @drop_pos from gmms_drop_frame where state='空架' and groups=@work_groups and (stops=1 or stops=2) limit 1;
						elseif kaobian='机器人' then
							select id into @drop_pos from gmms_drop_frame where state='空架' and groups=@work_groups and (stops=3 or stops=4 or stops=5) limit 1;
						end if;
						set kaobian=null;
						
						if @processcardid is not null and @drop_pos  is not null then 
							select name1 into @name1 from namess where groups=@work_groups;
							#  流程卡占架子
							select count(*) into @counts2 from gmms_galss_task where processcard_id=@processcardid and engineering_id=@engineering_id;
							if @counts2>0 then 
								UPDATE gmms_drop_frame set state='使用中',drop_frame=@drop_frame,processcard_id=@processcardid,processcard_count=@processcard_count,engineering_id=@engineering_id where id=@drop_pos;
								INSERT into hn_user(update_time,add_time) VALUES(@processcardid,@work_groups);
								UPDATE gmms_galss_task set stop_position=@drop_pos where processcard_id=@processcardid and engineering_id=@engineering_id;				
							end if;
						else
							set @processcardid=null;
							set @drop_pos=null;
						end if;
					end if;
					if @processcardid is not null then 
							SELECT @processcardid;	
							#  选定大小和槽
							SELECT  a.id,a.slicecage_rank,a.count,a.type_id,galss_id into @rkid,@rk,@count,@type_id,@galss_id from gmms_horizontal a 
							inner join gmms_galss_task b on a.id=b.slicecage_rank  
							where a.processcard_id=@processcardid and a.state='使用中' and a.gourp=2 and a.flag=0 and engineering_id=@engineering_id 
							order by b.width*b.height desc ,a.count DESC  limit 1;
							select @count;
							if @rk is not null and @rk<>0 then
							UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
							call intTo2ByteHex10_16_4(@rk,@rks);
							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
							call message(dates,'000d',@date);
							set messagedate=@date;
							insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼出库4',@rk,@date);
							end if;
						end if;
					
				end if;

				
	#进片
	elseif flags=1 and @counts7=0 and @counts9=0 then
		set @rk=null;
		set @type_id=0;
				set @processcardid=null;
				select rank,type_id,galss_id,processcard_id,width,height,slicecage_rank into @rankid,@type_id,@galss_id,@processcardid,@width,@height,@rk from gmms_galss_task where work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id order by rank limit 1;
				if @galss_id is not null and @rk<>0 and @rk is not null then
				UPDATE gmms_galss_task set work_state='进库中',finsh_time=now() WHERE rank=@rankid and work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id;
				UPDATE gmms_horizontal set state='进库中',count=1,type_id=@type_id,processcard_id=@processcardid where slicecage_rank=@rk and gourp=1;
				call intTo2ByteHex10_16_4(@rankid,@rankids);
				call intTo2ByteHex10_16_4(@rk,@rks);
				set dates=CONCAT("0001",@rankids,@rks);
				call message(dates,'000a',@date);
				set messagedate=@date;
				insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼进库2',@rk,@date);
				end if;
	
	end if;

#直通
-- elseif @var='2' then
-- 
-- 
#存笼
elseif @var='3' then
	if flags=3 and @counts7=0 and @counts8=0 and @counts9=0 then
	set @type_id=null;
	set @count=null;
		select slicecage_rank into @rk from gmms_horizontal  where (gourp=1 and state='使用中' and count>0 and flag=0) or (gourp=2 and state='空闲')  group by slicecage_rank having count(*)=2 order by count desc limit 1;
		select @rk;
		if @rk is not null  then
				select count(*) into @countstate from gmms_horizontal where  (state='出库中' or state='进库中');
				if @countstate=0 then
  			UPDATE gmms_galss_task SET work_state='传送中' where slicecage_rank=@rk and engineering_id=@engineering_id;
  			UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=1;
 			  select count,type_id,processcard_id into @count,@type_id,@processcard_id from gmms_horizontal where slicecage_rank=@rk and gourp=1;
				
  			UPDATE gmms_horizontal SET state='进库中',count=@count,type_id=@type_id,processcard_id=@processcard_id where slicecage_rank=@rk and gourp=2;
			select count(*) into @counts from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id;
			select @counts;
			if @counts=1 then
				if @type_id <>0 and (@processcard_id is not null or @processcard_id<>'') then
					select galss_id into @galss_id1 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id;
					call intTo2ByteHex10_16_4(@galss_id1,@galss_ids1);
					call intTo2ByteHex10_16_4(@rk,@rks);
					set dates=CONCAT("00020000",@rks,"000200000000",@rks,"000000000000",@galss_ids1);
					call message(dates,'000a',@date);
					set messagedate=@date;
					insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼传送3',@rk,@date);
				end if;
			else 
			     	select @counts;
				end if;
			else 
				
				select galss_id into @galss_id1 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id limit 1;
				select galss_id into @galss_id2 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id limit 2;
				select @galss_id1,@galss_id2;
				call intTo2ByteHex10_16_4(@galss_id1,@galss_ids1);
				call intTo2ByteHex10_16_4(@galss_id2,@galss_ids2);
				call intTo2ByteHex10_16_4(@rk,@rks);
				set dates=CONCAT("00020000",@rks,"000200000000",@rks,"000000000000",@galss_ids1,@galss_id2);
				call message(dates,'000a',@date);
				set messagedate=@date;
				insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼传送4',@rk,@date);
			end if;
		else
			set @type_id=0;
			set @processcardid=null;
			select rank,type_id,galss_id,processcard_id,width,height,slicecage_rank into @rankid,@type_id,@galss_id,@processcardid,@width,@height,@rk from gmms_galss_task where work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id order by rank limit 1;
			if @galss_id is not null and @rk<>0 and @rk is not null then
				#UPDATE gmms_galss_task set slicecage_rank=@rk,work_procedure='理片',work_state='进库中' WHERE rank=@rankid and engineering_id=@engineering_id;
				UPDATE gmms_galss_task set work_state='进库中',finsh_time=now() WHERE rank=@rankid and work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id;
				UPDATE gmms_horizontal set state='进库中',count=1,type_id=@type_id,processcard_id=@processcardid where slicecage_rank=@rk and gourp=1;
				UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
				UPDATE gmms_var SET Value=@rankid where varname='last_out_glass_id' and groups=@work_groups;
				select @rankid,@rk;
				call intTo2ByteHex10_16_4(@rankid,@rankids);
				call intTo2ByteHex10_16_4(@rk,@rks);
				set dates=CONCAT("0001",@rankids,@rks);
				call message(dates,'000a',@date);
				set messagedate=@date;
				insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼进库3',@rk,@date);
			end if;

			
		end if;
		


				
	#进片
	elseif flags=1 and @counts7=0 and @counts9=0 then
		set @type_id=0;
				set @processcardid=null;
				select rank,type_id,galss_id,processcard_id,width,height,slicecage_rank into @rankid,@type_id,@galss_id,@processcardid,@width,@height,@rk from gmms_galss_task where work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id order by rank limit 1;
				if @galss_id is not null and @rk<>0 and @rk is not null then
				UPDATE gmms_galss_task set work_state='进库中',finsh_time=now() WHERE rank=@rankid and work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id;
				UPDATE gmms_horizontal set state='进库中',count=1,type_id=@type_id,processcard_id=@processcardid where slicecage_rank=@rk and gourp=1;
				call intTo2ByteHex10_16_4(@rankid,@rankids);
				call intTo2ByteHex10_16_4(@rk,@rks);
				UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
				UPDATE gmms_var SET Value=@rankid where varname='last_out_glass_id' and groups=@work_groups;
				set dates=CONCAT("0001",@rankids,@rks);
				call message(dates,'000a',@date);
				set messagedate=@date;
				insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼进库4',@rk,@date);
				end if;
	end if;
		
#出片
elseif @var='4' then

	if (flags=3 or flags=2) and @counts6=0 and @counts8=0 and @counts9=0 then
	
		SET @processcardid=null;
				set @rkid=null;
				set @rk=null;
				set @galss_id=null;
				select @lastid;
				select `value` into @lastid from gmms_var where varname='last_out_glass_id'and groups=@work_groups;
				select `value` into @lastpos from gmms_var where varname='last_pos'and groups=@work_groups;
				if @lastid is not null then
					select @lastid;
					SELECT a.id,a.slicecage_rank,a.type_id,a.count,galss_id into @rkid,@rk,@type_id,@count,@galss_id from gmms_horizontal a 
					INNER JOIN gmms_galss_task b on a.id=b.slicecage_rank INNER JOIN gmms_drop_frame c on b.processcard_id=c.processcard_id 
					where a.state='使用中' and a.count>0 and a.type_id=@lastid and a.flag=0 and b.engineering_id=@engineering_id and  c.processcard_id is not null and a.gourp=2 and b.stop_position>0 
					order by a.count desc, ABS(a.slicecage_rank-@lastpos) asc LIMIT 1;
					if @rk is not null and @rk<>0 then
					select @rk;
						UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
							call intTo2ByteHex10_16_4(@rk,@rks);
							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
							call message(dates,'000d',@date);
							set messagedate=@date;
							insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼出库5',@rk,@date);
							set @发送字=1;
					end if;
				end if;
				if @发送字=0 then
					SELECT 流程卡 into @processcardid from processcard_ex where 下片位置 is not null and 库内数>0 and 总数-切割数=0 and 工程号=@engineering_id order by  总数-下架数  asc LIMIT 1;
					IF @processcardid is  null then
						select 2;
							#  后使用  未完全切完的
							SELECT 流程卡 into @processcardid from processcard_ex where 下片位置 is not null and 库内数>0 and 工程号=@engineering_id order by 总数-下架数  asc LIMIT 1;	
					end if;
					-- 看看空架 有没有 ，如果有  就找一个库能玻璃最多的下架
					IF @processcardid is null then 

						SET @drop_pos=null;
						SELECT 流程卡,架号,总数,工程号 into @processcardid,@drop_frame,@processcard_count,@engineering_ids from processcard_ex 
						where  (库内数>=@name1 or (库内数>0 and 总数<@name1)) and 工程号=@engineering_id order by  总数-库内数 LIMIT 1;
						
						#得到最小的 和最大的玻璃数据  判断往哪里靠边  2023-8-30
						select MAX(width),MAX(height),MIN(width),MIN(height) into maxw,maxh,minw,minh from gmms_galss_task where processcard_id=@processcardid;
						if maxw<2400 and maxh<1900 and minw>599 and minh>349 then
							set kaobian='机器人';
						else
							set kaobian='下片机';	
						end if;
						if kaobian='下片机' then
							select id into @drop_pos from gmms_drop_frame where state='空架' and groups=@work_groups and (stops=1 or stops=2) limit 1;
						elseif kaobian='机器人' then
							select id into @drop_pos from gmms_drop_frame where state='空架' and groups=@work_groups and (stops=3 or stops=4 or stops=5) limit 1;
						end if;
						set kaobian=null;
						
						if @processcardid is not null and @drop_pos  is not null then 
							select name1 into @name1 from namess where groups=@work_groups;
							#  流程卡占架子
							select count(*) into @counts2 from gmms_galss_task where processcard_id=@processcardid and engineering_id=@engineering_id;
							if  @counts2>0 then 
								UPDATE gmms_drop_frame set state='使用中',drop_frame=@drop_frame,processcard_id=@processcardid,processcard_count=@processcard_count,engineering_id=@engineering_id where id=@drop_pos;
								INSERT into hn_user(update_time,add_time) VALUES(@processcardid,@work_groups);
								UPDATE gmms_galss_task set stop_position=@drop_pos where processcard_id=@processcardid and engineering_id=@engineering_id;				
							end if;			
						else
							set @processcardid=null;
							set @drop_pos=null;
						end if;
					end if;
					if @processcardid is not null then 
							SELECT @processcardid;	
							#  选定大小和槽
							SELECT  a.id,a.slicecage_rank,a.count,a.type_id,galss_id into @rkid,@rk,@count,@type_id,@galss_id from gmms_horizontal a inner join gmms_galss_task b on a.id=b.slicecage_rank  where a.processcard_id=@processcardid and a.state='使用中' and a.gourp=2 and a.flag=0 and engineering_id=@engineering_id order by b.width*b.height desc ,a.count DESC  limit 1;
							select @rkid;
							select @count;
							UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
							call intTo2ByteHex10_16_4(@rk,@rks);
							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
							call message(dates,'000d',@date);
							set messagedate=@date;
							insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼出库6',@rk,@date);
							set a=1;
						end if;
					
				end if;

	
	end if;
	
end if;
-- 
-- 	#传送
-- 	if flag=3 then
-- 		SELECT a.slicecage_rank,a.type_id,a.count into @rk,@type_id,@count from gmms_horizontal a INNER JOIN gmms_galss_task b on a.id=b.slicecage_rank where a.state='使用中' and a.count>0  and a.flag=0 and engineering_id=@engineering_id and a.gourp=1  order by a.count desc,a.slicecage_rank LIMIT 1;
-- 		select count(*) into @count1 from gmms_galss_task where work_state='直通' and work_procedure='理片';
-- 		select @rk;
-- 		if @rk is not null then
-- 			UPDATE gmms_galss_task SET work_state='传送中' where slicecage_rank=@rk and engineering_id=@engineering_id;
-- 			UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=1;
-- 			select count,type_id,processcard_id into @count,@type_id,@processcard_id from gmms_horizontal where slicecage_rank=@rk and gourp=1;
-- 			UPDATE gmms_horizontal SET state='进库中',count=@count,type_id=@type_id,processcard_id=@processcard_id where slicecage_rank=@rk and gourp=2;
-- 			select count(*) into @counts from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id;
-- 			if @counts=1 then
-- 				select galss_id into @galss_id1 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id;
-- 				call intTo2ByteHex10_16_4(@galss_id1,@galss_ids1);
-- 				call intTo2ByteHex10_16_4(@rk,@rks);
-- 				set dates=CONCAT("00020000",@rks,"000200000000",@rks,"000000000000",@galss_ids1);
-- 				call message(dates,'000a',@date);
-- 				set messagedate=@date;
-- 			else 
-- 				select galss_id into @galss_id1 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id limit 1;
-- 				select galss_id into @galss_id2 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id limit 2;
-- 				call intTo2ByteHex10_16_4(@galss_id1,@galss_ids1);
-- 				call intTo2ByteHex10_16_4(@galss_id2,@galss_ids2);
-- 				call intTo2ByteHex10_16_4(@rk,@rks);
-- 				set dates=CONCAT("00020000",@rks,"000200000000",@rks,"000000000000",@galss_ids1,@galss_id2);
-- 				call message(dates,'000a',@date);
-- 				set messagedate=@date;
-- 			end if;
-- 		elseif @count1>0 then
-- 				set dates=CONCAT("000300000003","0003000000000003");
-- 				call message(dates,'000a',@date);
-- 				set messagedate=@date;
-- 		else
-- 			select 库内数 into @kuneishu from processcard1 where 工程号=@engineering_id limit 1;
-- 			select @work_groups;
-- 			select count(*) into @counts from gmms_horizontal where state='空闲' and gourp=2;
-- 			if a=0 then
-- 				if @kuneishu>0 or @counts<=1 then
-- 				#出片
-- 				SET @processcardid=null;
-- 				set @rkid=null;
-- 				set @rk=null;
-- 				set @galss_id=null;
-- 				select `value` into @lastid from gmms_var where varname='last_out_glass_id'and groups=@work_groups;
-- 				select `value` into @lastpos from gmms_var where varname='last_pos'and groups=@work_groups;
-- 				if @lastid is not null then
-- 					select @lastid;
-- 					SELECT a.id,a.slicecage_rank,a.type_id,a.count,galss_id into @rkid,@rk,@type_id,@count,@galss_id from gmms_horizontal a INNER JOIN gmms_galss_task b on a.id=b.slicecage_rank where a.state='使用中' and a.count>0 and a.type_id=@lastid and a.flag=0 and engineering_id=@engineering_id and a.gourp=2 and b.stop_position>0 order by a.count desc, ABS(a.slicecage_rank-@lastpos) asc LIMIT 1;
-- 					if @rk is not null then
-- 					select @rk;
-- 						UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
-- 							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
-- 							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
-- 							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
-- 							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
-- 							call intTo2ByteHex10_16_4(@rk,@rks);
-- 							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
-- 							call message(dates,'000d',@date);
-- 							set messagedate=@date;
-- 							set @发送字=1;
-- 					end if;
-- 				end if;
-- 				if @发送字=0 then
-- 					SELECT 流程卡 into @processcardid from processcard_ex1 where 下片位置 is not null and 库内数>0 and 总数-切割数=0 and 工程号=@engineering_id order by  总数-下架数  asc LIMIT 1;
-- 					IF @processcardid is  null then
-- 						select 2;
-- 							#  后使用  未完全切完的
-- 							SELECT 流程卡 into @processcardid from processcard_ex1 where 下片位置 is not null and 库内数>0 and 工程号=@engineering_id order by 总数-下架数  asc LIMIT 1;	
-- 					end if;
-- 					-- 看看空架 有没有 ，如果有  就找一个库能玻璃最多的下架
-- 					IF @processcardid is null then 
-- 
-- 						SET @drop_pos=null;
-- 						SELECT id into @drop_pos from gmms_drop_frame where state='空架' and groups=@work_groups limit 1;
-- 						if @drop_pos  is not null then 
-- 							select name1 into @name1 from namess where groups=@work_groups;
-- 							SELECT 流程卡,架号,总数,工程号 into @processcardid,@drop_frame,@processcard_count,@engineering_ids from processcard_ex1 where  (库内数>=@name1 or (库内数>0 and 总数<@name1)) order by  库内数 DESC LIMIT 1;
-- 							#  流程卡占架子
-- 							if @processcardid is not null then 
-- 								UPDATE gmms_drop_frame set state='使用中',drop_frame=@drop_frame,processcard_id=@processcardid,processcard_count=@processcard_count,engineering_id=@engineering_ids where id=@drop_pos;
-- 								UPDATE gmms_galss_task set stop_position=@drop_pos where processcard_id=@processcardid and engineering_id=@engineering_id;				
-- 							end if;			
-- 						end if;
-- 					end if;
-- 					if @processcardid is not null then 
-- 							SELECT @processcardid;	
-- 							#  选定大小和槽
-- 							SELECT  a.id,a.slicecage_rank,a.count,a.type_id,galss_id into @rkid,@rk,@count,@type_id,@galss_id from gmms_horizontal a inner join gmms_galss_task b on a.type_id=b.type_id where a.processcard_id=@processcardid and a.state='使用中' and a.gourp=2 and a.flag=0 and engineering_id=@engineering_id order by b.width*b.height desc ,a.count DESC  limit 1;
-- 							select @count;
-- 							UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
-- 							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
-- 							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
-- 							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
-- 							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
-- 							call intTo2ByteHex10_16_4(@rk,@rks);
-- 							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
-- 							call message(dates,'000d',@date);
-- 							set messagedate=@date;
-- 							set a=1;
-- 						end if;
-- 					
-- 				end if;
-- -- 							SELECT  a.id,a.slicecage_rank,a.count,a.type_id,galss_id into @rkid,@rk,@count,@type_id,@galss_id from gmms_horizontal a inner join gmms_galss_task b on a.id=b.slicecage_rank where  a.state='使用中' and a.gourp=2 and a.flag=0 and engineering_id=@engineering_id order by b.width*b.height desc ,a.count DESC  limit 1;
-- -- 							select @count;
-- -- 							UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
-- -- 							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
-- -- 							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
-- -- 							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
-- -- 							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
-- -- 							call intTo2ByteHex10_16_4(@rk,@rks);
-- -- 							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
-- -- 							call message(dates,'000d',@date);
-- -- 							set messagedate=@date;
-- -- 							set a=1;
-- 				end if;
-- 			end if;
-- 			
-- 			if a=0 then
-- 				#进片
-- 				select rank,type_id,galss_id,processcard_id,width,height,slicecage_rank into @rankid,@type_id,@galss_id,@processcardid,@width,@height,@rk from gmms_galss_task where work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id order by rank limit 1;
-- 				UPDATE gmms_galss_task set slicecage_rank=@rk,work_procedure='理片',work_state='进库中' WHERE rank=@rankid and engineering_id=@engineering_id;
-- 				UPDATE gmms_horizontal set state='进库中',count=1,type_id=@type_id,processcard_id=@processcardid where slicecage_rank=@rk and gourp=1;
-- 				call intTo2ByteHex10_16_4(@rankid,@rankids);
-- 				call intTo2ByteHex10_16_4(@rk,@rks);
-- 				set dates=CONCAT("0001",@rankids,@rks);
-- 				call message(dates,'000a',@date);
-- 				set messagedate=@date;
-- 			end if;
-- 				
-- 	
-- 		end if;
-- 		
-- 	#出片
-- 	elseif flag=2 then
-- 				SET @processcardid=null;
-- 				set @rkid=null;
-- 				set @rk=null;
-- 				set @galss_id=null;
-- 				select `value` into @lastid from gmms_var where varname='last_out_glass_id'and groups=@work_groups;
-- 				select `value` into @lastpos from gmms_var where varname='last_pos'and groups=@work_groups;
-- 				if @lastid is not null then
-- 					select @lastid;
-- 					SELECT a.id,a.slicecage_rank,a.type_id,a.count,galss_id into @rkid,@rk,@type_id,@count,@galss_id from gmms_horizontal a INNER JOIN gmms_galss_task b on a.id=b.slicecage_rank where a.state='使用中' and a.count>0 and a.type_id=@lastid and a.flag=0 and engineering_id=@engineering_id and a.gourp=2 and b.stop_position>0 order by a.count desc, ABS(a.slicecage_rank-@lastpos) asc LIMIT 1;
-- 					if @rk is not null then
-- 					select @rk;
-- 						UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
-- 							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
-- 							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
-- 							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
-- 							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
-- 							call intTo2ByteHex10_16_4(@rk,@rks);
-- 							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
-- 							call message(dates,'000d',@date);
-- 							set messagedate=@date;
-- 							set @发送字=1;
-- 					end if;
-- 				end if;
-- 				if @发送字=0 then
-- 					SELECT 流程卡 into @processcardid from processcard_ex1 where 下片位置 is not null and 库内数>0 and 总数-切割数=0 and 工程号=@engineering_id order by  总数-下架数  asc LIMIT 1;
-- 					IF @processcardid is  null then
-- 						select 2;
-- 							#  后使用  未完全切完的
-- 							SELECT 流程卡 into @processcardid from processcard_ex1 where 下片位置 is not null and 库内数>0 and 工程号=@engineering_id order by 总数-下架数  asc LIMIT 1;	
-- 					end if;
-- 					-- 看看空架 有没有 ，如果有  就找一个库能玻璃最多的下架
-- 					IF @processcardid is null then 
-- 
-- 						SET @drop_pos=null;
-- 						SELECT id into @drop_pos from gmms_drop_frame where state='空架' and groups=@work_groups limit 1;
-- 						if @drop_pos  is not null then 
-- 							select name1 into @name1 from namess where groups=@work_groups;
-- 							SELECT 流程卡,架号,总数,工程号 into @processcardid,@drop_frame,@processcard_count,@engineering_ids from processcard_ex1 where  (库内数>=@name1 or (库内数>0 and 总数<@name1)) order by  库内数 DESC LIMIT 1;
-- 							#  流程卡占架子
-- 							if @processcardid is not null then 
-- 								UPDATE gmms_drop_frame set state='使用中',drop_frame=@drop_frame,processcard_id=@processcardid,processcard_count=@processcard_count,engineering_id=@engineering_ids where id=@drop_pos;
-- 								UPDATE gmms_galss_task set stop_position=@drop_pos where processcard_id=@processcardid and engineering_id=@engineering_id;				
-- 							end if;			
-- 						end if;
-- 					end if;
-- 					if @processcardid is not null then 
-- 							SELECT @processcardid;	
-- 							#  选定大小和槽
-- 							SELECT  a.id,a.slicecage_rank,a.count,a.type_id,galss_id into @rkid,@rk,@count,@type_id,@galss_id from gmms_horizontal a inner join gmms_galss_task b on a.type_id=b.type_id where a.processcard_id=@processcardid and a.state='使用中' and a.gourp=2 and a.flag=0 and engineering_id=@engineering_id order by b.width*b.height desc ,a.count DESC  limit 1;
-- 							select @count;
-- 							UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
-- 							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
-- 							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
-- 							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
-- 							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
-- 							call intTo2ByteHex10_16_4(@rk,@rks);
-- 							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
-- 							call message(dates,'000d',@date);
-- 							set messagedate=@date;
-- 						end if;
-- 					
-- 				end if;
-- -- 						SELECT  a.id,a.slicecage_rank,a.count,a.type_id,galss_id into @rkid,@rk,@count,@type_id,@galss_id from gmms_horizontal a inner join gmms_galss_task b on a.id=b.slicecage_rank where  a.state='使用中' and a.gourp=2 and a.flag=0 and engineering_id=@engineering_id order by b.width*b.height desc ,a.count DESC  limit 1;
-- -- 							select @count;
-- -- 							UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
-- -- 							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
-- -- 							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
-- -- 							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
-- -- 							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
-- -- 							call intTo2ByteHex10_16_4(@rk,@rks);
-- -- 							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
-- -- 							call message(dates,'000d',@date);
-- -- 							set messagedate=@date;
-- 				
-- 	#进片
-- 	elseif flag=1 then
-- 		select rank,type_id,galss_id,processcard_id,width,height,slicecage_rank into @rankid,@type_id,@galss_id,@processcardid,@width,@height,@rk from gmms_galss_task where work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id order by rank limit 1;
-- 		UPDATE gmms_galss_task set slicecage_rank=@rk,work_procedure='理片',work_state='进库中' WHERE rank=@rankid and engineering_id=@engineering_id;
-- 		UPDATE gmms_horizontal set state='进库中',count=1,type_id=@type_id,processcard_id=@processcardid where slicecage_rank=@rk and gourp=1;
-- 		call intTo2ByteHex10_16_4(@rankid,@rankids);
-- 		call intTo2ByteHex10_16_4(@rk,@rks);
-- 		set dates=CONCAT("0001",@rankids,@rks);
-- 		call message(dates,'000a',@date);
-- 		set messagedate=@date;
-- 	
-- 	end if;
		
	set @end_time_=SYSDATE();
	insert into A_LengLipian(create_time,end_time,content) values (@create_time_,@end_time_,messagedate);
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for HorizontalSend_copy3
-- ----------------------------
DROP PROCEDURE IF EXISTS `HorizontalSend_copy3`;
delimiter ;;
CREATE PROCEDURE `HorizontalSend_copy3`(in machineids BIGINT,in IsExistChu VARCHAR(255),in returnvalue varchar(255),in flags int,out messagedate varchar(255))
BEGIN
	#理片笼请求存储过程
	#判断是进片还是出片还是进出片请求
	#发送数据：玻璃编号，理片笼架子
	DECLARE a int DEFAULT 0;
	DECLARE ranks int DEFAULT 0;
	DECLARE dates varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	DECLARE maxw int DEFAULT 0;
	DECLARE maxh int DEFAULT 0;
	DECLARE minw int DEFAULT 0;
	DECLARE minh int DEFAULT 0;
	DECLARE kaobian varchar(255) DEFAULT null;
	set @create_time_=null;
	set @end_time_=null;
	set @create_time_=SYSDATE();
	set messagedate=messagedates;
	

select machine_type_id,work_id into @mtid,@work_groups from gmms_machine where id=machineids;

select @work_groups;
	
select file_name into @engineering_id from gmms_raw_file where operator=@work_groups and state='已领取';

select @engineering_id;

select var into @var from gmms_slicecage_pattern where groups=@work_groups;

select @var;
set @counts6=0;
set @counts7=0;
set @counts8=0;
set @counts9=0;
set @counts10=0;
set @Lp_Mode=0;
SET @发送字=0;
select count(*) into @counts6 from gmms_galss_task  where work_state='直通' and work_procedure='理片' and engineering_id=@engineering_id;
select count(*) into @counts7 from gmms_galss_task  where work_state='进库中' and work_procedure='理片' and engineering_id=@engineering_id;
select count(*) into @counts8 from gmms_galss_task  where work_state='出库中' and work_procedure='理片' and engineering_id=@engineering_id;
select count(*) into @counts9 from gmms_galss_task  where work_state='传送中' and work_procedure='理片' and engineering_id=@engineering_id;

select count(*) into @counts10 from gmms_horizontal as gh left join gmms_galss_task as gt on gh.slicecage_rank =gt.slicecage_rank 
where gh.processcard_id is not null and gh.gourp=1 and work_state='传送中' and work_procedure='理片' and engineering_id=@engineering_id order by gh.slicecage_rank;
set @conts=null;
set @conts=CONCAT(IFNULL(flags,','),IFNULL(@counts6,','),IFNULL(@counts7,','),IFNULL(@counts8,','),IFNULL(@counts9,','),IFNULL(@counts10,','));

#进出片
if @var='1' then
		#传送
	if flags=3 then
		if a=0 and @counts8=0 and @counts9=0 then
			set @kuneishu=0;
			select 库内数 into @kuneishu from processcard1 where 工程号=@engineering_id and 库内数>0 order by 库内数 desc  limit 0,1;
			select @kuneishu;
			select @work_groups;
			select count(*) into @counts from gmms_horizontal where state='空闲' and gourp=2;
			
				if (@kuneishu>0 or @counts<=1) and @counts6=0 then
				#出片
				SET @processcardid=null;
				set @rkid=null;
				set @rk=null;
				set @galss_id=null;
				select `value` into @lastid from gmms_var where varname='last_out_glass_id'and groups=@work_groups;
				select `value` into @lastpos from gmms_var where varname='last_pos'and groups=@work_groups;
				if @lastid is not null then
					select @lastid;
					select @lastpos;
					select '进入';
					
					SELECT a.id,a.slicecage_rank,a.type_id,a.count,galss_id into @rkid,@rk,@type_id,@count,@galss_id from gmms_horizontal a 
					INNER JOIN gmms_galss_task b on a.id=b.slicecage_rank INNER JOIN gmms_drop_frame c on b.processcard_id=c.processcard_id 
					where a.state='使用中' and a.count>0 and a.type_id=@lastid and a.flag=0 and b.engineering_id=@engineering_id and  c.processcard_id is not null and a.gourp=2 and b.stop_position>0 
					order by a.count desc, ABS(a.slicecage_rank-@lastpos) asc LIMIT 1;
					select @rkid;
					if @rk is not null and @rk<>0 then
					select @rk;
						UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
							set @galss_ids=null;
							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
							select @galss_ids;
							call intTo2ByteHex10_16_4(@rk,@rks);
							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
							call message(dates,'000d',@date);
							set messagedate=@date;
							insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼出库1',@rk,@date);
							set @发送字=1;
							set a=1;
					end if;
				end if;
				if @发送字=0 then
					SELECT 流程卡 into @processcardid from processcard_ex where 下片位置 is not null and 库内数>0 and 总数-切割数=0 and 工程号=@engineering_id order by  总数-下架数  asc LIMIT 1;
					IF @processcardid is  null then
						select 2;
							#  后使用  未完全切完的
							SELECT 流程卡 into @processcardid from processcard_ex where 下片位置 is not null and 库内数>0 and 工程号=@engineering_id order by 总数-下架数  asc LIMIT 1;	
					end if;
					-- 看看空架 有没有 ，如果有  就找一个库能玻璃最多的下架

					IF @processcardid is null then 

						SET @drop_pos=null;
						
						select name1 into @name1 from namess where groups=@work_groups;
						SELECT 流程卡,架号,总数,工程号 into @processcardid,@drop_frame,@processcard_count,@engineering_ids from processcard_ex 
						where  (库内数>=@name1 or (库内数>0 and 总数<@name1)) and 工程号=@engineering_id order by  总数-库内数 LIMIT 1;
						
						#得到最小的 和最大的玻璃数据  判断往哪里靠边  2023-8-30
						select MAX(width),MAX(height),MIN(width),MIN(height) into maxw,maxh,minw,minh from gmms_galss_task where processcard_id=@processcardid;
						#if maxw<2400 and maxh<1900 and minw>599 and minh>349 then
						if maxw<2400 and maxh<1900 and minw>529 and minh>349 then
							set kaobian='机器人';
						else
							set kaobian='下片机';	
						end if;
						select kaobian;
						if kaobian='下片机' then
							select id into @drop_pos from gmms_drop_frame where state='空架' and groups=@work_groups and (stops=1 or stops=2) limit 1;
						elseif kaobian='机器人' then
							select id into @drop_pos from gmms_drop_frame where state='空架' and groups=@work_groups and (stops=3 or stops=4 or stops=5) limit 1;
						end if;
						set kaobian=null;
						if @processcardid is not null and @drop_pos  is not null then 						
							#  流程卡占架子
							select @processcardid;
							select @drop_pos;
							select count(*) into @counts2 from gmms_galss_task where processcard_id=@processcardid and engineering_id=@engineering_id;
							if @counts2>0 then 
								UPDATE gmms_drop_frame set state='使用中',drop_frame=@drop_frame,processcard_id=@processcardid,processcard_count=@processcard_count,engineering_id=@engineering_id where id=@drop_pos;
								INSERT into hn_user(update_time,add_time) VALUES(@processcardid,@work_groups);
								UPDATE gmms_galss_task set stop_position=@drop_pos where processcard_id=@processcardid and engineering_id=@engineering_id;				
							end if;
						else
							set @processcardid=null;
							set @drop_pos=null;
						end if;
					end if;
					if @processcardid is not null then 
							SELECT @processcardid;	
							#  选定大小和槽
							SELECT  a.id,a.slicecage_rank,a.count,a.type_id,galss_id into @rkid,@rk,@count,@type_id,@galss_id from gmms_horizontal a 
							inner join gmms_galss_task b on a.id=b.slicecage_rank  where a.processcard_id=@processcardid and a.state='使用中' and a.gourp=2 and a.flag=0 and engineering_id=@engineering_id 
							order by b.width*b.height desc ,a.count DESC  limit 1;
							select @rkid;
							select @count;
							select a;
							if @rk is not null and @rk<>0 then
							UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
							call intTo2ByteHex10_16_4(@rk,@rks);
							select @galss_ids,@rks,@date;
							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
							call message(dates,'000d',@date);
							set messagedate=@date;
							insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼出库2',@rk,@date);
							set a=1;
							end if;
						end if;
					
				end if;

				end if;
			
		end if;
		select a;
		if a=0 and @counts7=0 and @counts8=0 and @counts9=0 then
		
			set @rk=null;
			set @LastGalssid=null;
			set @ProcesscardA1=null;
			set @ProcesscardA2=null;
			
-- 			select slicecage_rank into @rk from gmms_horizontal  
-- 			where (gourp=1 and state='使用中' and count>0 and flag=0) or (gourp=2 and state='空闲')  group by slicecage_rank having count(*)=2 order by count desc limit 1;
			
			#2023-11-14  YZX更新
			select 
			(SELECT b.id from gmms_horizontal a 
			INNER JOIN gmms_galss_task b on a.id=b.slicecage_rank 
			INNER JOIN gmms_drop_frame c on b.processcard_id=c.processcard_id 
			where a.state='使用中' and a.count>0 and a.type_id=ls1.value and a.flag=0 and b.engineering_id=grf.file_name and  c.processcard_id is not null and a.gourp=1 and b.stop_position>0 
			order by a.count desc, ABS(a.slicecage_rank-ls2.value) asc LIMIT 1),
			(SELECT 流程卡 from processcard_ex where 下片位置 is not null and 库内数>0 and 工程号=grf.file_name order by 总数-下架数  asc LIMIT 1) ,
			(SELECT 流程卡 from processcard_ex where (库内数>=ns.name1 or (库内数>0 and 总数<ns.name2)) and 工程号=grf.file_name order by  总数-库内数 LIMIT 1)
			INTO @LastGalssid,@ProcesscardA1,@ProcesscardA2
			from gmms_machine as gm 
			LEFT JOIN gmms_raw_file as grf on gm.work_id=grf.operator and grf.state='已领取'
			LEFT JOIN gmms_var as ls1 on gm.work_id=ls1.groups and ls1.varname='last_out_glass_id'
			LEFT JOIN gmms_var as ls2 on gm.work_id=ls2.groups and ls2.varname='last_pos'
			LEFT JOIN namess as ns on gm.work_id=ns.groups
			where gm.id=27;
			
			if @LastGalssid is not null then
				select hl1.slicecage_rank into @rk from gmms_horizontal as hl1 
				left join gmms_horizontal as hl2 on hl1.slicecage_rank=hl2.slicecage_rank
				where hl1.gourp=1 and hl2.gourp=2 and hl1.state='使用中' and hl1.count>0 and hl1.flag=0 and hl2.gourp=2 and hl2.state='空闲' 
				and hl1.slicecage_rank=(select slicecage_rank from gmms_galss_task where id=@LastGalssid);
			elseif @ProcesscardA1 is not null then
				select hl1.slicecage_rank into @rk from gmms_horizontal as hl1 
				left join gmms_horizontal as hl2 on hl1.slicecage_rank=hl2.slicecage_rank
				where hl1.gourp=1 and hl2.gourp=2 and hl1.state='使用中' and hl1.count>0 and hl1.flag=0 and hl2.gourp=2 and hl2.state='空闲' 
				and hl1.processcard_id=@ProcesscardA1 order by hl1.count desc limit 1;
			elseif @ProcesscardA2 is not null then
				select hl1.slicecage_rank into @rk from gmms_horizontal as hl1 
				left join gmms_horizontal as hl2 on hl1.slicecage_rank=hl2.slicecage_rank
				where hl1.gourp=1 and hl2.gourp=2 and hl1.state='使用中' and hl1.count>0 and hl1.flag=0 and hl2.gourp=2 and hl2.state='空闲' 
				and hl1.processcard_id=@ProcesscardA2 order by hl1.count desc limit 1;
			end if;			
			
			if @rk is null then
				select slicecage_rank into @rk from gmms_horizontal  
				where (gourp=1 and state='使用中' and count>0 and flag=0) or (gourp=2 and state='空闲')  group by slicecage_rank having count(*)=2 order by count desc limit 1;
			end if;
			select @rk;
			
					
			if @rk is not null and @rk<>0 then
				select count(*) into @countstate from gmms_horizontal where  (state='出库中' or state='进库中');
				if @countstate=0 then
				UPDATE gmms_galss_task SET work_state='传送中' where slicecage_rank=@rk and engineering_id=@engineering_id;
				UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=1;
				select count,type_id,processcard_id into @count,@type_id,@processcard_id from gmms_horizontal where slicecage_rank=@rk and gourp=1;
				UPDATE gmms_horizontal SET state='进库中',count=@count,type_id=@type_id,processcard_id=@processcard_id where slicecage_rank=@rk and gourp=2;
				select count(*) into @counts from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id;
				select count(*) into @count1 from gmms_galss_task where work_state='直通' and work_procedure='理片' and engineering_id=@engineering_id;
				if @counts=1 then
					if @type_id <>0 and (@processcard_id is not null or @processcard_id<>'') then
						select galss_id into @galss_id1 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id;
						call intTo2ByteHex10_16_4(@galss_id1,@galss_ids1);
						call intTo2ByteHex10_16_4(@rk,@rks);
						set dates=CONCAT("00020000",@rks,"000200000000",@rks,"000000000000",@galss_ids1);
						call message(dates,'000a',@date);
						insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
							values (machineids,@work_groups,NOW(),'理片笼传送1',@rk,@date);
						set messagedate=@date;
						set a=1;
					end if;
				else 
					select galss_id into @galss_id1 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id limit 1;
					select galss_id into @galss_id2 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id limit 2;
					call intTo2ByteHex10_16_4(@galss_id1,@galss_ids1);
					call intTo2ByteHex10_16_4(@galss_id2,@galss_ids2);
					call intTo2ByteHex10_16_4(@rk,@rks);
					set dates=CONCAT("00020000",@rks,"000200000000",@rks,"000000000000",@galss_ids1,@galss_id2);
					call message(dates,'000a',@date);
					set a=1;
					set messagedate=@date;
					insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
							values (machineids,@work_groups,NOW(),'理片笼传送2',@rk,@date);
				end if;
				end if;
				
			elseif @count1>0 then
					set dates=CONCAT("000300000003","0003000000000003");
					call message(dates,'000a',@date);
					set messagedate=@date;
					set a=1;
			
			end if;
		end if;
		
		if a=0 and @counts7=0 and @counts10=0 then
				#进片
				set @type_id=0;
				set @processcardid=null;
				select rank,type_id,galss_id,processcard_id,width,height,slicecage_rank into @rankid,@type_id,@galss_id,@processcardid,@width,@height,@rk from gmms_galss_task where work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id order by rank limit 1;
				if @galss_id is not null and @rk<>0 and @rk is not null then
				UPDATE gmms_galss_task set work_state='进库中',finsh_time=now() WHERE rank=@rankid and work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id;
				UPDATE gmms_horizontal set state='进库中',count=1,type_id=@type_id,processcard_id=@processcardid where slicecage_rank=@rk and gourp=1;
				call intTo2ByteHex10_16_4(@rankid,@rankids);
				call intTo2ByteHex10_16_4(@rk,@rks);
				set dates=CONCAT("0001",@rankids,@rks);
				call message(dates,'000a',@date);
				set messagedate=@date;
				insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼进库1',@rk,@date);
				end if;
		end if;

		
	#出片
	elseif flags=2 and @counts6=0 and @counts8=0 and @counts9=0 then
				SET @processcardid=null;
				set @rkid=null;
				set @rk=null;
				set @galss_id=null;
				select `value` into @lastid from gmms_var where varname='last_out_glass_id'and groups=@work_groups;
				select `value` into @lastpos from gmms_var where varname='last_pos'and groups=@work_groups;
				if @lastid is not null then
					select @lastid;
					SELECT a.id,a.slicecage_rank,a.type_id,a.count,galss_id into @rkid,@rk,@type_id,@count,@galss_id from gmms_horizontal a 
					INNER JOIN gmms_galss_task b on a.id=b.slicecage_rank INNER JOIN gmms_drop_frame c on b.processcard_id=c.processcard_id 
					where a.state='使用中' and a.count>0 and a.type_id=@lastid and a.flag=0 and b.engineering_id=@engineering_id and  c.processcard_id is not null and a.gourp=2 and b.stop_position>0 
					order by a.count desc, ABS(a.slicecage_rank-@lastpos) asc LIMIT 1;
					if @rk is not null and @rk<>0 then
					select @rk;
						UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
							call intTo2ByteHex10_16_4(@rk,@rks);
							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
							call message(dates,'000d',@date);
							set messagedate=@date;
							insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼出库3',@rk,@date);
							set @发送字=1;
					end if;
				end if;
				if @发送字=0 then
					SELECT 流程卡 into @processcardid from processcard_ex where 下片位置 is not null and 库内数>0 and 总数-切割数=0 and 工程号=@engineering_id order by  总数-下架数  asc LIMIT 1;
					IF @processcardid is  null then
						select 2;
							#  后使用  未完全切完的
							SELECT 流程卡 into @processcardid from processcard_ex where 下片位置 is not null and 库内数>0 and 工程号=@engineering_id order by 总数-下架数  asc LIMIT 1;	
					end if;
					-- 看看空架 有没有 ，如果有  就找一个库能玻璃最多的下架
					IF @processcardid is null then 

						SET @drop_pos=null;
						SELECT 流程卡,架号,总数,工程号 into @processcardid,@drop_frame,@processcard_count,@engineering_ids from processcard_ex 
						where  (库内数>=@name1 or (库内数>0 and 总数<@name1)) and 工程号=@engineering_id order by  总数-库内数 LIMIT 1;
							
						#得到最小的 和最大的玻璃数据  判断往哪里靠边  2023-8-30
						select MAX(width),MAX(height),MIN(width),MIN(height) into maxw,maxh,minw,minh from gmms_galss_task where processcard_id=@processcardid;
						if maxw<2400 and maxh<1900 and minw>599 and minh>349 then
							set kaobian='机器人';
						else
							set kaobian='下片机';	
						end if;
						if kaobian='下片机' then
							select id into @drop_pos from gmms_drop_frame where state='空架' and groups=@work_groups and (stops=1 or stops=2) limit 1;
						elseif kaobian='机器人' then
							select id into @drop_pos from gmms_drop_frame where state='空架' and groups=@work_groups and (stops=3 or stops=4 or stops=5) limit 1;
						end if;
						set kaobian=null;
						
						if @processcardid is not null and @drop_pos  is not null then 
							select name1 into @name1 from namess where groups=@work_groups;
							#  流程卡占架子
							select count(*) into @counts2 from gmms_galss_task where processcard_id=@processcardid and engineering_id=@engineering_id;
							if @counts2>0 then 
								UPDATE gmms_drop_frame set state='使用中',drop_frame=@drop_frame,processcard_id=@processcardid,processcard_count=@processcard_count,engineering_id=@engineering_id where id=@drop_pos;
								INSERT into hn_user(update_time,add_time) VALUES(@processcardid,@work_groups);
								UPDATE gmms_galss_task set stop_position=@drop_pos where processcard_id=@processcardid and engineering_id=@engineering_id;				
							end if;
						else
							set @processcardid=null;
							set @drop_pos=null;
						end if;
					end if;
					if @processcardid is not null then 
							SELECT @processcardid;	
							#  选定大小和槽
							SELECT  a.id,a.slicecage_rank,a.count,a.type_id,galss_id into @rkid,@rk,@count,@type_id,@galss_id from gmms_horizontal a 
							inner join gmms_galss_task b on a.id=b.slicecage_rank  
							where a.processcard_id=@processcardid and a.state='使用中' and a.gourp=2 and a.flag=0 and engineering_id=@engineering_id 
							order by b.width*b.height desc ,a.count DESC  limit 1;
							select @count;
							if @rk is not null and @rk<>0 then
							UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
							call intTo2ByteHex10_16_4(@rk,@rks);
							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
							call message(dates,'000d',@date);
							set messagedate=@date;
							insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼出库4',@rk,@date);
							end if;
						end if;
					
				end if;

				
	#进片
	elseif flags=1 and @counts7=0 and @counts10=0 then
		set @rk=null;
		set @type_id=0;
				set @processcardid=null;
				select rank,type_id,galss_id,processcard_id,width,height,slicecage_rank into @rankid,@type_id,@galss_id,@processcardid,@width,@height,@rk from gmms_galss_task where work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id order by rank limit 1;
				if @galss_id is not null and @rk<>0 and @rk is not null then
				UPDATE gmms_galss_task set work_state='进库中',finsh_time=now() WHERE rank=@rankid and work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id;
				UPDATE gmms_horizontal set state='进库中',count=1,type_id=@type_id,processcard_id=@processcardid where slicecage_rank=@rk and gourp=1;
				call intTo2ByteHex10_16_4(@rankid,@rankids);
				call intTo2ByteHex10_16_4(@rk,@rks);
				set dates=CONCAT("0001",@rankids,@rks);
				call message(dates,'000a',@date);
				set messagedate=@date;
				insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼进库2',@rk,@date);
				end if;
	
	end if;

#直通
-- elseif @var='2' then
-- 
-- 
#存笼
elseif @var='3' then
	if flags=3 and @counts7=0 then
	set @type_id=null;
	set @count=null;
		select slicecage_rank into @rk from gmms_horizontal  where (gourp=1 and state='使用中' and count>0 and flag=0) or (gourp=2 and state='空闲')  group by slicecage_rank having count(*)=2 order by count desc limit 1;
		select @rk;
		if @rk is not null and @counts8=0 and @counts9=0  then
				select count(*) into @countstate from gmms_horizontal where  (state='出库中' or state='进库中');
				if @countstate=0 then
  			UPDATE gmms_galss_task SET work_state='传送中' where slicecage_rank=@rk and engineering_id=@engineering_id;
  			UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=1;
 			  select count,type_id,processcard_id into @count,@type_id,@processcard_id from gmms_horizontal where slicecage_rank=@rk and gourp=1;
				
  			UPDATE gmms_horizontal SET state='进库中',count=@count,type_id=@type_id,processcard_id=@processcard_id where slicecage_rank=@rk and gourp=2;
			select count(*) into @counts from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id;
			select @counts;
			if @counts=1 then
				if @type_id <>0 and (@processcard_id is not null or @processcard_id<>'') then
					select galss_id into @galss_id1 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id;
					call intTo2ByteHex10_16_4(@galss_id1,@galss_ids1);
					call intTo2ByteHex10_16_4(@rk,@rks);
					set dates=CONCAT("00020000",@rks,"000200000000",@rks,"000000000000",@galss_ids1);
					call message(dates,'000a',@date);
					set messagedate=@date;
					insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼传送3',@rk,@date);
				end if;
			else 
			     	select @counts;
				end if;
			else 
				
				select galss_id into @galss_id1 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id limit 1;
				select galss_id into @galss_id2 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id limit 2;
				select @galss_id1,@galss_id2;
				call intTo2ByteHex10_16_4(@galss_id1,@galss_ids1);
				call intTo2ByteHex10_16_4(@galss_id2,@galss_ids2);
				call intTo2ByteHex10_16_4(@rk,@rks);
				set dates=CONCAT("00020000",@rks,"000200000000",@rks,"000000000000",@galss_ids1,@galss_id2);
				call message(dates,'000a',@date);
				set messagedate=@date;
				insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼传送4',@rk,@date);
			end if;
		elseif @counts10=0 then
			set @type_id=0;
			set @processcardid=null;
			select rank,type_id,galss_id,processcard_id,width,height,slicecage_rank into @rankid,@type_id,@galss_id,@processcardid,@width,@height,@rk from gmms_galss_task where work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id order by rank limit 1;
			if @galss_id is not null and @rk<>0 and @rk is not null then
				#UPDATE gmms_galss_task set slicecage_rank=@rk,work_procedure='理片',work_state='进库中' WHERE rank=@rankid and engineering_id=@engineering_id;
				UPDATE gmms_galss_task set work_state='进库中',finsh_time=now() WHERE rank=@rankid and work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id;
				UPDATE gmms_horizontal set state='进库中',count=1,type_id=@type_id,processcard_id=@processcardid where slicecage_rank=@rk and gourp=1;
				UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
				UPDATE gmms_var SET Value=@rankid where varname='last_out_glass_id' and groups=@work_groups;
				select @rankid,@rk;
				call intTo2ByteHex10_16_4(@rankid,@rankids);
				call intTo2ByteHex10_16_4(@rk,@rks);
				set dates=CONCAT("0001",@rankids,@rks);
				call message(dates,'000a',@date);
				set messagedate=@date;
				insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼进库3',@rk,@date);
			end if;

			
		end if;
		


				
	#进片
	elseif flags=1 and @counts7=0 and @counts10=0 then
		set @type_id=0;
				set @processcardid=null;
				select rank,type_id,galss_id,processcard_id,width,height,slicecage_rank into @rankid,@type_id,@galss_id,@processcardid,@width,@height,@rk from gmms_galss_task where work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id order by rank limit 1;
				if @galss_id is not null and @rk<>0 and @rk is not null then
				UPDATE gmms_galss_task set work_state='进库中',finsh_time=now() WHERE rank=@rankid and work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id;
				UPDATE gmms_horizontal set state='进库中',count=1,type_id=@type_id,processcard_id=@processcardid where slicecage_rank=@rk and gourp=1;
				call intTo2ByteHex10_16_4(@rankid,@rankids);
				call intTo2ByteHex10_16_4(@rk,@rks);
				UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
				UPDATE gmms_var SET Value=@rankid where varname='last_out_glass_id' and groups=@work_groups;
				set dates=CONCAT("0001",@rankids,@rks);
				call message(dates,'000a',@date);
				set messagedate=@date;
				insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼进库4',@rk,@date);
				end if;
	end if;
		
#出片
elseif @var='4' then

	if (flags=3 or flags=2) and @counts6=0 and @counts8=0 and @counts9=0 then
	
		SET @processcardid=null;
				set @rkid=null;
				set @rk=null;
				set @galss_id=null;
				select @lastid;
				select `value` into @lastid from gmms_var where varname='last_out_glass_id'and groups=@work_groups;
				select `value` into @lastpos from gmms_var where varname='last_pos'and groups=@work_groups;
				if @lastid is not null then
					select @lastid;
					SELECT a.id,a.slicecage_rank,a.type_id,a.count,galss_id into @rkid,@rk,@type_id,@count,@galss_id from gmms_horizontal a 
					INNER JOIN gmms_galss_task b on a.id=b.slicecage_rank INNER JOIN gmms_drop_frame c on b.processcard_id=c.processcard_id 
					where a.state='使用中' and a.count>0 and a.type_id=@lastid and a.flag=0 and b.engineering_id=@engineering_id and  c.processcard_id is not null and a.gourp=2 and b.stop_position>0 
					order by a.count desc, ABS(a.slicecage_rank-@lastpos) asc LIMIT 1;
					if @rk is not null and @rk<>0 then
					select @rk;
						UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
							call intTo2ByteHex10_16_4(@rk,@rks);
							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
							call message(dates,'000d',@date);
							set messagedate=@date;
							insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼出库5',@rk,@date);
							set @发送字=1;
					end if;
				end if;
				if @发送字=0 then
					SELECT 流程卡 into @processcardid from processcard_ex where 下片位置 is not null and 库内数>0 and 总数-切割数=0 and 工程号=@engineering_id order by  总数-下架数  asc LIMIT 1;
					IF @processcardid is  null then
						select 2;
							#  后使用  未完全切完的
							SELECT 流程卡 into @processcardid from processcard_ex where 下片位置 is not null and 库内数>0 and 工程号=@engineering_id order by 总数-下架数  asc LIMIT 1;	
					end if;
					-- 看看空架 有没有 ，如果有  就找一个库能玻璃最多的下架
					IF @processcardid is null then 

						SET @drop_pos=null;
						SELECT 流程卡,架号,总数,工程号 into @processcardid,@drop_frame,@processcard_count,@engineering_ids from processcard_ex 
						where  (库内数>=@name1 or (库内数>0 and 总数<@name1)) and 工程号=@engineering_id order by  总数-库内数 LIMIT 1;
						
						#得到最小的 和最大的玻璃数据  判断往哪里靠边  2023-8-30
						select MAX(width),MAX(height),MIN(width),MIN(height) into maxw,maxh,minw,minh from gmms_galss_task where processcard_id=@processcardid;
						if maxw<2400 and maxh<1900 and minw>599 and minh>349 then
							set kaobian='机器人';
						else
							set kaobian='下片机';	
						end if;
						if kaobian='下片机' then
							select id into @drop_pos from gmms_drop_frame where state='空架' and groups=@work_groups and (stops=1 or stops=2) limit 1;
						elseif kaobian='机器人' then
							select id into @drop_pos from gmms_drop_frame where state='空架' and groups=@work_groups and (stops=3 or stops=4 or stops=5) limit 1;
						end if;
						set kaobian=null;
						
						if @processcardid is not null and @drop_pos  is not null then 
							select name1 into @name1 from namess where groups=@work_groups;
							#  流程卡占架子
							select count(*) into @counts2 from gmms_galss_task where processcard_id=@processcardid and engineering_id=@engineering_id;
							if  @counts2>0 then 
								UPDATE gmms_drop_frame set state='使用中',drop_frame=@drop_frame,processcard_id=@processcardid,processcard_count=@processcard_count,engineering_id=@engineering_id where id=@drop_pos;
								INSERT into hn_user(update_time,add_time) VALUES(@processcardid,@work_groups);
								UPDATE gmms_galss_task set stop_position=@drop_pos where processcard_id=@processcardid and engineering_id=@engineering_id;				
							end if;			
						else
							set @processcardid=null;
							set @drop_pos=null;
						end if;
					end if;
					if @processcardid is not null then 
							SELECT @processcardid;	
							#  选定大小和槽
							SELECT  a.id,a.slicecage_rank,a.count,a.type_id,galss_id into @rkid,@rk,@count,@type_id,@galss_id from gmms_horizontal a inner join gmms_galss_task b on a.id=b.slicecage_rank  where a.processcard_id=@processcardid and a.state='使用中' and a.gourp=2 and a.flag=0 and engineering_id=@engineering_id order by b.width*b.height desc ,a.count DESC  limit 1;
							select @rkid;
							select @count;
							UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
							call intTo2ByteHex10_16_4(@rk,@rks);
							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
							call message(dates,'000d',@date);
							set messagedate=@date;
							insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'理片笼出库6',@rk,@date);
							set a=1;
						end if;
					
				end if;

	
	end if;
	
end if;
-- 
-- 	#传送
-- 	if flag=3 then
-- 		SELECT a.slicecage_rank,a.type_id,a.count into @rk,@type_id,@count from gmms_horizontal a INNER JOIN gmms_galss_task b on a.id=b.slicecage_rank where a.state='使用中' and a.count>0  and a.flag=0 and engineering_id=@engineering_id and a.gourp=1  order by a.count desc,a.slicecage_rank LIMIT 1;
-- 		select count(*) into @count1 from gmms_galss_task where work_state='直通' and work_procedure='理片';
-- 		select @rk;
-- 		if @rk is not null then
-- 			UPDATE gmms_galss_task SET work_state='传送中' where slicecage_rank=@rk and engineering_id=@engineering_id;
-- 			UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=1;
-- 			select count,type_id,processcard_id into @count,@type_id,@processcard_id from gmms_horizontal where slicecage_rank=@rk and gourp=1;
-- 			UPDATE gmms_horizontal SET state='进库中',count=@count,type_id=@type_id,processcard_id=@processcard_id where slicecage_rank=@rk and gourp=2;
-- 			select count(*) into @counts from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id;
-- 			if @counts=1 then
-- 				select galss_id into @galss_id1 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id;
-- 				call intTo2ByteHex10_16_4(@galss_id1,@galss_ids1);
-- 				call intTo2ByteHex10_16_4(@rk,@rks);
-- 				set dates=CONCAT("00020000",@rks,"000200000000",@rks,"000000000000",@galss_ids1);
-- 				call message(dates,'000a',@date);
-- 				set messagedate=@date;
-- 			else 
-- 				select galss_id into @galss_id1 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id limit 1;
-- 				select galss_id into @galss_id2 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id limit 2;
-- 				call intTo2ByteHex10_16_4(@galss_id1,@galss_ids1);
-- 				call intTo2ByteHex10_16_4(@galss_id2,@galss_ids2);
-- 				call intTo2ByteHex10_16_4(@rk,@rks);
-- 				set dates=CONCAT("00020000",@rks,"000200000000",@rks,"000000000000",@galss_ids1,@galss_id2);
-- 				call message(dates,'000a',@date);
-- 				set messagedate=@date;
-- 			end if;
-- 		elseif @count1>0 then
-- 				set dates=CONCAT("000300000003","0003000000000003");
-- 				call message(dates,'000a',@date);
-- 				set messagedate=@date;
-- 		else
-- 			select 库内数 into @kuneishu from processcard1 where 工程号=@engineering_id limit 1;
-- 			select @work_groups;
-- 			select count(*) into @counts from gmms_horizontal where state='空闲' and gourp=2;
-- 			if a=0 then
-- 				if @kuneishu>0 or @counts<=1 then
-- 				#出片
-- 				SET @processcardid=null;
-- 				set @rkid=null;
-- 				set @rk=null;
-- 				set @galss_id=null;
-- 				select `value` into @lastid from gmms_var where varname='last_out_glass_id'and groups=@work_groups;
-- 				select `value` into @lastpos from gmms_var where varname='last_pos'and groups=@work_groups;
-- 				if @lastid is not null then
-- 					select @lastid;
-- 					SELECT a.id,a.slicecage_rank,a.type_id,a.count,galss_id into @rkid,@rk,@type_id,@count,@galss_id from gmms_horizontal a INNER JOIN gmms_galss_task b on a.id=b.slicecage_rank where a.state='使用中' and a.count>0 and a.type_id=@lastid and a.flag=0 and engineering_id=@engineering_id and a.gourp=2 and b.stop_position>0 order by a.count desc, ABS(a.slicecage_rank-@lastpos) asc LIMIT 1;
-- 					if @rk is not null then
-- 					select @rk;
-- 						UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
-- 							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
-- 							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
-- 							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
-- 							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
-- 							call intTo2ByteHex10_16_4(@rk,@rks);
-- 							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
-- 							call message(dates,'000d',@date);
-- 							set messagedate=@date;
-- 							set @发送字=1;
-- 					end if;
-- 				end if;
-- 				if @发送字=0 then
-- 					SELECT 流程卡 into @processcardid from processcard_ex1 where 下片位置 is not null and 库内数>0 and 总数-切割数=0 and 工程号=@engineering_id order by  总数-下架数  asc LIMIT 1;
-- 					IF @processcardid is  null then
-- 						select 2;
-- 							#  后使用  未完全切完的
-- 							SELECT 流程卡 into @processcardid from processcard_ex1 where 下片位置 is not null and 库内数>0 and 工程号=@engineering_id order by 总数-下架数  asc LIMIT 1;	
-- 					end if;
-- 					-- 看看空架 有没有 ，如果有  就找一个库能玻璃最多的下架
-- 					IF @processcardid is null then 
-- 
-- 						SET @drop_pos=null;
-- 						SELECT id into @drop_pos from gmms_drop_frame where state='空架' and groups=@work_groups limit 1;
-- 						if @drop_pos  is not null then 
-- 							select name1 into @name1 from namess where groups=@work_groups;
-- 							SELECT 流程卡,架号,总数,工程号 into @processcardid,@drop_frame,@processcard_count,@engineering_ids from processcard_ex1 where  (库内数>=@name1 or (库内数>0 and 总数<@name1)) order by  库内数 DESC LIMIT 1;
-- 							#  流程卡占架子
-- 							if @processcardid is not null then 
-- 								UPDATE gmms_drop_frame set state='使用中',drop_frame=@drop_frame,processcard_id=@processcardid,processcard_count=@processcard_count,engineering_id=@engineering_ids where id=@drop_pos;
-- 								UPDATE gmms_galss_task set stop_position=@drop_pos where processcard_id=@processcardid and engineering_id=@engineering_id;				
-- 							end if;			
-- 						end if;
-- 					end if;
-- 					if @processcardid is not null then 
-- 							SELECT @processcardid;	
-- 							#  选定大小和槽
-- 							SELECT  a.id,a.slicecage_rank,a.count,a.type_id,galss_id into @rkid,@rk,@count,@type_id,@galss_id from gmms_horizontal a inner join gmms_galss_task b on a.type_id=b.type_id where a.processcard_id=@processcardid and a.state='使用中' and a.gourp=2 and a.flag=0 and engineering_id=@engineering_id order by b.width*b.height desc ,a.count DESC  limit 1;
-- 							select @count;
-- 							UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
-- 							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
-- 							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
-- 							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
-- 							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
-- 							call intTo2ByteHex10_16_4(@rk,@rks);
-- 							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
-- 							call message(dates,'000d',@date);
-- 							set messagedate=@date;
-- 							set a=1;
-- 						end if;
-- 					
-- 				end if;
-- -- 							SELECT  a.id,a.slicecage_rank,a.count,a.type_id,galss_id into @rkid,@rk,@count,@type_id,@galss_id from gmms_horizontal a inner join gmms_galss_task b on a.id=b.slicecage_rank where  a.state='使用中' and a.gourp=2 and a.flag=0 and engineering_id=@engineering_id order by b.width*b.height desc ,a.count DESC  limit 1;
-- -- 							select @count;
-- -- 							UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
-- -- 							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
-- -- 							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
-- -- 							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
-- -- 							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
-- -- 							call intTo2ByteHex10_16_4(@rk,@rks);
-- -- 							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
-- -- 							call message(dates,'000d',@date);
-- -- 							set messagedate=@date;
-- -- 							set a=1;
-- 				end if;
-- 			end if;
-- 			
-- 			if a=0 then
-- 				#进片
-- 				select rank,type_id,galss_id,processcard_id,width,height,slicecage_rank into @rankid,@type_id,@galss_id,@processcardid,@width,@height,@rk from gmms_galss_task where work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id order by rank limit 1;
-- 				UPDATE gmms_galss_task set slicecage_rank=@rk,work_procedure='理片',work_state='进库中' WHERE rank=@rankid and engineering_id=@engineering_id;
-- 				UPDATE gmms_horizontal set state='进库中',count=1,type_id=@type_id,processcard_id=@processcardid where slicecage_rank=@rk and gourp=1;
-- 				call intTo2ByteHex10_16_4(@rankid,@rankids);
-- 				call intTo2ByteHex10_16_4(@rk,@rks);
-- 				set dates=CONCAT("0001",@rankids,@rks);
-- 				call message(dates,'000a',@date);
-- 				set messagedate=@date;
-- 			end if;
-- 				
-- 	
-- 		end if;
-- 		
-- 	#出片
-- 	elseif flag=2 then
-- 				SET @processcardid=null;
-- 				set @rkid=null;
-- 				set @rk=null;
-- 				set @galss_id=null;
-- 				select `value` into @lastid from gmms_var where varname='last_out_glass_id'and groups=@work_groups;
-- 				select `value` into @lastpos from gmms_var where varname='last_pos'and groups=@work_groups;
-- 				if @lastid is not null then
-- 					select @lastid;
-- 					SELECT a.id,a.slicecage_rank,a.type_id,a.count,galss_id into @rkid,@rk,@type_id,@count,@galss_id from gmms_horizontal a INNER JOIN gmms_galss_task b on a.id=b.slicecage_rank where a.state='使用中' and a.count>0 and a.type_id=@lastid and a.flag=0 and engineering_id=@engineering_id and a.gourp=2 and b.stop_position>0 order by a.count desc, ABS(a.slicecage_rank-@lastpos) asc LIMIT 1;
-- 					if @rk is not null then
-- 					select @rk;
-- 						UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
-- 							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
-- 							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
-- 							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
-- 							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
-- 							call intTo2ByteHex10_16_4(@rk,@rks);
-- 							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
-- 							call message(dates,'000d',@date);
-- 							set messagedate=@date;
-- 							set @发送字=1;
-- 					end if;
-- 				end if;
-- 				if @发送字=0 then
-- 					SELECT 流程卡 into @processcardid from processcard_ex1 where 下片位置 is not null and 库内数>0 and 总数-切割数=0 and 工程号=@engineering_id order by  总数-下架数  asc LIMIT 1;
-- 					IF @processcardid is  null then
-- 						select 2;
-- 							#  后使用  未完全切完的
-- 							SELECT 流程卡 into @processcardid from processcard_ex1 where 下片位置 is not null and 库内数>0 and 工程号=@engineering_id order by 总数-下架数  asc LIMIT 1;	
-- 					end if;
-- 					-- 看看空架 有没有 ，如果有  就找一个库能玻璃最多的下架
-- 					IF @processcardid is null then 
-- 
-- 						SET @drop_pos=null;
-- 						SELECT id into @drop_pos from gmms_drop_frame where state='空架' and groups=@work_groups limit 1;
-- 						if @drop_pos  is not null then 
-- 							select name1 into @name1 from namess where groups=@work_groups;
-- 							SELECT 流程卡,架号,总数,工程号 into @processcardid,@drop_frame,@processcard_count,@engineering_ids from processcard_ex1 where  (库内数>=@name1 or (库内数>0 and 总数<@name1)) order by  库内数 DESC LIMIT 1;
-- 							#  流程卡占架子
-- 							if @processcardid is not null then 
-- 								UPDATE gmms_drop_frame set state='使用中',drop_frame=@drop_frame,processcard_id=@processcardid,processcard_count=@processcard_count,engineering_id=@engineering_ids where id=@drop_pos;
-- 								UPDATE gmms_galss_task set stop_position=@drop_pos where processcard_id=@processcardid and engineering_id=@engineering_id;				
-- 							end if;			
-- 						end if;
-- 					end if;
-- 					if @processcardid is not null then 
-- 							SELECT @processcardid;	
-- 							#  选定大小和槽
-- 							SELECT  a.id,a.slicecage_rank,a.count,a.type_id,galss_id into @rkid,@rk,@count,@type_id,@galss_id from gmms_horizontal a inner join gmms_galss_task b on a.type_id=b.type_id where a.processcard_id=@processcardid and a.state='使用中' and a.gourp=2 and a.flag=0 and engineering_id=@engineering_id order by b.width*b.height desc ,a.count DESC  limit 1;
-- 							select @count;
-- 							UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
-- 							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
-- 							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
-- 							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
-- 							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
-- 							call intTo2ByteHex10_16_4(@rk,@rks);
-- 							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
-- 							call message(dates,'000d',@date);
-- 							set messagedate=@date;
-- 						end if;
-- 					
-- 				end if;
-- -- 						SELECT  a.id,a.slicecage_rank,a.count,a.type_id,galss_id into @rkid,@rk,@count,@type_id,@galss_id from gmms_horizontal a inner join gmms_galss_task b on a.id=b.slicecage_rank where  a.state='使用中' and a.gourp=2 and a.flag=0 and engineering_id=@engineering_id order by b.width*b.height desc ,a.count DESC  limit 1;
-- -- 							select @count;
-- -- 							UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rkid and engineering_id=@engineering_id;
-- -- 							UPDATE gmms_horizontal SET state='出库中' where slicecage_rank=@rk and gourp=2;
-- -- 							UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
-- -- 							UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
-- -- 							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
-- -- 							call intTo2ByteHex10_16_4(@rk,@rks);
-- -- 							set dates=CONCAT("0001",@galss_ids,"0000",@rks);
-- -- 							call message(dates,'000d',@date);
-- -- 							set messagedate=@date;
-- 				
-- 	#进片
-- 	elseif flag=1 then
-- 		select rank,type_id,galss_id,processcard_id,width,height,slicecage_rank into @rankid,@type_id,@galss_id,@processcardid,@width,@height,@rk from gmms_galss_task where work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id order by rank limit 1;
-- 		UPDATE gmms_galss_task set slicecage_rank=@rk,work_procedure='理片',work_state='进库中' WHERE rank=@rankid and engineering_id=@engineering_id;
-- 		UPDATE gmms_horizontal set state='进库中',count=1,type_id=@type_id,processcard_id=@processcardid where slicecage_rank=@rk and gourp=1;
-- 		call intTo2ByteHex10_16_4(@rankid,@rankids);
-- 		call intTo2ByteHex10_16_4(@rk,@rks);
-- 		set dates=CONCAT("0001",@rankids,@rks);
-- 		call message(dates,'000a',@date);
-- 		set messagedate=@date;
-- 	
-- 	end if;
	set @conts=CONCAT(IFNULL(@conts,','),IFNULL(messagedate,','));
	set @end_time_=SYSDATE();
	insert into A_LengLipian(create_time,end_time,content) values (@create_time_,@end_time_,@conts);
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for Horizontal_copy1
-- ----------------------------
DROP PROCEDURE IF EXISTS `Horizontal_copy1`;
delimiter ;;
CREATE PROCEDURE `Horizontal_copy1`(in sendMessageHex varchar(255),in machineids BIGINT,out messagedate varchar(255),out msg varchar(255))
BEGIN
	#理片笼存储过程
	#找到当前机器标志位
	#查询机器逻辑表中对应的逻辑进行对应的步骤
	DECLARE machinedisposeids int DEFAULT 0;
	DECLARE dates varchar(255);
	DECLARE findplc1 varchar(255);
	DECLARE findmes1 varchar(255);
	DECLARE taskplc1 varchar(255);
	DECLARE taskmes1 varchar(255);
	DECLARE findplc2 varchar(255);
	DECLARE findmes2 varchar(255);
	DECLARE taskplc2 varchar(255);
	DECLARE taskmes2 varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	DECLARE IsExistChu varchar(255);
	#2023/11/13   查看理片时间
	set @create_time1_=null;
	set @end_time1_=null;
	set @create_time1_=SYSDATE();
	
	
	
	set messagedate=messagedates;
	set @log_task="";
		SET msg:='Slicecage  查配置';	
	select machine_type_id,remarks into @mtid,@remarks from gmms_machine where id=machineids;
	

	
	select mf.flag into @进完成标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='进完成字';
	select mf.flag into @出完成标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='出完成字';
	
	select mf.flag into @进请求标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='进请求字';
	select mf.flag into @出请求标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='出请求字';
	
	select mf.flag into @进发送标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='进发送字';
	select mf.flag into @出发送标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='出发送字';
	
	select mf.flag into @进完成确认标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='进完成确认字';
	select mf.flag into @出完成确认标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='出完成确认字';
	
	set findplc1 = substring(sendMessageHex,(cast(@进完成标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskplc1 = substring(sendMessageHex,(cast(@进请求标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskmes1 = substring(sendMessageHex,(cast(@进发送标志位 as UNSIGNED INTEGER)*4)+19,4);
	set findmes1 = substring(sendMessageHex,(cast(@进完成确认标志位 as UNSIGNED INTEGER)*4)+19,4);
	
	set findplc2 = substring(sendMessageHex,(cast(@出完成标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskplc2 = substring(sendMessageHex,(cast(@出请求标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskmes2 = substring(sendMessageHex,(cast(@出发送标志位 as UNSIGNED INTEGER)*4)+19,4);
	set findmes2 = substring(sendMessageHex,(cast(@出完成确认标志位 as UNSIGNED INTEGER)*4)+19,4);
	-- 修改
	set IsExistChu = substring(sendMessageHex,(8*4)+19,4);  
	
-- 	select taskplc1;
-- 	select taskmes1;
-- 	select findplc1;
-- 	select findmes1;
-- 	
-- 	select taskplc2;
-- 	select taskmes2;
-- 	select findplc2;
-- 	select findmes2;
-- 	

	
	#完成逻辑查询
	select count(*),md.id into @进完成数量,@进完成逻辑编号 from gmms_machine_dispose md join gmms_machine_type mt on md.machine_type_id=mt.id where md.machine_type_id=@mtid  and md.plc_font_type='进完成字' and md.mes_font_type='进完成确认字' and md.plc_font_value=findplc1 and md.mes_font_value=findmes1;
	
	select count(*),md.id into @出完成数量,@出完成逻辑编号 from gmms_machine_dispose md join gmms_machine_type mt on md.machine_type_id=mt.id where md.machine_type_id=@mtid  and md.plc_font_type='出完成字' and md.mes_font_type='出完成确认字' and md.plc_font_value=findplc2 and md.mes_font_value=findmes2;
	
	#请求逻辑查询
	select count(*),md.id into @进请求数量,@进请求逻辑编号 from gmms_machine_dispose md join gmms_machine_type mt on md.machine_type_id=mt.id where md.machine_type_id=@mtid  and md.plc_font_type='进请求字' and md.mes_font_type='进发送字' and md.plc_font_value=taskplc1 and md.mes_font_value=taskmes1;
	
	select count(*),md.id into @出请求数量,@出请求逻辑编号 from gmms_machine_dispose md join gmms_machine_type mt on md.machine_type_id=mt.id where md.machine_type_id=@mtid  and md.plc_font_type='出请求字' and md.mes_font_type='出发送字' and md.plc_font_value=taskplc2 and md.mes_font_value=taskmes2;
	
	
	#判断逻辑是否存在
	if @进完成数量=1 or @出完成数量=1 or @进请求数量=1 or @出请求数量=1 then
	
		#判断完成逻辑是否为空
		if @进完成逻辑编号!=0 then
			set machinedisposeids=@进完成逻辑编号;
			
		elseif @出完成逻辑编号!=0 then
			set machinedisposeids=@出完成逻辑编号;
			
		#判断请求逻辑是否为空
		elseif @进请求逻辑编号!=0 then
			set machinedisposeids=@进请求逻辑编号;
			
		elseif @出请求逻辑编号!=0 then
			set machinedisposeids=@出请求逻辑编号;
			
		end if;
		select machinedisposeids;
		#查询当前执行逻辑进行判断
		select logic,return_value into @ExecutionLogic,@returnvalue from gmms_machine_dispose where id=machinedisposeids;
		select @ExecutionLogic;
		if taskmes2="0001" then
				set dates=CONCAT("0000","00000000000000000000000000000000");
				call message(dates,'000d',@date);
				set messagedate=@date;
		elseif @ExecutionLogic='进片完成' then
				call HorizontalFinsh(machineids,machinedisposeids,2,@messagedate);
				set messagedate=@messagedate;		
		elseif @ExecutionLogic='出片完成' then
				call HorizontalFinsh(machineids,machinedisposeids,1,@messagedate);
				set messagedate=@messagedate;
		elseif @ExecutionLogic='进片完成破损1' then
				call HorizontalFinsh(machineids,machinedisposeids,3,@messagedate);
				set messagedate=@messagedate;
		elseif @ExecutionLogic='进片完成破损2' then
				call HorizontalFinsh(machineids,machinedisposeids,4,@messagedate);
				set messagedate=@messagedate;
		elseif @ExecutionLogic='进片完成破损3' then
				call HorizontalFinsh(machineids,machinedisposeids,5,@messagedate);
				set messagedate=@messagedate;
 		elseif @ExecutionLogic='出片完成破损1' then
				call HorizontalFinsh(machineids,machinedisposeids,6,@messagedate);
				set messagedate=@messagedate;
		elseif @ExecutionLogic='出片完成破损2' then
				call HorizontalFinsh(machineids,machinedisposeids,7,@messagedate);
				set messagedate=@messagedate;
		elseif @ExecutionLogic='出片完成破损3' then
				call HorizontalFinsh(machineids,machinedisposeids,8,@messagedate);
				set messagedate=@messagedate;
		elseif @ExecutionLogic='进完成清空' then
				call message(@returnvalue,'0016',@date);
				set messagedate=@date;
 		elseif @ExecutionLogic='出完成清空' then
				call message(@returnvalue,'0018',@date);
				set messagedate=@date;
		elseif taskplc1='0001' and taskplc2='0001' then		
				call HorizontalSend(machineids,0001,3,@messagedate);
				set messagedate=@messagedate;
		elseif @ExecutionLogic='进片请求' then
				 call HorizontalSend(machineids,@returnvalue,1,@messagedate);
				set messagedate=@messagedate;
		elseif @ExecutionLogic='出片请求' then		
				 call HorizontalSend(machineids,@returnvalue,2,@messagedate);
				set messagedate=@messagedate;
		elseif @ExecutionLogic='出任务清空' then
				set dates=CONCAT(@returnvalue,"00000000000000000000000000000000");
				call message(dates,'000d',@date);
				set messagedate=@date;
 		elseif @ExecutionLogic='进任务清空' then
				set dates=CONCAT(@returnvalue,"00000000");
				call message(dates,'000a',@date);
				set messagedate=@date;
		end if;
	end if;
	set @end_time1_=SYSDATE();
	insert into A_LengLipian_copy1(create_time,end_time,content) values (@create_time1_,@end_time1_,messagedate);
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for Identify
-- ----------------------------
DROP PROCEDURE IF EXISTS `Identify`;
delimiter ;;
CREATE PROCEDURE `Identify`(in sendMessageHex varchar(255),in machineids BIGINT,out messagedate varchar(255))
BEGIN
	#测量台存储过程
	#找到当前机器标志位
	#查询机器逻辑表中对应的逻辑进行对应的步骤
	DECLARE machinedisposeids int DEFAULT 0;
	DECLARE dates varchar(255);
	DECLARE findplc varchar(255);
	DECLARE findmes varchar(255);
	DECLARE taskplc varchar(255);
	DECLARE taskmes varchar(255);
	DECLARE plcflag varchar(255);
	DECLARE mesflag varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	set messagedate=messagedates;
	set @log_task="";
	#此处修改，新版是没有gmms_machine表  为兼容当前，旧的代码依然可用，但是设备id 和gmms_machine已经没关系了强制给类型测量 
	#select machine_type_id into @mtid from gmms_machine where id=machineids;
	set @mtid=4;   
	
	
	select mf.flag into @完成标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='完成字';
	
	select mf.flag into @请求标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='请求字';
	
	select mf.flag into @发送标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='发送字';
	
	select mf.flag into @完成确认标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='完成确认字';
	
	set findplc = substring(sendMessageHex,(cast(@完成标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskplc = substring(sendMessageHex,(cast(@请求标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskmes = substring(sendMessageHex,(cast(@发送标志位 as UNSIGNED INTEGER)*4)+19,4);
	set findmes = substring(sendMessageHex,(cast(@完成确认标志位 as UNSIGNED INTEGER)*4)+19,4);
	
	set plcflag = substring(sendMessageHex,(cast(2 as UNSIGNED INTEGER)*4)+19,4);
	set mesflag = substring(sendMessageHex,(cast(23 as UNSIGNED INTEGER)*4)+19,4);
	
	select taskplc;
	select taskmes;
	select findplc;
	select findmes;
	
	#完成逻辑查询
	select count(*),md.id into @完成数量,@完成逻辑编号 from gmms_machine_dispose md join gmms_machine_type mt on md.machine_type_id=mt.id where md.machine_type_id=@mtid  and md.plc_font_type='完成字' and md.mes_font_type='完成确认字' and md.plc_font_value=findplc and md.mes_font_value=findmes;
	
	select *  from gmms_machine_dispose md join gmms_machine_type mt on md.machine_type_id=mt.id where md.machine_type_id=@mtid  and md.plc_font_type='完成字' and md.mes_font_type='完成确认字' and md.plc_font_value=findplc and md.mes_font_value=findmes;
	select machine_type_id,work_id,remarks into @mtid,@work_groups,@remarks from gmms_machine where id=machineids;
	select minwidths into @minwidth2 from gmms_preprocessing_glass where groups=@work_groups;
	select file_name into @engineering_id from gmms_raw_file where operator=@work_groups and state='已领取';
	select id into @patternid from gmms_pattern_task where groups=@work_groups and flag=0 order by rank limit 0,1;
	select count(*) into @counts from gmms_galss_task where work_state='待识别' and work_procedure='测量' and width>@minwidth2 and height>@minwidth2 and pattern_id=@patternid and engineering_id=@engineering_id;
	select @counts;
	if @counts=0 then
		update gmms_pattern_task set flag=1 where id=@patternid and groups=@work_groups;
	end if;


-- 	if plcflag='0000' and mesflag='0001' then
-- 			call message('0000','0017',@date);
-- 			set messagedate=@date;
-- 	elseif plcflag='0001' and mesflag='0000' then	
-- 			select id into @pattern_id from gmms_pattern_task where flag=0 order by rank limit 1;
-- 			update gmms_galss_task set work_procedure_number=100 where pattern_id=@pattern_id;
-- 			update gmms_pattern_task set flag=1 where id=@pattern_id;
-- 			call message('0001','0017',@date);
-- 			set messagedate=@date;
-- 	#判断逻辑是否存在
-- 	else
	if @完成数量=1 then
	
		#判断完成逻辑是否为空
			set machinedisposeids=@完成逻辑编号;
			
			
		
		#查询当前执行逻辑进行判断
		select logic,return_value into @ExecutionLogic,@returnvalue from gmms_machine_dispose where id=machinedisposeids;
		select @returnvalue;
		call log_key_value(@log_task, "@ExecutionLogic", @ExecutionLogic);

		if @ExecutionLogic='任务完成' then
				call IdentifyFinsh(sendMessageHex,machineids,@returnvalue,@messagedate);
				call log_key_value(@log_task, "@messagedate", @messagedate);
				set messagedate=@messagedate;
  	elseif @ExecutionLogic='完成清空' then
				set dates=CONCAT(@returnvalue,'00000000000000000000');
 				call message(dates,'0014',@date);
 				set messagedate=@date;
		elseif @ExecutionLogic='失败清空' then
 				set dates=CONCAT(@returnvalue,'00000000000000000000');
 				call message(dates,'0014',@date);
 				set messagedate=@date;
-- 		elseif @ExecutionLogic='任务请求' then
-- 				call LoadSend(machineids,@messagedate);
-- 				set messagedate=@messagedate;
--  		elseif @ExecutionLogic='任务清空' then
-- 				set dates=CONCAT(@returnvalue,"0000000000000000");
-- 				call message(dates,'0014',@date);
-- 				set messagedate=@date;
		end if;
	end if;
	
			set @log_nm=concat("测量",machineids);
    set @exist_nm=null;
	
    select `var_value`,var_name into @var_value,@exist_nm from memory_var_table where var_name=@log_nm;
		 if @exist_nm is null then 
		    insert into memory_var_table (var_name,var_value,update_time) values (@log_nm,@log_task,now());
			else
			    update memory_var_table set var_value=@log_task,update_time=now() where var_name=@log_nm;
		 end if;
	
	
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for IdentifyFinsh
-- ----------------------------
DROP PROCEDURE IF EXISTS `IdentifyFinsh`;
delimiter ;;
CREATE PROCEDURE `IdentifyFinsh`(in sendMessageHex varchar(255),in machineids BIGINT,in returnvalue varchar(255),out messagedate varchar(255))
BEGIN
	#测量自动匹配存储过程
	#根据发送过来的数据进行转换，并算出宽高的误差最大值跟最小值
	#根据误差值查询最为匹配的第一条数据进行修改，并判断为直通或者是存笼然后发送匹配成功
	#@flag=0为自动匹配，@flag>0为绑定了数据进行手动匹配，@flag<0为匹配失败拿走
	#发送数据：序号，模式
	DECLARE findplc varchar(255);
	DECLARE findmes varchar(255);
	DECLARE widths varchar(255);
	DECLARE heigths varchar(255);
	DECLARE falgs varchar(255);
	DECLARE liuchengkas varchar(255);
	DECLARE maxwidth double;
	DECLARE minwidth double;
	DECLARE maxheigth double;
	DECLARE minheigth double;
	DECLARE maxwidth1 double;
	DECLARE minwidth1 double;
	DECLARE maxheigth1 double;
	DECLARE minheigth1 double;
	DECLARE match_error int default 0;
	DECLARE dates varchar(255);
	DECLARE s int DEFAULT 0;
	DECLARE x int DEFAULT 0;
	DECLARE counts int DEFAULT 0;
	DECLARE galssid BIGINT DEFAULT 0;
	DECLARE plcgalssid BIGINT DEFAULT 0;
	DECLARE messagedates varchar(255) DEFAULT '';
	DECLARE isFinsh_ varchar(255) DEFAULT null;
	set messagedate=messagedates;
	
	select machine_type_id,work_id,remarks into @mtid,@work_groups,@remarks from gmms_machine where id=machineids;
	
	if machineids=4 or machineids=23 or machineids=31 then
 
	select file_name into @engineering_id from gmms_raw_file where operator=@work_groups and state='已领取' limit 0,1;
	select @engineering_id;
	select widtherror,heigtherror into @widtherror,@heigtherror from gmms_identify_error where groups=@work_groups;
	select minwidths into @minwidth2 from gmms_preprocessing_glass where groups=@work_groups;
	
		

	set widths = substring(sendMessageHex,(cast(6 as UNSIGNED INTEGER)*4)+19,4);
	set heigths = substring(sendMessageHex,(cast(7 as UNSIGNED INTEGER)*4)+19,4);
	
	
	set widths=conv(widths,16,10)/10;
	set heigths=conv(heigths,16,10)/10;
	
	select widths;
	select heigths;
	
	set minwidth=widths-@widtherror;
	set maxwidth=widths+@widtherror;
	set minheigth=heigths-@heigtherror;
	set maxheigth=heigths+@heigtherror;
	
	select maxwidth;
	select maxheigth;
	
	select flag,marking into @flag,@marking from gmms_preprocessing_glass where groups=@work_groups limit 0,1;
	select @flag;

	set @patternid=null;
	set @counts=0;
	set @counts2=0;
	
	select id into @patternid from gmms_pattern_task where groups=@work_groups and flag=0 order by rank limit 0,1;
	select @patternid;
	call log_key_value(@log_task, "@patternid",  @patternid);
	if @patternid is not null then
	
		select count(*) into @counts from (select width,height,thickness,color from gmms_galss_task where work_procedure='测量' and work_state='待识别' and pattern_id=@patternid and engineering_id=@engineering_id and work_procedure_number=100 and width>@minwidth2 and height>@minwidth2 and (((width<=maxwidth and width>=minwidth) and (height<=maxheigth and height>=minheigth))or((width<=maxheigth and width>=minheigth) and (height<=maxwidth and height>=minwidth))) group by width,height,thickness,color) as rowss;
	
		select count(*) into @counts2 from (select width,height,thickness,color from gmms_galss_task where work_procedure='测量' and work_state='待识别' and pattern_id=@patternid and engineering_id=@engineering_id and work_procedure_number=100 and width>@minwidth2 and height>@minwidth2 and ((width<=maxwidth and width>=minwidth) and (height<=maxheigth and height>=minheigth)) group by width,height,thickness,color) as rowss;
		
	else
	
		select count(*) into @counts from (select width,height,thickness,color from gmms_galss_task where work_procedure='测量' and work_state='待识别' and engineering_id=@engineering_id and work_procedure_number=100  and (((width<=maxwidth and width>=minwidth) and (height<=maxheigth and height>=minheigth))or((width<=maxheigth and width>=minheigth) and (height<=maxwidth and height>=minwidth))) group by width,height,thickness,color) as rowss;
	
		select count(*) into @counts2 from (select width,height,thickness,color from gmms_galss_task where work_procedure='测量' and work_state='待识别' and engineering_id=@engineering_id and work_procedure_number=100 and ((width<=maxwidth and width>=minwidth) and (height<=maxheigth and height>=minheigth)) group by width,height,thickness,color) as rowss;

	end if;
	call log_key_value(@log_task, "@flag",  @flag);
	#自动匹配
	select @counts,@counts2;
	if @flag=0 then
	
		if @counts=1 then 
		if @counts2=1 then
		
			if machineids=31 then
				set @id=null;
				select processcard_id into liuchengkas from gmms_drop_frame where groups=1  limit 0,1;
				
				if liuchengkas is not null and 1>2 then
					select id,width,height,marking_flag into @id,@width,@height,@marking_flag from gmms_galss_task 
					where work_procedure='测量' and work_state='待识别' and processcard_id=liuchengkas and engineering_id=@engineering_id and work_procedure_number=100 
					and (((width<=maxwidth and width>=minwidth) and (height<=maxheigth and height>=minheigth))or((width<=maxheigth and width>=minheigth) and (height<=maxwidth and height>=minwidth))) 
					limit 0,1;
					
					if @id is null then
						select id,width,height,marking_flag into @id,@width,@height,@marking_flag from gmms_galss_task 
						where work_procedure='测量' and work_state='待识别' and engineering_id=@engineering_id and work_procedure_number=100 
						and (((width<=maxwidth and width>=minwidth) and (height<=maxheigth and height>=minheigth))or((width<=maxheigth and width>=minheigth) and (height<=maxwidth and height>=minwidth))) 
						ORDER BY pattern_id ASC limit 0,1;
					end if;
				
				else
				
					select id,width,height,marking_flag into @id,@width,@height,@marking_flag from gmms_galss_task as gt
          where work_procedure='测量' and work_state='待识别' and engineering_id=@engineering_id and work_procedure_number=100 
          and (((width<=maxwidth and width>=minwidth) and (height<=maxheigth and height>=minheigth))or((width<=maxheigth and width>=minheigth) and (height<=maxwidth and height>=minwidth)))			
          ORDER BY (select count(*) from gmms_galss_task as gt2 where gt2.work_procedure='测量' and gt2.work_state='待识别' and gt2.engineering_id=@engineering_id and gt2.work_procedure_number=100 and gt2.processcard_id=gt.processcard_id) ASC limit 0,1;

				end if;												
			else			
				select id,width,height,marking_flag into @id,@width,@height,@marking_flag from gmms_galss_task 
				where work_procedure='测量' and work_state='待识别' and engineering_id=@engineering_id and work_procedure_number=100 
				and (((width<=maxwidth and width>=minwidth) and (height<=maxheigth and height>=minheigth))or((width<=maxheigth and width>=minheigth) and (height<=maxwidth and height>=minwidth))) 
				ORDER BY pattern_id ASC limit 0,1;						
			end if;
			select @id;
			
			call log_key_value(@log_task, "@id",  @id);
				###
			#此处修改,添加一个更新动作标识的程序
			if @id is not null and @id<>0 then	
				if machineids=4 then 
					if @marking_flag<10 then 
						set @z_line=1;
						if machineids=4 then 
					   set @z_line=3;
						end if;
						if machineids=23 then 
					    set @z_line=2;
						end if; 
					call dongzuo_get_number_copy1(@id,@z_line, @marking_flag);
					update gmms_galss_task set marking_flag=@marking_flag where id=@id and engineering_id=@engineering_id;
				end if;
			end if;
			###
			
			
			
			if @width>=2500 or @height>=2000 then
				set falgs='0002';
			else 
				set falgs='0001';
			end if;
				
				select max(rank) into @maxrank from gmms_galss_task where engineering_id=@engineering_id limit 0,1;
				select @id;
				update gmms_galss_task set work_state='等待中',rank=@maxrank+1,work_procedure='理片',work_procedure_number=200 where id=@id and engineering_id=@engineering_id;
				select @maxrank;
				select gt.galss_id,gt.width,gt.height,gt.thickness,pt.color into @glassid,@widths,@heights,@thicknesss,@colors from gmms_galss_task gt join gmms_pattern_task pt on gt.pattern_id=pt.id where gt.id=@id and engineering_id=@engineering_id limit 0,1;
				select @glassid;
				
				update gmms_preprocessing_glass set glass_id=@glassid,actual_length=widths,actual_width=heigths,length=@widths,width=@heights,statu='匹配成功',color=@colors,thickness=@thicknesss,flag=0 where groups=@work_groups;
				
				insert gmms_identify_error_details(galssid,width,heigth,error_width,error_heigth,groups,times,flag) values(@glassid,@width,@height,widths,heigths,@work_groups,NOW(),1);
				
			

			select rank,type_id,processcard_id,stop_position,drop_frame,engineering_id,galss_id into @rank,@type_id,@processcard_id,@stop_position,@drop_frame,@engineering_id,@galss_id from gmms_galss_task where id=@id and engineering_id=@engineering_id limit 0,1;
			
			if machineids=4 then
				if falgs='0001' then
					call intTo2ByteHex10_16_4(@rank,@ranks);
				#	call intTo2ByteHex10_16_4(@marking_flag,@marking_flags);
					
				#	set @marking=1;
					
					if @marking=1 then
					   select da_biao_mo_ban into @marking_flag from gmms_dongzuo_table where number=@marking_flag limit 0,1;
					 call intTo2ByteHex10_16_4(@marking_flag,@marking_flags);
						set dates=CONCAT('0001',@ranks,falgs,'0000',@marking_flags);
					elseif @marking=0 then
						set dates=CONCAT('0001',@ranks,falgs);
					end if;
					call message(dates,'0014',@date);
					set messagedate=@date;
				end if;
				
			elseif machineids=23 then
				if falgs='0001' then
					call intTo2ByteHex10_16_4(@rank,@ranks);
					set dates=CONCAT('0001',@ranks,falgs);
					call message(dates,'0014',@date);
					set messagedate=@date;
				end if;
			else 
				call log_key_value(@log_task, "@width",  @width);
				if @width>=2400 or @height>=2400 then
					set falgs='0002';
				else 
					set falgs='0001';
				end if;
			
				call log_key_value(@log_task, "@flags",  falgs);
				if falgs='0001' then
					set @slicecage_rank=null;
					select slicecage_rank into @ransk from gmms_galss_task where work_state='等待中' and work_procedure='理片' and engineering_id=@engineering_id order by rank limit 1;
					if @ransk is not null or @ransk<>0 then
					
						
						select slicecage_rank into @slicecage_rank from gmms_horizontal  where ((gourp=1 and state='空闲' ) or (gourp=2 and state='使用中' and type_id=@type_id and processcard_id=@processcard_id)) and flag=0 and slicecage_rank <>3 and slicecage_rank<>@ransk group by slicecage_rank having count(*)=2 order by id  limit 1;

						if @slicecage_rank is  null then
							select slicecage_rank into @slicecage_rank from gmms_horizontal  where ((gourp=1 and state='空闲' ) or (gourp=2 and state='空闲')) and flag=0 and slicecage_rank<>3 and slicecage_rank<>@ransk group by slicecage_rank having count(*)=2 order by id  limit 1;
						end if;
						if @slicecage_rank is not null then		
							update gmms_galss_task set slicecage_rank=@slicecage_rank where id=@id and engineering_id=@engineering_id;
						else
						select 1;
							update gmms_galss_task set work_state='待识别',work_procedure='测量',work_procedure_number=100,rank=0,broken_state=2 where id=@id and engineering_id=@engineering_id;
						end if;
						
						
					else
						select slicecage_rank into @slicecage_rank from gmms_horizontal  where ((gourp=1 and state='空闲' ) or (gourp=2 and state='使用中' and type_id=@type_id and processcard_id=@processcard_id)) and flag=0 and slicecage_rank <>3 group by slicecage_rank having count(*)=2 order by id  limit 1;
						
						if @slicecage_rank is  null then
							select slicecage_rank into @slicecage_rank from gmms_horizontal  where ((gourp=1 and state='空闲' ) or (gourp=2 and state='空闲')) and flag=0 and slicecage_rank <>3 group by slicecage_rank having count(*)=2 order by id  limit 1;
						end if;
						if @slicecage_rank is not null then		
							update gmms_galss_task set slicecage_rank=@slicecage_rank where id=@id and engineering_id=@engineering_id;
						else
						select 1;
							update gmms_galss_task set work_state='待识别',work_procedure='测量',work_procedure_number=100,slicecage_rank=null,rank=0,broken_state=2 where id=@id and engineering_id=@engineering_id;
						end if;
					end if;
					
					
					select @slicecage_rank;
					if @slicecage_rank is not null and @slicecage_rank<>0 then	
						call intTo2ByteHex10_16_4(@rank,@ranks);
						call intTo2ByteHex10_16_4(@slicecage_rank,@slicecage_ranks);
						set dates=CONCAT('0001',@ranks,falgs,@slicecage_ranks);
						call message(dates,'0014',@date);
						insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
						values (machineids,@work_groups,NOW(),'测量台发送',@slicecage_rank,@date);
						set messagedate=@date;
					end if;
					
				else
-- 				  call log_key_value(@log_task, "@stop_position",  @stop_position);
					#一线直通
					if @stop_position > 0 then
						update gmms_galss_task set work_state='直通' where id=@id and engineering_id=@engineering_id;
						call intTo2ByteHex10_16_4(@rank,@ranks);
						call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
						set dates=CONCAT('0001',@galss_ids,falgs,'0003');
						call message(dates,'0014',@date);
						set messagedate=@date;	
					else

						select id into @drop_pos from gmms_drop_frame where state='空架' and groups=@work_groups and (stops=1 or stops=2) limit 1;
						if @drop_pos is not null then
							select 总数 into @processcard_count from processcard_ex1 where 流程卡=@processcard_id and `工程号`=@engineering_id;
							UPDATE gmms_drop_frame set state='使用中',drop_frame=@drop_frame,processcard_id=@processcard_id,processcard_count=@processcard_count,engineering_id=@engineering_id where id=@drop_pos;
							UPDATE gmms_galss_task set stop_position=@drop_pos where processcard_id=@processcard_id and engineering_id=@engineering_id;	
							update gmms_galss_task set work_state='直通' where id=@id and engineering_id=@engineering_id;
							call intTo2ByteHex10_16_4(@rank,@ranks);
							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
							set dates=CONCAT('0001',@galss_ids,falgs,'0003');
							call message(dates,'0014',@date);
							set messagedate=@date;
						else
							update gmms_galss_task set work_state='待识别',work_procedure='测量',work_procedure_number=100,rank=0 where id=@id and engineering_id=@engineering_id;
						end if;
					end if;
					
				end if;
			end if;
			end if;
		else
			update gmms_preprocessing_glass set statu='匹配失败1',actual_length=widths,actual_width=heigths,glass_id=null where groups=@work_groups;
			set  match_error=1;
			set messagedate=messagedates;
		end if;
		elseif @counts>1  then
			update gmms_preprocessing_glass set statu='多数据匹配',actual_length=widths,actual_width=heigths,glass_id=null where groups=@work_groups;
			set messagedate=messagedates;
		elseif  @counts=0 then
			update gmms_preprocessing_glass set statu='匹配失败',actual_length=widths,actual_width=heigths,glass_id=null where groups=@work_groups;
			set messagedate=messagedates;
			set  match_error=1;
		end if;
	
	#手动匹配，判断是否绑定玻璃信息
	elseif @flag>0 then		
		call Identifymanual(machineids,returnvalue,@messagedate);
		set messagedate=@messagedate;
	#人工拿走
	elseif @flag<0 then
		update gmms_preprocessing_glass set flag=0 where groups=@work_groups;
		set dates=CONCAT('00020000');
		call message(dates,'0014',@date);
		set messagedate=@date;
	end if;
	
	
	
	
elseif machineids=50 then

	select widtherror,heigtherror into @widtherror,@heigtherror from gmms_identify_error where groups=@work_groups;
	
	set widths = substring(sendMessageHex,(cast(6 as UNSIGNED INTEGER)*4)+19,4);
	set heigths = substring(sendMessageHex,(cast(7 as UNSIGNED INTEGER)*4)+19,4);
	set plcgalssid = conv(substring(sendMessageHex,(cast(5 as UNSIGNED INTEGER)*4)+19,4),16,10);

	set widths=conv(widths,16,10)/10;
	set heigths=conv(heigths,16,10)/10;
	#set plcgalssid=conv(plcgalssid,16,10);
	set minwidth=widths-@widtherror;
	set maxwidth=widths+@widtherror;
	set minheigth=heigths-@heigtherror;
	set maxheigth=heigths+@heigtherror;
	
-- 	set minwidth1=widths-2;
-- 	set maxwidth1=widths+2;
-- 	set minheigth1=heigths-2;
-- 	set maxheigth1=heigths+2;
	
	set @taskid=null;
	set @flag1=null;
	select flag,marking into @flag1,@marking from gmms_preprocessing_glass where groups=@work_groups;
	#找到有任务的第一个工位,流程卡 默认B  A
	select lk.liuchengka into @processcardid from gmms_drop_frame_5 as dr5 
	left join gmms_shelf_rack_2 as sr on dr5.rank=sr.id
	left join gmms_liuchengka as lk on sr.processcard_id=lk.liuchengka
	where groups_=3 and drop_groups=1 and dr5.sign=3 and sr.id is not null and lk.liuchengka is not null and 
	lk.current_status='正在工作' and lk.current_work_position='理片' limit 0,1;
	
	select @processcardid;
	select id,galss_id,edg_width,edg_height,thickness,color,stop_rank 
	into @taskid,@glassid,@widths,@heights,@thicknesss,@colors,@stop_rank from gmms_galss_task_shelf 
	where work_procedure='理片笼上片' and work_state='已完成' and processcard_id=@processcardid
	order by stop_rank desc limit 0,1;
	select @widths,@heights;
	
	select galss_id into isFinsh_ from gmms_galss_task_shelf 
	where work_procedure='理片' and galss_id=plcgalssid and processcard_id=@processcardid
	order by stop_rank desc limit 0,1;
  
	select isFinsh_,@flag1,widths,heigths,plcgalssid,minwidth,@taskid,@widtherror,@heigtherror,sendMessageHex;
	#自动匹配
	select  @flag1;
	if isFinsh_ is not null and @flag1=0 then
		set falgs='0001';
    call intTo2ByteHex10_16_4(isFinsh_,@ranks);
		set dates=CONCAT('0001',@ranks,falgs);
		call message(dates,'0014',@date);
		set messagedate=@date;
	elseif @flag1=0 and @widths is not null  then
		if (((@widths<=maxwidth and @widths>=minwidth) and (@heights<=maxheigth and @heights>=minheigth))
		 or ((@widths<=maxheigth and @widths>=minheigth) and (@heights<=maxwidth and @heights>=minwidth)))  then 
			set falgs='0001';	
			update gmms_preprocessing_glass 
			set glass_id=@glassid,actual_length=widths,actual_width=heigths,length=@widths,width=@heights,statu='匹配成功',
			color=IFNULL(@colors,''),thickness=IFNULL(@thicknesss,0),flag=0 where groups=@work_groups;	
			update gmms_galss_task_shelf set work_state='等待中',work_procedure='理片' 
			where id=@taskid and processcard_id=@processcardid;	
			
			call intTo2ByteHex10_16_4(@glassid,@ranks);
			set dates=CONCAT('0001',@ranks,falgs);
			call message(dates,'0014',@date);
			set messagedate=@date;
		else
			update gmms_preprocessing_glass set flag=1,statu='匹配失败',actual_length=widths,actual_width=heigths,glass_id=IFNULL(@glassid,0),length=IFNULL(@widths,0),width=IFNULL(@heights,0) where groups=@work_groups;
			set messagedate=messagedates;
			set match_error=1;			
		end if;
	elseif @flag1=3 then
		
		set falgs='0001';	
		update gmms_preprocessing_glass 
		set glass_id=@glassid,actual_length=widths,actual_width=heigths,length=@widths,width=@heights,statu='匹配成功',
		color=IFNULL(@colors,''),thickness=IFNULL(@thicknesss,0),flag=0 where groups=@work_groups;	
		update gmms_galss_task_shelf set work_state='等待中',work_procedure='理片' 
		where id=@taskid and processcard_id=@processcardid;	
		call intTo2ByteHex10_16_4(@glassid,@ranks);
		set dates=CONCAT('0001',@ranks,falgs);
		call message(dates,'0014',@date);
		set messagedate=@date;
		
	elseif @flag1<0 then
		update gmms_preprocessing_glass set flag=0 where groups=@work_groups;
		set dates=CONCAT('00020000');
		call message(dates,'0014',@date);
		set messagedate=@date;
	end if;
end if;
	select match_error;
	
	if match_error=1 then 
		 select int_value into match_error from gmms_globel_var where var_name='冷测量' and line=@work_groups;
		 if match_error=0 then 
-- 			set dates=CONCAT('00030000');
-- 			call message(dates,'0014',@date);
-- 			set messagedate=@date;
			update gmms_globel_var set int_value=1 where var_name='冷测量' and line=@work_groups;
		end if;				
		set match_error=1;
  else 
    if messagedate is not null and messagedate<>"" then 
					update gmms_globel_var set int_value=0 where var_name='冷测量' and line=@work_groups;
		end if;
	end if ;
	
		
-- 	select slicecage_rank into @slicecage_rank from gmms_horizontal  where ((gourp=1 and state='空闲' ) or (gourp=2 and state='空闲')) and flag=0 and slicecage_rank <>3 group by slicecage_rank having count(*)=2 order by id  limit 1;
-- 
-- 					if @slicecage_rank is  null then
-- 						select slicecage_rank into @slicecage_rank from gmms_horizontal where state="空闲" and flag=0 and slicecage_rank!=3  and gourp=1  order by id  limit 1;
-- 					end if;
-- 				end if;
-- 	
	
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for Identifymanual
-- ----------------------------
DROP PROCEDURE IF EXISTS `Identifymanual`;
delimiter ;;
CREATE PROCEDURE `Identifymanual`(in machineids BIGINT,in returnvalue varchar(255),out messagedate varchar(255))
BEGIN
	#测量台手动匹配存储过程
	#找到测量匹配表在界面绑定的数据进行修改
	DECLARE dates varchar(255);
	DECLARE falgs varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	set messagedate=messagedates;
	
	select machine_type_id,work_id,remarks into @mtid,@work_groups,@remarks from gmms_machine where id=machineids;
	
	if machineids=4 or machineids=23 or machineids=31 then
	
	select file_name into @engineering_id from gmms_raw_file where operator=@work_groups and state='已领取' limit 0,1;
	
	select flag,marking into @flag,@marking from gmms_preprocessing_glass where groups=@work_groups;
	select @flag;
	
	if @flag>0 then
	   SELECT width,height into @z_width,@z_height from gmms_galss_task where id=@flag and engineering_id=@engineering_id;
	
	
		select width,height,galss_id into @width,@heigth,@galss_id from gmms_galss_task where id=@flag and engineering_id=@engineering_id;
		select actual_width,actual_length into @actual_width,@actual_length from gmms_preprocessing_glass  where groups=@work_groups;
		select @width;
		select @heigth;
		if @width>=35000 or	@heigth>=35000 then
			set falgs='0002';
		else
			set falgs='0001';
		end if;
		
		select max(rank) into @maxrank from gmms_galss_task where engineering_id=@engineering_id;

		update gmms_galss_task set work_state='等待中',work_procedure='理片',rank=@maxrank+1,work_procedure_number=200 where id=@flag and engineering_id=@engineering_id;
		
		update gmms_preprocessing_glass set statu='匹配成功',glass_id=@galss_id,length=@width,width=@heigth,flag=0 where groups=@work_groups;
		
		insert gmms_identify_error_details(galssid,width,heigth,error_width,error_heigth,groups,times,flag) values(@galss_id,@width,@heigth,@actual_length,@actual_width,@work_groups,NOW(),2);

		
		select rank,type_id,processcard_id,stop_position,drop_frame,engineering_id,galss_id into @rank,@type_id,@processcard_id,@stop_position,@drop_frame,@engineering_id,@galss_id from gmms_galss_task where id=@flag and engineering_id=@engineering_id limit 0,1;
		
		select @rank;
		
		
		
		if machineids =31 then
				if @width>=2500 or @height>=2500 then
				set falgs='0002';
			else 
				set falgs='0001';
			end if;
				if falgs='0001' then
					select slicecage_rank into @ransk from gmms_galss_task where work_state='等待中' and work_procedure='理片' and engineering_id=@engineering_id order by rank limit 1;
					if @ransk is not null or @ransk<>0 then
					
						
						select slicecage_rank into @slicecage_rank from gmms_horizontal  where ((gourp=1 and state='空闲' ) or (gourp=2 and state='使用中' and type_id=@type_id and processcard_id=@processcard_id)) and flag=0 and slicecage_rank <>3 and slicecage_rank<>@ransk group by slicecage_rank having count(*)=2 order by id  limit 1;

						if @slicecage_rank is  null then
							select slicecage_rank into @slicecage_rank from gmms_horizontal  where ((gourp=1 and state='空闲' ) or (gourp=2 and state='空闲')) and flag=0 and slicecage_rank<>3 and slicecage_rank<>@ransk group by slicecage_rank having count(*)=2 order by id  limit 1;
						end if;
						if @slicecage_rank is not null then		
							update gmms_galss_task set slicecage_rank=@slicecage_rank where id=@flag and engineering_id=@engineering_id;
						else
							update gmms_galss_task set work_state='待识别',work_procedure='测量',work_procedure_number=100,rank=0,broken_state=2 where id=@flag and engineering_id=@engineering_id;
						end if;
						
					else
					
						
						select slicecage_rank into @slicecage_rank from gmms_horizontal  where ((gourp=1 and state='空闲' ) or (gourp=2 and state='使用中' and type_id=@type_id and processcard_id=@processcard_id)) and flag=0 and slicecage_rank <>3 group by slicecage_rank having count(*)=2 order by id  limit 1;
						
						if @slicecage_rank is  null then
							select slicecage_rank into @slicecage_rank from gmms_horizontal  where ((gourp=1 and state='空闲' ) or (gourp=2 and state='空闲')) and flag=0 and slicecage_rank <>3 group by slicecage_rank having count(*)=2 order by id  limit 1;
						end if;
						if @slicecage_rank is not null then		
							update gmms_galss_task set slicecage_rank=@slicecage_rank where id=@flag and engineering_id=@engineering_id;
						else
							update gmms_galss_task set work_state='待识别',work_procedure='测量',work_procedure_number=100,rank=0,broken_state=2 where id=@flag and engineering_id=@engineering_id;
						end if;
					end if;
					
					
					if @slicecage_rank is not null and @slicecage_rank<>0 then	
						call intTo2ByteHex10_16_4(@rank,@ranks);
						call intTo2ByteHex10_16_4(@slicecage_rank,@slicecage_ranks);
						set dates=CONCAT('0001',@ranks,falgs,@slicecage_ranks);
						call message(dates,'0014',@date);
						set messagedate=@date;
					end if;
				else
				
					if @stop_position > 0 then
						update gmms_galss_task set work_state='直通' where id=@flag and engineering_id=@engineering_id;
						call intTo2ByteHex10_16_4(@rank,@ranks);
						call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
						set dates=CONCAT('0001',@galss_ids,falgs,'0003');
						call message(dates,'0014',@date);
						set messagedate=@date;	
					else
						select id into @drop_pos from gmms_drop_frame where state='空架' and groups=@work_groups limit 1;
						if @drop_pos is not null then
							select 总数 into @processcard_count from processcard_ex1 where 流程卡=@processcard_id and `工程号`=@engineering_id;
							UPDATE gmms_drop_frame set state='使用中',drop_frame=@drop_frame,processcard_id=@processcard_id,processcard_count=@processcard_count,engineering_id=@engineering_id where id=@drop_pos;
							UPDATE gmms_galss_task set stop_position=@drop_pos where processcard_id=@processcard_id and engineering_id=@engineering_id;	
							update gmms_galss_task set work_state='直通' where id=@flag and engineering_id=@engineering_id;
							call intTo2ByteHex10_16_4(@rank,@ranks);
							call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
							set dates=CONCAT('0001',@galss_ids,falgs,'0003');
							call message(dates,'0014',@date);
							set messagedate=@date;
						else
							update gmms_galss_task set work_state='待识别',work_procedure='测量',work_procedure_number=100,rank=0 where id=@id and engineering_id=@engineering_id;
						end if;
					end if;
					end if;
		elseif machineids =4 then
				call intTo2ByteHex10_16_4(@rank,@ranks);
				set @z_mak=0;
				
				select marking_flag into @marking_flag from gmms_galss_task where id=@flag;
				
			select da_biao_mo_ban into @z_mak from gmms_dongzuo_table where number=@marking_flag LIMIT 1;
		
				
				call intTo2ByteHex10_16_4(@z_mak,@marking_flags);
				select @marking_flags;
				if @marking=1 then
					set dates=CONCAT('0001',@ranks,falgs,'0000',@marking_flags);
				elseif @marking=0 then
					set dates=CONCAT('0001',@ranks,falgs);
				end if;
				call message(dates,'0014',@date);
				set messagedate=@date;
		else
				call intTo2ByteHex10_16_4(@rank,@ranks);
				set dates=CONCAT('0001',@ranks,falgs);
				call message(dates,'0014',@date);
				set messagedate=@date;
		end if;
		end if;
		
		
	elseif machineids=50 then
	
	
	select flag into @flag from gmms_preprocessing_glass where groups=@work_groups;
	select @flag;
	
	if @flag>0 then

		select edg_width,edg_height,galss_id into @width,@heigth,@galss_id from gmms_galss_task_slicecage where id=@flag and processcard_id=@remarks;
		select @width;
		select @heigth;

		set falgs='0001';

		
		select max(rank) into @maxrank from gmms_galss_task_slicecage where processcard_id=@remarks;
			
		update gmms_galss_task_slicecage set work_state='等待中',work_procedure='理片',rank=@maxrank+1,work_procedure_number=900 where id=@flag and processcard_id=@remarks;
		
			update gmms_preprocessing_glass set statu='匹配成功',glass_id=@galss_id,length=@width,width=@heigth,flag=0 where groups=@work_groups;
		
		select rank into @rank from gmms_galss_task_slicecage where id=@flag and processcard_id=@remarks;
		
		
		
		
	
				call intTo2ByteHex10_16_4(@rank,@ranks);
				set dates=CONCAT('0001',@ranks,falgs);
				call message(dates,'0014',@date);
				set messagedate=@date;
		

	end if;
		end if;
		
		
		    if messagedate is not null and messagedate<>"" then 
					update gmms_globel_var set int_value=0 where var_name='冷测量' and line=@work_groups;
		end if;
-- 	elseif 	@flag=2 then
-- 		update gmms_preprocessing_glass set statu='匹配失败',flag=0 where id=1;

				
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for Identify_Mark_UpdateModel
-- ----------------------------
DROP PROCEDURE IF EXISTS `Identify_Mark_UpdateModel`;
delimiter ;;
CREATE PROCEDURE `Identify_Mark_UpdateModel`(in line int ,in model int,out backMsg varchar(255))
BEGIN
# 函数名：测量-打标-更改打标模式
# 设置某线正在生产的工程中的钢化小片不激光打标
# 逻辑：传入值：线号，模式，输出值：返回信息
# 1. 根据线号查询当前生产线正在生产的工程
# 2. 根据工程修改小片打标标识
DECLARE gch varchar(255);
select file_name into gch from gmms_raw_file where operator = line and state = '已领取' order by create_time Desc limit 1;
select gch;
if gch is null then 
set backMsg =CONCAT("当前 ",line, " 号生产线没有正在生产的工程");
end if;
#if @gongchenghao is not null then
if gch is not null then
	if model = 1 then
#set gch = @gongchenghao;
update gmms_galss_task set marking_flag = 15 where marking_flag in (11,12,13,14,17,18) and engineering_id = gch;
insert into gmms_identify_mark_update (gongchenghao,DBMark,GHMark,line) Values (gch,0,0,line);
set backMsg = CONCAT("已成功设置",line,"号产线正在生产的 ",gch," 工程中需要钢化的小片不自动打标！");
	end if;
	if model = 0 then
	insert into gmms_identify_mark_update (gongchenghao,DBMark,GHMark,line) Values (gch,1,0,line);
	set backMsg = "非钢化打标已关闭";
	end if;
	end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for Inclinedroller
-- ----------------------------
DROP PROCEDURE IF EXISTS `Inclinedroller`;
delimiter ;;
CREATE PROCEDURE `Inclinedroller`(in sendMessageHex varchar(255),in machineids BIGINT,out messagedate varchar(255))
BEGIN
	#斜滚轮存储过程
	#找到当前机器标志位
	#查询机器逻辑表中对应的逻辑进行对应的步骤
	#根据发送过来的玻璃编号查找数据进行修改状态
	DECLARE machinedisposeids int DEFAULT 0;
	DECLARE dates varchar(255);
	DECLARE typeid BIGINT;
	DECLARE taskplc varchar(255);
	DECLARE taskmes varchar(255);
	DECLARE idflag varchar(255);
	DECLARE oldmessagedates varchar(255) DEFAULT sendMessageHex;
	DECLARE messagedates varchar(255) DEFAULT '';
	set messagedate=messagedates;
	
	select machine_type_id,work_id into @mtid,@work_groups from gmms_machine where id=machineids;
	select file_name into @engineering_id from gmms_raw_file where operator=@work_groups and state='已领取';
	
	
	select mf.flag into @请求标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='请求字';
	
	select mf.flag into @发送标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='发送字';
	
	set idflag=cast(@请求标志位 as UNSIGNED INTEGER)+1;
	
	set taskplc = substring(sendMessageHex,(cast(@请求标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskmes = substring(sendMessageHex,(cast(@发送标志位 as UNSIGNED INTEGER)*4)+19,4);
	set typeid = conv(substring(sendMessageHex,(cast(idflag as UNSIGNED INTEGER)*4)+19,4),16,10);
	
  select taskplc;
  select taskmes;
	select speed1 into @speed1 from gmms_edg_speed where groups=@work_groups;

	
	#请求逻辑查询
	select count(*),md.id into @请求数量,@请求逻辑编号 from gmms_machine_dispose md join gmms_machine_type mt on md.machine_type_id=mt.id where md.machine_type_id=@mtid  and md.plc_font_type='请求字' and md.mes_font_type='发送字' and md.plc_font_value=taskplc and md.mes_font_value=taskmes;


		if @请求逻辑编号!=0 then
			set machinedisposeids=@请求逻辑编号;
		end if;
		
		#查询当前执行逻辑进行判断
		select logic,return_value into @ExecutionLogic,@returnvalue from gmms_machine_dispose where id=machinedisposeids;
		select @ExecutionLogic;
		if @ExecutionLogic='任务请求' then
		select typeid;
		if machineids=34 then
			 select count(*) into @count from gmms_galss_task where galss_id=typeid and work_procedure='理片' and work_state='已出库' and engineering_id=@engineering_id;
				select count(*) into @counts from gmms_galss_task where galss_id=typeid and work_procedure='理片' and work_state='直通' and engineering_id=@engineering_id;
				select speed1 into @speed1 from gmms_edg_speed where groups=@work_groups;
				select @count;
				if @count>0 then
					update gmms_galss_task set work_state='等待中',work_procedure='磨边' where galss_id=typeid and  work_procedure='理片' and work_state='已出库' and engineering_id=@engineering_id;
					call intTo2ByteHex10_16_4(@speed1,@speed1s);
					set dates=CONCAT(@returnvalue,@speed1s);
					call message(dates,'000a',@messagedate);
					set messagedate=@messagedate;
					insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,1,NOW(),'允许信号',oldmessagedates,messagedate);
				elseif @counts>0 then
					select max(out_rank) into @maxrank from gmms_galss_task where engineering_id=@engineering_id; 
					update gmms_galss_task set work_state='等待中',work_procedure='磨边',out_rank=@maxrank+1 
					where galss_id=typeid and  work_procedure='理片' and work_state='直通' and engineering_id=@engineering_id;
					call intTo2ByteHex10_16_4(@speed1,@speed1s);
					set dates=CONCAT(@returnvalue,@speed1s);
					call message(dates,'000a',@messagedate);
					set messagedate=@messagedate;
					insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,1,NOW(),'直通允许信号',oldmessagedates,messagedate);
				end if;
		elseif machineids=26 then
			select count(*) into @count from gmms_galss_task where galss_id=typeid and work_procedure='理片' and work_state='已出库' and engineering_id=@engineering_id;
				
				select speed1 into @speed1 from gmms_edg_speed where groups=@work_groups;
				select @count;

					update gmms_galss_task set work_state='等待中',work_procedure='磨边' where galss_id=typeid and  work_procedure='理片' and work_state='已出库' and engineering_id=@engineering_id;
					select count(*) into @count2 from gmms_galss_task where galss_id= typeid and work_procedure='磨边' and work_state='正在工作' and engineering_id=@engineering_id;
					select @count2;
					if @count2=1 then
						call intTo2ByteHex10_16_4(@speed1,@speed1s);
						set dates=CONCAT(@returnvalue,@speed1s);
						call message(dates,'000a',@messagedate);
						set messagedate=@messagedate;
					end if;
					

		else
			select count(*) into @count from gmms_galss_task where galss_id=typeid and work_procedure='理片' and work_state='已出库' and engineering_id=@engineering_id;
				
				select speed1 into @speed1 from gmms_edg_speed where groups=@work_groups;
				select @count;
				if @count>0 then
					update gmms_galss_task set work_state='等待中',work_procedure='磨边' where galss_id=typeid and  work_procedure='理片' and work_state='已出库' and engineering_id=@engineering_id;
					call intTo2ByteHex10_16_4(@speed1,@speed1s);
					set dates=CONCAT(@returnvalue,@speed1s);
					call message(dates,'000a',@messagedate);
					set messagedate=@messagedate;
				end if;
		end if;
				
				
 		elseif @ExecutionLogic='任务清空' then		
				call message(@returnvalue,'000a',@messagedate);
				set messagedate=@messagedate;
				insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,1,NOW(),'清空信号',oldmessagedates,messagedate);
		else 
				call intTo2ByteHex10_16_4(@speed1,@speed1s);
					set dates=@speed1s;
					#set dates=CONCAT('0000',@speed1s);
					call message(dates,'000b',@messagedate);
					set messagedate=@messagedate;
					#insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,1,NOW(),'发送速度信号',oldmessagedates,messagedate);
		end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for input
-- ----------------------------
DROP PROCEDURE IF EXISTS `input`;
delimiter ;;
CREATE PROCEDURE `input`(in machineids BIGINT,out 发送字 int,out 发送ID int,out 格子 int,out err char(255))
label:BEGIN
	#理片笼进片逻辑
	#先判断玻璃是否直通，然后找到空闲的架子把玻璃放入
	#超过1000的玻璃，一个架子只能放一块
	#找类型相同的玻璃并且还能再放一块的架子，如果没有则重新找一个架子放入
 
	#这是一个日志变量，用来跟踪运行情况	 
   DECLARE task_id int ;
	 call log_msg(@log_task,"[input]");	
	 SET 发送字=0;
	 select machine_type_id,work_id into @mtid,@work_groups from gmms_machine where id=machineids;	
	 select @work_groups;	
	 select file_name into @engineering_id from gmms_raw_file where operator=@work_groups and state='已领取';	
   select @engineering_id;
		-- 获取最后一次行为里片库的位置
   SET @lastpos=null;
	 SELECT `value` into @lastpos from gmms_var where varname='last_pos' and groups=@work_groups;
	 SELECT `value` into @lastpos2 from gmms_var where varname='last_out_glass_id' and groups=@work_groups;	
	 SELECT @lastpos2;	
	 -- 通过小片任务ID查询小片任务尺寸
	 SET @type_id=null;
	 select rank,type_id,galss_id,processcard_id,width,height,id into @rankid,@type_id,@galss_id,@processcardid,@width,@height,@z_gid from gmms_galss_task 
	 where work_procedure='理片' and work_state='等待中'  and engineering_id=@engineering_id order by rank limit 0,1;	
	 select @rankid;
 	 call log_key_value(@log_task,"玻璃ID",@z_gid);
 
   if @type_id is null  then
	 set 	发送字=-1;
	 SET err='玻璃ID不存在';
--  else
-- 	 	if @width<@height then
-- 	   set @width=@height;
--    end if;
	 
   end if;
 
--  这里判断是否发生队列错乱，错乱一定要报警，防止乱片
--  if 发送字=0 then
-- 			SET @id=null;
-- 			SELECT id ,processcard_id into @id,@processcardid FROM gmms_galss_task where work_procedure='理片' and work_state='等待中' order by rank ASC limit 1;
-- 			if @id<>请求ID or @id is null then
-- 				SET 发送字=-2;	
-- 				SET err='玻璃队列不匹配';
-- 			end if; 
--  end if;
	  --  判断是否因为和最后磨边玻璃同架同尺寸 ，给直通标记直通
	 SET @zhitong=0;		--  需要直通出库的标志
-- 		select count(*) into @counts from gmms_drop_frame where processcard_id=@processcardid and engineering_id=@engineering_id;
-- 		if @type_id=@lastpos2 and @counts>0 then
-- 			SET @zhitong=1;	
-- 		end if;
		
		-- 直通出库
   if ((@width>3500 or @height>=3500) or @zhitong=1) and 发送字=0 and false then 
		
			call log_key_value(@log_task,"直通模式",1);
		
      SET 发送字=3;
      SET 发送ID=@galss_id;
      SET 格子=1;
			UPDATE gmms_galss_task SET work_procedure='理片',work_state='出库中' where rank=@rankid and engineering_id=@engineering_id;
			UPDATE gmms_var set value=格子 where varname='last_pos' and groups=@work_groups;
		  update gmms_machine set remarks='3' where id=machineids;
   end if;
			
-- 	 if(@width<1000 and @height<1000) and 发送字=0 and @work_groups<>1 and flase   then 
-- 		  set @rk=0;
-- 			call log_key_value(@log_task,"入库模式","小片");
--       SELECT slicecage_rank into @rk from gmms_slicecage 
-- 			where  count=1 and flag=0 and type_id=@type_id  and processcard_id=@processcardid and gourp=@work_groups and state='使用中' and id<>1
--       order by id-@last_pos asc limit 1;
-- 			
-- 			call log_key_value(@log_task,"入库位置",@rk);	
--       
-- 			if @rk>0 then 
-- 					UPDATE gmms_galss_task set slicecage_rank=@rk,work_procedure='理片',work_state='进库中' WHERE rank=@rankid and engineering_id=@engineering_id;
-- 					UPDATE gmms_slicecage set state='进库中',count=2,type_id=@type_id,processcard_id=@processcardid where slicecage_rank=@rk and gourp=@work_groups;
-- 					select @type_id;
-- 					UPDATE gmms_var set value=@rk where varname='last_pos' and groups=@work_groups;
-- 					select @type_id;
-- 					SET 发送ID=@type_id;
-- 					SET 格子=@rk;
-- 					SET 发送字=1;		
-- 			end if;
-- 	 end if;
		
   
   
	 
	 
	 #if(@Width<1000 and @height<1000) and (@Width>10 or @height>10) and 发送字=0 then
		 
	 if (@Width<=10 and @height<=10) and 发送字=0 then 
			SET @rk=0;
			-- 这里查找一个可放两片的槽
      SELECT slicecage_rank into @rk from gmms_slicecage 
			where count=1 and type_id=@type_id  and processcard_id=@processcardid and gourp=@work_groups and state='使用中' and id<>1
      order by id-@last_pos asc limit 1;
			select @rk;
      if @rk>0 then
					UPDATE gmms_galss_task set slicecage_rank=@rk,work_procedure='理片',work_state='进库中' WHERE rank=@rankid and work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id;
					UPDATE gmms_slicecage set count=2 where slicecage_rank=@rk and gourp=@work_groups;
					UPDATE gmms_var set value=@rk where varname='last_pos' and groups=@work_groups;
          SET 发送字=1;
          SET 发送ID=@type_id;
          SET 格子=@rk;	
			end if;
			if @rk=0 then 
					SET @rk=0;
					SELECT slicecage_rank into @rk from gmms_slicecage where state="空闲" and count=0 and flag=0 and gourp=@work_groups and slicecage_rank<>1
          order by ABS(slicecage_rank-@last_pos)  asc limit 1;
				 	SELECT @rk;
					if @rk >0 then
							UPDATE gmms_galss_task set slicecage_rank=@rk,work_procedure='理片',work_state='进库中' WHERE rank=@rankid and work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id;
							UPDATE gmms_slicecage set state='进库中',count=1,type_id=@type_id,processcard_id=@processcardid where slicecage_rank=@rk and gourp=@work_groups;
							UPDATE gmms_var set value=@rk where varname='last_pos' and groups=@work_groups;     
							SET 发送字=1;
							SET 发送ID=@type_id;
							SET 格子=@rk;
					else 
							SET 发送字=-3;
							SET err='发生暴笼';
					end if;
			end if;
	end if;
	
	
	
	IF (@Width<2500 and @height<2500) and (@Width>10 or @height>10) and 发送字=0 then
			SET @rk=0;
 	 		call log_key_value(@log_task,"入库模式","大片");
			SELECT slicecage_rank into @rk from gmms_slicecage where state="空闲" and count=0 and flag=0 and gourp=@work_groups and slicecage_rank<>1
      order by ABS(slicecage_rank-@last_pos)  asc limit 1;
			call log_key_value(@log_task,"入库位置",@rk);
			if @rk >0 then
					select @type_id;
					UPDATE gmms_galss_task set slicecage_rank=@rk,work_procedure='理片',work_state='进库中' WHERE rank=@rankid and work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id;
					UPDATE gmms_slicecage set state='进库中',count=1,type_id=@type_id,processcard_id=@processcardid where slicecage_rank=@rk and gourp=@work_groups;
					select @type_id;
					UPDATE gmms_var set value=@rk where varname='last_pos' and groups=@work_groups;
					select @type_id;
					SET 发送ID=@type_id;
					SET 格子=@rk;
					SET 发送字=1;
			else 
					SET 发送字=-3;
					SET err='发生暴笼';
			end if;
   end if;
	
	
	
	
	IF 发送字=0 then 
			SET 发送字=-4;
			SET err='无任何相应';
    --  此处发送
  end if;
	if 格子 is null or 发送ID is null  then
			
			set  格子=0;
			set  发送ID=0;
	end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for insert_tempere_layout
-- ----------------------------
DROP PROCEDURE IF EXISTS `insert_tempere_layout`;
delimiter ;;
CREATE PROCEDURE `insert_tempere_layout`(_work_id int,_layout_number int,_layout_rate double,_glass_number int,_x int,_y int,_rotate int,pendulum int,_distance int,_row int,_col int,_glass_id long,_head_info text,out result int)
BEGIN
	#Routine body goes here...

set @width=0;
SELECT width,height,galss_id into @width,@height,@erp_glass_id from gmms_galss_task_2 where id=_glass_id;

if @width<>0 then 

INSERT INTO `gmms_tempere_layout`(`work_id`, `layout_number`, `insert_time`, `layout_rate`, `glass_number`, `x`, `y`, `width`, `height`, `rotate`, `row`, `column`, `glass_id`, `erp_glass_id`, `head_info`) VALUES (_work_id,_layout_number,now(),_layout_rate,_glass_number,_x,_y,@width,@height,_rotate,_row,_col,_glass_id,@erp_glass_id,_head_info);



UPDATE gmms_galss_task_2  set orders=_glass_number,grouporder=_row, furnace=_layout_number,angle=_rotate,distance=_distance,
pendulum_piece=pendulum,work_state="已优化" where id=_glass_id;
set result=1;
end if;

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for interrupttask
-- ----------------------------
DROP PROCEDURE IF EXISTS `interrupttask`;
delimiter ;;
CREATE PROCEDURE `interrupttask`(in rawfilename varchar(255),in line BIGINT)
BEGIN
	#Routine body goes here...

	select id into @raw_id from gmms_raw_file where file_name=rawfilename and operator=line;
	select count(*) into @count1 from gmms_pattern_task_interrupt where raw_id=@raw_id and groups=line;
	select count(*) into @count2 from gmms_galss_task_interrupt where engineering_id=rawfilename;
	if @count1>0 or @count2>0 then
		delete from gmms_pattern_task_interrupt where raw_id=@raw_id;
		delete from gmms_galss_task_interrupt where engineering_id=rawfilename;
	end if;
	insert into gmms_pattern_task_interrupt select * from gmms_pattern_task where raw_id=@raw_id and groups=line;
	insert into gmms_galss_task_interrupt select * from gmms_galss_task where engineering_id=rawfilename;
	
	

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for intoutfinish
-- ----------------------------
DROP PROCEDURE IF EXISTS `intoutfinish`;
delimiter ;;
CREATE PROCEDURE `intoutfinish`(in machineids BIGINT,in 确认字 int,out 完成字 int ,out err char(255))
BEGIN
	#理片笼完成存储过程
	#先判断为出库还是进库，分别进行对应的数据修改
	#出库则先判断是两片还是一片
	select machine_type_id,work_id into @mtid,@work_groups from gmms_machine where id=machineids;
	
	select file_name into @engineering_id from gmms_raw_file where operator=@work_groups and state='已领取';
		set 完成字=0;
	   SET @id=null;
		 SET @slicecage_rank=null;
		SELECT type_id,id,slicecage_rank,work_state into @type_id,@id ,@slicecage_rank,@work_state from   gmms_galss_task where work_procedure='理片'  and (work_state='进库中'
		 or work_state='出库中') and engineering_id=@engineering_id order by rank ASC
		 limit 1;
		 
		if @id is null then 
			 SET  完成字=-1;    # 一个异常的确认
			 SET  err='没有待确认的进片任务';
		end if; 
		
		
		--  判断确认ID 是否队列正确
	/*
		if @id<>确认ID and 完成字=0 then 
			SET  完成字=-1;
			SET  err='确认ID不匹配';
		end if;
		*/

	 -- SELECT @slicecage_rank;
		if 确认字=1 and 完成字=0 then
				if @work_state='出库中' then 
				--  获取队列编号 代码   
				select count(*) into @counts from gmms_galss_task where work_procedure='理片' and work_state='出库中' and engineering_id=@engineering_id;
					if @counts=1 then
						select max(out_rank) into @maxrank from gmms_galss_task where engineering_id=@engineering_id;
				   	UPDATE gmms_galss_task SET work_state='已出库',finsh_time=now(),slicecage_rank=null,out_rank=@maxrank+1,work_procedure_number=300  where slicecage_rank=@slicecage_rank and work_state='出库中' and engineering_id=@engineering_id;	
					else 
						select galss_id into @galss_id1 from gmms_galss_task where work_procedure='理片' and work_state='出库中' and engineering_id=@engineering_id order by rank limit 0,1;
						select galss_id into @galss_id2 from gmms_galss_task where work_procedure='理片' and work_state='出库中' and engineering_id=@engineering_id order by rank limit 1,1;
						select max(out_rank) into @maxrank from gmms_galss_task where engineering_id=@engineering_id;
				   	UPDATE gmms_galss_task SET work_state='已出库',finsh_time=now(),slicecage_rank=null,out_rank=@maxrank+1,work_procedure_number=300  where galss_id=@galss_id1 and work_state='出库中' and engineering_id=@engineering_id;
						UPDATE gmms_galss_task SET work_state='已出库',finsh_time=now(),slicecage_rank=null,out_rank=@maxrank+2,work_procedure_number=300  where galss_id=@galss_id2 and work_state='出库中' and engineering_id=@engineering_id;
					end if;	
				 else 
				     	UPDATE gmms_galss_task SET work_state='已进库' where slicecage_rank=@slicecage_rank and work_state='进库中' and engineering_id=@engineering_id;   
				end if;
				
				
				
				
				SET 完成字=1;
		end if;
		
		#破损
		if 确认字=2 and 完成字=0 then
			if @work_state='出库中' then 
				--  获取队列编号 代码   
				select count(*) into @counts from gmms_galss_task where work_procedure='理片' and work_state='出库中' and engineering_id=@engineering_id;
					if @counts=1 then
						select max(out_rank) into @maxrank from gmms_galss_task where engineering_id=@engineering_id;
				   	UPDATE gmms_galss_task SET work_state='已进库',finsh_time=now()where slicecage_rank=@slicecage_rank and work_state='出库中' and engineering_id=@engineering_id;	
					else 
						select galss_id into @galss_id1 from gmms_galss_task where work_procedure='理片' and work_state='出库中' and engineering_id=@engineering_id order by rank limit 0,1;
						select galss_id into @galss_id2 from gmms_galss_task where work_procedure='理片' and work_state='出库中' and engineering_id=@engineering_id order by rank limit 1,1;
						select max(out_rank) into @maxrank from gmms_galss_task where engineering_id=@engineering_id;
				   	UPDATE gmms_galss_task SET work_state='已出库',finsh_time=now() where galss_id=@galss_id1 and work_state='出库中' and engineering_id=@engineering_id;
						UPDATE gmms_galss_task SET work_state='已出库',finsh_time=now()  where galss_id=@galss_id2 and work_state='出库中' and engineering_id=@engineering_id;
					end if;	
				 else 
				     	UPDATE gmms_galss_task SET work_state='等待中',slicecage_rank=null where slicecage_rank=@slicecage_rank and work_state='进库中' and engineering_id=@engineering_id;   
				end if;
		  SET 完成字=1;
		end if;
-- 		if 确认字=2 and 完成字=0 then
-- 		 	UPDATE gmms_galss_task SET work_state='破损',broken_state=1,slicecage_rank=null  where type_id=@type_id and (work_state='进库中' or work_state='出库中') and engineering_id=@engineering_id;
-- 		  SET 完成字=1;
-- 		end if;
		
		select 完成字;
		if 完成字>0 and @slicecage_rank is not null then 
				SET @count=0;
				SELECT count(*) into @count from gmms_galss_task WHERE  slicecage_rank=@slicecage_rank and engineering_id=@engineering_id;
				if @count>0 then 
					UPDATE gmms_slicecage SET state='使用中'  ,count=@count  where slicecage_rank=@slicecage_rank and gourp=@work_groups; 
				else
				  UPDATE gmms_slicecage SET state='空闲'  ,count=0,type_id=null,processcard_id=null where slicecage_rank=@slicecage_rank and gourp=@work_groups; 
				end if;
		end if;
		
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for intTo2ByteHex10_16_2
-- ----------------------------
DROP PROCEDURE IF EXISTS `intTo2ByteHex10_16_2`;
delimiter ;;
CREATE PROCEDURE `intTo2ByteHex10_16_2`(in number int,out date VARCHAR(255))
BEGIN
	#10转2位16进制存储过程
	DECLARE numbers VARCHAR(255);
	DECLARE number4 VARCHAR(255);
	
	
	set numbers=conv(number,10,16);
	if char_length(numbers)='1' then
		set number4=CONCAT('0',numbers);
	else
		set number4=numbers;
	end if;
	set date=number4;
	
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for intTo2ByteHex10_16_4
-- ----------------------------
DROP PROCEDURE IF EXISTS `intTo2ByteHex10_16_4`;
delimiter ;;
CREATE PROCEDURE `intTo2ByteHex10_16_4`(in number int,out date VARCHAR(255))
BEGIN
	#10转4位16进制存储过程
	DECLARE numbers VARCHAR(255);
	DECLARE number4 VARCHAR(255);
	
	
	set numbers=conv(number,10,16);
	if char_length(numbers)='1' then
		set number4=CONCAT('000',numbers);
	elseif char_length(numbers)='2' then
		set number4=CONCAT('00',numbers);
	elseif char_length(numbers)='3' then
		set number4=CONCAT('0',numbers);
	elseif char_length(numbers)='4' then
		set number4=numbers;
	end if;
	set date=number4;
	
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for intTo2ByteHex2_16
-- ----------------------------
DROP PROCEDURE IF EXISTS `intTo2ByteHex2_16`;
delimiter ;;
CREATE PROCEDURE `intTo2ByteHex2_16`(in number VARCHAR(255),out date VARCHAR(255))
BEGIN
	#2转16进制存储过程
	DECLARE numbers VARCHAR(255);
	DECLARE number4 VARCHAR(255);
	
	
	set numbers=conv(number,2,16);
	if char_length(numbers)='1' then
		set number4=CONCAT('0',numbers);
	else
		set number4=numbers;
	end if;
	set date=number4;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for liuchengka_change
-- ----------------------------
DROP PROCEDURE IF EXISTS `liuchengka_change`;
delimiter ;;
CREATE PROCEDURE `liuchengka_change`(oldwk varchar(50),oldst varchar(50),newwk varchar(50),
newst varchar(50), stops int,line int, segment int)
BEGIN



	if segment=1 then 
			if oldwk<>newwk then 
					 if oldwk is not null then 
						call	liuchengka_change_inner(oldwk,"切磨线","下架");
					 end if;
					 if newwk is not null then
						call   liuchengka_change_inner(newwk,"切磨线","上架");
					 end if;
				end if;
	end if;
	if segment=2 then 
			if oldwk<>newwk then 
					 if oldwk is not null then 
						call	liuchengka_change_inner(oldwk,"钢化前","下架");
					 end if;
					 if newwk is not null then
						call   liuchengka_change_inner(newwk,"钢化前","上架");
					 end if;
				end if;
	end if;
	
		if segment=3 then 
			if oldwk<>newwk then 
					 if oldwk is not null then 
						call	liuchengka_change_inner(oldwk,"钢化后","下架");
					 end if;
					 if newwk is not null then
						call   liuchengka_change_inner(newwk,"钢化后","上架");
					 end if;
				end if;
	end if;
	

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for liuchengka_change_inner
-- ----------------------------
DROP PROCEDURE IF EXISTS `liuchengka_change_inner`;
delimiter ;;
CREATE PROCEDURE `liuchengka_change_inner`(in _liuchengka varchar(50),in _current_work varchar(50),in _status varchar(50))
BEGIN
	#Routine body goes here...
	# 先检查参数是否是有效参数...
	
	#	先检查是否可以修改...   
	#UPDATE gmms_liuchengka set current_work_position=_current_work,current_status=_status,last_changed_time=now() where current_frame=_liuchengka;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for liuchengka_change_qiemo
-- ----------------------------
DROP PROCEDURE IF EXISTS `liuchengka_change_qiemo`;
delimiter ;;
CREATE PROCEDURE `liuchengka_change_qiemo`(oldwk varchar(50),oldst varchar(50),newwk varchar(50),
newst varchar(50), line int, segment int)
BEGIN

	set @seg=null;
	
	if segment=1 then 
			if oldwk<>newwk then 
					 if oldwk is not null then 
						call	liuchengka_change_inner(oldwk,"切割线","已完成");
					 end if;
					 if newwk is not null then
						call   liuchengka_change_inner(newwk,"切割线","已开始");
					 end if;
				end if;
	end if;
	if segment=2 then 
			if oldwk<>newwk then 
					 if newwk is not null then 
						call	liuchengka_change_inner(newwk,"切割线","已开始");
					 end if;
				end if;
	end if;
	if segment=3 then 
		if oldwk<>newwk then 
					 if oldwk is not null then 
						call	liuchengka_change_inner(oldwk,"切割线","已完成");
					 end if;
				end if;
	end if;
	

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for liuchengka_create
-- ----------------------------
DROP PROCEDURE IF EXISTS `liuchengka_create`;
delimiter ;;
CREATE PROCEDURE `liuchengka_create`(in gongcheng varchar(50))
BEGIN
	#Routine body goes here...
		set @count=0;
		select count(*) into @count from gmms_liuchengka where gongchenghao=gongcheng;
		if @count=0 then
			set @gc=gongcheng;
			set @tm=now();
			insert into gmms_liuchengka 
			(

liuchengka
,gongchenghao
,parent_liuchengka
,is_single
,child_count
,current_status
,current_work_position
,current_frame
,last_changed_time
,piece_count

			)
				select processcard_id,@gc,null,1,1,'已完成','创建',processcard_id,@tm,count(id)  from gmms_galss_task_buffer where engineering_id=gongcheng group by processcard_id;
		end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for liuchengka_create_v1
-- ----------------------------
DROP PROCEDURE IF EXISTS `liuchengka_create_v1`;
delimiter ;;
CREATE PROCEDURE `liuchengka_create_v1`(gongcheng varchar(50))
BEGIN
	#Routine body goes here...
	DECLARE done int default 0;
	DECLARE card varchar(50);

   DECLARE cur CURSOR FOR
	 		select processcard_id from  gmms_galss_task_buffer where engineering_id=gongcheng group by processcard_id;
	DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;

	OPEN cur;
	while done=0 do
		FETCH cur into card;
		CALL liuchengka_load_rel(card);
	end while;

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for liuchengka_load_rel
-- ----------------------------
DROP PROCEDURE IF EXISTS `liuchengka_load_rel`;
delimiter ;;
CREATE PROCEDURE `liuchengka_load_rel`(ka varchar(50))
BEGIN
	#Routine body goes here...
DECLARE id  int DEFAULT null;
		DECLARE i  int DEFAULT  0;
		DECLARE hao VARCHAR(50) DEFAULT null;
		DECLARE gy  VARCHAR(50) DEFAULT null;
		DECLARE ct int DEFAULT 0;
		DECLARE ys  varchar(50) DEFAULT null;
		DECLARE tk   double DEFAULT null;
	
		DECLARE tmpc int;
		DECLARE has_j int;
		DECLARE has_k int;
		set has_j=false;
		set has_k=false;
		
		#这里把ka 的后缀去掉，变成流程卡,并查询是否已经导入过了
		set tmpc=instr(ka,'/');
		
		if tmpc>0 then 
			set ka=SUBSTR(ka from 1 for tmpc-1);
		end if;
		
			set tmpc=0;
			select count(*) into tmpc from gmms_liuchengka where main_liuchengka=ka;
	
		
		
		if tmpc=0 then
			select count(a.ceng) into tmpc  from 
			(SELECT ceng  FROM gmms_liuchengka_buffer where liuchengkahao=ka group by ceng) a;
			
		   
			
			if tmpc>0 then 
					set i=0;
					while i<tmpc do
			       set i=i+1;
							
							SELECT gongyiliucheng,liuchengkahaoceng,shuliang,moxi,houdu into 
							 gy,hao,ct,ys,tk
							 from gmms_liuchengka_buffer where liuchengkahao=ka and ceng=i limit 1;
	
					INSERT INTO `gmms`.`gmms_liuchengka`(`liuchengka`, `main_liuchengka`,`parent_liuchengka`,`is_single`,`child_count`,`current_status`, `current_work_position`, `current_frame`, `last_changed_time`, `piece_count`,
					gy_gang,gy_jiajiao,gy_zhongkong
					) VALUES
					 (concat(ka,'/',i),ka, null,b'1', 1, '已完成', '创建',concat(ka,'/',i),null, ct,
						case when INSTR(gy,"钢化")>0 then b'1' else b'0' end,
						case when INSTR(gy,"夹层")>0 then b'1' else b'0' end,
						case when INSTR(gy,"中空")>0 then b'1' else b'0' end
					 );
					 if has_j=0 and INSTR(gy,"夹层")>0 then 
					   set has_j=1;				 
					 end if;
					 IF has_k=0 and INSTR(gy,"中空")>0 then 
					 
					   set has_k=1;	
					 end if;
					end while;					
		
	
		if tmpc>1 then 
				
				
				
				
			if has_j=1 then 
				set i=1;
				
				
				
				
				INSERT INTO `gmms`.`gmms_liuchengka`(`liuchengka`, `main_liuchengka`,`parent_liuchengka`,`is_single`,`child_count`,`current_status`, `current_work_position`, `current_frame`, `last_changed_time`, `piece_count`,
					gy_gang,gy_jiajiao,gy_zhongkong,gongyi
					) VALUES
					 (concat('J',ka),ka,null, b'0', 1, '已完成', '创建',null,null, ct,
						0,
						0,
						has_k,
						"夹层"
					 );
				update `gmms`.`gmms_liuchengka` set parent_liuchengka=concat('J',ka) where main_liuchengka=ka and gy_jiajiao=1;
				
				select count(*) into @z_ct from gmms_liuchengka where parent_liuchengka=	concat('J',ka);
				update gmms_liuchengka set child_count=@z_ct where liuchengka=	concat('J',ka);
						
			end if;
			
			if has_k=1 then
			   
									INSERT INTO `gmms`.`gmms_liuchengka`(`liuchengka`, `main_liuchengka`,`parent_liuchengka`,`is_single`,`child_count`,`current_status`, `current_work_position`, `current_frame`, `last_changed_time`, `piece_count`,
					gy_gang,gy_jiajiao,gy_zhongkong,gongyi
					) VALUES
					 (concat('K',ka),ka,null, b'0', 1, '已完成', '创建',null,null, ct,
						0,
						0,
						0,
						'中空'
					 );
				
					 update `gmms`.`gmms_liuchengka` set parent_liuchengka=concat('K',ka) where main_liuchengka=ka and parent_liuchengka is null and liuchengka<> concat('K',ka); 	
					select count(*) into @z_ct from gmms_liuchengka where parent_liuchengka=	concat('K',ka);
				update gmms_liuchengka set child_count=@z_ct where liuchengka=	concat('K',ka);
		
			end if;
			
			
			
		end if;		
		
		end if;
	end if;
	
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for LoadFinsh
-- ----------------------------
DROP PROCEDURE IF EXISTS `LoadFinsh`;
delimiter ;;
CREATE PROCEDURE `LoadFinsh`(in machineids BIGINT,in machinedisposeids BIGINT,in flag int,out messagedate varchar(255))
BEGIN
	#上片完成存储过程
	#循环上片位找到有架子正在工作的任务进行修改，flag=3为破损，flag=1为完成
	#如果为最后一个任务则自动生成退库任务
	DECLARE s int DEFAULT 0;
	DECLARE dates varchar(255);
		DECLARE width double;
		DECLARE height double;
		DECLARE thickness double;
		DECLARE leftpieces int;
		DECLARE messagedates varchar(255) DEFAULT '';
	set messagedate=messagedates;
		
		 select count(*),r.groups into @料架表数量,@groups from gmms_machine m join gmms_load_rack r on m.id=r.machine_id join gmms_shelf_task st on st.id=r.shelf_task_id where machine_id=machineids;
		 select id,color,raw_id into @id,@color,@raw_id from gmms_pattern_task where work_state='正在工作' and groups=@groups limit 1;
		 #修改此句   修改班組
		 select banzu into @dengluname from gmms_user_current where gongyi='磨边' and chanxian=@groups limit 0,1;
		 #select `name` into @dengluname from gmms_user_details where groups=@groups;
		 
		 select file_name into @file_name from gmms_raw_file where id=@raw_id;
		 
		 
		 select return_value into @returnvalue from gmms_machine_dispose where id=machinedisposeids;
		select @料架表数量;
			#循环上片位
				select r.id,r.falg_shelf,r.number,r.shelf_task_id,r.state,st.groups,st.file_id,st.shelf_rack_id,r.id into @料架序号,@料架标志位,@料架编号,@任务编号,@料架状态,@groups,@file_id,@shelf_rack_id,@load_rack_id from gmms_machine m join gmms_load_rack r on m.id=r.machine_id join gmms_shelf_task st on st.id=r.shelf_task_id  where machine_id=machineids limit 1;
				

				
				select rp.id,rp.left_pieces,rp.raw_width,rp.raw_height,rp.raw_thickness,rp.color,rp.production_time,rp.batch_id,rp.create_time into @包号编号,@剩余数量,@raw_width,@raw_height,@raw_thickness,@color,@production_time,@batch_id,@create_time from gmms_load_rack r join gmms_shelf_task st on st.id=r.shelf_task_id join gmms_shelf_rack sr on sr.id=st.shelf_rack_id join gmms_raw_package rp on rp.id=sr.raw_package_id1 where machine_id=machineids  and r.id=@料架序号 limit 1;
				
				select @剩余数量;
				select @料架标志位;
				select @料架状态;
				#判断上片位是否有架子
				if @料架标志位="1" then
				
				#判断上片位是否正在工作
					if @料架状态="正在工作" then

							
							#破损
							if flag=3 then
								update gmms_pattern_task set work_state='破损',finsh_time=NOW() where work_state='正在工作' and groups=@groups limit 1;
								set leftpieces=@剩余数量-1;
								update gmms_raw_package set left_pieces=leftpieces where id=@包号编号;
							
							#完成
							elseif flag=1 then
							select @任务编号;
							set @zz_gid=null;
							select id into @zz_gid from gmms_pattern_task where work_state='正在工作' and groups=@groups limit 1;
							if @zz_gid is not null then
							#添加余料
							 call yuliao_create_task(@zz_gid,@zz_tmp1,@zz_tmp2);  
								
							end if;
							
							
								update gmms_pattern_task set work_state='已完成',finsh_time=NOW() where work_state='正在工作' and groups=@groups limit 1;
								set leftpieces=@剩余数量-1;
								update gmms_raw_package set left_pieces=leftpieces where id=@包号编号;
								insert into gmms_cutmanage (width,height,thickness,color,pieces,groups,statrtime,engineering_id,production_time,batch_id,warehousing_time,dengluname) values(@raw_width,@raw_height,@raw_thickness,@color,1,@groups,now(),@file_name,@production_time,@batch_id,@create_time,@dengluname);
								update gmms_galss_task set chamfer_size=@batch_id where pattern_id=@id;
								
							#未完成	
							elseif flag=4 then
							
							
							
								update gmms_pattern_task set work_state='等待中',finsh_time=NOW() where work_state='正在工作' and groups=@groups limit 1;
							end if;
-- 							if @color='白玻' or @color='超白' then
-- 								if leftpieces=0 then						
-- 								select max(task_id) into @maxtask_id from gmms_shelf_task;
-- 								update gmms_machine set work_state='暂停' where id=machineids;
-- 								select id into @id from gmms_load_rack where shelf_task_id=@shelf_rack_id;
--  								insert into gmms_shelf_task set task_type='从上片位到仓位',task_state='等待中',groups=@groups,shelf_rack_id=@shelf_rack_id,start_time=Now(),task_id=(@maxtask_id+1);
-- 								end if;
-- 							else
-- 								if leftpieces=1 then						
-- 								select max(task_id) into @maxtask_id from gmms_shelf_task;
-- 								update gmms_machine set work_state='暂停' where id=machineids;
-- 								select id into @id from gmms_load_rack where shelf_task_id=@shelf_rack_id;
--  								insert into gmms_shelf_task set task_type='从上片位到仓位',task_state='等待中',groups=@groups,shelf_rack_id=@shelf_rack_id,start_time=Now(),task_id=(@maxtask_id+1);
-- 								end if;
-- 							end if;
							
							
							select count(*) into @finshnum from gmms_pattern_task where groups=@groups and work_state='已完成';
							select count(*) into @posunnum from gmms_pattern_task where groups=@groups and work_state='破损';
							select @returnvalue;
							call message(@returnvalue,'0014',@date);
							set messagedate=@date;
					end if;
				end if;

			

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for Loadpullshelf
-- ----------------------------
DROP PROCEDURE IF EXISTS `Loadpullshelf`;
delimiter ;;
CREATE PROCEDURE `Loadpullshelf`(in machineids BIGINT)
BEGIN
	#Routine body goes here...
	
		select groups into @groups from gmms_load_rack r where machine_id=machineids GROUP BY groups;
		
		select r.falg_shelf,r.number,r.shelf_task_id into @料架标志one,@料架编号one,@任务编号one from gmms_load_rack r where machine_id=machineids limit 0,1;
		
		
		select r.falg_shelf,r.number,r.shelf_task_id into @料架标志two,@料架编号two,@任务编号two from gmms_load_rack r where machine_id=machineids limit 1,1;
		
		select @料架标志one;
		select @料架标志two;
		select count(*) into @counts from gmms_pattern_task where work_state='等待中' and groups=@groups;
		if @counts >0 then
			if @料架标志one=0 then
				if @料架标志two=0 then
					select raw_width,raw_height,raw_thickness,color into @raw_width,@raw_height,@raw_thickness,@color from gmms_pattern_task where groups=@groups and work_state='等待中' group by raw_width,raw_height,raw_thickness,color order by rank limit 0,1;
								
				else
					select rp.left_pieces,rp.raw_width,rp.raw_height,rp.raw_thickness,rp.color,sr.id,sr.color_falg into @剩余片数,@宽度,@高度,@厚度,@颜色,@shelf_rack_id,@膜系标志 from gmms_load_rack r join gmms_shelf_task s on r.shelf_task_id=s.id join gmms_shelf_rack sr on s.shelf_rack_id=sr.id join gmms_raw_package rp on sr.raw_package_id1=rp.id where r.number=@料架编号two;
					select raw_width,raw_height,raw_thickness,color into @raw_width,@raw_height,@raw_thickness,@color from gmms_pattern_task where groups=@groups and work_state='等待中' and (raw_width!=@宽度 or raw_height!=@高度 or raw_thickness!=@厚度) group by raw_width,raw_height,raw_thickness order by rank limit 0,1;
				end if;
				
				select count(*) into @counts2 from gmms_shelf_task where task_type='从仓位到上片位' and load_rack=@料架编号one and groups=@groups  and (task_state='等待中' or task_state='正在工作');
				select @counts2;
				if @counts2=0 then
						select sr.number into @number from gmms_shelf_rack sr join gmms_raw_package rp on sr.raw_package_id1=rp.id where rp.raw_width=@raw_width and rp.raw_height=@raw_height and rp.raw_thickness=@raw_thickness and sr.state='使用中' and sr.falg='启用' and rp.left_pieces>0 and rp.color=@color order by rp.left_pieces limit 1;
						
						if @number is not null and @料架编号one is not null then
						
							select max(task_id) into @maxtask_id from gmms_shelf_task;
							insert into gmms_shelf_task set task_type='从仓位到上片位',task_state='等待中',load_rack=@料架编号one,groups=@groups,shelf_rack_id=@number,start_time=Now(),task_id=(@maxtask_id+1);
							update gmms_shelf_rack set state='正在使用' where number=@number;
						end if;
				end if;
				
			
			elseif @料架标志two=0 then
			
				if @料架标志one=0 then
					select raw_width,raw_height,raw_thickness,color into @raw_width,@raw_height,@raw_thickness,@color from gmms_pattern_task where groups=@groups and work_state='等待中' group by raw_width,raw_height,raw_thickness,color order by rank limit 0,1;
								
				else
					select rp.left_pieces,rp.raw_width,rp.raw_height,rp.raw_thickness,rp.color,sr.id,sr.color_falg into @剩余片数,@宽度,@高度,@厚度,@颜色,@shelf_rack_id,@膜系标志 from gmms_load_rack r join gmms_shelf_task s on r.shelf_task_id=s.id join gmms_shelf_rack sr on s.shelf_rack_id=sr.id join gmms_raw_package rp on sr.raw_package_id1=rp.id where r.number=@料架编号one;
				
					select raw_width,raw_height,raw_thickness,color into @raw_width,@raw_height,@raw_thickness,@color from gmms_pattern_task where groups=@groups and work_state='等待中' and (raw_width!=@宽度 or raw_height!=@高度 or raw_thickness!=@厚度) group by raw_width,raw_height,raw_thickness order by rank limit 0,1;
				end if;
				
				select count(*) into @counts2 from gmms_shelf_task where task_type='从仓位到上片位' and load_rack=@料架编号two and groups=@groups  and (task_state='等待中' or task_state='正在工作');
				select @counts2;
				if @counts2=0 then
						select sr.number into @number from gmms_shelf_rack sr join gmms_raw_package rp on sr.raw_package_id1=rp.id where rp.raw_width=@raw_width and rp.raw_height=@raw_height and rp.raw_thickness=@raw_thickness and sr.state='使用中' and sr.falg='启用' and rp.left_pieces>0 and rp.color=@color order by rp.left_pieces limit 1;
						
						if @number is not null and @料架编号two is not null then
						
							select max(task_id) into @maxtask_id from gmms_shelf_task;
							insert into gmms_shelf_task set task_type='从仓位到上片位',task_state='等待中',load_rack=@料架编号two,groups=@groups,shelf_rack_id=@number,start_time=Now(),task_id=(@maxtask_id+1);
							update gmms_shelf_rack set state='正在使用' where number=@number;
						end if;
				end if;
			
			end if;
		end if;

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for Loads
-- ----------------------------
DROP PROCEDURE IF EXISTS `Loads`;
delimiter ;;
CREATE PROCEDURE `Loads`(in sendMessageHex varchar(255),in machineids BIGINT,out messagedate varchar(255))
BEGIN
	#上片存储过程
	#找到当前机器标志位
	#查询机器逻辑表中对应的逻辑进行对应的步骤
	DECLARE machinedisposeids int DEFAULT 0;
	DECLARE dates varchar(255);
	DECLARE findplc varchar(255);
	DECLARE findmes varchar(255);
	DECLARE taskplc varchar(255);
	DECLARE taskmes varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	set messagedate=messagedates;
	
	select machine_type_id into @mtid from gmms_machine where id=machineids;
	
	
	
	select mf.flag into @完成标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='完成字';
	
	select mf.flag into @请求标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='请求字';
	
	select mf.flag into @发送标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='发送字';
	
	select mf.flag into @完成确认标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='完成确认字';
	
	set findplc = substring(sendMessageHex,(cast(@完成标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskplc = substring(sendMessageHex,(cast(@请求标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskmes = substring(sendMessageHex,(cast(@发送标志位 as UNSIGNED INTEGER)*4)+19,4);
	set findmes = substring(sendMessageHex,(cast(@完成确认标志位 as UNSIGNED INTEGER)*4)+19,4);
 	select taskplc;
 	select taskmes;
 	select findplc;
	select findmes;
	
	#完成逻辑查询
	select count(*),md.id into @完成数量,@完成逻辑编号 from gmms_machine_dispose md join gmms_machine_type mt on md.machine_type_id=mt.id where md.machine_type_id=@mtid  and md.plc_font_type='完成字' and md.mes_font_type='完成确认字' and md.plc_font_value=findplc and md.mes_font_value=findmes;
	
	#请求逻辑查询
	select count(*),md.id into @请求数量,@请求逻辑编号 from gmms_machine_dispose md join gmms_machine_type mt on md.machine_type_id=mt.id where md.machine_type_id=@mtid  and md.plc_font_type='请求字' and md.mes_font_type='发送字' and md.plc_font_value=taskplc and md.mes_font_value=taskmes;
	

	#判断逻辑是否存在
	if @完成数量=1 or @请求数量=1 then
	
		#判断完成逻辑是否为空
		if @完成逻辑编号!=0 then
			set machinedisposeids=@完成逻辑编号;
			
		#判断请求逻辑是否为空
		elseif @请求逻辑编号!=0 then
			set machinedisposeids=@请求逻辑编号;
			
		end if;
		select machinedisposeids;
		#查询当前执行逻辑进行判断
		select logic,return_value into @ExecutionLogic,@returnvalue from gmms_machine_dispose where id=machinedisposeids;
		select @ExecutionLogic;
		if @ExecutionLogic='任务完成' then
				call LoadFinsh(machineids,machinedisposeids,1,@messagedate);
				set messagedate=@messagedate;
		elseif @ExecutionLogic='任务未完成' then
				call LoadFinsh(machineids,machinedisposeids,4,@messagedate);
				set messagedate=@messagedate;
 		elseif @ExecutionLogic='完成破损' then
				call LoadFinsh(machineids,machinedisposeids,3,@messagedate);
				set messagedate=@messagedate;
 		elseif @ExecutionLogic='完成清空' then
-- 				select count(*) into @counts from gmms_load_rack where machine_id=machineids and state='正在工作';
-- 				if @counts!=0 then
-- 					update gmms_load_rack set state='等待中' where machine_id=machineids and state='正在工作';
-- 				end if;
				call message(@returnvalue,'0014',@date);
				set messagedate=@date;
		elseif @ExecutionLogic='任务请求' then
				call LoadSend(machineids,@returnvalue,@messagedate);
				set messagedate=@messagedate;
 		elseif @ExecutionLogic='任务清空' then
				set dates=CONCAT(@returnvalue,"0000000000000000");
				call message(dates,'000a',@date);
				set messagedate=@date;
		end if;
	end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for LoadSend
-- ----------------------------
DROP PROCEDURE IF EXISTS `LoadSend`;
delimiter ;;
CREATE PROCEDURE `LoadSend`(in machineids BIGINT,in returnvalue varchar(255),out messagedate varchar(255))
BEGIN
	#上片请求存储过程
	#循环找到工位上面有架子并且剩余数量大于零并且没有完成的上片任务进行修改
		DECLARE s int DEFAULT 0;
		DECLARE dates varchar(255);
		DECLARE width int;
		DECLARE heigth int;
		DECLARE pgknum int;
		DECLARE locknum int;
		DECLARE messagedates varchar(255) DEFAULT '';
	  set messagedate=messagedates;
		
		select count(*) into @数量 from gmms_load_rack r where machine_id=machineids;
		select groups into @groups from gmms_load_rack r where machine_id=machineids GROUP BY groups;
		
		select @数量;
		
		select id,raw_width,raw_height,raw_thickness,color into @pattern_id,@raw_width,@raw_height,@raw_thickness,@color from gmms_pattern_task where work_state='等待中'    and groups=@groups order by rank limit 1;
		select count(*) into @countss from gmms_pattern_task where work_state='等待中' and groups=@groups;
		select count(*) into @counts2 from gmms_pattern_task where work_state='正在工作' and groups=@groups;
		select @pattern_id;
		select r.falg_shelf,r.number,r.shelf_task_id into @料架标志one,@料架编号one,@任务编号one from gmms_load_rack r where machine_id=machineids limit 0,1;
		
		select @料架标志one;
		select @料架编号one;
		select @任务编号one;
		
		select r.falg_shelf,r.number,r.shelf_task_id into @料架标志two,@料架编号two,@任务编号two from gmms_load_rack r where machine_id=machineids limit 1,1;
		
		select @料架标志two;
		
		if @countss>0 and @counts2=0 then
		
			if @料架标志one="1" then
			
				select rp.left_pieces,rp.raw_width,rp.raw_height,rp.raw_thickness,rp.color,sr.id,sr.color_falg into @剩余片数,@宽度,@高度,@厚度,@颜色,@shelf_rack_id,@膜系标志 from gmms_load_rack r join gmms_shelf_task s on r.shelf_task_id=s.id join gmms_shelf_rack sr on s.shelf_rack_id=sr.id join gmms_raw_package rp on sr.raw_package_id1=rp.id where r.number=@料架编号one;
				
				#@color='白玻' or @color='超白' or @color='C' or @膜系标志=1
				if 1=1 then
					if @raw_width=@宽度 and @raw_height=@高度 and @raw_thickness=@厚度 and @color=@颜色 and @剩余片数>0 then
					
						update gmms_machine a set work_state="正在工作" where id=machineids;
						update gmms_load_rack set state="正在工作" where machine_id=machineids and number=@料架编号one;
						update gmms_pattern_task set work_state="正在工作" where  work_state="等待中" and groups=@groups limit 1  ;
						set width=cast(@宽度 as UNSIGNED INTEGER);
						set heigth=cast(@高度 as UNSIGNED INTEGER);
	-- 					set pgknum=cast(@完成任务数量 as UNSIGNED INTEGER);
	-- 					set locknum=cast(@等待中任务数量 as UNSIGNED INTEGER);
						call intTo2ByteHex10_16_4(width,@width);
						call intTo2ByteHex10_16_4(heigth,@heigth);
	-- 					call intTo2ByteHex10_16_4(pgknum,@pgknum);
	-- 					call intTo2ByteHex10_16_4(locknum,@locknum);
						set dates=CONCAT(returnvalue,"00000002",@width,@heigth);
						call message(dates,'000a',@date);
						set messagedate=@date;
					else
						if @料架编号one is not null and @groups is not null and @shelf_rack_id is not null then
							select count(*) into @counts from gmms_shelf_task where task_type='从上片位到仓位' and load_rack=@料架编号one and groups=@groups and shelf_rack_id=@shelf_rack_id and (task_state='等待中' or task_state='正在工作');
							if @counts=0 then
							
								select max(task_id) into @maxtask_id from gmms_shelf_task;
								insert into gmms_shelf_task set task_type='从上片位到仓位',task_state='等待中',load_rack=@料架编号one,groups=@groups,shelf_rack_id=@shelf_rack_id,start_time=Now(),task_id=(@maxtask_id+1);	

							end if;
						end if;
									
					end if;
				else
					if @raw_width=@宽度 and @raw_height=@高度 and @raw_thickness=@厚度 and @color=@颜色 and @剩余片数>1 then
					
					
						update gmms_machine a set work_state="正在工作" where id=machineids;
						update gmms_load_rack set state="正在工作" where machine_id=machineids and number=@料架编号one;
						update gmms_pattern_task set work_state="正在工作" where  work_state="等待中" and groups=@groups limit 1;
						set width=cast(@宽度 as UNSIGNED INTEGER);
						set heigth=cast(@高度 as UNSIGNED INTEGER);
	-- 					set pgknum=cast(@完成任务数量 as UNSIGNED INTEGER);
	-- 					set locknum=cast(@等待中任务数量 as UNSIGNED INTEGER);
						call intTo2ByteHex10_16_4(width,@width);
						call intTo2ByteHex10_16_4(heigth,@heigth);
	-- 					call intTo2ByteHex10_16_4(pgknum,@pgknum);
	-- 					call intTo2ByteHex10_16_4(locknum,@locknum);
						set dates=CONCAT(returnvalue,"00000002",@width,@heigth);
						call message(dates,'000a',@date);
						set messagedate=@date;
					else
						if @料架编号one is not null and @groups is not null and @shelf_rack_id is not null then
							select count(*) into @counts from gmms_shelf_task where task_type='从上片位到仓位' and load_rack=@料架编号one and groups=@groups and shelf_rack_id=@shelf_rack_id and (task_state='等待中' or task_state='正在工作');
							if @counts=0 then
							
								select max(task_id) into @maxtask_id from gmms_shelf_task;
								insert into gmms_shelf_task set task_type='从上片位到仓位',task_state='等待中',load_rack=@料架编号one,groups=@groups,shelf_rack_id=@shelf_rack_id,start_time=Now(),task_id=(@maxtask_id+1);	

							end if;
						end if;
									
					end if;
				end if;
			
			elseif @料架标志two="1" then
			
				select rp.left_pieces,rp.raw_width,rp.raw_height,rp.raw_thickness,rp.color into @剩余片数,@宽度,@高度,@厚度,@颜色 from gmms_load_rack r join gmms_shelf_task s on r.shelf_task_id=s.id join gmms_shelf_rack sr on s.shelf_rack_id=sr.id join gmms_raw_package rp on sr.raw_package_id1=rp.id where r.number=@料架编号two;
				
				#@color='白玻' or @color='超白' or @color='C' or @膜系标志=1
				if 1=1 then
				select @raw_width,@宽度,@raw_height,@高度,@raw_thickness,@厚度,@color,@颜色,@剩余片数;
					if @raw_width=@宽度 and @raw_height=@高度 and @raw_thickness=@厚度 and @color=@颜色 and @剩余片数>0 then
						update gmms_machine a set work_state="正在工作" where id=machineids;
						update gmms_load_rack set state="正在工作" where machine_id=machineids and number=@料架编号two;
						update gmms_pattern_task set work_state="正在工作" where work_state="等待中" and groups=@groups limit 1;
						set width=cast(@宽度 as UNSIGNED INTEGER);
						set heigth=cast(@高度 as UNSIGNED INTEGER);
	-- 					set pgknum=cast(@完成任务数量 as UNSIGNED INTEGER);
	-- 					set locknum=cast(@等待中任务数量 as UNSIGNED INTEGER);
						call intTo2ByteHex10_16_4(width,@width);
						call intTo2ByteHex10_16_4(heigth,@heigth);
	-- 					call intTo2ByteHex10_16_4(pgknum,@pgknum);
	-- 					call intTo2ByteHex10_16_4(locknum,@locknum);
						set dates=CONCAT(returnvalue,"00000002",@width,@heigth);
						call message(dates,'000a',@date);
						set messagedate=@date;
					else
						select 1;
-- 						if @料架编号one is not null and @groups is not null then
-- 							select count(*) into @counts1 from gmms_shelf_task where task_type='从仓位到上片位' and load_rack=@料架编号one and groups=@groups  and (task_state='等待中' or task_state='正在工作');
-- 							select @counts1;
-- 							if @counts1=0 then
-- 							select sr.number into @number from gmms_shelf_rack sr join gmms_raw_package rp on sr.raw_package_id1=rp.id where rp.raw_width=@raw_width and rp.raw_height=@raw_height and rp.raw_thickness=@raw_thickness and sr.falg='启用' and sr.state='使用中' and rp.left_pieces>0	and rp.color=@color limit 1;
-- 								if @number is not null and @料架编号one is not null then
-- 						
-- 									select max(task_id) into @maxtask_id from gmms_shelf_task;
-- 									insert into gmms_shelf_task set task_type='从仓位到上片位',task_state='等待中',load_rack=@料架编号one,groups=@groups,shelf_rack_id=@number,start_time=Now(),task_id=(@maxtask_id+1);
-- 									update gmms_shelf_rack set state='正在使用' where number=@shelf_rack_id;
-- 								end if;
-- 							end if;
-- 						end if;
					
					end if;
				else
					if @raw_width=@宽度 and @raw_height=@高度 and @raw_thickness=@厚度 and @color=@颜色 and @剩余片数>1 then
						update gmms_machine a set work_state="正在工作" where id=machineids;
						update gmms_load_rack set state="正在工作" where machine_id=machineids and number=@料架编号two;
						update gmms_pattern_task set work_state="正在工作" where work_state="等待中" and groups=@groups limit 1;
						set width=cast(@宽度 as UNSIGNED INTEGER);
						set heigth=cast(@高度 as UNSIGNED INTEGER);
	-- 					set pgknum=cast(@完成任务数量 as UNSIGNED INTEGER);
	-- 					set locknum=cast(@等待中任务数量 as UNSIGNED INTEGER);
						call intTo2ByteHex10_16_4(width,@width);
						call intTo2ByteHex10_16_4(heigth,@heigth);
	-- 					call intTo2ByteHex10_16_4(pgknum,@pgknum);
	-- 					call intTo2ByteHex10_16_4(locknum,@locknum);
						set dates=CONCAT(returnvalue,"00000002",@width,@heigth);
						call message(dates,'000a',@date);
						set messagedate=@date;
					else
					select 1;
-- 						if @料架编号one is not null and @groups is not null then
-- 							select count(*) into @counts1 from gmms_shelf_task where task_type='从仓位到上片位' and load_rack=@料架编号one and groups=@groups  and (task_state='等待中' or task_state='正在工作');
-- 							select @counts1;
-- 							if @counts1=0 then
-- 				
-- 							/*
-- 							#新修改 防止吊装位错误拉片 未实际装
-- 										select sr.number into @number from gmms_shelf_rack sr join gmms_raw_package rp on sr.raw_package_id1=rp.id 
-- 										left join gmms_load_rack c on sr.id=c.shelf_task_id 
-- 										where rp.raw_width=@raw_width and rp.raw_height=@raw_height and rp.raw_thickness=@raw_thickness and sr.falg='启用'  and sr.state='使用中' and rp.left_pieces>0 and rp.color=@color and c.id<>99 order by rp.left_pieces limit 1;
-- 							*/
-- 							
-- 								select sr.number into @number from gmms_shelf_rack sr join gmms_raw_package rp on sr.raw_package_id1=rp.id where rp.raw_width=@raw_width and rp.raw_height=@raw_height and rp.raw_thickness=@raw_thickness and sr.falg='启用'  and sr.state='使用中' and rp.left_pieces>0 and rp.color=@color order by rp.left_pieces limit 1;
-- 								if @number is not null and @料架编号one is not null then
-- 						
-- 									select max(task_id) into @maxtask_id from gmms_shelf_task;
-- 									insert into gmms_shelf_task set task_type='从仓位到上片位',task_state='等待中',load_rack=@料架编号one,groups=@groups,shelf_rack_id=@number,start_time=Now(),task_id=(@maxtask_id+1);
-- 									update gmms_shelf_rack set state='正在使用' where number=@shelf_rack_id;
-- 								end if;
-- 							end if;
-- 						end if;
					
					end if;
				end if;

			else
				select 1;
-- 				if @料架编号one is not null and @groups is not null then
-- 					select count(*) into @counts2 from gmms_shelf_task where task_type='从仓位到上片位' and load_rack=@料架编号one and groups=@groups  and (task_state='等待中' or task_state='正在工作');
-- 					select @counts2;
-- 					if @counts2=0 then
-- 						select sr.number into @number from gmms_shelf_rack sr join gmms_raw_package rp on sr.raw_package_id1=rp.id where rp.raw_width=@raw_width and rp.raw_height=@raw_height and rp.raw_thickness=@raw_thickness and sr.state='使用中' and sr.falg='启用' and rp.left_pieces>0 and rp.color=@color order by rp.left_pieces limit 1;
-- 						
-- 						if @number is not null and @料架编号one is not null then
-- 						
-- 							select max(task_id) into @maxtask_id from gmms_shelf_task;
-- 							insert into gmms_shelf_task set task_type='从仓位到上片位',task_state='等待中',load_rack=@料架编号one,groups=@groups,shelf_rack_id=@number,start_time=Now(),task_id=(@maxtask_id+1);
-- 							update gmms_shelf_rack set state='正在使用' where number=@shelf_rack_id;
-- 						end if;
-- 					end if;
-- 				end if;
				
			end if;
		end if;

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for LoadSend_copy1
-- ----------------------------
DROP PROCEDURE IF EXISTS `LoadSend_copy1`;
delimiter ;;
CREATE PROCEDURE `LoadSend_copy1`(in machineids BIGINT,in returnvalue varchar(255),out messagedate varchar(255))
BEGIN
	#上片请求存储过程
	#循环找到工位上面有架子并且剩余数量大于零并且没有完成的上片任务进行修改
		DECLARE s int DEFAULT 0;
		DECLARE dates varchar(255);
		DECLARE width int;
		DECLARE heigth int;
		DECLARE pgknum int;
		DECLARE locknum int;
		DECLARE messagedates varchar(255) DEFAULT '';
	  set messagedate=messagedates;
		
		select count(*) into @数量 from gmms_load_rack r where machine_id=machineids;
		select groups into @groups from gmms_load_rack r where machine_id=machineids GROUP BY groups;
		
		select @数量;
		
		select id,raw_width,raw_height,raw_thickness,color into @pattern_id,@raw_width,@raw_height,@raw_thickness,@color from gmms_pattern_task where work_state='等待中'    and groups=@groups order by rank limit 1;
		select count(*) into @countss from gmms_pattern_task where work_state='等待中' and groups=@groups;
		select count(*) into @counts2 from gmms_pattern_task where work_state='正在工作' and groups=@groups;
		select @pattern_id;
		select r.falg_shelf,r.number,r.shelf_task_id into @料架标志one,@料架编号one,@任务编号one from gmms_load_rack r where machine_id=machineids limit 0,1;
		
		select @料架标志one;
		select @料架编号one;
		select @任务编号one;
		
		select r.falg_shelf,r.number,r.shelf_task_id into @料架标志two,@料架编号two,@任务编号two from gmms_load_rack r where machine_id=machineids limit 1,1;
		
		select @料架标志two;
		
		if @countss>0 and @counts2=0 then
		
			if @料架标志one="1" then
			
				select rp.left_pieces,rp.raw_width,rp.raw_height,rp.raw_thickness,rp.color,sr.id,sr.color_falg into @剩余片数,@宽度,@高度,@厚度,@颜色,@shelf_rack_id,@膜系标志 from gmms_load_rack r join gmms_shelf_task s on r.shelf_task_id=s.id join gmms_shelf_rack sr on s.shelf_rack_id=sr.id join gmms_raw_package rp on sr.raw_package_id1=rp.id where r.number=@料架编号one;
				
				#@color='白玻' or @color='超白' or @color='C' or @膜系标志=1
				if 1=1 then
					if @raw_width=@宽度 and @raw_height=@高度 and @raw_thickness=@厚度 and @color=@颜色 and @剩余片数>0 then
					
						update gmms_machine a set work_state="正在工作" where id=machineids;
						update gmms_load_rack set state="正在工作" where machine_id=machineids and number=@料架编号one;
						update gmms_pattern_task set work_state="正在工作" where  work_state="等待中" and groups=@groups limit 1  ;
						set width=cast(@宽度 as UNSIGNED INTEGER);
						set heigth=cast(@高度 as UNSIGNED INTEGER);
	-- 					set pgknum=cast(@完成任务数量 as UNSIGNED INTEGER);
	-- 					set locknum=cast(@等待中任务数量 as UNSIGNED INTEGER);
						call intTo2ByteHex10_16_4(width,@width);
						call intTo2ByteHex10_16_4(heigth,@heigth);
	-- 					call intTo2ByteHex10_16_4(pgknum,@pgknum);
	-- 					call intTo2ByteHex10_16_4(locknum,@locknum);
						set dates=CONCAT(returnvalue,"00000002",@width,@heigth);
						call message(dates,'000a',@date);
						set messagedate=@date;
					else
						if @料架编号one is not null and @groups is not null and @shelf_rack_id is not null then
							select count(*) into @counts from gmms_shelf_task where task_type='从上片位到仓位' and load_rack=@料架编号one and groups=@groups and shelf_rack_id=@shelf_rack_id and (task_state='等待中' or task_state='正在工作');
							if @counts=0 then
							
								select max(task_id) into @maxtask_id from gmms_shelf_task;
								insert into gmms_shelf_task set task_type='从上片位到仓位',task_state='等待中',load_rack=@料架编号one,groups=@groups,shelf_rack_id=@shelf_rack_id,start_time=Now(),task_id=(@maxtask_id+1);	

							end if;
						end if;
									
					end if;
				else
					if @raw_width=@宽度 and @raw_height=@高度 and @raw_thickness=@厚度 and @color=@颜色 and @剩余片数>1 then
					
					
						update gmms_machine a set work_state="正在工作" where id=machineids;
						update gmms_load_rack set state="正在工作" where machine_id=machineids and number=@料架编号one;
						update gmms_pattern_task set work_state="正在工作" where  work_state="等待中" and groups=@groups limit 1;
						set width=cast(@宽度 as UNSIGNED INTEGER);
						set heigth=cast(@高度 as UNSIGNED INTEGER);
	-- 					set pgknum=cast(@完成任务数量 as UNSIGNED INTEGER);
	-- 					set locknum=cast(@等待中任务数量 as UNSIGNED INTEGER);
						call intTo2ByteHex10_16_4(width,@width);
						call intTo2ByteHex10_16_4(heigth,@heigth);
	-- 					call intTo2ByteHex10_16_4(pgknum,@pgknum);
	-- 					call intTo2ByteHex10_16_4(locknum,@locknum);
						set dates=CONCAT(returnvalue,"00000002",@width,@heigth);
						call message(dates,'000a',@date);
						set messagedate=@date;
					else
						if @料架编号one is not null and @groups is not null and @shelf_rack_id is not null then
							select count(*) into @counts from gmms_shelf_task where task_type='从上片位到仓位' and load_rack=@料架编号one and groups=@groups and shelf_rack_id=@shelf_rack_id and (task_state='等待中' or task_state='正在工作');
							if @counts=0 then
							
								select max(task_id) into @maxtask_id from gmms_shelf_task;
								insert into gmms_shelf_task set task_type='从上片位到仓位',task_state='等待中',load_rack=@料架编号one,groups=@groups,shelf_rack_id=@shelf_rack_id,start_time=Now(),task_id=(@maxtask_id+1);	

							end if;
						end if;
									
					end if;
				end if;
			
			elseif @料架标志two="1" then
			
				select rp.left_pieces,rp.raw_width,rp.raw_height,rp.raw_thickness,rp.color into @剩余片数,@宽度,@高度,@厚度,@颜色 from gmms_load_rack r join gmms_shelf_task s on r.shelf_task_id=s.id join gmms_shelf_rack sr on s.shelf_rack_id=sr.id join gmms_raw_package rp on sr.raw_package_id1=rp.id where r.number=@料架编号two;
				
				#@color='白玻' or @color='超白' or @color='C' or @膜系标志=1
				if 1=1 then
				select @raw_width,@宽度,@raw_height,@高度,@raw_thickness,@厚度,@color,@颜色,@剩余片数;
					if @raw_width=@宽度 and @raw_height=@高度 and @raw_thickness=@厚度 and @color=@颜色 and @剩余片数>0 then
						update gmms_machine a set work_state="正在工作" where id=machineids;
						update gmms_load_rack set state="正在工作" where machine_id=machineids and number=@料架编号two;
						update gmms_pattern_task set work_state="正在工作" where work_state="等待中" and groups=@groups limit 1;
						set width=cast(@宽度 as UNSIGNED INTEGER);
						set heigth=cast(@高度 as UNSIGNED INTEGER);
	-- 					set pgknum=cast(@完成任务数量 as UNSIGNED INTEGER);
	-- 					set locknum=cast(@等待中任务数量 as UNSIGNED INTEGER);
						call intTo2ByteHex10_16_4(width,@width);
						call intTo2ByteHex10_16_4(heigth,@heigth);
	-- 					call intTo2ByteHex10_16_4(pgknum,@pgknum);
	-- 					call intTo2ByteHex10_16_4(locknum,@locknum);
						set dates=CONCAT(returnvalue,"00000002",@width,@heigth);
						call message(dates,'000a',@date);
						set messagedate=@date;
					else
						select 1;
						#二工位不对 则生成一工位任务
						if @料架编号one is not null and @groups is not null then
							select count(*) into @counts1 from gmms_shelf_task where task_type='从仓位到上片位' and load_rack=@料架编号one and groups=@groups  and (task_state='等待中' or task_state='正在工作');
							select @counts1;
							if @counts1=0 then
							select sr.number into @number from gmms_shelf_rack sr join gmms_raw_package rp on sr.raw_package_id1=rp.id where rp.raw_width=@raw_width and rp.raw_height=@raw_height and rp.raw_thickness=@raw_thickness and sr.falg='启用' and sr.state='使用中' and rp.left_pieces>0	and rp.color=@color limit 1;
								if @number is not null and @料架编号one is not null then
						
									select max(task_id) into @maxtask_id from gmms_shelf_task;
									insert into gmms_shelf_task set task_type='从仓位到上片位',task_state='等待中',load_rack=@料架编号one,groups=@groups,shelf_rack_id=@number,start_time=Now(),task_id=(@maxtask_id+1);
									update gmms_shelf_rack set state='正在使用' where number=@shelf_rack_id;
								end if;
							end if;
						end if;
					
					end if;
				else
					if @raw_width=@宽度 and @raw_height=@高度 and @raw_thickness=@厚度 and @color=@颜色 and @剩余片数>1 then
						update gmms_machine a set work_state="正在工作" where id=machineids;
						update gmms_load_rack set state="正在工作" where machine_id=machineids and number=@料架编号two;
						update gmms_pattern_task set work_state="正在工作" where work_state="等待中" and groups=@groups limit 1;
						set width=cast(@宽度 as UNSIGNED INTEGER);
						set heigth=cast(@高度 as UNSIGNED INTEGER);
	-- 					set pgknum=cast(@完成任务数量 as UNSIGNED INTEGER);
	-- 					set locknum=cast(@等待中任务数量 as UNSIGNED INTEGER);
						call intTo2ByteHex10_16_4(width,@width);
						call intTo2ByteHex10_16_4(heigth,@heigth);
	-- 					call intTo2ByteHex10_16_4(pgknum,@pgknum);
	-- 					call intTo2ByteHex10_16_4(locknum,@locknum);
						set dates=CONCAT(returnvalue,"00000002",@width,@heigth);
						call message(dates,'000a',@date);
						set messagedate=@date;
					else
					select 1;
						if @料架编号one is not null and @groups is not null then
							select count(*) into @counts1 from gmms_shelf_task where task_type='从仓位到上片位' and load_rack=@料架编号one and groups=@groups  and (task_state='等待中' or task_state='正在工作');
							select @counts1;
							if @counts1=0 then
				
							/*
							#新修改 防止吊装位错误拉片 未实际装
										select sr.number into @number from gmms_shelf_rack sr join gmms_raw_package rp on sr.raw_package_id1=rp.id 
										left join gmms_load_rack c on sr.id=c.shelf_task_id 
										where rp.raw_width=@raw_width and rp.raw_height=@raw_height and rp.raw_thickness=@raw_thickness and sr.falg='启用'  and sr.state='使用中' and rp.left_pieces>0 and rp.color=@color and c.id<>99 order by rp.left_pieces limit 1;
							*/
							
								select sr.number into @number from gmms_shelf_rack sr join gmms_raw_package rp on sr.raw_package_id1=rp.id where rp.raw_width=@raw_width and rp.raw_height=@raw_height and rp.raw_thickness=@raw_thickness and sr.falg='启用'  and sr.state='使用中' and rp.left_pieces>0 and rp.color=@color order by rp.left_pieces limit 1;
								if @number is not null and @料架编号one is not null then
						
									select max(task_id) into @maxtask_id from gmms_shelf_task;
									insert into gmms_shelf_task set task_type='从仓位到上片位',task_state='等待中',load_rack=@料架编号one,groups=@groups,shelf_rack_id=@number,start_time=Now(),task_id=(@maxtask_id+1);
									update gmms_shelf_rack set state='正在使用' where number=@shelf_rack_id;
								end if;
							end if;
						end if;
					
					end if;
				end if;

			else
				select 1;
				if @料架编号one is not null and @groups is not null then
					select count(*) into @counts2 from gmms_shelf_task where task_type='从仓位到上片位' and load_rack=@料架编号one and groups=@groups  and (task_state='等待中' or task_state='正在工作');
					select @counts2;
					if @counts2=0 then
						select sr.number into @number from gmms_shelf_rack sr join gmms_raw_package rp on sr.raw_package_id1=rp.id where rp.raw_width=@raw_width and rp.raw_height=@raw_height and rp.raw_thickness=@raw_thickness and sr.state='使用中' and sr.falg='启用' and rp.left_pieces>0 and rp.color=@color order by rp.left_pieces limit 1;
						
						if @number is not null and @料架编号one is not null then
						
							select max(task_id) into @maxtask_id from gmms_shelf_task;
							insert into gmms_shelf_task set task_type='从仓位到上片位',task_state='等待中',load_rack=@料架编号one,groups=@groups,shelf_rack_id=@number,start_time=Now(),task_id=(@maxtask_id+1);
							update gmms_shelf_rack set state='正在使用' where number=@shelf_rack_id;
						end if;
					end if;
				end if;
				
			end if;
		end if;

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for LoadShelfTaskAdd
-- ----------------------------
DROP PROCEDURE IF EXISTS `LoadShelfTaskAdd`;
delimiter ;;
CREATE PROCEDURE `LoadShelfTaskAdd`(in machineids BIGINT)
BEGIN
	#上片仓储任务
	DECLARE FlagType_ BIGINT DEFAULT 1;            #工位类型
	DECLARE Groups_ BIGINT DEFAULT 99;             #工位线路
	
	DECLARE Number_ varchar(255) DEFAULT null;     #上片工位编号
	DECLARE Shelfid_ BIGINT DEFAULT 0;            #仓储任务料架ID
	DECLARE maxtaskid_ BIGINT DEFAULT 0;           #最大任务ID
	
	DECLARE TKNumber_ varchar(255) DEFAULT null;    #退库编号
	DECLARE TKShelfid_ BIGINT DEFAULT 0;            #退库编号料架ID
	
	DECLARE Rawwidth_ BIGINT DEFAULT 0;            #需要宽
	DECLARE Rawheight_ BIGINT DEFAULT 0;           #需要高
	DECLARE Rawtype_ varchar(255) DEFAULT null;    #需要类型
	DECLARE XuyaoPieces_ BIGINT DEFAULT 0;         #需要片数
	
	DECLARE RawwidthNext_ BIGINT DEFAULT 0;            #需要宽
	DECLARE RawheightNext_ BIGINT DEFAULT 0;           #需要高
	DECLARE RawtypeNext_ varchar(255) DEFAULT null;    #需要类型
	DECLARE XuyaoPiecesNext_ BIGINT DEFAULT 0;         #需要片数
	
	DECLARE Did_ BIGINT DEFAULT 0;                  #工位id
	DECLARE DRawwidth_ BIGINT DEFAULT 0;            #当前能吸到的工位1宽
	DECLARE DRawheight_ BIGINT DEFAULT 0;           #当前能吸到的工位1高
	DECLARE DRawtype_ varchar(255) DEFAULT null;    #当前能吸到的工位1类型
	DECLARE DPieces_ BIGINT DEFAULT 0;              #当前能吸到的工位1片数
	
	DECLARE IsExistcount_ BIGINT DEFAULT 0;         #需要片数
	
	#获取单工位/双工位  以及线路
	select gongwei_mode,gm.work_id into FlagType_,Groups_ from gmms_prod_line_state as gp left join gmms_machine as gm on gp.line=gm.work_id where gm.id=machineids;
	
	
	
	#获取此线路当前所需要的原片   into Rawwidth_,Rawheight_,Rawtype_,XuyaoPieces_    and pt.work_state!='已完成'
	select raw_width,raw_height,raw_type,count(*) as sumtkprices into Rawwidth_,Rawheight_,Rawtype_,XuyaoPieces_ from 
	(select pt.rank,pt.raw_width,pt.raw_height,pt.raw_type,
	@count:=IF(@last=CONCAT(pt.raw_width,pt.raw_height,pt.raw_type),@count,@count+1) as tkprices,@last:=CONCAT(pt.raw_width,pt.raw_height,pt.raw_type) as last  from gmms_raw_file as rf 
	left join gmms_pattern_task as pt on rf.id=pt.raw_id,(select @count:=0,@last:=null) as b
	where rf.operator=Groups_ and rf.state='已领取' and pt.work_state!='已完成') as result 
	group by tkprices order by rank asc limit 0,1;
	
	#获取此线路下一类型所需要的原片   into RawwidthNext_,RawheightNext_,RawtypeNext_,XuyaoPiecesNext_   
	select raw_width,raw_height,raw_type,count(*) as sumtkprices into RawwidthNext_,RawheightNext_,RawtypeNext_,XuyaoPiecesNext_ from 
	(select pt.rank,pt.raw_width,pt.raw_height,pt.raw_type,
	@count:=IF(@last=CONCAT(pt.raw_width,pt.raw_height,pt.raw_type),@count,@count+1) as tkprices,@last:=CONCAT(pt.raw_width,pt.raw_height,pt.raw_type) as last  from gmms_raw_file as rf 
	left join gmms_pattern_task as pt on rf.id=pt.raw_id,(select @count:=0,@last:=null) as b
	where rf.operator=Groups_ and rf.state='已领取' and pt.work_state!='已完成') as result 
	group by tkprices order by rank asc limit 1,1;

	#获取此线路当前已有的玻璃    into DRawwidth_,DRawheight_,DRawtype_,DPieces_
	select lr.id,rp.raw_width,rp.raw_height,rp.raw_type,rp.left_pieces into Did_,DRawwidth_,DRawheight_,DRawtype_,DPieces_ from
		(select *,IF(shelf_task_id is null,(select gst.id from gmms_shelf_task as gst  
								where gst.load_rack=glr.number and gst.task_state!='已完成' and gst.task_type='从仓位到上片位'),glr.shelf_task_id) as shelftaskid from gmms_load_rack as glr
								where glr.groups=Groups_ and number!='11') as lr 
	left join gmms_shelf_task as st on lr.shelftaskid=st.id 
	left join gmms_shelf_rack as sr on st.shelf_rack_id=sr.id 
	left join gmms_raw_package as rp on sr.raw_package_id1=rp.id
	where shelftaskid is not null and rp.raw_width=Rawwidth_ and rp.raw_height=Rawheight_ and rp.raw_type=Rawtype_ and rp.left_pieces>0 order by lr.id limit 0,1;
	
	select MIN(id) from gmms_load_rack where groups=Groups_;
	select '任务需要：',Rawwidth_,Rawheight_,Rawtype_,XuyaoPieces_,'有任务的第一工位：',Did_,DRawwidth_,DRawheight_,DRawtype_,DPieces_;
	#对比当前所需要的原片是否某 工位上有？  
	#			有:  1.且是一工位，2.二工位无任务，3.一工位玻璃完成后有多余任务   则：二工位补架  
	#   没有:  1.查找空位补架
	select max(task_id) into maxtaskid_ from gmms_shelf_task;
	#查找空位补架
	select lr.number into Number_ from 
				(select *,IF(shelf_task_id is null,(select gst.id from gmms_shelf_task as gst  
								where gst.load_rack=glr.number and gst.task_state!='已完成' and gst.task_type='从仓位到上片位'),glr.shelf_task_id) as shelftaskid from gmms_load_rack as glr
								where glr.groups=Groups_ and number!='11') as lr 
		where lr.shelftaskid is null order by id asc limit 0,1;
	
	#二工位编号和  架子号
	select lr.number,st.shelf_rack_id into TKNumber_,TKShelfid_ from 
				(select *,IF(shelf_task_id is null,(select gst.id from gmms_shelf_task as gst  
								where gst.load_rack=glr.number and gst.task_state!='已完成'),glr.shelf_task_id) as shelftaskid from gmms_load_rack as glr
								where glr.groups=Groups_ and number!='11' and glr.id=(select max(id) from gmms_load_rack where groups=Groups_ and number!='11')) as lr 
	left join gmms_shelf_task as st on lr.shelftaskid=st.id 
	order by lr.id asc limit 0,1;
	
	if (select MIN(id) from gmms_load_rack where groups=Groups_)=Did_ and Did_>0 then
		#补二工位
		if DPieces_>=XuyaoPieces_ and RawwidthNext_>0 then
			select sr.id into Shelfid_ from gmms_shelf_rack as sr LEFT JOIN gmms_raw_package as rp on sr.raw_package_id1=rp.id 
			where sr.state='使用中' and rp.left_pieces>0 and rp.raw_width=RawwidthNext_ and rp.raw_height=RawheightNext_ and rp.raw_type=RawtypeNext_ and sr.falg='启用' 
			order by rp.left_pieces asc limit 0,1;
			select '当前工位玻璃种类剩余数>任务所需数，补下一类型玻璃';
		else
			select sr.id into Shelfid_ from gmms_shelf_rack as sr LEFT JOIN gmms_raw_package as rp on sr.raw_package_id1=rp.id 
			where sr.state='使用中' and rp.left_pieces>0 and rp.raw_width=Rawwidth_ and rp.raw_height=Rawheight_ and rp.raw_type=Rawtype_ and sr.falg='启用'  
			order by rp.left_pieces asc limit 0,1;
			select '当前工位玻璃种类剩余数<任务所需数';
		end if;
	elseif Did_=0 then
		#没有工位符合所需玻璃 
		select sr.id into Shelfid_ from gmms_shelf_rack as sr LEFT JOIN gmms_raw_package as rp on sr.raw_package_id1=rp.id 
		where sr.state='使用中' and rp.left_pieces>0 and rp.raw_width=Rawwidth_ and rp.raw_height=Rawheight_ and rp.raw_type=Rawtype_  and sr.falg='启用' 
		order by rp.left_pieces asc limit 0,1;
		select '没有工位符合当前所需玻璃';
		if TKNumber_ is not null and TKShelfid_>0 and FlagType_=2 and 
			(select count(*) from gmms_shelf_task where (load_rack=TKNumber_ or shelf_rack_id=TKShelfid_) and (task_state='等待中' or task_state='正在工作'))=0	
		then
			select '二工位退库','从上片位到仓位','等待中',TKNumber_,Groups_,TKShelfid_,Now(),(maxtaskid_+1);
			insert into gmms_shelf_task set task_type='从上片位到仓位',task_state='等待中',load_rack=TKNumber_,groups=Groups_,shelf_rack_id=TKShelfid_,start_time=Now(),task_id=(maxtaskid_+1);	
		end if;
	end if;
	select '从仓位到上片位','等待中',Number_,Groups_,Shelfid_,Now(),(maxtaskid_+1);
	
	select count(*) into IsExistcount_ from gmms_shelf_task 
	where (load_rack=Number_ or shelf_rack_id=Shelfid_) and (task_state='等待中' or task_state='正在工作');
	
	if Number_ is not null and Shelfid_>0 and Shelfid_ is not null and IsExistcount_=0 and 
		(((select number from gmms_load_rack where groups=Groups_ and number!='11' order by  id desc LIMIT 0,1)!=Number_ and FlagType_=1) or FlagType_=2) then
		update gmms_shelf_rack set state='正在使用' where id=Shelfid_;
		insert into gmms_shelf_task set task_type='从仓位到上片位',task_state='等待中',load_rack=Number_,groups=Groups_,shelf_rack_id=Shelfid_,start_time=Now(),task_id=(maxtaskid_+1);
		select * from gmms_shelf_rack as sr LEFT JOIN gmms_raw_package as rp on sr.raw_package_id1=rp.id 
		where sr.id=Shelfid_;
		select '成功';
	end if;

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for LoadShelfTaskAdd_
-- ----------------------------
DROP PROCEDURE IF EXISTS `LoadShelfTaskAdd_`;
delimiter ;;
CREATE PROCEDURE `LoadShelfTaskAdd_`(in machineids BIGINT)
BEGIN
	#上片仓储任务
	DECLARE id_ varchar(255) DEFAULT null;         #id
	DECLARE Number_ varchar(255) DEFAULT null;     #编号
	DECLARE FlagShelf_ varchar(255) DEFAULT null;  #是否有架子
	DECLARE ShelfTaskId_ BIGINT DEFAULT 0;         #任务id
	DECLARE groups_ BIGINT DEFAULT 1;              #工位线路
	DECLARE maxtaskid_ BIGINT DEFAULT 0;           #最大任务ID
	
	DECLARE FlagType_ BIGINT DEFAULT 1;            #工位类型
	DECLARE count_ BIGINT DEFAULT 0;               #循环数量
	DECLARE kxcount_ BIGINT DEFAULT 0;             #当前线路有几个工位没有架子
	DECLARE rankcount_ BIGINT DEFAULT 0;           #第几个任务
	
	DECLARE rank_ BIGINT DEFAULT 0;                #第几个任务
	DECLARE width_ BIGINT DEFAULT 0;                #需要长
	DECLARE height_ BIGINT DEFAULT 0;               #需要宽
	DECLARE rawtype_ varchar(255) DEFAULT null;     #需要类型
	
	DECLARE shelfid_ BIGINT DEFAULT 0;              #第几个任务
	DECLARE leftpieces_ BIGINT DEFAULT 0;           #剩余片数
	DECLARE poor_ BIGINT DEFAULT 0;                 #差值
	
	DECLARE rank2_ BIGINT DEFAULT 0;                #第几个任务
	DECLARE taskcount_ BIGINT DEFAULT 0;            #任务连续数量
	
	DECLARE IsExistcount_ BIGINT DEFAULT 0;         #是否已经生成
	DECLARE IsExisttask_ BIGINT DEFAULT 0;          #当前线路生成了几个任务
	
	#获取单工位/双工位
	select gongwei_mode,gm.work_id into FlagType_,groups_ from gmms_prod_line_state as gp left join gmms_machine as gm on gp.line=gm.work_id where gm.id=machineids;
	select count(*) into kxcount_ from gmms_load_rack 
	where machine_id=machineids and number!='11' and falg_shelf=0;
	#当两工位都无架子时生成任务
	select count(*) into IsExisttask_ from gmms_load_rack as lr left join gmms_shelf_task as gt on lr.number=gt.load_rack 
	where (task_state='等待中' or task_state='正在工作') and lr.groups=groups_  and lr.number!='11';
	#and lr.falg_shelf=0
	WHILE FlagType_>count_ and kxcount_=2 and IsExisttask_=0 DO
		set rank_=0,width_=0,height_=0,rawtype_=0,shelfid_=0,leftpieces_=0,IsExistcount_=0,Number_=null,maxtaskid_=0;
		
		select id,number,falg_shelf,shelf_task_id 
		into id_,Number_,FlagShelf_,ShelfTaskId_ from gmms_load_rack 
		where machine_id=machineids and number!='11' limit count_,1;
		
		select count_,kxcount_,id_,Number_,FlagShelf_,ShelfTaskId_,groups_,FlagType_,rankcount_,'开始';
		#最新未完成任务
		select pt.rank,pt.raw_width,pt.raw_height,pt.raw_type into rank_,width_,height_,rawtype_ from gmms_raw_file as rf left join gmms_pattern_task as pt on rf.id=pt.raw_id 
		where rf.operator=groups_ and rf.state='已领取' and pt.work_state!='已完成' order by pt.rank asc limit rankcount_,1;
		#符合所需原片的最少玻璃
		select sr.id,rp.left_pieces into shelfid_,leftpieces_ from gmms_shelf_rack as sr LEFT JOIN gmms_raw_package as rp on sr.raw_package_id1=rp.id 
		where sr.state='使用中' and rp.left_pieces>0 and rp.raw_width=width_ and rp.raw_height=height_ and rp.raw_type=rawtype_ order by rp.left_pieces asc limit 0,1;
		
		#计算当前仓储任务后的下一个任务
		select pt.rank into rank2_ from gmms_raw_file as rf left join gmms_pattern_task as pt on rf.id=pt.raw_id 
		where rf.operator=groups_ and rf.state='已领取' and pt.work_state!='已完成' and (pt.raw_width!=width_ or pt.raw_height!=height_ or pt.raw_type!=rawtype_) 
		order by pt.rank asc limit 0,1;
		
		select count(*) into taskcount_ from gmms_raw_file as rf left join gmms_pattern_task as pt on rf.id=pt.raw_id 
		where rf.operator=groups_ and rf.state='已领取' and pt.work_state!='已完成' and rank<rank2_;
		
		select rank_,width_,height_,rawtype_,shelfid_,leftpieces_,rank2_,taskcount_,'计算';
		select * from gmms_raw_file as rf left join gmms_pattern_task as pt on rf.id=pt.raw_id 
		where rf.operator=groups_ and rf.state='已领取';
		
		#leftpieces_ 剩余片数=0停止任务生成
		if leftpieces_=0 then
			set FlagType_=0;
		elseif	leftpieces_>=taskcount_ then
			#剩余片数大于任务数
			set rankcount_=taskcount_;
		else
			#剩余片数大于任务数
			set rankcount_=leftpieces_;
		end if;
		
		select count(*) into IsExistcount_ from gmms_shelf_task 
		where task_type='从仓位到上片位' and load_rack=Number_ and groups=groups_ and (task_state='等待中' or task_state='正在工作');
		
		if FlagShelf_=0 and shelfid_>0 and leftpieces_>0 and IsExistcount_=0 and Number_ is not null then
			#添加任务
			select * from gmms_raw_file as rf left join gmms_pattern_task as pt on rf.id=pt.raw_id 
			where rf.operator=groups_ and rf.state='已领取';
			select max(task_id) into maxtaskid_ from gmms_shelf_task;
			select '从仓位到上片位','等待中',Number_,groups_,shelfid_,Now(),maxtaskid_+1;
			insert into gmms_shelf_task set task_type='从仓位到上片位',task_state='等待中',load_rack=Number_,groups=groups_,shelf_rack_id=shelfid_,start_time=Now(),task_id=(maxtaskid_+1);
			update gmms_shelf_rack set state='正在使用' where number=shelfid_;
		end if;
		
		
		set count_=count_+1;
	END WHILE;

	
	
	
	
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for log_glass
-- ----------------------------
DROP PROCEDURE IF EXISTS `log_glass`;
delimiter ;;
CREATE PROCEDURE `log_glass`(in gid bigint,proc varchar(50),stus varchar(50), ln int)
BEGIN
	#Routine body goes here...
		DECLARE st INT DEFAULT 0;
		DECLARE seg VARCHAR(50) DEFAULT NULL;
		DECLARE worknm VARCHAR(50) DEFAULT NULL;
		DECLARE segst INT DEFAULT NULL;
	  DECLARE lid bigint DEFAULT NULL;
		DECLARE _w double DEFAULT 0;
		DECLARE _h double DEFAULT 0;
		DECLARE tk double default 0;
		DECLARE ys varchar(50);
		DECLARE cfg_id bigint;
		DECLARE done int DEFAULT 0;
		DECLARE sql_err int default 0;
		DECLARE cur CURSOR FOR 
		 select id,segment,`status`,work_name,segment_status from gmms_glass_log_config where work_proc=proc and work_status=stus;
		DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
		DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET sql_err=1;
		
		
		set st=null;
		set segst=null;
		SET done=0;
			set ys=null;
			SELECT w,h,thickness,color into _w,_h,tk,ys from gmms_galss_task_buffer where id=gid;
			set st=null;
			set segst=null;
			if ys is not null then 
			set @z_area=_w*_h/1000/1000;
			open cur;
		  FETCH cur into cfg_id,seg,st,worknm,segst;
		 while done=0 do
			
				set lid=null;
				#对整个工艺流程进行日志记录
			 if segst is not null then 
							 SELECT id,`status` into lid,@z_c_st from gmms_glass_log where segment_name=seg and glass_id=gid and work_name is null
							 and time_out is null  LIMIT 1;

			IF lid is not null then 
					# 插入新条目 表示重新开始 插入新条目，作废旧条目
					if segst=1 then 
							UPDATE gmms_glass_log  SET time_out=1 where id=lid;
							#插入新的
							INSERT INTO `gmms`.`gmms_glass_log`( `main_id`, `glass_id`, `segment_name`, `work_name`, `line`, `begin_time`, `end_time`, `status`, `parent_id`, `area`, `freeze`) VALUES (cfg_id, gid,seg,NULL,ln, now(), NULL,segst, NULL, @z_area, b'0');					
					ELSE
						#目前不是开始就是结束
						 UPDATE gmms_glass_log set end_time=now(),`status`=segst    where id=lid;	
					end if;   #if segst=1 then 
			else 
						if segst=1 then 		
					 #插入新条目
					INSERT INTO `gmms`.`gmms_glass_log`( `main_id`, `glass_id`, `segment_name`, `work_name`, `line`, `begin_time`, `end_time`, `status`, `parent_id`, `area`, `freeze`) VALUES (cfg_id, gid,seg,NULL,ln, now(), NULL,segst, NULL, @z_area, b'0');
					else
					INSERT INTO `gmms`.`gmms_glass_log`( `main_id`, `glass_id`, `segment_name`, `work_name`, `line`, `begin_time`, `end_time`, `status`, `parent_id`, `area`, `freeze`) VALUES (cfg_id, gid,seg,NULL,ln, now(), now(),segst, NULL, @z_area, b'0');					
						end if;    	
			end if;  
	   end if;
		set lid=null;
		#对单个工艺进行日志记录
				set segst=st;
					 if segst is not null then 
							 SELECT id,`status` into lid,@z_c_st from gmms_glass_log where segment_name=seg and glass_id=gid and work_name=worknm 
							 	 and time_out is null  LIMIT 1;

			IF lid is not null then 
		
					# 插入新条目 表示重新开始 插入新条目，作废旧条目
					if segst=1 then 
							UPDATE gmms_glass_log  SET time_out=1 where id=lid;
							#插入新的
							INSERT INTO `gmms`.`gmms_glass_log`( `main_id`, `glass_id`, `segment_name`, `work_name`, `line`, `begin_time`, `end_time`, `status`, `parent_id`, `area`, `freeze`) VALUES (cfg_id, gid,seg,worknm,ln, now(), NULL,segst, NULL, @z_area, b'0');						
					ELSE
						#目前不是开始就是结束
						 UPDATE gmms_glass_log set end_time=now(),`status`=segst    where id=lid;	
					end if; #if segst=1 then 
			else 
					if segst=1 then 		
					 #插入新条目
					INSERT INTO `gmms`.`gmms_glass_log`( `main_id`, `glass_id`, `segment_name`, `work_name`, `line`, `begin_time`, `end_time`, `status`, `parent_id`, `area`, `freeze`) VALUES (cfg_id, gid,seg,worknm,ln, now(), NULL,segst, NULL, @z_area, b'0');
					else
					INSERT INTO `gmms`.`gmms_glass_log`( `main_id`, `glass_id`, `segment_name`, `work_name`, `line`, `begin_time`, `end_time`, `status`, `parent_id`, `area`, `freeze`) VALUES (cfg_id, gid,seg,worknm,ln, now(), now(),segst, NULL, @z_area, b'0');					
						end if;  
			end if;  			
		end if;  
	FETCH cur into cfg_id,seg,st,worknm,segst; 
	end while;
  close cur;
			 end if;

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for log_key_value
-- ----------------------------
DROP PROCEDURE IF EXISTS `log_key_value`;
delimiter ;;
CREATE PROCEDURE `log_key_value`(inout _var text,in _key varchar(50),in _value varchar(200))
BEGIN
	#Routine body goes here...
 set _var =  concat(_var,IFNULL(_key,""),":",IFNULL(_value,""),"\r\n");
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for log_msg
-- ----------------------------
DROP PROCEDURE IF EXISTS `log_msg`;
delimiter ;;
CREATE PROCEDURE `log_msg`(inout _var text,in _key varchar(50))
BEGIN
	#Routine body goes here...
 set _var =  concat(_var,IFNULL(_key,""),"\r\n");
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for machine_state_begin_update
-- ----------------------------
DROP PROCEDURE IF EXISTS `machine_state_begin_update`;
delimiter ;;
CREATE PROCEDURE `machine_state_begin_update`()
BEGIN
	#Routine body goes here...
			set @zzzz_update_state=null;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for machine_state_tick_update
-- ----------------------------
DROP PROCEDURE IF EXISTS `machine_state_tick_update`;
delimiter ;;
CREATE PROCEDURE `machine_state_tick_update`(in _device_id bigint)
BEGIN
	#Routine body goes here...
		DECLARE tk bigint default null;
		DECLARE _err int default 0;
		DECLARE CONTINUE  HANDLER FOR SQLEXCEPTION,SQLWARNING,NOT FOUND set _err=1;
		SELECT tick into tk from gmms_machine_state where device_id=_device_id and var_name='tick'  LIMIT 1;
		if tk is not null then 
			update gmms_machine_state set tick=tk+1 where device_id=_device_id;
		end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for machine_state_update_var
-- ----------------------------
DROP PROCEDURE IF EXISTS `machine_state_update_var`;
delimiter ;;
CREATE PROCEDURE `machine_state_update_var`(in _device bigint ,in _name varchar(100), in _value text)
BEGIN
	#Routine body goes here...
	 		DECLARE tk bigint default null;
		  DECLARE cv text default null;
			DECLARE ex int default null;
			DECLARE _err int default 0;
			DECLARE n_tk bigint default null;
		  DECLARE CONTINUE  HANDLER FOR SQLEXCEPTION,SQLWARNING,NOT FOUND set _err=1;
	  	SELECT tick,next_tick into tk,n_tk from gmms_machine_state where device_id=_device and var_name='tick'  LIMIT 1;
		
			if tk is not null then 		#不用记录
						if @zzzz_update_state is null then 
							set @zzzz_update_state=1;
							set tk=tk+1;						
							update gmms_machine_state set tick=tk where device_id=_device and var_name='tick';
						end if;
						if  _name<>'tick' then 	#该变量不可修改
						
								SELECT count(*) into ex from gmms_machine_state where device_id=_device and var_name=_name;
								if ex=0 then 
										insert into gmms_machine_state (device_id,
			tick,
			time_stamp,
			var_name,
			var_value)
			value (_device,tk,now(),_name,_value);
							else
							
							update gmms_machine_state set var_value=_value,time_stamp=now(),tick=tk where device_id=_device and var_name=_name;

								end if; 
								
						end if;
			end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for make_galss_task_typeid
-- ----------------------------
DROP PROCEDURE IF EXISTS `make_galss_task_typeid`;
delimiter ;;
CREATE PROCEDURE `make_galss_task_typeid`(in gongcheng varchar(30))
BEGIN
	#Routine body goes here..
	
	# 根据工程名更新小片的类型ID
		DECLARE typeid int;
		 DECLARE done int;
		 DECLARE _width double;
		 DECLARE _height double ;
		 DECLARE _color varchar(30);
		 DECLARE _thickness int;
	 
	 DECLARE cur cursor for 
	 SELECT width,height,color,thickness from gmms_galss_task_buffer where  engineering_id=gongcheng
	 group by width,height,color,thickness;
	 DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
	 set done=0;
	 set typeid=1;
	  open cur;
	  FETCH cur into _width,_height,_color,_thickness;
	 while done<>1 do 
	     UPDATE gmms_galss_task_buffer set type_id=typeid where engineering_id=gongcheng and 
			 width=_width and height=_height and color=_color and thickness=_thickness;
				set typeid=typeid+1;
				FETCH cur into _width,_height,_color,_thickness;
	  end while;	
	 close cur;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for make_order
-- ----------------------------
DROP PROCEDURE IF EXISTS `make_order`;
delimiter ;;
CREATE PROCEDURE `make_order`(in gongcheng varchar(50))
BEGIN
DECLARE done int;
DECLARE w int;
 DECLARE h int;
 DECLARE count int;
 DECLARE color varchar(255);
 DECLARE  thickness double;
 DECLARE _name VARCHAR(50);
 DECLARE idx int;
 
 DECLARE nm varchar(50);
 DECLARE c varchar(255);
 DECLARE thick double;
 DECLARE width int;
 DECLARE height int;
 DECLARE stockid int;
	#Routine body goes here...
declare cur_test CURSOR FOR
SELECT gc_number,color,thickness,gc_width,gc_height,gc_stockid FROM gmms_galss_task_copy1
WHERE gc_number= gongcheng
GROUP BY gc_number,gc_width,gc_height,gc_stockid ,color,thickness
order by gc_stockid;
 DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
 open cur_test;  
 
 SET count:=0;
 SET idx=0;
 posLoop:LOOP  
-- 判断是否结束循环  
    
-- 取游标中的值  
    FETCH  cur_test into nm,c,thick,width,height,stockid;
		 IF done=1 THEN    
      LEAVE posLoop;  
    END IF;  
		if count=0 then
				SET w:=width;
				SET h:=height;
				SET thickness:=thick;
				SET color:=c;
			  SET	count:=1;
				SET _name:=nm;
				SET idx:=idx+1;
		else
			if (w=width and h=height) or (w=height and h=width) then 
				SET count:=	count+1;
			else 
			    SELECT c,thick;
					INSERT INTO gongcheng_raw (gc_name,order_index,color,thickness,width,height,count,state)
					values (_name,idx,color,thickness,w,h,count,0);
					 SET count=0;				
					SET w:=width;
				SET h:=height;
				SET thickness:=thick;
				SET color:=c;
			  SET	count:=1;	 
			  SET _name:=nm;
				SET idx:=idx+1;
			end if;
		end if;
  END LOOP posLoop;
		if count>0 then 
					INSERT INTO gongcheng_raw (gc_name,order_index,color,thickness,width,height,count,state)
					values (_name,idx,color,thickness,w,h,count,0);				
		end if;  		
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for make_pattern_task_buffer_data
-- ----------------------------
DROP PROCEDURE IF EXISTS `make_pattern_task_buffer_data`;
delimiter ;;
CREATE PROCEDURE `make_pattern_task_buffer_data`(in gongcheng varchar(30),in pA varchar(30),in sA varchar(30),in aU varchar(30),in currentColor varchar(30))
BEGIN
  declare w,h,t,pid int;
	declare cl varchar(30);
  declare done int default 0;
  declare cur cursor for
		  	 select gc_width,gc_height,thickness,color,gc_stockid from gmms_galss_task_buffer where engineering_id=gongcheng
			  GROUP BY gc_width,gc_height,thickness,color,gc_stockid
	  		order by gc_stockid  ASC;
				DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
	#Routine body goes here...
	  set @count=0;
		SELECT count(*) into @count from gmms_pattern_task_buffer where engineering_id=gongcheng;
		
		if @count=0 then 
		   open cur;
			  loop1:loop 
						
					
					 FETCH cur into w,h,t,cl,pid;
					if done=1 then 
						  select @count;
					   LEAVE loop1;
					 end if;
				 
			
				  INSERT INTO gmms_pattern_task_buffer
					 (rank,raw_type,raw_width,raw_height,raw_thickness,work_state,color,content,task_id,finsh_time,engineering_id)
					VALUES 
					(@count+1, CONCAT(cast(t as decimal(10,0)),currentColor),w,h,t,'等待中',currentColor,null,0,null,gongcheng);
					set @zid=0;
					SET @zid=LAST_INSERT_ID();
					SELECT @zid;
					update gmms_galss_task_buffer set pattern_id=@zid where gc_stockid=pid and engineering_id=gongcheng;
					
					SET @count=@count+1;
				end loop loop1;
				close cur;

				INSERT INTO gmms_raw_file (file_name,state,operator,create_time,galssmeasure,patternmeasure,cutting_rate) value (gongcheng,'已下发',0,now(),pA,sA,aU);
								call liuchengka_create_v1(gongcheng);
		end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for Marking
-- ----------------------------
DROP PROCEDURE IF EXISTS `Marking`;
delimiter ;;
CREATE PROCEDURE `Marking`(in sendMessageHex varchar(255),in machineids BIGINT,out messagedate varchar(255))
BEGIN
	#加速台存储过程
	#找到当前机器标志位
	#查询机器逻辑表中对应的逻辑进行对应的步骤
	DECLARE machinedisposeids int DEFAULT 0;
	DECLARE ids int ;
	DECLARE plcfindid varchar(255);
	DECLARE dates varchar(255);
	DECLARE plcid varchar(255);
	DECLARE findplc varchar(255);
	DECLARE findmes varchar(255);
	DECLARE taskplc varchar(255);
	DECLARE taskmes varchar(255);
	DECLARE finsh_rankid BIGINT;
	DECLARE send_rankid BIGINT;
	DECLARE rankids varchar(255);
	DECLARE posunmes varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	set messagedate=messagedates;
	
	
	select machine_type_id,remarks into @mtid,@remarks from gmms_machine where id=machineids;
	set ids = cast(@remarks as UNSIGNED INTEGER);
	
	select mf.flag into @完成标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='完成字';
	
	select mf.flag into @请求标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='请求字';
	
	select mf.flag into @发送标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='发送字';
	
	select mf.flag into @完成确认标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='完成确认字';
	
	set finsh_rankid = cast(@完成标志位 as UNSIGNED INTEGER)+1;
	set send_rankid = cast(@请求标志位+1 as UNSIGNED INTEGER)+1;
	select send_rankid;
	
	set findplc = substring(sendMessageHex,(cast(@完成标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskplc = substring(sendMessageHex,(cast(@请求标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskmes = substring(sendMessageHex,(cast(@发送标志位 as UNSIGNED INTEGER)*4)+19,4);
	set findmes = substring(sendMessageHex,(cast(@完成确认标志位 as UNSIGNED INTEGER)*4)+19,4);
	
	select taskplc;
	select taskmes;
	select findplc;
	select findmes;
	select plcfindid;
	
	set rankids = conv(substring(sendMessageHex,(cast(1 as UNSIGNED INTEGER)*4)+19,4),16,10);
	select rankids;

	
	#请求逻辑查询
	select count(*),md.id into @请求数量,@请求逻辑编号 from gmms_machine_dispose md join gmms_machine_type mt on md.machine_type_id=mt.id where md.machine_type_id=@mtid  and md.plc_font_type='请求字' and md.mes_font_type='发送字' and md.plc_font_value=taskplc and md.mes_font_value=taskmes;
	
	
	#判断逻辑是否存在
	if  @请求数量=1  then
	
		if @请求逻辑编号!=0 then
			set machinedisposeids=@请求逻辑编号;
			
		end if;
		
		#查询当前执行逻辑进行判断
		select logic,return_value into @ExecutionLogic,@returnvalue from gmms_machine_dispose where id=machinedisposeids;
		select @ExecutionLogic;
		select @returnvalue;
		if @ExecutionLogic='任务请求' then
				call MarkingSend(machineids,@returnvalue,rankids,@messagedate);
				set messagedate=@messagedate;
				
 		elseif @ExecutionLogic='任务清空' then
				set dates=CONCAT(@returnvalue,"0000");
				call message(dates,'000a',@date);
				set messagedate=@date;
		end if;

	end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for MarkingSend
-- ----------------------------
DROP PROCEDURE IF EXISTS `MarkingSend`;
delimiter ;;
CREATE PROCEDURE `MarkingSend`(in machineids BIGINT,in returnvalue varchar(255),in rankids varchar(255),out messagedate varchar(255))
BEGIN
	#加速台请求存储过程
	#找到当前对应的工序的玻璃信息
	#序号，高度，宽度，厚度发送给plc
	DECLARE number int DEFAULT 0;
	DECLARE out_ranks int;
	DECLARE widths int;
	DECLARE heigths int;
	DECLARE thicknesss int;
	DECLARE drop_frames int;
	DECLARE dates varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	set messagedate=messagedates;
	
	select machine_type_id,work_id into @mtid,@work_groups from gmms_machine where id=machineids;
	
	select file_name into @engineering_id from gmms_raw_file where operator=@work_groups and state='已领取';
	
	select rankids;
	if machineids=41 then
		select marking_flag into @marking_flag from gmms_galss_task where work_procedure='理片' and galss_id=rankids and engineering_id=@engineering_id;
		if @marking_flag is not null then
		
			select da_biao_mo_ban into  @marking_flag from gmms_dongzuo_table where gmms_dongzuo_table.number= @marking_flag LIMIT 1;
		
			call intTo2ByteHex10_16_4(@marking_flag,@marking_flags);
			set dates=CONCAT(returnvalue,@marking_flags);
			call message(dates,'000a',@date);
			set messagedate=@date;
		end if;
	else
		select marking_flag into @marking_flag from gmms_galss_task where work_procedure='理片' and galss_id=rankids and engineering_id=@engineering_id;
		if @marking_flag is not null then
			select da_biao_mo_ban into  @marking_flag from gmms_dongzuo_table where gmms_dongzuo_table.number= @marking_flag LIMIT 1;
			call intTo2ByteHex10_16_4(@marking_flag,@marking_flags);
			set dates=CONCAT(returnvalue,@marking_flags);
			call message(dates,'000a',@date);
			set messagedate=@date;
		end if;
	end if;
	
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for message
-- ----------------------------
DROP PROCEDURE IF EXISTS `message`;
delimiter ;;
CREATE PROCEDURE `message`(in senddate VARCHAR(255),in address VARCHAR(255),out date varchar(255))
BEGIN
	#返回报文进行处理存储过程
	DECLARE Herd varchar(255);
	DECLARE length int;
	DECLARE length1 int;
	DECLARE dates varchar(255);
	DECLARE lengths int;
	DECLARE lengths1 int;
	
	if senddate is null then
		set @softwareconnect=null;
-- 		select software_connect into @softwareconnect from gmms_machine where id=@div_machine;
-- 		update gmms_machine set software_connect=@softwareconnect+1 where id=@div_machine;
		
	end if;
	
	set Herd=CONCAT('0110',address);
	set length1 = char_length(senddate);
	set length = char_length(senddate) / 4;
	if length*4>length1 then
		set length=length-1;
	elseif length*4<length1 then
		set length=length+1;
	end if;
	
	call intTo2ByteHex10_16_4(length,@date);
	call intTo2ByteHex10_16_2(length*2,@date1);
	set dates=CONCAT(Herd,@date,@date1,senddate);
-- 	set lengths=char_length(dates)/2;
	
	set lengths1 = char_length(dates);
	set lengths = char_length(dates)/2;
	if lengths*2>lengths1 then
		set lengths=lengths-1;
	elseif lengths*2<lengths1 then
		set lengths=lengths+1;
	end if;
	call intTo2ByteHex10_16_4(lengths,@date2);
	set date =CONCAT('00000000',@date2,dates);
	

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for numberlength
-- ----------------------------
DROP PROCEDURE IF EXISTS `numberlength`;
delimiter ;;
CREATE PROCEDURE `numberlength`(in number int,out numbers int)
BEGIN
	#处理数据相除的存储过程
	DECLARE number1 int;
	DECLARE count int;
	DECLARE sum int;
	set number1=number;
	set count=0;
	set sum=1;
	

	while sum=1 do
		if number1/10>1 or number1/10=10 then
			set count=count+1;
			set number1=number1/10;
		elseif number1=0 then
			set count=count+1;
			set sum=0;
		else 
			set count=count+1;
			set sum=0;
		end if;
	end while;
	set numbers=count;

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for output
-- ----------------------------
DROP PROCEDURE IF EXISTS `output`;
delimiter ;;
CREATE PROCEDURE `output`(in machineids BIGINT,out 发送字 int ,out 发送ID1 int,out 发送ID2 int,out 格子 int,out err varchar(255))
BEGIN
	#理片笼出片存储过程
	#查找有下片位置，并且判断跟上一次出库类型是否相同，相同则出库
	#找到最优的出片方法，判断下片是否还有位置，有的话就绑定当前流程卡跟落架号
	#查找符合出库的玻璃根据玻璃大小依次出片
   DECLARE task_id int;
	 DECLARE cur_id int;
	 
	CALL log_msg(@log_task,"[output]");
	 
	 select machine_type_id,work_id into @mtid,@work_groups from gmms_machine where id=machineids;
	
	select file_name into @engineering_id from gmms_raw_file where operator=@work_groups and state='已领取';
	
	
	 SET @rk=null;
		SET 发送字=0;
		SET @lastid=null;
		SET @lastpos=null;
	 select `value` into @lastid from gmms_var where varname='last_out_glass_id'and groups=@work_groups;
	 select `value` into @lastpos from gmms_var where varname='last_pos'and groups=@work_groups;
	 if @lastpos=null then 
	 SET 	@lastpos=2;
	 end if;
	 SET @processcardid=null;
	 #	判断同尺寸出库条件   去查找一个和最后一次磨边尺寸相同，并且可以下架的玻璃
	 if @lastid is not null then
		
	   	 CALL log_key_value(@log_task,"出库模式","同尺寸出库");
	 
			SELECT a.slicecage_rank,a.type_id,a.count into @rk,@type_id,@count from gmms_slicecage a INNER JOIN gmms_galss_task b on a.slicecage_rank=b.slicecage_rank where a.state='使用中' and a.count>0 and a.type_id=@lastid and a.gourp=@work_groups and a.flag=0 and engineering_id=@engineering_id and b.stop_position>0 order by a.count desc,rank, ABS(a.slicecage_rank-@lastpos) asc LIMIT 1;
			CALL log_key_value(@log_task,"出库位置",@rk);
			
			
			if @rk is not null then
					if @count=1 then
					select galss_id into @galss_id from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id;
					select @galss_id;
						SET 发送字=2;
						SET 格子=@rk;					
						SET 发送ID1=@galss_id;
						SET 发送ID2=0;
						CALL log_key_value(@log_task,"出一片",@rk);
						UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rk and engineering_id=@engineering_id;
						UPDATE gmms_slicecage SET state='出库中' where slicecage_rank=@rk and gourp=@work_groups;
					else
						select galss_id into @galss_id1 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id order by rank limit 0,1;
						select galss_id into @galss_id2 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id order by rank limit 1,1;
						SET 发送字=2;
						SET 格子=@rk;					
						SET 发送ID1=@galss_id1;
						SET 发送ID2=@galss_id2;
						CALL log_key_value(@log_task,"出二片",@rk);
						UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rk and engineering_id=@engineering_id;
						UPDATE gmms_slicecage SET state='出库中' where slicecage_rank=@rk and gourp=@work_groups;
					end if;
					
			end if;
	 end if;

		#   选定一个最符合出库条件的架子  1； 全部切割， 2. 剩余数量最少  3  优先有下片位置的
			if 发送字=0 then
			  
				#先使用 全切完的，这样能保证尽快下完架子
				SELECT 流程卡 into @processcardid from processcard_ex where 下片位置 is not null and 库内数>0  and 工程号=@engineering_id order by  总数-下架数 asc LIMIT 1;
				  	 CALL log_key_value(@log_task,"流程卡",@processcardid);
				select @engineering_id;
select @processcardid;
				IF @processcardid is  null then
				select 2;
					#  后使用  未完全切完的
					SELECT 流程卡 into @processcardid from processcard_ex where 下片位置 is not null and 库内数>0 and 工程号=@engineering_id order by 总数-下架数 asc LIMIT 1;	
				end if;

				-- 看看空架 有没有 ，如果有  就找一个库能玻璃最多的下架
				IF @processcardid is null then 
					select @processcardid;
					SET @drop_pos=null;
					SELECT id,stops into @drop_pos,@stops from gmms_drop_frame where state='空架' and groups=@work_groups limit 0,1;
					select @drop_pos;
					if @drop_pos  is not null then 
						select name1 into @name1 from namess where groups=@work_groups;
						select @name1;
						SELECT 流程卡,架号,总数,工程号 into @processcardid,@drop_frame,@processcard_count,@engineering_ids from processcard_ex where  (库内数>=@name1 or (库内数>0 and 总数<@name1)) and 工程号=@engineering_id order by  总数-库内数 LIMIT 1;
						#  流程卡占架子
						select @processcardid;
						if @processcardid is not null then 
							UPDATE gmms_drop_frame set state='使用中',drop_frame=@drop_frame,processcard_id=@processcardid,processcard_count=@processcard_count,engineering_id=@engineering_ids where id=@drop_pos;
							INSERT into hn_user(phone,idcard) VALUES(@processcardid,@work_groups);
							UPDATE gmms_galss_task set stop_position=@stops where processcard_id=@processcardid and engineering_id=@engineering_id;				
						end if;			
					end if;
				end if;
	   end if;
		 #    确定出库流程卡后  确定出库尺寸   条件   面积最大
	if @processcardid is not null then 
	  					SELECT @processcardid;	
							
		 #  选定大小和槽
		 SELECT  a.slicecage_rank,a.count,a.type_id into @rk,@count,@type_id from gmms_slicecage a inner join gmms_galss_task b on a.slicecage_rank=b.slicecage_rank where a.processcard_id=@processcardid and a.state='使用中' and a.flag=0 and a.gourp=@work_groups and engineering_id=@engineering_id order by b.width*b.height desc ,a.count DESC  limit 1;
		 select @count;
					if @count=1 then
						select galss_id into @galss_id from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id;
						SET 发送字=2;
						SET 格子=@rk;					
						SET 发送ID1=@galss_id;
						SET 发送ID2=0;
					else
						select galss_id into @galss_id1 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id order by rank limit 0,1;
						select galss_id into @galss_id2 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id order by rank limit 1,1;
						SET 发送字=2;
						SET 格子=@rk;					
						SET 发送ID1=@galss_id1;
						SET 发送ID2=@galss_id2;
					end if;
					UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rk and engineering_id=@engineering_id;
					UPDATE gmms_slicecage SET state='出库中' where slicecage_rank=@rk and gourp=@work_groups;
					UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
					UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups;
	end if;
	select @rk;
-- 	if 格子 is null or 发送ID1 is null or 发送ID2 is null then
-- 			
-- 			set  格子=0;
-- 			set  发送ID1=0;
-- 			set  发送ID2=0;
-- 	end if;
		
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for output2
-- ----------------------------
DROP PROCEDURE IF EXISTS `output2`;
delimiter ;;
CREATE PROCEDURE `output2`(in machineids BIGINT,out 发送字 int ,out 发送ID1 int,out 发送ID2 int,out 格子 int,out err varchar(255))
BEGIN
	#理片笼出片存储过程
	#查找有下片位置，并且判断跟上一次出库类型是否相同，相同则出库
	#找到最优的出片方法，判断下片是否还有位置，有的话就绑定当前流程卡跟落架号
	#查找符合出库的玻璃根据玻璃大小依次出片
   DECLARE task_id int;
	 DECLARE cur_id int;
	#Routine body goes here...
	 DECLARE gid long DEFAULT(null);
	 DECLARE tmp int default(null);
	 select machine_type_id,work_id into @mtid,@work_groups from gmms_machine where id=machineids;	
	 select file_name into @engineering_id from gmms_raw_file where operator=@work_groups and state='已领取';
	 
	 select `value` into @lastid from gmms_var where varname='last_out_glass_id'and groups=@work_groups;
	 select `value` into @lastpos from gmms_var where varname='last_pos'and groups=@work_groups;

	
			 set @count=null;
			 set 发送字=0;
	 
	 #更新出库锁（出库锁用来控制出笼玻璃范围）
	if  position("P" in @engineering_id)>0 then
			 
	 call output2_update_suo(@engineering_id,false,@work_groups);
			 
	end if;	 
			 
			 
	 
	 
	 
	 
	 # 解锁状态1同规格(顺序落架)
		select @lastpos,@lastid;
		set @rk=null;
		set @lck=null;
		SELECT a.slicecage_rank,a.type_id,a.count into @rk,@type_id,@count from gmms_slicecage a INNER JOIN gmms_galss_task b on a.slicecage_rank=b.slicecage_rank where a.state='使用中' and a.count>0 and a.gourp=@work_groups and a.flag=0 and engineering_id=@engineering_id and b.stop_position>0
					and b.gc_height=1
					 order by a.count desc,rank, ABS(a.slicecage_rank-@lastpos) asc LIMIT 1;	

  #  解锁状态2同规格(无序落架)
		select @rk;
	if @rk is null then 

		set @rk=null;
		set @lck=null;
		SELECT a.slicecage_rank,a.type_id,a.count into @rk,@type_id,@count from gmms_slicecage a INNER JOIN gmms_galss_task b on a.slicecage_rank=b.slicecage_rank where a.state='使用中' and a.count>0  and a.gourp=@work_groups and a.flag=0 and engineering_id=@engineering_id and b.stop_position>0
					and b.gc_height>0
					 order by a.count desc,rank, ABS(a.slicecage_rank-@lastpos) asc LIMIT 1;	
	end if;
		
		
		#寻找合适的流程卡
	if @rk is null then 

				set @processcardid=null;
				SELECT 流程卡 into @processcardid from processcard_ex inner join gmms_galss_task b on processcard_ex.流程卡=b.processcard_id where 下片位置 is not null and 库内数>0  and 工程号=@engineering_id and b.gc_height<>0 and b.stop_position>0  and b.work_procedure='理片' and b.work_state='已进库' order by  总数-下架数 asc LIMIT 1;
				 CALL log_key_value(@log_task,"流程卡",@processcardid);
				

		set @countslieceage=0;
		set @countstop=0;	
		set @countsumgalss=0;		
		select count(*) into @countslieceage from  gmms_slicecage where gourp=@work_groups and flag=0 and slicecage_rank<>1 and `state`='空闲';		
		SELECT count(*) into @countstop from gmms_drop_frame where (state='空架' or state='空闲') and groups=@work_groups;
		select count(*) into @countsumgalss from gmms_galss_task where engineering_id=@engineering_id;
		if @countstop=5 or @countsumgalss<75 then
		
			if @countslieceage<=5  then
			
				IF @processcardid is null then 
					select @processcardid;
					SET @drop_pos=null;
			
					SELECT id,stops into @drop_pos,@stops from gmms_drop_frame where state='空架' and groups=@work_groups limit 0,1;
					select @drop_pos;
					if @drop_pos  is not null then 
						select name1 into @name1 from namess where groups=@work_groups;
						select @name1;
						SELECT 流程卡,架号,总数,工程号 into @processcardid,@drop_frame,@processcard_count,@engineering_ids from processcard_ex  inner join	gmms_galss_task b on processcard_ex.流程卡=b.processcard_id where b.gc_height>0 and b.work_procedure='理片' and b.work_state='已进库'  and 下片位置 is null and 库内数>0 and 工程号=@engineering_id order by  库内数 desc,总数-库内数 LIMIT 1;
						#  流程卡占架子
						select @processcardid;
						if @processcardid is not null then 
							UPDATE gmms_drop_frame set state='使用中',drop_frame=@drop_frame,processcard_id=@processcardid,processcard_count=@processcard_count,engineering_id=@engineering_ids where id=@drop_pos;
							INSERT into hn_user(phone,idcard) VALUES(@processcardid,@work_groups);
							UPDATE gmms_galss_task set stop_position=@stops where processcard_id=@processcardid and engineering_id=@engineering_id;				
						end if;			
					end if;
				end if;
				
			end if;
			
		else
			IF @processcardid is null then 
					select @processcardid;
					SET @drop_pos=null;
			
					SELECT id,stops into @drop_pos,@stops from gmms_drop_frame where state='空架' and groups=@work_groups limit 0,1;
					select @drop_pos;
					if @drop_pos  is not null then 
						select name1 into @name1 from namess where groups=@work_groups;
						select @name1;
						SELECT 流程卡,架号,总数,工程号 into @processcardid,@drop_frame,@processcard_count,@engineering_ids from processcard_ex  inner join	gmms_galss_task b on processcard_ex.流程卡=b.processcard_id where b.gc_height>0 and b.work_procedure='理片' and b.work_state='已进库'  and 下片位置 is null and 库内数>0 and 工程号=@engineering_id order by   库内数 desc,总数-库内数 LIMIT 1;
						#  流程卡占架子
						select @processcardid;
						if @processcardid is not null then 
							UPDATE gmms_drop_frame set state='使用中',drop_frame=@drop_frame,processcard_id=@processcardid,processcard_count=@processcard_count,engineering_id=@engineering_ids where id=@drop_pos;
							INSERT into hn_user(phone,idcard) VALUES(@processcardid,@work_groups);
							UPDATE gmms_galss_task set stop_position=@stops where processcard_id=@processcardid and engineering_id=@engineering_id;				
						end if;			
					end if;
				end if;
		end if;
	
	end if;
		
	if @processcardid is not null then 		
				 SELECT  a.slicecage_rank,a.count,a.type_id into @rk,@count,@type_id from gmms_slicecage a inner join gmms_galss_task b on a.slicecage_rank=b.slicecage_rank where
			b.gc_height>0	and 
					a.processcard_id=@processcardid and a.state='使用中' and a.flag=0 and a.gourp=@work_groups and engineering_id=@engineering_id order by b.width*b.height desc ,a.count DESC  limit 1;
	end if;
		
		
		if @rk is not null then 
			if @count=1 then
						select galss_id into @galss_id from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id;
						SET 发送字=2;
						SET 格子=@rk;					
						SET 发送ID1=@galss_id;
						SET 发送ID2=0;
					else
						select galss_id into @galss_id1 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id order by rank limit 0,1;
						select galss_id into @galss_id2 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id order by rank limit 1,1;
						SET 发送字=2;
						SET 格子=@rk;					
						SET 发送ID1=@galss_id1;
						SET 发送ID2=@galss_id2;
					end if;
					UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rk and engineering_id=@engineering_id;
					UPDATE gmms_slicecage SET state='出库中' where slicecage_rank=@rk and gourp=@work_groups;
					UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
					UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups; 

		end if;
		#爆笼检测
		select 发送字;
		if 发送字=0 then
		set @count=null;
		select count(*) into @count from  gmms_slicecage where gourp=@work_groups and flag=0 and slicecage_rank<>1 and `state`='空闲';
		SELECT count(*) into @countstop2 from gmms_drop_frame where state='空架' and groups=@work_groups;

		if @count=0 and @countstop2>0 then 
		  call output2_update_suo(@engineering_id,true,@work_groups);

		end if;
		end if;





-- 	#理片笼出片存储过程
-- 	#查找有下片位置，并且判断跟上一次出库类型是否相同，相同则出库
-- 	#找到最优的出片方法，判断下片是否还有位置，有的话就绑定当前流程卡跟落架号
-- 	#查找符合出库的玻璃根据玻璃大小依次出片
--    DECLARE task_id int;
-- 	 DECLARE cur_id int;
-- 	#Routine body goes here...
-- 	 DECLARE gid long DEFAULT(null);
-- 	 DECLARE tmp int default(null);
-- 	 select machine_type_id,work_id into @mtid,@work_groups from gmms_machine where id=machineids;	
-- 	 select file_name into @engineering_id from gmms_raw_file where operator=@work_groups and state='已领取';
-- 	 
-- 	 select `value` into @lastid from gmms_var where varname='last_out_glass_id'and groups=@work_groups;
-- 	 select `value` into @lastpos from gmms_var where varname='last_pos'and groups=@work_groups;
-- 
-- 	
-- 			 set @count=null;
-- 			 set 发送字=0;
-- 	 
-- 	 #更新出库锁（出库锁用来控制出笼玻璃范围）
-- 	if  position("P" in @engineering_id)>0 then
-- 			 
-- 	 call output2_update_suo(@engineering_id,false,@work_groups);
-- 			 
-- 	end if;	 
-- 			 
-- 			 
-- 	 
-- 	 
-- 	 
-- 	 
-- 	 # 解锁状态1同规格(顺序落架)
-- select @lastpos,@lastid;
-- 			set @rk=null;
-- 			set @lck=null;
-- 					SELECT a.slicecage_rank,a.type_id,a.count into @rk,@type_id,@count from gmms_slicecage a INNER JOIN gmms_galss_task b on a.slicecage_rank=b.slicecage_rank where a.state='使用中' and a.count>0 and a.gourp=@work_groups and a.flag=0 and engineering_id=@engineering_id and b.stop_position>0
-- 					and b.gc_height=1
-- 					 order by a.count desc,rank, ABS(a.slicecage_rank-@lastpos) asc LIMIT 1;	
-- 
--   #  解锁状态2同规格(无序落架)
-- 	select @rk;
-- 	  if @rk is null then 
-- 
-- 			set @rk=null;
-- 			set @lck=null;
-- 					SELECT a.slicecage_rank,a.type_id,a.count into @rk,@type_id,@count from gmms_slicecage a INNER JOIN gmms_galss_task b on a.slicecage_rank=b.slicecage_rank where a.state='使用中' and a.count>0  and a.gourp=@work_groups and a.flag=0 and engineering_id=@engineering_id and b.stop_position>0
-- 					and b.gc_height>0
-- 					 order by a.count desc,rank, ABS(a.slicecage_rank-@lastpos) asc LIMIT 1;	
-- 		end if;
-- 		
-- 		
-- 		#寻找合适的流程卡
-- 		if @rk is null then 
-- 
-- 		set @processcardid=null;
-- 						SELECT 流程卡 into @processcardid from processcard_ex 
-- 						inner join 
-- 						gmms_galss_task b on processcard_ex.流程卡=b.processcard_id
-- 						where 下片位置 is not null and 库内数>0  and 工程号=@engineering_id 
-- 						and b.gc_height<>0  and b.work_procedure='理片' and b.work_state='已进库'
-- 						order by  总数-下架数 asc LIMIT 1;
-- 				  	 CALL log_key_value(@log_task,"流程卡",@processcardid);
-- 				
-- 
-- 
-- 
-- 				IF @processcardid is  null then
-- 				select 2;
-- 					#  后使用  未完全切完的
-- 					SELECT 流程卡 into @processcardid from processcard_ex
-- 					inner join 
-- 						gmms_galss_task b on processcard_ex.流程卡=b.processcard_id		
-- 					 where b.gc_height<>0 and b.work_procedure='理片' and b.work_state='已进库' and 下片位置 is not null and 库内数>0 and 工程号=@engineering_id order by 总数-下架数 asc LIMIT 1;	
-- 				end if;
-- 				
-- 				
-- 					IF @processcardid is null then 
-- 					select @processcardid;
-- 					SET @drop_pos=null;
-- 			
-- 					SELECT id,stops into @drop_pos,@stops from gmms_drop_frame where state='空架' and groups=@work_groups limit 0,1;
-- 					select @drop_pos;
-- 					if @drop_pos  is not null then 
-- 						select name1 into @name1 from namess where groups=@work_groups;
-- 						select @name1;
-- 						SELECT 流程卡,架号,总数,工程号 into @processcardid,@drop_frame,@processcard_count,@engineering_ids from processcard_ex  inner join	gmms_galss_task b on processcard_ex.流程卡=b.processcard_id where b.gc_height>0 and b.work_procedure='理片' and b.work_state='已进库'  and 下片位置 is null and 库内数>0 and 工程号=@engineering_id order by  总数-库内数 LIMIT 1;
-- 						#  流程卡占架子
-- 						select @processcardid;
-- 						if @processcardid is not null then 
-- 							UPDATE gmms_drop_frame set state='使用中',drop_frame=@drop_frame,processcard_id=@processcardid,processcard_count=@processcard_count,engineering_id=@engineering_ids where id=@drop_pos;
-- 							INSERT into hn_user(phone,idcard) VALUES(@processcardid,@work_groups);
-- 							UPDATE gmms_galss_task set stop_position=@stops where processcard_id=@processcardid and engineering_id=@engineering_id;				
-- 						end if;			
-- 					end if;
-- 				end if;
-- 	   end if;
-- 		
-- 	if @processcardid is not null then 		
-- 				 SELECT  a.slicecage_rank,a.count,a.type_id into @rk,@count,@type_id from gmms_slicecage a inner join gmms_galss_task b on a.slicecage_rank=b.slicecage_rank where
-- 			b.gc_height>0	and 
-- 					a.processcard_id=@processcardid and a.state='使用中' and a.flag=0 and a.gourp=@work_groups and engineering_id=@engineering_id order by b.width*b.height desc ,a.count DESC  limit 1;
-- 	end if;
-- 		
-- 		
-- 		if @rk is not null then 
-- 			if @count=1 then
-- 						select galss_id into @galss_id from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id;
-- 						SET 发送字=2;
-- 						SET 格子=@rk;					
-- 						SET 发送ID1=@galss_id;
-- 						SET 发送ID2=0;
-- 					else
-- 						select galss_id into @galss_id1 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id order by rank limit 0,1;
-- 						select galss_id into @galss_id2 from gmms_galss_task where slicecage_rank=@rk and engineering_id=@engineering_id order by rank limit 1,1;
-- 						SET 发送字=2;
-- 						SET 格子=@rk;					
-- 						SET 发送ID1=@galss_id1;
-- 						SET 发送ID2=@galss_id2;
-- 					end if;
-- 					UPDATE gmms_galss_task SET work_state='出库中' where slicecage_rank=@rk and engineering_id=@engineering_id;
-- 					UPDATE gmms_slicecage SET state='出库中' where slicecage_rank=@rk and gourp=@work_groups;
-- 					UPDATE gmms_var SET Value=@rk where varname='last_pos' and groups=@work_groups;
-- 					UPDATE gmms_var SET Value=@type_id where varname='last_out_glass_id' and groups=@work_groups; 
-- 
-- 		end if;
-- 		#爆笼检测
-- 		select 发送字;
-- 		if 发送字=0 then
-- 		set @count=null;
-- 		select count(*) into @count from  gmms_slicecage where gourp=@work_groups and flag=0 and slicecage_rank<>1 and `state`='空闲';
-- 		if @count=0 then 
-- 		  call output2_update_suo(@engineering_id,true,@work_groups);
-- 		end if;
-- 		end if;
-- -- 		if 格子 is null or 发送ID1 is null or 发送ID2 is null then
-- -- 
-- -- 			
-- -- 			set  格子=0;
-- -- 			set  发送ID1=0;
-- -- 			set  发送ID2=0;
-- -- 		end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for output2_update_suo
-- ----------------------------
DROP PROCEDURE IF EXISTS `output2_update_suo`;
delimiter ;;
CREATE PROCEDURE `output2_update_suo`(in _gongcheng varchar(50), baolong int,in groups int(50))
BEGIN
	#Routine body goes here...
  
	DECLARE lck  varchar(50);
	DECLARE st  int;
	DECLARE max_h  int;
	DECLARE cur_h  int;
	DECLARE _suo   INT;
	DECLARE _break int ;
	DECLARE _flags int ;
	DECLARE cur cursor for
	SELECT  liuchengka,
state,
maxxuhao,
currentxuhao,
suo
  from liuchengka_output where gongcheng=_gongcheng;
	DECLARE CONTINUE HANDLER FOR NOT FOUND  SET _break=1;
	
	set _break=0;
	if baolong=false then
	open cur;
	while _break=0 do 
	   fetch cur into lck,st,max_h,cur_h,_suo;
		if _break=0 then 
		
		if st=1 then 
			set @zcount=null;
			
		    SELECT  count(*) into  @zcount	 from gmms_galss_task 
				where processcard_id=lck and gc_width=cur_h and work_state<>'破损' and work_state<>'已出库' and (work_procedure='测量' or work_procedure='理片');
				
				if @zcount=0 then 
				     if cur_h<max_h then 
						    set cur_h=cur_h+1;
								update liuchengka_output set currentxuhao=cur_h where liuchengka=lck;
								update gmms_galss_task set gc_height=1 where processcard_id=lck and  gc_width=cur_h;
						 end if;
				end if;
		end if;			
		end if;	
	 
	end while;
	close cur;
	
   else
	 set @lck=null;

select 	a.lck into @lck from 
     (SELECT 
count(case when a.gc_width=b.currentxuhao then 1 else null end) as c1,
count(*) as c2,a.processcard_id as lck
 from gmms_galss_task a inner join liuchengka_output b on a.processcard_id=b.liuchengka
where a.work_procedure='理片' and a.work_state='已进库' and a.engineering_id=_gongcheng and b.State=1) a
where c2>0
order by  c2-c1 desc LIMIT 1;
		set @counts=0;
		select count(*) into @counts from gmms_galss_task where engineering_id=_gongcheng and work_procedure='理片' and work_state='已进库' and gc_height=2;
	  if @lck is not null and @counts=0 then
		
		  update gmms_galss_task set gc_height=2 where processcard_id=@lck;
		  update liuchengka_output set `state`=2 where liuchengka=@lck;
			insert into gmms_slicecage_details (processcard_id,groups,time) values(@lck,groups,now());
		end if;
	end if;
	
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for output_create
-- ----------------------------
DROP PROCEDURE IF EXISTS `output_create`;
delimiter ;;
CREATE PROCEDURE `output_create`(in gc varchar(50))
BEGIN
	#Routine body goes here...
    
	 DECLARE w double ;
	 declare h double;
	 declare xh int;
	 declare c int;
	 declare lck varchar(50);
		declare _break int default(0);

		declare nxh int;
		declare lastlck varchar(50);
		declare lw double;
		declare lh double;
		declare tmp int;
		declare countsss int;
		
		
			declare o_err int default(0);
		
		DECLARE cur CURSOR FOR
		SELECT edg_width,edg_height,cast(edg_speed2 as signed) as xuhao,count(*),processcard_id FROM gmms_galss_task where engineering_id=gc group by processcard_id , edg_speed2,processcard_id
	 ,edg_width,edg_height order by processcard_id,cast(edg_speed2 as signed);
	 DECLARE  CONTINUE HANDLER FOR NOT FOUND SET _break=1;
	 set _break=0;

set o_err =1;

	 SELECT edg_width,edg_height,cast(edg_speed2 as signed) as xuhao,count(*),processcard_id FROM gmms_galss_task where engineering_id=gc group by processcard_id , edg_speed2,processcard_id
	 ,edg_width,edg_height order by processcard_id,cast(edg_speed2 as signed);
	 
	 set o_err =2;
	 
	 open cur;
 	 set nxh=0;
	 set tmp=0;
	 select count(*) into tmp from liuchengka_output where gongcheng=gc; 
	 if tmp>0 then 
	   set _break=1;
	 end if;
	 set countsss=0;
	 while _break=0 do
	 
	 set o_err =3;
	 
	    FETCH cur into w,h,xh,c,lck;	
			
			select o_err;
			
			if _break=0 then
				-- cselect 1;
				if nxh=0 then 
				
				set o_err =4;
				
					set nxh=1;
					set lastlck=lck;
					set lw=w;
					set lh=h;
					update gmms_galss_task set gc_width=nxh where processcard_id=lck  and edg_speed2=xh;
					select o_err;
					
				else			
								 set o_err =5;
							
					if lastlck=lck then 
					
						 set o_err =6;
					
						if lw=w and lh=lh then 
						set o_err =31;
						
							update gmms_galss_task set gc_width=nxh where processcard_id=lck  and edg_speed2=xh;
						else
						set o_err =32;
							set nxh=nxh+1;
							set   lw=w;
							set   lh=h;
							update gmms_galss_task set gc_width=nxh where processcard_id=lck  and edg_speed2=xh;											 
						end if;
						select o_err;
					else	
						#	INSERT INTO liuchengka_output 
						set tmp=1;
						if nxh>3 then 
							set	tmp=2;
						end if;
set o_err=33;
-- select CONCAT(lastlck,',0;',tmp,',1;',gc,',2:',nxh,',0,3;',w,',4:',h,',0');
						INSERT INTO `gmms`.`liuchengka_output`(`liuchengka`, `guige`, `state`, `gongcheng`, `maxxuhao`, `currentxuhao`, `width`, `height`, `suo`) VALUES (lastlck,NULL,tmp,gc,nxh,0,w,h,0);
						select o_err;	
							set o_err =7;
						
						if tmp=1 then 
							set	tmp=0;
						end if;
						update gmms_galss_task set gc_height=tmp where processcard_id=lastlck;
									
						set nxh=1;
						set lastlck=lck;
						set lw=w;
						set lh=h;
						update gmms_galss_task set gc_width=nxh where processcard_id=lck  and edg_speed2=xh;
					
					end if;			select o_err;
					
				end if;
			else
				#结束
					 set o_err =8;
				if nxh<>0 then 
					set tmp=1;
					if nxh>3 then 
						set	tmp=2;
					end if;
					if (SELECT count(*) from liuchengka_output where liuchengka=lastlck)=0 then
						INSERT INTO `gmms`.`liuchengka_output`(`liuchengka`, `guige`, `state`, `gongcheng`, `maxxuhao`, `currentxuhao`, `width`, `height`, `suo`) VALUES (lastlck,NULL,tmp,gc,nxh,0,w,h,0);
				  end if;
					if tmp=1 then 
						set	tmp=0;
					end if;
					update gmms_galss_task set gc_height=tmp where processcard_id=lastlck;				 					 					 
				end if;
				
			end if;
				select o_err;
				 set o_err =9;
	 end while;
	 close cur;
	 select o_err;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for PendulumPieceFinsh_2
-- ----------------------------
DROP PROCEDURE IF EXISTS `PendulumPieceFinsh_2`;
delimiter ;;
CREATE PROCEDURE `PendulumPieceFinsh_2`(in sendMessageHex varchar(255),in machineids BIGINT,in returnvalue varchar(255),in flag int,out messagedate varchar(255))
BEGIN
	#下片机完成存储过程
	#找到正在工作的任务进行完成
	DECLARE count1 int;
	DECLARE dates varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	set messagedate=messagedates;
	
	select machine_type_id,work_id into @mtid,@work_groups from gmms_machine where id=machineids;
	
	
	select count(*),galss_id,pendulum_piece,grouporder,furnace into @counts,@galssid,@pendulum_piece,@grouporder,@furnace from gmms_galss_task_2 gt  where gt.groups=@work_groups and  gt.work_state='正在工作' and work_procedure='钢化前摆片' and gt.orders is not null ORDER BY gt.orders limit 0,1;
	#原句
-- 	select count(*),galss_id,pendulum_piece,grouporder,furnace into @counts,@galssid,@pendulum_piece,@grouporder,@furnace from gmms_galss_task_2 gt  where  gt.work_state='正在工作' and work_procedure='钢化前摆片' and gt.orders is not null ORDER BY gt.orders limit 0,1;
	
	
	
	select @counts;
	if machineids=40 or machineids=65 then
		#任务完成
		if flag=1 then
			if @counts>0 then
			  if @pendulum_piece=1 then
				  update gmms_galss_task_2 set work_state='已完成' where galss_id=@galssid and work_procedure='钢化前摆片' and work_state!='破损' and groups=@work_groups;
			  elseif @pendulum_piece=2 then
				  update gmms_galss_task_2 set work_state='等待中',work_procedure='钢化' where grouporder=@grouporder and furnace=@furnace and work_procedure='钢化前摆片' and work_state!='破损' and groups=@work_groups;
			  elseif @pendulum_piece=3 then
			  
					update gmms_galss_task_2 set work_state='正在工作',work_procedure='钢化' where  furnace=@furnace and (work_procedure='钢化前摆片' or work_procedure='钢化') and work_state!='破损' and groups=@work_groups;
				 
					call message(returnvalue,'0014',@date);
					set messagedate=@date;
				end if;			
			else
				call message(returnvalue,'0014',@date);
				set messagedate=@date;
			end if;
		#任务未完成
		elseif flag=2 then
		
			if @counts>0 then
			
				update gmms_galss_task_2 set work_state='等待中' where galss_id=@galssid and work_procedure='钢化前摆片' and groups=@work_groups;
				call message(returnvalue,'0014',@date);
				set messagedate=@date;
		
			end if;
			#任务破损
		elseif flag=3 then
		
			if @counts>0 then
			
				update gmms_galss_task_2 set work_state='破损' where galss_id=@galssid and work_procedure='钢化前摆片' and groups=@work_groups;
				call message(returnvalue,'0014',@date);
				set messagedate=@date;
		
			end if;
		
		end if;
	elseif machineids=42 or machineids=66 then
		select count(*),grouporder,furnace into @counts2,@grouporder,@furnace from gmms_galss_task_2 gt  where  gt.work_state='正在工作' and work_procedure='钢化后摆片' and groups=@work_groups limit 0,1;
		#任务完成
		if flag=1 then
		
			if @counts2>0 then
			 
				update gmms_galss_task_2 set work_state='待复合',work_procedure='钢化后复合' where grouporder=@grouporder and work_state='正在工作' and work_procedure='钢化后摆片' and groups=@work_groups;
				call message(returnvalue,'0014',@date);
				set messagedate=@date;
				insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,@work_groups,NOW(),'摆片完成','',messagedate);
			end if;
		#任务破损
		elseif flag=2 then
		
			if @counts2>0 then
			
				update gmms_galss_task_2 set work_state='已完成',work_procedure='钢化' where grouporder=@grouporder and work_state='正在工作' and work_procedure='钢化后摆片' and groups=@work_groups;
				call message(returnvalue,'0014',@date);
				set messagedate=@date;
		
			end if;
			
		elseif flag=3 then
		
			if @counts2>0 then
			
				update gmms_galss_task_2 set work_state='破损' where grouporder=@grouporder and work_state='正在工作' and work_procedure='钢化后摆片' and groups=@work_groups;
				call message(returnvalue,'0014',@date);
				set messagedate=@date;
		
			end if;
		end if;
	end if;

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for PendulumPieceSend_2
-- ----------------------------
DROP PROCEDURE IF EXISTS `PendulumPieceSend_2`;
delimiter ;;
CREATE PROCEDURE `PendulumPieceSend_2`(in machineids BIGINT,in returnvalue varchar(255),in rankid BIGINT,out messagedate varchar(255))
BEGIN
	#下片机发送任务存储过程
	#找到等待中的任务，把下片位置，宽度，高度，厚度发送给plc
	DECLARE s int DEFAULT 0;
	DECLARE dates1 varchar(255) DEFAULT '';
	DECLARE dates varchar(255);
	DECLARE isjing varchar(255) DEFAULT null;
	DECLARE messagedates varchar(255) DEFAULT '';
	set messagedate=messagedates;
	
	select machine_type_id,work_id,state into @mtid,@work_groups,isjing from gmms_machine where id=machineids;
	
	set @furnace2=null;
	select count(*),galss_id,pendulum_piece,distance,grouporder,furnace,edg_width,edg_height,angle
	into @counts,@galssid,@pendulum_piece,@distance,@grouporder,@furnace,@width,@height,@angle from gmms_galss_task_2 gt  
	where  gt.work_state='等待中' and gt.work_procedure='钢化前摆片' and gt.groups=@work_groups and gt.orders is not null and gt.galss_id=rankid ORDER BY gt.orders limit 1;
	
	select galss_id,pendulum_piece,distance,grouporder,furnace 
	into @galssid2,@pendulum_piece2,@distance2,@grouporder2,@furnace2 from gmms_galss_task_2 gt  
	where  ((gt.work_state='已完成' and gt.work_procedure='钢化前摆片') or (gt.work_state='等待中' and gt.work_procedure='钢化')) 
	and gt.groups=@work_groups and gt.orders is not null ORDER BY furnace,gt.orders desc	limit 1;
	
	
	select galss_id,pendulum_piece,distance,grouporder,furnace 
	into @galssid3,@pendulum_piece3,@distance3,@grouporder3,@furnace3 from gmms_galss_task_2 gt  
	where gt.work_state='已完成' and gt.work_procedure='钢化前摆片' 
	and gt.groups=@work_groups and gt.orders is not null ORDER BY furnace,gt.orders desc limit 1;
	#select count(*) into @counts2 from gmms_galss_task_2 gt  where  ((gt.work_state='已完成' and gt.work_procedure='钢化前摆片') or (gt.work_state='等待中' and gt.work_procedure='钢化')) and gt.groups=@work_groups and gt.orders is not null ORDER BY furnace,gt.orders desc	;
		
	
	#select count(*) into @counts3 from gmms_galss_task_2 gt  where  gt.work_state='已完成' and gt.work_procedure='钢化前摆片' and gt.groups=@work_groups and gt.orders is not null;
	

	select @counts;
	select @counts2;
	#判断是否有等待的任务
	if machineids=40 or machineids=65 then
	
	
	set messagedate=null;
	
	
		if @counts=1 then
			
			
		  set @is_timing=true;
			
			if @pendulum_piece=3 then
			  #select count(*) into @count5 from gmms_galss_task_2 where  work_procedure='钢化' and (work_state='已完成' or work_state='正在工作');
					if isjing='正常' then
						set @timer_name="ganghua_2";
						set @timeinterval=16;#每一片的时间  默认22秒   15片以上25秒
						if @work_groups=1 then
							set @timer_name="ganghua_1";
						end if;
						select @timer_name;
						call timer_ontime(@timer_name, @is_timing);
						if @is_timing then
							select count(*) into @t_count from gmms_galss_task_2 where groups=@work_groups and furnace=@furnace;
							if @t_count>16 then
								set @timeinterval=17;
							end if;
							set @z_time_out=@t_count*@timeinterval+40;
							call timer_set(@timer_name,@z_time_out);
						end if;
					else
						set @is_timing=false;
					end if;		
		   end if;
		  select @is_timing;
			if @is_timing then
				if @furnace=@furnace2 or @furnace2 is null then
					#上一片和当前片 属于同一炉
					if @grouporder=@grouporder2 or @furnace2 is null or @galssid3 is null then
						#上一片和当前片 属于同排 或此炉第一片   或摆片台无玻璃 
						if (@pendulum_piece=3 or @pendulum_piece=2) then
							if @work_groups=2 then
								if (select count(*) from gmms_galss_task_2 where furnace=@furnace and grouporder=@grouporder and groups=2)>1 then
									#此排玻璃数量>1时   发送除了最后一片外的总长度
									set @distance=(select SUM(distance) from gmms_galss_task_2 
									where furnace=@furnace and grouporder=@grouporder and groups=2 and pendulum_piece<2);
								else
									#此排玻璃数量=1时   发送最后一片-玻璃长度
									if @angle=0 then
										set @distance=@distance-@width+20;
									elseif @angle=1 then
										set @distance=@distance-@height+20;
									end if;
								end if;
							elseif @work_groups=1 then
								if @angle=0 then
									set @distance=@width+20;
								elseif @angle=1 then
									set @distance=@height+20;
								end if;
							end if;
																			
						end if;
						update gmms_galss_task_2 set work_state='正在工作' 
						where work_state='等待中' and work_procedure='钢化前摆片' and groups=@work_groups and galss_id=rankid order by orders limit 1;						
						call intTo2ByteHex10_16_4(@pendulum_piece,@pendulum_pieces);
						call intTo2ByteHex10_16_4(@distance,@distances);
						call intTo2ByteHex10_16_4(@galssid,@galssids);
						set dates=CONCAT(returnvalue,'0000','00000000',@pendulum_pieces,@distances);
						call message(dates,'000a',@date);
						set messagedate=@date;
						insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,@work_groups,NOW(),CONCAT('前摆片发1:',@distance),@galssid,@pendulum_pieces);
					elseif @grouporder!=@grouporder2 and @galssid3 is not null then						
							#上一片和当前片 属于不同排 且摆片台上有玻璃   则进炉   4 送入  5 进炉		
							if @work_groups=2 then
									set @distance=(select SUM(distance) from gmms_galss_task_2 
														where furnace=@furnace and grouporder=@grouporder2 and groups=2 and pendulum_piece<2);
									set @pendulum_piece=4;	
							elseif @work_groups=1 then 
								set @distance=0;
								set @pendulum_piece=2;
							end if;	
							update gmms_galss_task_2 set work_state='等待中',work_procedure='钢化' 
							where work_state='已完成' and work_procedure='钢化前摆片' and grouporder=@grouporder2 and groups=@work_groups ;
							call intTo2ByteHex10_16_4(@pendulum_piece,@pendulum_pieces);
							call intTo2ByteHex10_16_4(@distance,@distances);
							call intTo2ByteHex10_16_4(@galssid,@galssids);
							set dates=CONCAT(returnvalue,'0000','00000000',@pendulum_pieces,@distances);
							call message(dates,'000a',@date);
							set messagedate=@date;
							insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,@work_groups,NOW(),CONCAT('前摆片发2:',@distance),@galssid,@pendulum_pieces);
					end if;
				else
					#select furnace into @furnace3 from gmms_galss_task_2 where furnace<@furnace2 and groups=@work_groups group by furnace order by furnace desc limit 0,1;
					if @furnace2 is not null and isjing='正常' then
						if @work_groups=2 and @grouporder2 is not null then
							#二线 当前片为新一炉   有上一炉玻璃未进炉
							set @distance=(select IFNULL(SUM(distance),0) from gmms_galss_task_2 
														where furnace=@furnace2 and grouporder=@grouporder2 and groups=2 and pendulum_piece<2 and work_procedure='钢化前摆片' and work_state='已完成');
							set @pendulum_piece=5;
						elseif @work_groups=1 then 
							set @distance=0;
							set @pendulum_piece=3;
						end if;	
						update gmms_galss_task_2 set work_state='正在工作',work_procedure='钢化' where  furnace=@furnace2 and groups=@work_groups and work_state!='破损';  
						call intTo2ByteHex10_16_4(@pendulum_piece,@pendulum_pieces);
						call intTo2ByteHex10_16_4(@distance,@distances);
						call intTo2ByteHex10_16_4(@galssid,@galssids);


						set dates=CONCAT(returnvalue,'0000','00000000',@pendulum_pieces,@distances);
						call message(dates,'000a',@date);
						set messagedate=@date;
						insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,@work_groups,NOW(),CONCAT('前摆片发3:',@distance),@galssid,@pendulum_pieces);
					end if;
					
				end if;
		
			
		end if;
		
		end if;
	elseif  machineids=42 or machineids=66 then 
		select grouporder,furnace into @grouporder,@furnace from gmms_galss_task_2 gt 
		where  gt.work_state='已完成' and work_procedure='钢化' and groups=@work_groups  ORDER BY gt.grouporder  limit 0,1;
		select @grouporder;
		select count(*) into @count2 from gmms_galss_task_2 where grouporder=@grouporder and work_state='已完成' and work_procedure='钢化' and groups=@work_groups;
		select @count2;
		if @count2>0 then
			while s<@count2 do
			#原句：玻璃数据同行完全相反 select galss_id into @galss_id from gmms_galss_task_2 where grouporder=@grouporder and work_state='已完成' and work_procedure='钢化' and groups=@work_groups order by orders desc limit s,1;
			select galss_id into @galss_id from gmms_galss_task_2 where grouporder=@grouporder and work_state='已完成' and work_procedure='钢化' and groups=@work_groups order by orders asc limit s,1;
-- 			if @work_groups=2 then
-- 				select galss_id into @galss_id from gmms_galss_task_2 where grouporder=@grouporder and work_state='已完成' and work_procedure='钢化' and groups=@work_groups order by orders desc limit s,1;
-- 			end if;
			set @maxrank=0;
			
			select max(fine_throwing) into @maxrank from gmms_galss_task_2 where furnace=@furnace and groups=@work_groups;
			update gmms_galss_task_2 set fine_throwing=@maxrank+1,slicecage_flag=@maxrank+1 where galss_id=@galss_id and grouporder=@grouporder and work_state='已完成' and work_procedure='钢化' and groups=@work_groups;
			
			select @galss_id;
			call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
			set dates1=CONCAT(dates1,@galss_ids);
			set s=s+1;
		end while;
		update gmms_galss_task_2 set work_state='正在工作',work_procedure='钢化后摆片' where grouporder=@grouporder and work_state='已完成' and work_procedure='钢化' and groups=@work_groups;
		 
		select dates1;
		set dates=CONCAT(returnvalue,dates1);
		select dates;
		call message(dates,'000a',@date);
		set messagedate=@date;
		insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,@work_groups,NOW(),'摆片发送',@galss_id,messagedate);
		end if;
		
	end if;

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for PendulumPieceSend_2_copy1
-- ----------------------------
DROP PROCEDURE IF EXISTS `PendulumPieceSend_2_copy1`;
delimiter ;;
CREATE PROCEDURE `PendulumPieceSend_2_copy1`(in machineids BIGINT,in returnvalue varchar(255),in rankid BIGINT,out messagedate varchar(255))
BEGIN
	#下片机发送任务存储过程
	#找到等待中的任务，把下片位置，宽度，高度，厚度发送给plc
	DECLARE s int DEFAULT 0;
	DECLARE dates1 varchar(255) DEFAULT '';
	DECLARE dates varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	set messagedate=messagedates;
	
	select machine_type_id,work_id into @mtid,@work_groups from gmms_machine where id=machineids;
	
	set @furnace2=null;
	select count(*),galss_id,pendulum_piece,distance,grouporder,furnace into @counts,@galssid,@pendulum_piece,@distance,@grouporder,@furnace from gmms_galss_task_2 gt  where  gt.work_state='等待中' and gt.work_procedure='钢化前摆片' and gt.groups=@work_groups and gt.orders is not null and gt.galss_id=rankid ORDER BY gt.orders limit 1;
	
	select galss_id,pendulum_piece,distance,grouporder,furnace 
	into @galssid2,@pendulum_piece2,@distance2,@grouporder2,@furnace2 from gmms_galss_task_2 gt  
	where  ((gt.work_state='已完成' and gt.work_procedure='钢化前摆片') or (gt.work_state='等待中' and gt.work_procedure='钢化')) 
	and gt.groups=@work_groups and gt.orders is not null ORDER BY furnace,gt.orders desc	limit 1;
	
	
	select galss_id,pendulum_piece,distance,grouporder,furnace 
	into @galssid3,@pendulum_piece3,@distance3,@grouporder3,@furnace3 from gmms_galss_task_2 gt  
	where gt.work_state='已完成' and gt.work_procedure='钢化前摆片' 
	and gt.groups=@work_groups and gt.orders is not null ORDER BY furnace,gt.orders desc limit 1;
	#select count(*) into @counts2 from gmms_galss_task_2 gt  where  ((gt.work_state='已完成' and gt.work_procedure='钢化前摆片') or (gt.work_state='等待中' and gt.work_procedure='钢化')) and gt.groups=@work_groups and gt.orders is not null ORDER BY furnace,gt.orders desc	;
		
	
	#select count(*) into @counts3 from gmms_galss_task_2 gt  where  gt.work_state='已完成' and gt.work_procedure='钢化前摆片' and gt.groups=@work_groups and gt.orders is not null;
	

	select @counts;
	select @counts2;
	#判断是否有等待的任务
	if machineids=40 or machineids=65 then
	
	
	set messagedate=null;
	
	
		if @counts=1 then
			
			
		  set @is_timing=true;
			
			if @pendulum_piece=3 then
			  #select count(*) into @count5 from gmms_galss_task_2 where  work_procedure='钢化' and (work_state='已完成' or work_state='正在工作');
					set @timer_name="ganghua_2";
					set @timeinterval=16;#每一片的时间  默认22秒   15片以上25秒
					if @work_groups=1 then
						set @timer_name="ganghua_1";
					end if;
					
					select @timer_name;
					
					call timer_ontime(@timer_name, @is_timing);
					if @is_timing then
							select count(*) into @t_count from gmms_galss_task_2 where groups=@work_groups and furnace=@furnace;
							if @t_count>16 then
								set @timeinterval=17;
							end if;
							set @z_time_out=@t_count*@timeinterval+40;
							call timer_set(@timer_name,@z_time_out);
					end if;
					 
-- 					 
-- 					#原程序
-- 				  set @timer_name="ganghua_1"; 					
-- 					select @timer_name;
-- 					
-- 					call timer_ontime(@timer_name, @is_timing);
-- 					 if @is_timing then 
-- 								 select count(*) into @t_count from gmms_galss_task_2 where furnace=@furnace;
-- 								 set @z_time_out=@t_count*22+40;
-- 									call timer_set(@timer_name,@z_time_out);
-- 					 end if;
		   end if;
		  select @is_timing;
			if @is_timing then

			
				if @furnace=@furnace2 or @furnace2 is null then
					#上一片和当前片 属于同一炉
					if @grouporder=@grouporder2 or @furnace2 is null or @galssid3 is null then
						#上一片和当前片 属于同排 或此炉第一片   或摆片台无玻璃  
						update gmms_galss_task_2 set work_state='正在工作' where work_state='等待中' and work_procedure='钢化前摆片' and groups=@work_groups and galss_id=rankid order by orders limit 1;						
						call intTo2ByteHex10_16_4(@pendulum_piece,@pendulum_pieces);
						call intTo2ByteHex10_16_4(@distance,@distances);
						call intTo2ByteHex10_16_4(@galssid,@galssids);
						set dates=CONCAT(returnvalue,'0000','00000000',@pendulum_pieces,@distances);
						call message(dates,'000a',@date);
						set messagedate=@date;
						insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,@work_groups,NOW(),'前摆片发1',@galssid,@pendulum_pieces);
					elseif @grouporder!=@grouporder2 and @galssid3 is not null then	
							#上一片和当前片 属于不同排 且摆片台上有玻璃   则进炉
							update gmms_galss_task_2 set work_state='等待中',work_procedure='钢化' where work_state='已完成' and work_procedure='钢化前摆片' and grouporder=@grouporder2 and groups=@work_groups ;
							call intTo2ByteHex10_16_4(@pendulum_piece,@pendulum_pieces);
							call intTo2ByteHex10_16_4(@distance,@distances);
							call intTo2ByteHex10_16_4(@galssid,@galssids);
							set dates=CONCAT(returnvalue,'0000','00000000','0002','00000000');
							call message(dates,'000a',@date);
							set messagedate=@date;
							insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,@work_groups,NOW(),'前摆片发2',@galssid,@pendulum_pieces);
					end if;
				else
					#select furnace into @furnace3 from gmms_galss_task_2 where furnace<@furnace2 and groups=@work_groups group by furnace order by furnace desc limit 0,1;
					if @furnace2 is not null then
						update gmms_galss_task_2 set work_state='正在工作',work_procedure='钢化' where  furnace=@furnace2 and groups=@work_groups and work_state!='破损';  
						call intTo2ByteHex10_16_4(@pendulum_piece,@pendulum_pieces);
						call intTo2ByteHex10_16_4(@distance,@distances);
						call intTo2ByteHex10_16_4(@galssid,@galssids);


						set dates=CONCAT(returnvalue,'0000','00000000','0003','00000000');
						call message(dates,'000a',@date);
						set messagedate=@date;
						insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,@work_groups,NOW(),'前摆片发送3',@galssid,@pendulum_pieces);
					end if;
					
				end if;
		
			
		end if;
		
		end if;
	elseif  machineids=42 or machineids=66 then 
		select grouporder,furnace into @grouporder,@furnace from gmms_galss_task_2 gt 
		where  gt.work_state='已完成' and work_procedure='钢化' and groups=@work_groups  ORDER BY gt.grouporder  limit 0,1;
		select @grouporder;
		select count(*) into @count2 from gmms_galss_task_2 where grouporder=@grouporder and work_state='已完成' and work_procedure='钢化' and groups=@work_groups;
		select @count2;
		if @count2>0 then
			while s<@count2 do
			#原句：玻璃数据同行完全相反 select galss_id into @galss_id from gmms_galss_task_2 where grouporder=@grouporder and work_state='已完成' and work_procedure='钢化' and groups=@work_groups order by orders desc limit s,1;
			select galss_id into @galss_id from gmms_galss_task_2 where grouporder=@grouporder and work_state='已完成' and work_procedure='钢化' and groups=@work_groups order by orders asc limit s,1;
			if @work_groups=2 then
				select galss_id into @galss_id from gmms_galss_task_2 where grouporder=@grouporder and work_state='已完成' and work_procedure='钢化' and groups=@work_groups order by orders desc limit s,1;
			end if;
			set @maxrank=0;
			
			select max(fine_throwing) into @maxrank from gmms_galss_task_2 where furnace=@furnace and groups=@work_groups;
			update gmms_galss_task_2 set fine_throwing=@maxrank+1,slicecage_flag=@maxrank+1 where galss_id=@galss_id and grouporder=@grouporder and work_state='已完成' and work_procedure='钢化' and groups=@work_groups;
			
			select @galss_id;
			call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
			set dates1=CONCAT(dates1,@galss_ids);
			set s=s+1;
		end while;
		update gmms_galss_task_2 set work_state='正在工作',work_procedure='钢化后摆片' where grouporder=@grouporder and work_state='已完成' and work_procedure='钢化' and groups=@work_groups;
		 
		select dates1;
		set dates=CONCAT(returnvalue,dates1);
		select dates;
		call message(dates,'000a',@date);
		set messagedate=@date;
		insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,@work_groups,NOW(),'摆片发送',@galss_id,messagedate);
		end if;
		
	end if;

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for PendulumPiece_2
-- ----------------------------
DROP PROCEDURE IF EXISTS `PendulumPiece_2`;
delimiter ;;
CREATE PROCEDURE `PendulumPiece_2`(in sendMessageHex varchar(255),in machineids BIGINT,out messagedate varchar(255))
BEGIN
	#下片机存储过程
	#找到当前机器标志位
	#查询机器逻辑表中对应的逻辑进行对应的步骤
	DECLARE machinedisposeids int DEFAULT 0;
	DECLARE ids int ;
	DECLARE plcfindid varchar(255);
	DECLARE dates varchar(255);
	DECLARE plcid varchar(255);
	DECLARE findplc varchar(255);
	DECLARE findmes varchar(255);
	DECLARE taskplc varchar(255);
	DECLARE taskmes varchar(255);
	DECLARE finsh_rankid BIGINT;
	DECLARE send_rankid BIGINT;
	DECLARE rankids varchar(255);
	DECLARE falgs varchar(255);
	DECLARE posunmes varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	set messagedate=messagedates;
	
	
	select machine_type_id,work_id into @mtid,@remarks from gmms_machine where id=machineids;
	set ids = cast(@remarks as UNSIGNED INTEGER);
	
	select mf.flag into @完成标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='完成字';
	
	select mf.flag into @请求标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='请求字';
	
	select mf.flag into @发送标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='发送字';
	
	select mf.flag into @完成确认标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='完成确认字';
	
	set finsh_rankid = cast(@完成标志位 as UNSIGNED INTEGER)+1;
	set send_rankid = cast(@请求标志位 as UNSIGNED INTEGER)+1;
	
	set findplc = substring(sendMessageHex,(cast(@完成标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskplc = substring(sendMessageHex,(cast(@请求标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskmes = substring(sendMessageHex,(cast(@发送标志位 as UNSIGNED INTEGER)*4)+19,4);
	set findmes = substring(sendMessageHex,(cast(@完成确认标志位 as UNSIGNED INTEGER)*4)+19,4);
	set falgs = substring(sendMessageHex,(cast(3 as UNSIGNED INTEGER)*4)+19,4);
	
	select taskplc;
	select taskmes;
	select findplc;
	select findmes;
	select plcfindid;
	
	if falgs='0001' then
		select count(*) into @counts from gmms_galss_task_2 where work_state='已完成' and work_procedure='钢化' and groups=@remarks ;
		select count(*) into @counts5 from gmms_galss_task_2 where work_state='正在工作' and work_procedure='钢化后摆片' and groups=@remarks ;
		if @counts=0 and @counts5=0 then
			select furnace into @furnace from gmms_galss_task_2 where work_state='正在工作' and work_procedure='钢化' and groups=@remarks  order by furnace limit 0,1;
			update gmms_galss_task_2 set  work_state='已完成'  where  work_state='正在工作' and work_procedure='钢化' and groups=@remarks  and furnace=@furnace;
		end if;
		
	end if;
	
	set rankids = conv(substring(sendMessageHex,(cast(send_rankid as UNSIGNED INTEGER)*4)+19,4),16,10);
	select rankids;

#完成逻辑查询
	select count(*),md.id into @完成数量,@完成逻辑编号 from gmms_machine_dispose md join gmms_machine_type mt on md.machine_type_id=mt.id where md.machine_type_id=@mtid  and md.plc_font_type='完成字' and md.mes_font_type='完成确认字' and md.plc_font_value=findplc and md.mes_font_value=findmes;
	
	#请求逻辑查询
	select count(*),md.id into @请求数量,@请求逻辑编号 from gmms_machine_dispose md join gmms_machine_type mt on md.machine_type_id=mt.id where md.machine_type_id=@mtid  and md.plc_font_type='请求字' and md.mes_font_type='发送字' and md.plc_font_value=taskplc and md.mes_font_value=taskmes;
	
	
	#判断逻辑是否存在
	if @完成数量=1 or @请求数量=1  then
		#判断请求逻辑是否为空
		if @请求逻辑编号!=0 then
			set machinedisposeids=@请求逻辑编号;
			#判断完成逻辑是否为空
		elseif @完成逻辑编号!=0 then
			set machinedisposeids=@完成逻辑编号;
		end if;
		
		#查询当前执行逻辑进行判断
		select logic,return_value into @ExecutionLogic,@returnvalue from gmms_machine_dispose where id=machinedisposeids;
		select @ExecutionLogic;
		select @returnvalue;
		if @ExecutionLogic='任务完成' then
				call 	PendulumPieceFinsh_2(sendMessageHex,machineids,@returnvalue,1,@messagedate);
				set messagedate=@messagedate;
		elseif @ExecutionLogic='任务失败' then
				call 	PendulumPieceFinsh_2(sendMessageHex,machineids,@returnvalue,2,@messagedate);
				set messagedate=@messagedate;	
		elseif @ExecutionLogic='玻璃破损' then
				call 	PendulumPieceFinsh_2(sendMessageHex,machineids,@returnvalue,3,@messagedate);
				set messagedate=@messagedate;		
 		elseif @ExecutionLogic='完成清空' then	
				set dates=CONCAT(@returnvalue,"0000");	
				call message(dates,'0014',@date);
				set messagedate=@date;
		elseif @ExecutionLogic='任务请求' then
				if machineids=40 then
					insert names5 (name2) values(now());
				end if;
				call PendulumPieceSend_2(machineids,@returnvalue,rankids,@messagedate);
				set messagedate=@messagedate;
				select 1;
 		elseif @ExecutionLogic='任务清空' then
				set dates=CONCAT(@returnvalue,"000000000000000000000000000000000000");
				call message(dates,'000a',@date);
				set messagedate=@date;
		end if;

	end if;

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for print_log
-- ----------------------------
DROP PROCEDURE IF EXISTS `print_log`;
delimiter ;;
CREATE PROCEDURE `print_log`(in _liuchengka varchar(50),in _line int,out result int,out msg varchar(255))
BEGIN
	#Routine body goes here...
	set msg="未知错误";
	set @a=null;
	set @z_count=null;
	select sum(shuliang),sum(mianji),liuchengkahaoceng into @a,@b,@c from  gmms_liuchengka_buffer where liuchengkahaoceng=_liuchengka  group by liuchengkahaoceng;
	 if @a is null then 
	 			set msg="没有该流程卡信息";
 else
					select count(*) into @z_count from gmms_print_log where liuchengka=_liuchengka;
		if @z_count then 
				set msg="该流程卡已经打印过,继续打印不会再计入生产日报";
		else
						insert into gmms_print_log (
			liuchengka,
			time_stamp,
			piece_count,
			piece_area,
			line)
			value (
				_liuchengka,
				now(),
				@a,
				@b,
				_line
			);
			set msg="ok";
 end if;		
end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for proc_ePiercedsByGlassIdSave
-- ----------------------------
DROP PROCEDURE IF EXISTS `proc_ePiercedsByGlassIdSave`;
delimiter ;;
CREATE PROCEDURE `proc_ePiercedsByGlassIdSave`(in gId BIGINT,in cpId int,in cpXh int,in gy varchar(50),in pline varchar(50),out back_Num int,out back_Msg varchar(255))
BEGIN

	#输入参数：gId-玻璃ID,cpId-次破类型Id,cpXh-次破原因序号,gy-工艺,pline-生产线
	#输出参数：back_Num 数值类型参数,back_Msg 字符串类型参数

	#设置玻璃破损
	Declare current_banzu varchar(50);#当前班组
	Declare cipo_leixing varchar(50);#次破类型
	Declare cipo_yuanyin varchar(50);#次破原因
	Declare shuliang varchar(50);#数量

	if gId is null or gId = '' then
			set back_Num = 3;
			set back_Msg="玻璃ID 为空！";
	else
			#判断当前玻璃是否已经报破损
			select count(*) into shuliang from e_pierceds where glass_id = gId;
			if shuliang = 1 then
					set back_Num = 0;
					set back_Msg = "当前玻璃已经报过破损";
			else
					#查询当前班组
					select banzu into current_banzu from gmms_user_current where gongyi = gy and chanxian = pline limit 0,1;
					select gy;
					select current_banzu;
					
					#查询破损类型及原因
					select cipoleixing into cipo_leixing from cipoleixing where id = cpId;
					select cipo_leixing;
					
					select cipoyuanyin into cipo_yuanyin from cipoyuanyin where xuhao = cpXh;
					select cipo_yuanyin;
						if gy = '钢化' then		
							#修改玻璃状态
							update gmms_galss_task_2 set work_state = '破损' where Id = gId;
							#插入破损数据
							INSERT INTO e_pierceds(processcard_id, type_num, pieces, pierced_type, pierced_cause, is_report, glass_id, create_time, process, line, gc_number, cs_number, team_name) 
							select gs.processcard_id,gs.edg_speed2,1,cipo_leixing,cipo_yuanyin,0,gId,NOW(),'钢化',gs.groups,gs.engineering_id,0,current_banzu from gmms_galss_task_2 as gs where gs.id=gid;
						elseif gy='中空' then
							#修改玻璃状态
							update gmms_galss_task_shelf set work_state = '破损' where Id = gId;
							#插入破损数据
							INSERT INTO e_pierceds(processcard_id, type_num, pieces, pierced_type, pierced_cause, is_report, glass_id, create_time, process, line, gc_number, cs_number, team_name) 
							select gs.processcard_id,gs.edg_speed2,1,cipo_leixing,cipo_yuanyin,0,gId,NOW(),'中空',gs.groupsss,gs.engineering_id,0,current_banzu from gmms_galss_task_shelf as gs where gs.id=gid;
						elseif gy = '仓储' then
							#修改玻璃状态
							update gmms_galss_task_shelf set work_state = '破损' where Id = gId;
							select team_name into current_banzu from e_record_tempering where glass_id = gId limit 0,1;
							#插入破损数据
							INSERT INTO e_pierceds(processcard_id, type_num, pieces, pierced_type, pierced_cause, is_report, glass_id, create_time, process, line, gc_number, cs_number, team_name) 
							select gs.processcard_id,gs.edg_speed2,1,cipo_leixing,cipo_yuanyin,0,gId,NOW(),'中空',gs.groupsss,gs.engineering_id,0,current_banzu from gmms_galss_task_shelf as gs where gs.id=gid;
						elseif gy = '磨边' then
							#插入破损数据
							INSERT INTO e_pierceds(processcard_id, type_num, pieces, pierced_type, pierced_cause, is_report, glass_id, create_time, process, line, gc_number, cs_number, team_name) 
							select gs.processcard_id,gs.edg_speed2,1,cipo_leixing,cipo_yuanyin,0,gId,NOW(),'磨边',pline,gs.engineering_id,0,current_banzu from gmms_galss_task_buffer as gs
							left join e_pierceds as ep on gs.id=ep.glass_id	where gs.id=gId and ep.glass_id is null;
						end if;

				select count(*) into shuliang from e_pierceds where glass_id = gId;
				if shuliang = 1 then
					set back_Num = 1;
					set back_Msg="成功";
				else
					set back_Num = 2;
					set back_Msg = "玻璃破损信息插入失败";
				END If;
		END if;
	end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for proc_eRecordStockByGlassIdSave
-- ----------------------------
DROP PROCEDURE IF EXISTS `proc_eRecordStockByGlassIdSave`;
delimiter ;;
CREATE PROCEDURE `proc_eRecordStockByGlassIdSave`(in lckh varchar(50),in pline varchar(50),out back_Num int,out back_Msg varchar(255))
BEGIN

	#输入参数：lckh-流程卡号,pline-生产产线
	#输出参数：back_Num 数值类型参数,back_Msg 字符串类型参数

	#设置玻璃破损
	Declare current_banzu varchar(50);#当前班组
	Declare GID varchar(20);
	Declare zNum int;#总数
	Declare fNum int  default 0;#循环数
	Declare drid_ int  default 0;#循环数
		Declare sNum int  default 0;#循环数
		Declare minarea_ double  default 0;#最小面积
	#定义游标
	Declare lck_cursor cursor for select id from gmms_galss_task_buffer where  processcard_id = lckh and id not in (select glass_id from e_pierceds where process ='磨边' and line = pline );#当前流程卡未破损的玻璃下片
	select lckh;
	select count(1) into zNum from gmms_galss_task_buffer where  processcard_id = lckh and id not in (select glass_id from e_pierceds where process ='磨边' and line = pline );
	select zNum;
	if zNum =0 then 
		set back_num = 0;
		set back_Msg = "没有当前流程卡完成数据";
	else
		if lckh is null or lckh = '' then
			set back_Num = 3;
			set back_Msg="流程卡号 为空";
		else
			select banzu into current_banzu from gmms_user_current where gongyi = '磨边' and chanxian = pline;
			select current_banzu;
			if current_banzu is null or current_banzu = '' then 
				set back_Num = 2;
				set back_Msg= concat("磨边",pline,"线班组为空");
			else
				open lck_cursor;
					REPEAT
					FETCH lck_cursor INTO GID;
							set fNum = fNum+1;
							select count(1) into sNum from e_record_stock where glass_id = gid;
							if sNum = 0 then
								INSERT INTO e_record_stock(processcard_id, glass_id, width, height,thinkess, color, sequence, create_time, line, team_name, is_report, rank,type_num) 
								select gs.processcard_id,gs.id,gs.edg_width,gs.edg_height,gs.thickness,gs.color,gs.drop_frame,NOW(),pline,current_banzu,0,'',gs.edg_speed2 from gmms_galss_task_buffer as gs
								left join e_record_stock as es on gs.id=es.glass_id 
								where gs.id=gid and es.glass_id is null;
							end if;
							#select	fNum;
					#退出重复条件
					UNTIL fNum=zNum
					END REPEAT;
						set back_Num = 1;
						set back_Msg= "成功";
				close lck_cursor;
			end if;
			select min(edg_width*edg_height)/1000000 into minarea_ from gmms_galss_task_buffer where processcard_id=lckh GROUP BY processcard_id;
			
			update gmms_liuchengka set minArea=minarea_,isgang="冷加工下片完成"					 
			where liuchengka=lckh and gy_gang=1  and Occupied=0 and isgang='';
			
			select id into drid_ from gmms_drop_frame where processcard_id=lckh limit 1;
			if pline='1' and drid_>12 then
				update gmms_drop_frame as df left join gmms_machine as gm on df.groups=gm.work_id 
				set df.state='空闲',df.drop_frame=null,df.processcard_count=0,df.processcard_id=null,df.engineering_id=null,df.finsh_count=0,gm.remarks=df.id
				where gm.id=75 and processcard_id=lckh;
			else
				update gmms_drop_frame as df left join gmms_machine as gm on df.groups=gm.work_id 
				set df.state='空闲',df.drop_frame=null,df.processcard_count=0,df.processcard_id=null,df.engineering_id=null,df.finsh_count=0,gm.remarks=df.id
				where (gm.id=5 or gm.id=22 or gm.id=17) and processcard_id=lckh;
			end if;
		end if;
	end if;
	
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for proc_eRecordStockByLineUpdata
-- ----------------------------
DROP PROCEDURE IF EXISTS `proc_eRecordStockByLineUpdata`;
delimiter ;;
CREATE PROCEDURE `proc_eRecordStockByLineUpdata`(in pline varchar(50),out back_Num int,out back_Msg varchar(255))
BEGIN

	#输入参数：pline-生产产线
	#输出参数：back_Num 数值类型参数,back_Msg 字符串类型参数
	DECLARE Dbanzu varchar(255) DEFAULT '';#当前班组
	#select banzu into current_banzu from gmms_user_current where gongyi = '磨边' and chanxian = pline;
-- 	update (select processcard_id from e_record_stock where is_report = 0 and line = pline group by processcard_id) as lck 
-- 	left join gmms_liuchengka as lk on lck.processcard_id=lk.liuchengka set lk.isgang="冷加工下片完成" where lk.gy_gang=1 and lk.Occupied=0;
	
	update e_record_stock set is_report = 1,report_time = now() where is_report = 0 and line = pline;
	
	
	update e_pierceds set is_report=1,report_time = now() where is_report = 0 and line = pline and process = '磨边';
	select banzu into Dbanzu from gmms_user_current where chanxian=pline and gongyi='磨边';
	
	update Y_Edg_Statistics set banzu=Dbanzu,report_time=NOW() where banzu is null and Line = pline;
-- 	update Y_Edg_Statistics AS ES LEFT JOIN (select * from e_record_stock where line=pline order by create_time desc limit 4500) AS ERS on ERS.glass_id=ES.Glassid
-- 	set ES.banzu=ERS.team_name,ES.report_time=ERS.report_time
-- 	where ERS.glass_id is not null and ES.banzu is null and ES.Line = pline;
	
-- 	update Y_Edg_Statistics AS ES LEFT JOIN e_pierceds AS ep on ep.glass_id=ES.Glassid
-- 	set ES.banzu=ep.team_name
-- 	where ep.is_report = 0 and ES.Line = 1 and ep.process = '磨边';
	#update gmms_cutmanage set BgTime=now() where BgTime is null and groups=pline;
	set back_Num = 1;
	set back_Msg = "成功";
	
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for read_event
-- ----------------------------
DROP PROCEDURE IF EXISTS `read_event`;
delimiter ;;
CREATE PROCEDURE `read_event`(in evt varchar(50),IN msg varchar(255),IN mid int,IN oldoffset int,IN newmsg int,in iswrite int)
BEGIN
  SET @a:=1;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for record_event
-- ----------------------------
DROP PROCEDURE IF EXISTS `record_event`;
delimiter ;;
CREATE PROCEDURE `record_event`(in evt varchar(50),in evtid int,IN msg varchar(255),IN mid int,IN oldoffset int,IN msgoffset int,in iswrite int)
BEGIN
		DECLARE _end int;
		DECLARE flag int;
		DECLARE _i int;
	 
		SET flag:=0;
		SET oldoffset:=19+oldoffset*4;
		if iswrite then 
				/*
				set @addr:=substring(msg,11,4);
				SELECT CONV(@addr,16,10);
			  if CONV(@addr,16,10)<>msgoffset then 
			
				set flag:=100;
			  else 
			     SET    msgoffset:=(msgoffset-CONV(@addr,16,10))*4+27;      
				end if;
				*/
				SET    msgoffset:=msgoffset*4+27;   
		
		 
		else
		 SET    msgoffset:=msgoffset*4+19;
		end if;

	#Routine body goes here...
		SET @lastmsg=null;
		SELECT message into @lastmsg from gmms_machine_message where  machine_id=mid;
	 
	  if @lastmsg is not null and flag=0 then
				SET @str1:=substring(@lastmsg,oldoffset,4);
				SET @str2:=substring(msg,msgoffset,4);
		
				if @str1<>@str2 then
							
						  if @str1='0000' and flag=0 then 
							   set flag:=1;
							end if;
							if @str2='0000' and flag=0 then 
							    set flag:=2;
							end if;
							if flag=0 then 
							    SET flag:=3;
							end if;
	/*
				else 
				
					   if @str1<>'0000' then 
					    SET @id=null;
					    SELECT id into @id from event_tab where machineid=mid and eventid=evtid;
							if @id is not null then 
							   UPDATE event_tab SET invoke=invoke+1 where id=@id;
							end if;
					 
					 end if;
					 */
				end if;
		end if;
	
if flag=1 then 
	INSERT INTO event_tab (
time_stamp,
eventname,
eventid,
machineid,
oldmsg,
newmsg,
flag,
invoke,
iswrite
)	VALUES
(
	now(),
	evt,
	evtid,
	mid,
	
	@lastmsg,
	msg,
	flag,
	1,
	iswrite
);
end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for Reset_Debug
-- ----------------------------
DROP PROCEDURE IF EXISTS `Reset_Debug`;
delimiter ;;
CREATE PROCEDURE `Reset_Debug`()
BEGIN
	#清空理片笼视图存储过程
	UPDATE gmms_slicecage SET State='空闲',COUNT=0 ,TYPE_ID=NULL ;
	UPDATE gmms_galss_task SET work_state='待识别', work_procedure='测量',work_procedure_number=100,finsh_time=null,stop_position=null,
	slicecage_rank=null,rank=0,out_rank=0;
	UPDATE gmms_drop_frame SET state='空架',engineering_id=null,drop_frame=null,processcard_id=null;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for RobotHandFinsh_2
-- ----------------------------
DROP PROCEDURE IF EXISTS `RobotHandFinsh_2`;
delimiter ;;
CREATE PROCEDURE `RobotHandFinsh_2`(in sendMessageHex varchar(255),in machineids BIGINT,in returnvalue varchar(255),in flag int,out messagedate varchar(255))
BEGIN
	#下片机完成存储过程
	#找到正在工作的任务进行完成
	DECLARE count1 int;
	DECLARE dates varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	
	
	DECLARE y int DEFAULT 0;
	DECLARE processcardid2 varchar(255);
	set messagedate=messagedates;
	set @finshcount=null;
	set @processcard_id=null;
	select machine_type_id,work_id,remarks into @mtid,@remarks,@flag1 from gmms_machine where id=machineids;
	
	
	#select @counts;
	#钢化前上片机器人
	if machineids=38 or machineids=63 then
		#任务完成
		select galss_id,dr.id into @galssid,@dropframeid from gmms_galss_task_2 gt INNER JOIN gmms_drop_frame_2 dr on gt.processcard_id=dr.processcard_id and gt.groups=dr.groups where work_state='正在工作' and work_procedure='钢化上片' and gt.groups=@remarks  limit 0,1;
		if flag=1 then
		
			if @galssid>0 then
			#select @galssid;
			#select @remarks;
				update gmms_galss_task_2 set work_state='正在工作',work_procedure='钢化前复合' where galss_id=@galssid and groups=@remarks;
				update gmms_drop_frame_2 set gmms_drop_frame_2.surplus_count=gmms_drop_frame_2.surplus_count-1 where gmms_drop_frame_2.id=@dropframeid;
				set messagedate='';
				if false then 
					call `Robot_gangqian_finish_ex`(machineids, messagedate);		   
				end if;
				if messagedate is null or messagedate='' then 
			   	call message(returnvalue,'0014',@date);
				   set messagedate=@date;		
			   end if;		
			end if;
		#任务未完成
		elseif flag=2 then		
			if @galssid>0 then			
				update gmms_galss_task_2 set work_state='已优化' where galss_id=@galssid and groups=@remarks;					
			end if;
			call message(returnvalue,'0014',@date);
			set messagedate=@date;
		elseif flag=3 then		
			if @galssid>0 then		
				update gmms_galss_task_2 set work_state='破损' where galss_id=@galssid and groups=@remarks;
				update gmms_drop_frame_2 set surplus_count=surplus_count-1 where id=@dropframeid;
				call message(returnvalue,'0014',@date);
				set messagedate=@date;	
			end if;	
		end if;
		#查找此架是否已上完片
		update gmms_drop_frame_2 set surplus_count=0,state='空闲',processcard_id=null,processcard_count=0 
		where processcard_id is not null and processcard_count>0 and surplus_count=0 and (select count(1) from gmms_galss_task_2 
 		where gmms_drop_frame_2.processcard_id=gmms_galss_task_2.processcard_id and (work_procedure='钢化上片' and work_state!='破损'))=0;
--  		update gmms_drop_frame_2 set surplus_count=0,state='空闲',processcard_id=null,processcard_count=0 
--  		where id=@dropframeid and (select count(1) from gmms_galss_task_2 
--  		where gmms_drop_frame_2.processcard_id=gmms_galss_task_2.processcard_id and (work_procedure='钢化上片' and work_state!='破损'))=0;
	#中空理片笼上片机	
	elseif machineids=47 then
		call y_RobotHollowSlicecageFinsh(machineids,returnvalue,flag,dates);
		set messagedate=dates;
	elseif machineids=44 or machineids=68 then
		#钢化后机器人
		call y_RobotTemperingAfterFinsh(flag,machineids,returnvalue,dates);
		set messagedate=dates;	
	elseif machineids=52 or machineids=71 or machineids=73  then
		#中空机器人 
		call y_RobotHollowFinsh(flag,machineids,returnvalue,dates);
		set messagedate=dates;
	end if;
	

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for RobotHandSend_2
-- ----------------------------
DROP PROCEDURE IF EXISTS `RobotHandSend_2`;
delimiter ;;
CREATE PROCEDURE `RobotHandSend_2`(in rankid varchar(255),in machineids BIGINT,in returnvalue varchar(255),out messagedate varchar(255))
BEGIN
	#下片机发送任务存储过程
	#找到等待中的任务，把下片位置，宽度，高度，厚度发送给plc
		DECLARE s int DEFAULT 0;
	DECLARE finshcount1 int;
	
	
	DECLARE y int DEFAULT 0;
	DECLARE j int DEFAULT 0;
	DECLARE processcardid2 varchar(255);
	
	DECLARE dates varchar(255);
	DECLARE noodles1 varchar(255);
	DECLARE finshcount int;
	DECLARE messagedates varchar(255) DEFAULT '';
	set messagedate=messagedates;
	
		select machine_type_id,work_id,remarks into @mtid,@remarks,@flag1 from gmms_machine where id=machineids;

	
	
	
	/*
	select @counts;
	select @galssid;
	#判断是否有等待的任务
	select @loadfalg;
	*/
	
	#select @remarks;
	#select machineids;
	if machineids=38 or machineids=63 then
	
	
		select count(*) into @counts from gmms_galss_task_2 gt INNER JOIN gmms_drop_frame_2 dr on gt.processcard_id=dr.processcard_id where dr.flag=1 and gt.work_state='已优化' and work_procedure='钢化上片' and gt.groups=@remarks and gt.orders is not null ;
		#select @counts;
		select count(*) into @counts3 from gmms_galss_task_2  where work_state='正在工作' and work_procedure='钢化上片' and groups=@remarks ;
	

	
	select gt.id,width,height,thickness,dr.surplus_count,angle,dr.stops,dr.noodles,gt.marking_flag,gt.galss_id,gt.processcard_id,gt.stop_position 
	into @id,@width,@height,@thickness,@surplus_count,@angle,@stops,@noodles,@marking_flag,@galss_id2,@gtprocesscard_id,@stop_position from gmms_galss_task_2 gt 
	INNER JOIN gmms_drop_frame_2 dr on gt.processcard_id=dr.processcard_id 
	where dr.flag=1 and dr.surplus_count>0 and gt.work_state='已优化' and work_procedure='钢化上片' and gt.groups=@remarks 
	and gt.orders is not null ORDER BY gt.furnace,gt.orders limit 0,1;
	#select @galss_id2;
	select count(*) into @processcardidsum from gmms_drop_frame_2 where groups=@remarks and processcard_id=@gtprocesscard_id;
	if @galss_id2=0 then
		select max(galss_id) into @galss1 from gmms_galss_task_2 where groups=@remarks;
		update gmms_galss_task_2 set galss_id=@galss1+1 where id=@id;
	end if;
	select galss_id into @galssid from gmms_galss_task_2 where id=@id;
	#旋转料架三个都有请求
	select count(*) into @counts2 from gmms_machine where state='允许' 
	and (id=35 or id=36 or id=37 or id=60 or id=61 or id=62) and remarks=@remarks;
	#旋转料架1  当前面无玻璃/背面有玻璃时 不吸片
	select surplus_count into @D_Taskcount from gmms_drop_frame_2 
	where stops=1 and flag=1 and groups=@remarks;
	select surplus_count into @B_Taskcount from gmms_drop_frame_2
	where stops=1 and flag=0 and groups=@remarks;
		if @counts>0 and @counts2=3 and @galssid!=0 and @counts3=0 and @processcardidsum=1 
				and !((@D_Taskcount=0 or @D_Taskcount is null) and @B_Taskcount>0) then
		 if @flag1=1 then
		 #select @galssid;
			update gmms_galss_task_2 set work_state='正在工作' where work_state='已优化' and work_procedure='钢化上片' and groups=@remarks and galss_id=@galssid;
			
			
			#修改 添加动作识别
			if @angle=1 then 
				select gang_qian_zhuan into @angle from gmms_dongzuo_table where number=@marking_flag;
			else 
			    select gang_qian_bu_zhuan into @angle from gmms_dongzuo_table where number=@marking_flag ;
			end if;			
			select gang_qian_zhua into @zhua_qu from gmms_dongzuo_table where number=@marking_flag;
			
			if @stop_position>12 then
				if @angle=0 then
					set @angle=3;
				elseif @angle=1 then
					set @angle=2;
				elseif @angle=2 then
					set @angle=1;
				elseif @angle=3 then
					set @angle=0;
				end if;
			end if;
			#####
			
			call intTo2ByteHex10_16_4(@galssid,@galssids);
			call intTo2ByteHex10_16_4(@surplus_count,@surplus_counts);
			call intTo2ByteHex10_16_4(@stops,@stopss);
			call intTo2ByteHex10_16_4(@width,@widths);
			call intTo2ByteHex10_16_4(@height,@heights);
			call intTo2ByteHex10_16_4(@thickness,@thicknesss);
			call intTo2ByteHex10_16_4(@angle,@angles);
			/*
			select @widths;
			select @heights;
			select @thicknesss;
			select @angles;
			*/
			if @noodles='A' then
				set noodles1='0001';
			else
				set noodles1='0002';
			end if;

			call intTo2ByteHex10_16_4(@angle,@angles);
			call intTo2ByteHex10_16_4(@zhua_qu,@z_zhuas);
			
			set dates=CONCAT(returnvalue,@galssids,noodles1,@z_zhuas,@stopss,@widths,@heights,@thicknesss,@angles,@surplus_counts);		
			
		#	set dates=CONCAT(returnvalue,@galssids,noodles1,'0000',@stopss,@widths,@heights,@thicknesss,@angles,@surplus_counts);
			call message(dates,'000a',@date);
			set messagedate=@date;
			insert into A_Robotlog(groups,glassid,beigntime,endtime,state,processcard_id)
			VALUES(@remarks,@id,NOW(),null,messagedate,@gtprocesscard_id);
		 end if;
			
	
		end if;
	#中空理片机器人
	elseif machineids=47 then
	  select @flag1;
		call y_RobotHollowSlicecage(machineids,returnvalue,dates);	
		set messagedate=dates;
	elseif machineids=44 or machineids=68 then
		#钢化后机械手
		call y_RobotTemperingAfter(rankid,machineids,returnvalue,dates);
		set messagedate=dates;
	elseif machineids=52 or machineids=71 or machineids=73 then
		#中空机器人
		call y_RobotHollow(machineids,returnvalue,dates);
		set messagedate=dates;
	end if;
	

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for RobotHandSend_2_copy1
-- ----------------------------
DROP PROCEDURE IF EXISTS `RobotHandSend_2_copy1`;
delimiter ;;
CREATE PROCEDURE `RobotHandSend_2_copy1`(in rankid varchar(255),in machineids BIGINT,in returnvalue varchar(255),out messagedate varchar(255))
BEGIN
	#下片机发送任务存储过程
	#找到等待中的任务，把下片位置，宽度，高度，厚度发送给plc
		DECLARE s int DEFAULT 0;
	DECLARE finshcount1 int;
	
	
	DECLARE y int DEFAULT 0;
	DECLARE j int DEFAULT 0;
	DECLARE processcardid2 varchar(255);
	
	DECLARE dates varchar(255);
	DECLARE noodles1 varchar(255);
	DECLARE finshcount int;
	DECLARE messagedates varchar(255) DEFAULT '';
	set messagedate=messagedates;
	
		select machine_type_id,work_id,remarks into @mtid,@remarks,@flag1 from gmms_machine where id=machineids;

	
	
	
	/*
	select @counts;
	select @galssid;
	#判断是否有等待的任务
	select @loadfalg;
	*/
	
	#select @remarks;
	#select machineids;
	if machineids=38 or machineids=63 then
	
	
		select count(*) into @counts from gmms_galss_task_2 gt INNER JOIN gmms_drop_frame_2 dr on gt.processcard_id=dr.processcard_id where dr.flag=1 and gt.work_state='已优化' and work_procedure='钢化上片' and gt.groups=@remarks and gt.orders is not null ;
		#select @counts;
		select count(*) into @counts3 from gmms_galss_task_2  where work_state='正在工作' and work_procedure='钢化上片' and groups=@remarks ;
	

	
	select gt.id,width,height,thickness,dr.surplus_count,angle,dr.stops,dr.noodles,gt.marking_flag,gt.galss_id,gt.processcard_id into @id,@width,@height,@thickness,@surplus_count,@angle,@stops,@noodles,@marking_flag,@galss_id2,@gtprocesscard_id from gmms_galss_task_2 gt INNER JOIN gmms_drop_frame_2 dr on gt.processcard_id=dr.processcard_id where dr.flag=1 and dr.surplus_count>0 and gt.work_state='已优化' and work_procedure='钢化上片' and gt.groups=@remarks and gt.orders is not null ORDER BY gt.furnace,gt.orders limit 0,1;
	#select @galss_id2;
	if @galss_id2=0 then
		select max(galss_id) into @galss1 from gmms_galss_task_2 where groups=@remarks;
		update gmms_galss_task_2 set galss_id=@galss1+1 where id=@id;
	end if;
	select galss_id into @galssid from gmms_galss_task_2 where id=@id;
	select count(*) into @counts2 from gmms_machine where state='允许' and remarks=@remarks;
		if @counts>0 and @counts2=3 and @galssid!=0 and @counts3=0 then
		 if @flag1=1 then
		 #select @galssid;
			update gmms_galss_task_2 set work_state='正在工作' where work_state='已优化' and work_procedure='钢化上片' and groups=@remarks and galss_id=@galssid;
			
			
			#修改 添加动作识别
			if @angle=1 then 
				select gang_qian_zhuan into @angle from gmms_dongzuo_table where number=@marking_flag;
			else 
			    select gang_qian_bu_zhuan into @angle from gmms_dongzuo_table where number=@marking_flag ;
			end if;			
			   select gang_qian_zhua into @zhua_qu from gmms_dongzuo_table where number=@marking_flag;
			
			#####
			
			call intTo2ByteHex10_16_4(@galssid,@galssids);
			call intTo2ByteHex10_16_4(@surplus_count,@surplus_counts);
			call intTo2ByteHex10_16_4(@stops,@stopss);
			call intTo2ByteHex10_16_4(@width,@widths);
			call intTo2ByteHex10_16_4(@height,@heights);
			call intTo2ByteHex10_16_4(@thickness,@thicknesss);
			call intTo2ByteHex10_16_4(@angle,@angles);
			/*
			select @widths;
			select @heights;
			select @thicknesss;
			select @angles;
			*/
			if @noodles='A' then
				set noodles1='0001';
			else
				set noodles1='0002';
			end if;

	
			call intTo2ByteHex10_16_4(@angle,@angles);
			call intTo2ByteHex10_16_4(@zhua_qu,@z_zhuas);
			
			set dates=CONCAT(returnvalue,@galssids,noodles1,@z_zhuas,@stopss,@widths,@heights,@thicknesss,@angles,@surplus_counts);
			
			
			
			
		#	set dates=CONCAT(returnvalue,@galssids,noodles1,'0000',@stopss,@widths,@heights,@thicknesss,@angles,@surplus_counts);
			call message(dates,'000a',@date);
			set messagedate=@date;
			insert into A_Robotlog(groups,glassid,beigntime,endtime,state,processcard_id)
			VALUES(@remarks,@id,NOW(),null,messagedate,@gtprocesscard_id);
		 end if;
			
	
		end if;
	#中空理片机器人
	elseif machineids=47 then
	  select @flag1;
		
		if @flag1=1 then
			select count(*) into @count1 from gmms_drop_frame_4 where groups=10;
			select remarks into @remarks from gmms_machine where id=50;
			select @count1;
			while s<@count1 do
				select stop_rank,stops into @stop_rank,@stops from gmms_drop_frame_4 where groups=10 limit s,1;
				select @stops;
				select @stop_rank;
				set @galss_id=null;
				if @remarks is not null then
					select gt.id,galss_id,edg_width,edg_height,thickness,gt.stop_rank,gt.processcard_id,df.finsh_count,df.processcard_count,
				gt.edg_speed2 into @id,@galss_id,@edg_width,@edg_height,@thickness,@stop_ranks,@processcard_id,@finsh_count,@processcard_count,
				@edg_speed2 from gmms_drop_frame_4 df left join gmms_galss_task_slicecage gt on gt.processcard_id=df.processcard_id 
				where df.processcard_id is not null and work_state='已完成' and df.stop_rank=@stop_rank and sign=3 and work_procedure='钢化后下片' and df.processcard_id=@remarks and df.finsh_count >0 order by stop_rank desc limit 0,1;
				else
					select gt.id,galss_id,edg_width,edg_height,thickness,gt.stop_rank,gt.processcard_id,df.finsh_count,df.processcard_count,
				gt.edg_speed2 into @id,@galss_id,@edg_width,@edg_height,@thickness,@stop_ranks,@processcard_id,@finsh_count,@processcard_count,
				@edg_speed2 from gmms_drop_frame_4 df left join gmms_galss_task_slicecage gt on gt.processcard_id=df.processcard_id 
				where df.processcard_id is not null and work_state='已完成' and df.stop_rank=@stop_rank and sign=3 and work_procedure='钢化后下片' and df.finsh_count >0 order by stop_rank desc limit 0,1;
				end if;
				
										
						set @galss_id1=null;
						select galss_id into @galss_id1 from gmms_galss_task_slicecage where galss_id=@galss_id and processcard_id=@processcard_id and stop_rank=out_rank;
						if @galss_id1 is null then
							
							select remarks into @remarks from gmms_machine where id=machineids;
							if @remarks!=2 then
								
								update gmms_machine set remarks=2 where id=machineids;
								update gmms_drop_frame_4 set sign=3 where processcard_id=@processcard_id and groups=10;

								set s=100;
							end if;							

					end if;
				select @galss_id1;
				
				if @galss_id1 is not null then
					select stop_rank into @laststoprank from (
					select stop_rank from gmms_galss_task_shelf where processcard_id=@processcard_id or processcard_id=@liuchengka
					group by stop_rank,edg_speed2) as g  group by g.stop_rank having count(*)>1 order by g.stop_rank limit 0,1;
					
					select stop_rank into @laststoprank2 from (
					select stop_rank from gmms_galss_task_shelf where processcard_id=@processcard_id or processcard_id=@liuchengka
					group by stop_rank,width,height) as g  group by g.stop_rank having count(*)>1 order by g.stop_rank limit 0,1;
					update gmms_galss_task_slicecage set work_state='正在工作',work_procedure='理片笼上片' where galss_id=@galss_id1;

					call intTo2ByteHex10_16_4(@galss_id1,@galss_ids);
					call intTo2ByteHex10_16_4(@stops,@stopss);
					call intTo2ByteHex10_16_4(@edg_width,@widths);
					call intTo2ByteHex10_16_4(@edg_height,@heights);
					call intTo2ByteHex10_16_4(@thickness,@thicknesss);
					call intTo2ByteHex10_16_4(@stop_ranks,@stop_rankss);
					call intTo2ByteHex10_16_4(@processcard_count,@processcard_counts);
					if @laststoprank=@stop_ranks or @laststoprank2=@stop_ranks then
						set dates=CONCAT(returnvalue,@galss_ids,@processcard_counts,'0001',@stopss,@widths,@heights,@thicknesss,@stop_rankss,"0000");
					else
						set dates=CONCAT(returnvalue,@galss_ids,@processcard_counts,'0001',@stopss,@widths,@heights,@thicknesss,@stop_rankss,"0001");
					end if;	
									
					call message(dates,'000a',@date);
					set messagedate=@date;
					set s=100;
					insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,1,NOW(),'理片进任务',@stop_ranks,messagedate);
				end if;
				select s;
				set s=s+1;
			end while;
		elseif 	@flag1=2 then
			select galss_id,edg_width,edg_height,thickness,gt.processcard_id into @galss_id,@edg_width,@edg_height,@thickness,@processcard_id from gmms_galss_task_slicecage gt join gmms_drop_frame_4 df on gt.processcard_id=df.processcard_id where work_state='已出库'  and sign=3 and work_procedure='理片'  order by gt.stop_rank  limit 0,1;
			select stops,finsh_count,processcard_count into @stops,@finsh_count,@processcard_count from gmms_drop_frame_4 where processcard_id=@processcard_id and groups=10;
			set finshcount1=@finsh_count+1;
			
					
			if @galss_id is not null then
				
					update gmms_galss_task_slicecage set work_state='正在工作',work_procedure='理片笼下片' where galss_id=@galss_id;
					call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
					call intTo2ByteHex10_16_4(@stops,@stopss);
					call intTo2ByteHex10_16_4(@edg_width,@widths);
					call intTo2ByteHex10_16_4(@edg_height,@heights);
					call intTo2ByteHex10_16_4(@thickness,@thicknesss);
					call intTo2ByteHex10_16_4(finshcount1,@stop_rankss);
					if @processcard_count=finshcount1 then
						set dates=CONCAT(returnvalue,@galss_ids,'00000002',@stopss,@widths,@heights,@thicknesss,@stop_rankss,'0000');
					else
						set dates=CONCAT(returnvalue,@galss_ids,'00000002',@stopss,@widths,@heights,@thicknesss,@stop_rankss,'0001');
					end if;
					
					call message(dates,'000a',@date);
					set messagedate=@date;
					
					insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,1,NOW(),'理片出任务',finshcount1,messagedate);
			end if;
		end if;
	elseif machineids=44 or machineids=68 then
		#钢化后机械手
		call y_RobotTemperingAfter(rankid,machineids,returnvalue,dates);
		set messagedate=dates;
	elseif machineids=52 or machineids=71 or machineids=73 or machineids=75 then
		#中空机器人
		call y_RobotHollow(machineids,returnvalue,dates);
		set messagedate=dates;
	end if;
	

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for RobotHand_2
-- ----------------------------
DROP PROCEDURE IF EXISTS `RobotHand_2`;
delimiter ;;
CREATE PROCEDURE `RobotHand_2`(in sendMessageHex varchar(255),in machineids BIGINT,out messagedate varchar(255),out falgs varchar(255))
BEGIN
	#下片机存储过程
	#找到当前机器标志位
	#查询机器逻辑表中对应的逻辑进行对应的步骤
	DECLARE machinedisposeids int DEFAULT 0;
	DECLARE ids int ;
	DECLARE plcfindid varchar(255);
	DECLARE dates varchar(255);
	DECLARE plcid varchar(255);
	DECLARE findplc varchar(255);
	DECLARE findmes varchar(255);
	DECLARE taskplc varchar(255);
	DECLARE taskmes varchar(255);
	DECLARE finsh_rankid BIGINT;
	DECLARE send_rankid BIGINT;
	DECLARE rankids varchar(255);
	DECLARE posunmes varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	set messagedate=messagedates;
	set falgs=0;

	select machine_type_id,work_id into @mtid,@remarks from gmms_machine where id=machineids;
	set ids = cast(@remarks as UNSIGNED INTEGER);
	
	select mf.flag into @完成标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='完成字';
	
	select mf.flag into @请求标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='请求字';
	
	select mf.flag into @发送标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='发送字';
	
	select mf.flag into @完成确认标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='完成确认字';
	
	set finsh_rankid = cast(@完成标志位 as UNSIGNED INTEGER)+1;
	set send_rankid = cast(@请求标志位 as UNSIGNED INTEGER)+1;
	
	#select @完成标志位;
	
	set findplc = substring(sendMessageHex,(cast(@完成标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskplc = substring(sendMessageHex,(cast(@请求标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskmes = substring(sendMessageHex,(cast(@发送标志位 as UNSIGNED INTEGER)*4)+19,4);
	set findmes = substring(sendMessageHex,(cast(@完成确认标志位 as UNSIGNED INTEGER)*4)+19,4);
	
	#select taskplc;
	#select taskmes;
	#select findplc;
	#select findmes;
	if findmes="0002" and machineids=63   then 
	  set  findmes="0001";
	end if;
	
	set rankids = conv(substring(sendMessageHex,(cast(1 as UNSIGNED INTEGER)*4)+19,4),16,10);
	#select rankids;

set @完成数量=null;
set @完成逻辑编号=null;
#完成逻辑查询
	select count(*),md.id into @完成数量,@完成逻辑编号 from gmms_machine_dispose md join gmms_machine_type mt on md.machine_type_id=mt.id where md.machine_type_id=@mtid  and md.plc_font_type='完成字' and md.mes_font_type='完成确认字' and md.plc_font_value=findplc and md.mes_font_value=findmes;
	
	#请求逻辑查询
	select count(*),md.id into @请求数量,@请求逻辑编号 from gmms_machine_dispose md join gmms_machine_type mt on md.machine_type_id=mt.id where md.machine_type_id=@mtid  and md.plc_font_type='请求字' and md.mes_font_type='发送字' and md.plc_font_value=taskplc and md.mes_font_value=taskmes;
	
	
	#判断逻辑是否存在
	if @完成数量=1 or @请求数量=1  then
	
		#判断完成逻辑是否为空
		if @完成逻辑编号!=0 then
			set machinedisposeids=@完成逻辑编号;
			
		#判断请求逻辑是否为空
		elseif @请求逻辑编号!=0 then
			set machinedisposeids=@请求逻辑编号;
			
		end if;
		
		#查询当前执行逻辑进行判断
		select logic,return_value into @ExecutionLogic,@returnvalue from gmms_machine_dispose where id=machinedisposeids;
		#select @ExecutionLogic;
		#select @returnvalue;
		if @ExecutionLogic='任务完成' then
				call 	RobotHandFinsh_2(sendMessageHex,machineids,@returnvalue,1,@messagedate);
				set messagedate=@messagedate;
		elseif @ExecutionLogic='任务失败' then
				call 	RobotHandFinsh_2(sendMessageHex,machineids,@returnvalue,2,@messagedate);
				set messagedate=@messagedate;	
		elseif @ExecutionLogic='玻璃破损' then
				call 	RobotHandFinsh_2(sendMessageHex,machineids,@returnvalue,3,@messagedate);
				set messagedate=@messagedate;	
 		elseif @ExecutionLogic='完成清空' then	
				set dates=CONCAT(@returnvalue,"0000");	
				call message(dates,'0014',@date);
				set messagedate=@date;
		elseif @ExecutionLogic='任务请求' then
				if machineids=38 or machineids=63 then
				  #select @remarks;
					select count(*) into @counts from gmms_galss_task_2  
					where work_state='已优化' and work_procedure='钢化上片' and orders is not null and groups=@remarks;
					select count(*) into @counts2 from gmms_drop_frame_2 
					where flag=1 and groups=@remarks and surplus_count<>0;
					#select @counts;
					#select @counts2;
					if @counts=0 and @counts2<>0 then
					#select @remarks;
						if @remarks=2 then
							set falgs=2;
						elseif @remarks=1 then 
							set falgs=1;
						end if;
					else
						set falgs=0;
					end if;
				end if;
				
				call RobotHandSend_2(rankids,machineids,@returnvalue,@messagedate);
				set messagedate=@messagedate;
				
 		elseif @ExecutionLogic='任务清空' then
			insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,"",NOW(),'发送清空任务',"","");
				set dates=CONCAT(@returnvalue,"000000000000000000000000000000000000");
				call message(dates,'000a',@date);
				set messagedate=@date;
		end if;

	end if;



END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for Robot_gangqian_finish_ex
-- ----------------------------
DROP PROCEDURE IF EXISTS `Robot_gangqian_finish_ex`;
delimiter ;;
CREATE PROCEDURE `Robot_gangqian_finish_ex`(in machineids BIGINT,out messagedate varchar(255))
BEGIN
	#下片机发送任务存储过程
	#找到等待中的任务，把下片位置，宽度，高度，厚度发送给plc
		DECLARE s int DEFAULT 0;
	DECLARE finshcount1 int;
	
	
	DECLARE y int DEFAULT 0;
	DECLARE j int DEFAULT 0;
	DECLARE processcardid2 varchar(255);
	
	DECLARE dates varchar(255);
	DECLARE noodles1 varchar(255);
	DECLARE finshcount int;
	DECLARE messagedates varchar(255) DEFAULT '';
	set messagedate=messagedates;
	
		select machine_type_id,work_id,remarks into @mtid,@remarks,@flag1 from gmms_machine where id=machineids;

	
	
	
	/*
	select @counts;
	select @galssid;
	#判断是否有等待的任务
	select @loadfalg;
	*/
	
	select @remarks;
	select machineids;
	if machineids=38 or machineids=63 then
	
	
		select count(*) into @counts from gmms_galss_task_2 gt INNER JOIN gmms_drop_frame_2 dr on gt.processcard_id=dr.processcard_id where dr.flag=1 and gt.work_state='已优化' and work_procedure='钢化上片' and gt.groups=@remarks and gt.orders is not null ;
		select @counts;
		select count(*) into @counts3 from gmms_galss_task_2  where work_state='正在工作' and work_procedure='钢化上片' and groups=@remarks ;
	

	
	select gt.id,width,height,thickness,dr.surplus_count,angle,dr.stops,dr.noodles,gt.marking_flag,gt.galss_id,gt.processcard_id into @id,@width,@height,@thickness,@surplus_count,@angle,@stops,@noodles,@marking_flag,@galss_id2,@gtprocesscard_id from gmms_galss_task_2 gt INNER JOIN gmms_drop_frame_2 dr on gt.processcard_id=dr.processcard_id where dr.flag=1 and dr.surplus_count>0 and gt.work_state='已优化' and work_procedure='钢化上片' and gt.groups=@remarks and gt.orders is not null ORDER BY gt.furnace,gt.orders limit 0,1;
	select @galss_id2;
	if @galss_id2=0 then
		select max(galss_id) into @galss1 from gmms_galss_task_2 where groups=@remarks;
		update gmms_galss_task_2 set galss_id=@galss1+1 where id=@id;
	end if;
	select galss_id into @galssid from gmms_galss_task_2 where id=@id;
	select count(*) into @counts2 from gmms_machine where state='允许' and remarks=@remarks;
		if @counts>0 and @counts2=3 and @galssid!=0 and @counts3=0 then
		 if @flag1=1 then
		 select @galssid;
			update gmms_galss_task_2 set work_state='正在工作' where work_state='已优化' and work_procedure='钢化上片' and groups=@remarks and galss_id=@galssid;
			
			
			#修改 添加动作识别
			if @angle=1 then 
				select gang_qian_zhuan into @angle from gmms_dongzuo_table where number=@marking_flag;
			else 
			    select gang_qian_bu_zhuan into @angle from gmms_dongzuo_table where number=@marking_flag ;
			end if;			
			   select gang_qian_zhua into @zhua_qu from gmms_dongzuo_table where number=@marking_flag;
			
			#####
			
			call intTo2ByteHex10_16_4(@galssid,@galssids);
			call intTo2ByteHex10_16_4(@surplus_count,@surplus_counts);
			call intTo2ByteHex10_16_4(@stops,@stopss);
			call intTo2ByteHex10_16_4(@width,@widths);
			call intTo2ByteHex10_16_4(@height,@heights);
			call intTo2ByteHex10_16_4(@thickness,@thicknesss);
			call intTo2ByteHex10_16_4(@angle,@angles);
			/*
			select @widths;
			select @heights;
			select @thicknesss;
			select @angles;
			*/
			if @noodles='A' then
				set noodles1='0001';
			else
				set noodles1='0002';
			end if;

	
			call intTo2ByteHex10_16_4(@angle,@angles);
			call intTo2ByteHex10_16_4(@zhua_qu,@z_zhuas);
			
			set dates=CONCAT(@galssids,noodles1,@z_zhuas,@stopss,@widths,@heights,@thicknesss,@angles,@surplus_counts,'0002');
			
			
			
			
		#	set dates=CONCAT(returnvalue,@galssids,noodles1,'0000',@stopss,@widths,@heights,@thicknesss,@angles,@surplus_counts);
			call message(dates,'000b',@date);
			set messagedate=@date;
			insert into A_Robotlog(groups,glassid,beigntime,endtime,state,processcard_id)
			VALUES(@remarks,@id,NOW(),null,messagedate,@gtprocesscard_id);
		 end if;
			
	
		end if;
	end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for RotatingRackFinsh_2
-- ----------------------------
DROP PROCEDURE IF EXISTS `RotatingRackFinsh_2`;
delimiter ;;
CREATE PROCEDURE `RotatingRackFinsh_2`(in sendMessageHex varchar(255),in machineids BIGINT,in returnvalue varchar(255),in flag int,out messagedate varchar(255))
BEGIN
	#下片机完成存储过程
	#找到正在工作的任务进行完成
	DECLARE count1 int DEFAULT 1;
	DECLARE dates varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	set messagedate=messagedates;
	
	select machine_type_id,work_id,remarks into @mtid,@work_groups,@remarks from gmms_machine where id=machineids;
	
	select flag;

	#任务完成
	if flag=1 then
		if machineids=55  then
			if (select count(*) from gmms_drop_frame_5 where groups_=2 and drop_groups=1 and sign=5)>0 then
				update gmms_drop_frame_5 set sign=6 where groups_=2 and drop_groups=1 and sign=5;
				call message(returnvalue,'0014',@date);
				set messagedate=@date;
				insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 				
				values (machineids,@work_groups,NOW(),'发送旋转完成',@date,messagedate);
			end if;								
		else
			select noodles into @noodles from gmms_drop_frame_2 dr where stops=@work_groups and dr.flag=1 and groups=@remarks ;
			select @noodles;
			update gmms_drop_frame_2 set flag=0  where noodles=@noodles and groups=@remarks and stops=@work_groups;
			update gmms_drop_frame_2 set flag=1  where noodles!=@noodles and groups=@remarks and stops=@work_groups;
			call message(returnvalue,'0014',@date);
			set messagedate=@date;
		end if;		
	#任务破损
	elseif flag=2 then
		if machineids=55 then		
			update gmms_drop_frame_5 set sign=3 where groups_=2 and drop_groups=1 and sign=5;		
		end if;
		call message(returnvalue,'0014',@date);
		set messagedate=@date;
	
	end if;

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for RotatingRackSend_2
-- ----------------------------
DROP PROCEDURE IF EXISTS `RotatingRackSend_2`;
delimiter ;;
CREATE PROCEDURE `RotatingRackSend_2`(in machineids BIGINT,in sendMessageHex varchar(255),in returnvalue varchar(255),out messagedate varchar(255))
BEGIN
	#下片机发送任务存储过程
	#找到等待中的任务，把下片位置，宽度，高度，厚度发送给plc
	DECLARE dates varchar(255);
	DECLARE noodless varchar(255);
	DECLARE noodless2 varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	set messagedate=messagedates;
	select machine_type_id,work_id,remarks into @mtid,@work_groups,@remarks from gmms_machine where id=machineids;
if machineids=55 then
	set noodless2 = substring(sendMessageHex,(cast(45 as UNSIGNED INTEGER)*4)+19,4);
	select count(*) into @count2 from gmms_drop_frame_5 where groups_=2 and drop_groups=1 and sign=3;
	
	if @count2>0 then
		if noodless2='0001' then
			set dates=CONCAT(returnvalue,'0000','0002');
			call message(dates,'000a',@date);
			set messagedate=@date;
			update gmms_drop_frame_5 set sign=5 where groups_=2 and drop_groups=1 and sign=3;
		elseif noodless2='0002' then
			set dates=CONCAT(returnvalue,'0000','0001');
			call message(dates,'000a',@date);
			set messagedate=@date;
			update gmms_drop_frame_5 set sign=5 where groups_=2 and drop_groups=1 and sign=3;
		end if;		
		
--  		insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) 
--  		values (machineids,@work_groups,NOW(),'发送旋转',@date,sendMessageHex);
	end if;	
else
		select noodles,surplus_count into @noodles,@surplus_count from gmms_drop_frame_2 
		where stops=@work_groups and flag=1 and groups=@remarks;
		select noodles,surplus_count into @noodles2,@surplus_count2 from gmms_drop_frame_2
		where stops=@work_groups and flag=0 and groups=@remarks;
		
		select count(*) into @loadworkingcount from gmms_galss_task_2  where work_state='正在工作' and work_procedure='钢化上片' and groups=@remarks ;
		select @noodles;
		select @surplus_count;
		select @surplus_count2;
		if (@surplus_count=0 or @surplus_count is null) and @surplus_count2>0 and @loadworkingcount=0 then
				if @noodles='B' then
					set noodless='0001';
				else
					set noodless='0002';
				end if;			
				set dates=CONCAT(returnvalue,'0000',noodless);
				call message(dates,'000a',@date);
				set messagedate=@date;
		end if;	
end if;
	
	
	
	
	
	
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for RotatingRack_2
-- ----------------------------
DROP PROCEDURE IF EXISTS `RotatingRack_2`;
delimiter ;;
CREATE PROCEDURE `RotatingRack_2`(in sendMessageHex varchar(255),in machineids BIGINT,out messagedate varchar(255))
BEGIN
	#下片机存储过程
	#找到当前机器标志位
	#查询机器逻辑表中对应的逻辑进行对应的步骤
	DECLARE machinedisposeids int DEFAULT 0;
	DECLARE ids int ;
	DECLARE plcfindid varchar(255);
	DECLARE dates varchar(255);
	DECLARE plcid varchar(255);
	DECLARE findplc varchar(255);
	DECLARE findmes varchar(255);
	DECLARE taskplc varchar(255);
	DECLARE taskmes varchar(255);
	DECLARE finsh_rankid BIGINT;
	DECLARE send_rankid BIGINT;
	DECLARE noodless2 varchar(255);
	DECLARE rankids varchar(255);
	DECLARE posunmes varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	set messagedate=messagedates;
	
	
	select machine_type_id,remarks into @mtid,@remarks from gmms_machine where id=machineids;
	set ids = cast(@remarks as UNSIGNED INTEGER);
	
	select mf.flag into @完成标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='完成字';
	
	select mf.flag into @请求标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='请求字';
	
	select mf.flag into @发送标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='发送字';
	
	select mf.flag into @完成确认标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='完成确认字';
	
	set finsh_rankid = cast(@完成标志位 as UNSIGNED INTEGER)+1;
	set send_rankid = cast(@请求标志位 as UNSIGNED INTEGER)+1;
	
	set findplc = substring(sendMessageHex,(cast(@完成标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskplc = substring(sendMessageHex,(cast(@请求标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskmes = substring(sendMessageHex,(cast(@发送标志位 as UNSIGNED INTEGER)*4)+19,4);
	set findmes = substring(sendMessageHex,(cast(@完成确认标志位 as UNSIGNED INTEGER)*4)+19,4);
	set noodless2 = substring(sendMessageHex,(cast(45 as UNSIGNED INTEGER)*4)+19,4);
	
	select taskplc;
	select taskmes;
	select findplc;
	select findmes;
	select plcfindid;
	
-- 	if machineids=55 then
-- 		if noodless2='0001' then
-- 			update gmms_drop_frame_4 set falg=1 where noodles=98;
-- 			update gmms_drop_frame_4 set falg=0 where noodles=99;
-- 		elseif noodless2='0002' then
-- 			update gmms_drop_frame_4 set falg=0 where noodles=98;
-- 			update gmms_drop_frame_4 set falg=1 where noodles=99;
-- 		end if;
-- 	end if;
	if machineids=55 then
		if noodless2='0001' or noodless2='0002' then
			update gmms_machine set state='允许' where id=machineids;
		else 
			update gmms_machine set state='禁止' where id=machineids;
		end if;
	else
		if taskplc='0001' then
			update gmms_machine set state='允许' where id=machineids;
		else 
			update gmms_machine set state='禁止' where id=machineids;
		end if;
	end if;
	
	
	set rankids = conv(substring(sendMessageHex,(cast(send_rankid as UNSIGNED INTEGER)*4)+19,4),16,10);
	select rankids;

#完成逻辑查询
	select count(*),md.id into @完成数量,@完成逻辑编号 from gmms_machine_dispose md join gmms_machine_type mt on md.machine_type_id=mt.id where md.machine_type_id=@mtid  and md.plc_font_type='完成字' and md.mes_font_type='完成确认字' and md.plc_font_value=findplc and md.mes_font_value=findmes;
	
	#请求逻辑查询
	select count(*),md.id into @请求数量,@请求逻辑编号 from gmms_machine_dispose md join gmms_machine_type mt on md.machine_type_id=mt.id where md.machine_type_id=@mtid  and md.plc_font_type='请求字' and md.mes_font_type='发送字' and md.plc_font_value=taskplc and md.mes_font_value=taskmes;
	
	
	#判断逻辑是否存在
	if @完成数量=1 or @请求数量=1  then
	
		#判断完成逻辑是否为空
		if @完成逻辑编号!=0 then
			set machinedisposeids=@完成逻辑编号;
			
		#判断请求逻辑是否为空
		elseif @请求逻辑编号!=0 then
			set machinedisposeids=@请求逻辑编号;
			
		end if;
		#查询当前执行逻辑进行判断
		select logic,return_value into @ExecutionLogic,@returnvalue from gmms_machine_dispose where id=machinedisposeids;
		select @ExecutionLogic;
		select @returnvalue;
		if @ExecutionLogic='任务完成' then
				call 	RotatingRackFinsh_2(sendMessageHex,machineids,@returnvalue,1,@messagedate);
				set messagedate=@messagedate;
		elseif @ExecutionLogic='任务失败' then
				call 	RotatingRackFinsh_2(sendMessageHex,machineids,@returnvalue,2,@messagedate);
				set messagedate=@messagedate;		
 		elseif @ExecutionLogic='完成清空' then	
				set dates=CONCAT(@returnvalue,"0000");	
				call message(dates,'0014',@date);
				set messagedate=@date;
		elseif @ExecutionLogic='任务请求' then
				call RotatingRackSend_2(machineids,sendMessageHex,@returnvalue,@messagedate);
				set messagedate=@messagedate;			
 		elseif @ExecutionLogic='任务清空' then
				set dates=CONCAT(@returnvalue,"0000000000000000");
				call message(dates,'000a',@date);
				set messagedate=@date;
		end if;

	end if;


END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for Shelf
-- ----------------------------
DROP PROCEDURE IF EXISTS `Shelf`;
delimiter ;;
CREATE PROCEDURE `Shelf`(in sendMessageHex varchar(255),in machineids BIGINT,out messagedate varchar(255))
BEGIN
	#仓储存储过程
	#找到当前机器标志位
	#查询机器逻辑表中对应的逻辑进行对应的步骤
	DECLARE machinedisposeids int DEFAULT 0;
	DECLARE dates varchar(255);
	DECLARE findplc varchar(255);
	DECLARE findmes varchar(255);
	DECLARE taskplc varchar(255);
	DECLARE taskmes varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	set messagedate=messagedates;
	
	select machine_type_id into @mtid from gmms_machine where id=machineids;
  call LoadShelfTaskAdd(1);
	call LoadShelfTaskAdd(12);
	call LoadShelfTaskAdd(13);
	
	
	select mf.flag into @完成标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='完成字';
	
	select mf.flag into @请求标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='请求字';
	
	select mf.flag into @发送标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='发送字';
	
	select mf.flag into @完成确认标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='完成确认字';
	
	set findplc = substring(sendMessageHex,(cast(@完成标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskplc = substring(sendMessageHex,(cast(@请求标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskmes = substring(sendMessageHex,(cast(@发送标志位 as UNSIGNED INTEGER)*4)+19,4);
	set findmes = substring(sendMessageHex,(cast(@完成确认标志位 as UNSIGNED INTEGER)*4)+19,4);
	select taskplc;
	select taskmes;
	select findplc;
	select findmes;

	#完成逻辑查询
	select count(*),md.id into @完成数量,@完成逻辑编号 from gmms_machine_dispose md join gmms_machine_type mt on md.machine_type_id=mt.id where md.machine_type_id=@mtid  and md.plc_font_type='完成字' and md.mes_font_type='完成确认字' and md.plc_font_value=findplc and md.mes_font_value=findmes;
	
	#请求逻辑查询
	select count(*),md.id into @请求数量,@请求逻辑编号 from gmms_machine_dispose md join gmms_machine_type mt on md.machine_type_id=mt.id where md.machine_type_id=@mtid  and md.plc_font_type='请求字' and md.mes_font_type='发送字' and md.plc_font_value=taskplc and md.mes_font_value=taskmes;


	#判断逻辑是否存在
	if @完成数量=1 or @请求数量=1 then
	
		#判断完成逻辑是否为空
		if @完成逻辑编号!=0 then
			set machinedisposeids=@完成逻辑编号;
			
		#判断请求逻辑是否为空
		elseif @请求逻辑编号!=0 then
			set machinedisposeids=@请求逻辑编号;
			
		end if;
		#查询当前执行逻辑进行判断
		select logic,return_value,mes_font_value into @ExecutionLogic,@returnvalue,@mes_font_value from gmms_machine_dispose where id=machinedisposeids;
		select @returnvalue;
		if @ExecutionLogic='任务完成' then
				call ShelfFinsh(sendMessageHex,machineids,@returnvalue,1,@messagedate);
				set messagedate=@messagedate;
 		elseif @ExecutionLogic='任务失败' then
				call ShelfFinsh(sendMessageHex,machineids,@returnvalue,2,@messagedate);
				set messagedate=@messagedate;
		elseif @ExecutionLogic='手动完成' then
				call ShelfFinsh(sendMessageHex,machineids,@returnvalue,1,@messagedate);
				set messagedate=@messagedate;
 		elseif @ExecutionLogic='完成清空' then
				select @returnvalue;
				call message(@returnvalue,'0014',@date);
				set messagedate=@date;
		elseif @ExecutionLogic='任务请求' then
				call ShelfSend(machineids,machinedisposeids,@messagedate);
				set messagedate=@messagedate;
 		elseif @ExecutionLogic='任务清空' then
				set dates=CONCAT(@returnvalue,"0000000000000000");
				select dates;
				call message(dates,'000a',@date);
				set messagedate=@date;
		else
				set messagedate='';
		end if;
	end if;
	
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for ShelfFinsh
-- ----------------------------
DROP PROCEDURE IF EXISTS `ShelfFinsh`;
delimiter ;;
CREATE PROCEDURE `ShelfFinsh`(in sendMessageHex varchar(255),in machineids BIGINT,in returnvalue varchar(255),in flag int,out messagedate varchar(255))
BEGIN
	#仓储完成存储过程
	#找到正在工作的任务，判断是什么类型的仓储任务，分别进行什么处理
	DECLARE messagedates varchar(255) DEFAULT '';
	set messagedate=messagedates;
	
	
	
	select count(*) into @counts from gmms_shelf_task where task_state='正在工作';
	select id,task_type,shelf_rack_id,load_rack into @taskid,@task_type,@shelf_rack_id,@load_rack_id from gmms_shelf_task where task_state='正在工作' limit 1;
	select rp.color into @color from  gmms_shelf_rack sr join gmms_raw_package rp on sr.raw_package_id1=rp.id where sr.id=@shelf_rack_id;
	
	#任务完成
	if flag=1 then 
	select @counts;
		if @counts>0 then
		
			#仓位上片位之间类型的任务
			if @task_type='从仓位到上片位' or @task_type='从上片位到仓位' then
			
				if @task_type='从仓位到上片位' then
				select @load_rack_id;
				select @shelf_rack_id;
					update gmms_shelf_rack set state='正在使用' where id=@shelf_rack_id;
					update gmms_load_rack set shelf_task_id=@taskid,state='正在工作',falg_shelf='1' where number=@load_rack_id;
					update gmms_shelf_task set task_state='已完成',finsh_time=NOW(),file_id=1 where id=@taskid;
					select returnvalue;
					call message(returnvalue,'0014',@date);
					set messagedate=@date;
				else
				select @taskid;
					select rp.left_pieces into @left_pieces from gmms_shelf_rack sr join gmms_raw_package rp on sr.raw_package_id1=rp.id where sr.id=@shelf_rack_id;
					#判断架子上面是否还有玻璃
					if @left_pieces=0 then
						update gmms_shelf_rack set state='空闲',raw_package_id1=null,mod_time=now(),color_falg=0 where id=@shelf_rack_id;
					else 
					# @color='白玻' or @color='超白' or @color='C' 
						if 1=1 then
							update gmms_shelf_rack set state='使用中',mod_time=now() where id=@shelf_rack_id;
						else
							if @left_pieces=1 then
								update gmms_shelf_rack set state='使用中',falg='禁用',mod_time=now() where id=@shelf_rack_id;
							else
								update gmms_shelf_rack set state='使用中',mod_time=now() where id=@shelf_rack_id;
							end if;
						end if;
					end if;
					update gmms_load_rack set state='空闲',shelf_task_id=Null,falg_shelf='0' where number=@load_rack_id;
					update gmms_shelf_task set task_state='已完成',finsh_time=NOW(),file_id=1 where id=@taskid;
					call message(returnvalue,'0014',@date);
					set messagedate=@date;
				end if;
			
			#仓位吊装位之间类型的任务
			elseif  @task_type='从仓位到吊装位' or @task_type='从吊装位到仓位' then
			
				if  @task_type='从仓位到吊装位' then
				
					update gmms_shelf_rack set state='正在使用' where id=@shelf_rack_id;
					update gmms_load_rack set shelf_task_id=@taskid,state='正在使用',falg_shelf='1' where number='99';
					update gmms_shelf_task set task_state='已完成',finsh_time=NOW(),file_id=1 where id=@taskid;
					call message(returnvalue,'0014',@date);
					set messagedate=@date;
				else 
					select rp.left_pieces into @剩余片数 from gmms_shelf_rack sr join gmms_raw_package rp on rp.id=sr.raw_package_id1 where sr.number=@shelf_rack_id;
					if @剩余片数=1 then
						update gmms_shelf_rack set state='使用中',mod_time=now(),falg='启用',color_falg=1 where id=@shelf_rack_id;
					elseif  @剩余片数>1 then
						update gmms_shelf_rack set state='使用中',mod_time=now(),falg='启用' where id=@shelf_rack_id;
					else
						update gmms_shelf_rack set state='空闲',raw_package_id1=null,mod_time=now(),falg='启用' where id=@shelf_rack_id;
					end if;
					update gmms_load_rack set state='空闲',shelf_task_id=Null,falg_shelf='0' where number='99';
					update gmms_shelf_task set task_state='已完成',finsh_time=NOW(),file_id=1 where id=@taskid;
					call message(returnvalue,'0014',@date);
					set messagedate=@date;
				end if;
			
			end if;
		else
			set messagedate='';
		end if;
		
		
		
	#任务失败，重新修改状态
	elseif flag=2 then
		if @counts=0 then
			update gmms_shelf_task set task_state='等待中' where id=@taskid;
		end if;
			call message(returnvalue,'0014',@date);
			set messagedate=@date;
		else
			set messagedate='';
	end if;

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for ShelfSend
-- ----------------------------
DROP PROCEDURE IF EXISTS `ShelfSend`;
delimiter ;;
CREATE PROCEDURE `ShelfSend`(in machineids BIGINT,in machinedisposeids BIGINT,out messagedate varchar(255))
BEGIN
	#仓储请求存储过程
	#循环等待中的仓储任务，循环上片位，判断是什么类型任务分别进行处理
	DECLARE s int DEFAULT 0;
	DECLARE x int DEFAULT 0;
	DECLARE load_racks int DEFAULT 0;
	DECLARE dates varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	set messagedate=messagedates;
	
	select return_value into @returnvalue from gmms_machine_dispose where id=machinedisposeids;
	
	set @shelfcount=null;
	set @shelfcounts=null;

	select count(*) into @shelfcount from gmms_shelf_task where task_state='等待中';
	select count(*) into @shelfcounts from gmms_shelf_task where task_state='正在工作';
	
		select id,task_id,task_state,task_type,groups,shelf_rack_id,load_rack into @id,@shelf_task_id,@task_state,@task_type,@groups,@shelf_rack_id,@load_rack from gmms_shelf_task where task_state='等待中' order by start_time limit 1;
	if @shelfcounts=0 then
		select @load_rack;
			select falg_shelf,falg into @falg_shelf,@falg from gmms_load_rack where number=@load_rack;
			select @falg_shelf;
			select @shelf_task_id;
			select @task_type;
			if @task_type='从上片位到仓位' then
				#判断上片位架子上面有玻璃，上片位是否启用
				if @falg='启用' and @falg_shelf='1' then
					update gmms_shelf_task set task_state='正在工作' where task_state='等待中' and id=@id;
					
					call intTo2ByteHex10_16_4(@shelf_rack_id,@shelfrackid);
					call intTo2ByteHex10_16_4(@load_rack,@loadrackid);
					set dates=CONCAT(@returnvalue,"0000",@loadrackid,@shelfrackid,"0001");
					call message(dates,'000a',@date);
					set messagedate=@date;
					
				end if;
			
			elseif @task_type='从仓位到上片位' then
				#判断上片位架子上面没有玻璃，上片位是否启用
				if @falg='启用' and @falg_shelf='0'  then
					update gmms_shelf_task set task_state='正在工作' where task_state='等待中' and id=@id;
					update gmms_shelf_rack set state='正在使用' where id=@shelf_rack_id;
					call intTo2ByteHex10_16_4(@shelf_rack_id,@shelfrackid);
					call intTo2ByteHex10_16_4(@load_rack,@loadrackid);
					set dates=CONCAT(@returnvalue,"0000",@shelfrackid,@loadrackid,"0001");
					select dates;
					call message(dates,'000a',@date);
					set messagedate=@date;
			
				end if;
				
			elseif @task_type='从吊装位到仓位' then
				#判断吊装位架子上面有玻璃，上片位是否启用
				if @falg='启用' and @falg_shelf='1'  then
					update gmms_shelf_task set task_state='正在工作' where task_state='等待中' and id=@id;
					call intTo2ByteHex10_16_4(@shelf_rack_id,@shelfrackid);
					call intTo2ByteHex10_16_4(@load_rack,@loadrackid);
					set dates=CONCAT(@returnvalue,"0000",@loadrackid,@shelfrackid,"0001");
					call message(dates,'000a',@date);
					set messagedate=@date;
			
				end if;
		
			elseif @task_type='从仓位到吊装位' then
			select @shelf_task_id;
				#判断吊装位架子上面没有玻璃，上片位是否启用，任务编号不等于null
				if @falg='启用' and @falg_shelf='0'  then
					update gmms_shelf_task set task_state='正在工作' where task_state='等待中' and id=@id;
					call intTo2ByteHex10_16_4(@shelf_rack_id,@shelfrackid);
					call intTo2ByteHex10_16_4(@load_rack,@loadrackid);
					set dates=CONCAT(@returnvalue,"0000",@shelfrackid,@loadrackid,"0001");
					call message(dates,'000a',@date);
					set messagedate=@date;
			
				end if;
			
			end if;

	end if;
	

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for Slicecage
-- ----------------------------
DROP PROCEDURE IF EXISTS `Slicecage`;
delimiter ;;
CREATE PROCEDURE `Slicecage`(in sendMessageHex varchar(255),in machineids BIGINT,out messagedate varchar(255),inout msg varchar(255))
BEGIN
	#理片笼存储过程
	#找到当前机器标志位
	#查询机器逻辑表中对应的逻辑进行对应的步骤
	DECLARE machinedisposeids int DEFAULT 0;
	DECLARE dates varchar(255);
	DECLARE findplc varchar(255);
	DECLARE findmes varchar(255);
	DECLARE taskplc varchar(255);
	DECLARE taskmes varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	set messagedate=messagedates;
	
	
		 	set @log_task="";


		SET msg:='Slicecage  查配置';	
	select machine_type_id,remarks into @mtid,@remarks from gmms_machine where id=machineids;
	

	
	select mf.flag into @完成标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='完成字';
	
	select mf.flag into @请求标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='请求字';
	
	select mf.flag into @发送标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='发送字';
	
	select mf.flag into @完成确认标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='完成确认字';
	
	set findplc = substring(sendMessageHex,(cast(@完成标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskplc = substring(sendMessageHex,(cast(@请求标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskmes = substring(sendMessageHex,(cast(@发送标志位 as UNSIGNED INTEGER)*4)+19,4);
	set findmes = substring(sendMessageHex,(cast(@完成确认标志位 as UNSIGNED INTEGER)*4)+19,4);
	select taskplc;
	select taskmes;
	select findplc;
	select findmes;
	call log_key_value(@log_task,"完成标志",findplc);
	call log_key_value(@log_task,"请求标志",taskplc);
	call log_key_value(@log_task,"发送标志",taskmes);
	call log_key_value(@log_task,"完成确认标志",findmes);
			
	#完成逻辑查询
	select count(*),md.id into @完成数量,@完成逻辑编号 from gmms_machine_dispose md join gmms_machine_type mt on md.machine_type_id=mt.id where md.machine_type_id=@mtid  and md.plc_font_type='完成字' and md.mes_font_type='完成确认字' and md.plc_font_value=findplc and md.mes_font_value=findmes;
	
	#请求逻辑查询
	select count(*),md.id into @请求数量,@请求逻辑编号 from gmms_machine_dispose md join gmms_machine_type mt on md.machine_type_id=mt.id where md.machine_type_id=@mtid  and md.plc_font_type='请求字' and md.mes_font_type='发送字' and md.plc_font_value=taskplc and md.mes_font_value=taskmes;
	
	#判断逻辑是否存在
	if @完成数量=1 or @请求数量=1 then
	
		#判断完成逻辑是否为空
		if @完成逻辑编号!=0 then
			set machinedisposeids=@完成逻辑编号;
			
		#判断请求逻辑是否为空
		elseif @请求逻辑编号!=0 then
			set machinedisposeids=@请求逻辑编号;
			
		end if;
			SET msg:='Slicecage  查逻辑标志';	
		#查询当前执行逻辑进行判断
		select logic,return_value into @ExecutionLogic,@returnvalue from gmms_machine_dispose where id=machinedisposeids;
		select @ExecutionLogic;
		
	
			call log_key_value(@log_task,"执行逻辑",@ExecutionLogic);
				call log_key_value(@log_task,"returnvalue",@returnvalue);
		
		if @ExecutionLogic='任务完成' then
			SET msg:='Slicecage  	#直通模式';	
				#直通模式
				if @remarks='3' then
					call SlicecageFinsh(machineids,machinedisposeids,3,@messagedate);
					set messagedate=@messagedate;	
				else
					call SlicecageFinsh(machineids,machinedisposeids,1,@messagedate);
					set messagedate=@messagedate;
				end if;
				
 		elseif @ExecutionLogic='完成破损' then
				call SlicecageFinsh(machineids,machinedisposeids,2,@messagedate);
				set messagedate=@messagedate;
 		elseif @ExecutionLogic='完成清空' then
				call message(@returnvalue,'0014',@date);
				set messagedate=@date;
		elseif @ExecutionLogic='进片请求' then
			SET msg:='Slicecage  	#进片请求';	
				call SlicecageSend(machineids,@returnvalue,1,@messagedate);
				set messagedate=@messagedate;
		elseif @ExecutionLogic='出片请求' then
		SET msg:='Slicecage  	#出片请求';	
		
				call SlicecageSend(machineids,@returnvalue,2,@messagedate);
				set messagedate=@messagedate;
		elseif @ExecutionLogic='进出片请求' then
			SET msg:='Slicecage  	#进出片请求';	
			call SlicecageSend(machineids,@returnvalue,3,@messagedate);
			set messagedate=@messagedate;
 		elseif @ExecutionLogic='任务清空' then
		     SET msg:='Slicecage  	#任务清空';	
				set dates=CONCAT(@returnvalue,"0000000000000000");
				call message(dates,'000a',@date);
				set messagedate=@date;
		end if;
	end if;
		set @log_nm=concat("冷加工理片",machineids);
    set @exist_nm=null;
	
    select `var_value`,var_name into @var_value,@exist_nm from memory_var_table where var_name=@log_nm;
		 if @exist_nm is null then 
		    insert into memory_var_table (var_name,var_value,update_time) values (@log_nm,@log_task,now());
			else
			    update memory_var_table set var_value=@log_task,update_time=now() where var_name=@log_nm;
		 end if;
		 
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for SlicecageFinsh
-- ----------------------------
DROP PROCEDURE IF EXISTS `SlicecageFinsh`;
delimiter ;;
CREATE PROCEDURE `SlicecageFinsh`(in machineids BIGINT,in machinedisposeids BIGINT,in flag int,out messagedate varchar(255))
BEGIN
	#理片笼完成存储过程
	#判断完成，破损，直通
	DECLARE messagedates varchar(255) DEFAULT '';
	set messagedate=messagedates;
	
	select machine_type_id,work_id into @mtid,@work_groups from gmms_machine where id=machineids;
	
	select file_name into @engineering_id from gmms_raw_file where operator=@work_groups and state='已领取';
	
	select return_value into @returnvalue from gmms_machine_dispose where id=machinedisposeids;
	
	select galss_id into @galss_id from gmms_galss_task where work_state='出库中' and work_procedure='理片' and engineering_id=@engineering_id limit 1;
	select @returnvalue;
	
	#完成
	if flag=1 then
		call intoutfinish(machineids,flag,@finsh,@err);
		call message('0001','0014',@date);
		set messagedate=@date;
	#破损
	elseif flag=2 then
		call intoutfinish(machineids,flag,@finsh,@err);
		call message('0001','0014',@date);
		set messagedate=@date;
	#直通
	elseif flag=3 then
		select max(out_rank) into @maxrank from gmms_galss_task where  engineering_id=@engineering_id;
		update gmms_galss_task set work_state='已出库',work_procedure='理片',finsh_time=NOW(),out_rank=@maxrank+1  where galss_id=@galss_id and work_state='出库中' and engineering_id=@engineering_id;
		update gmms_machine set remarks='' where id=machineids;
		call message(@returnvalue,'0014',@date);
		set messagedate=@date;
	end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for SlicecageSend
-- ----------------------------
DROP PROCEDURE IF EXISTS `SlicecageSend`;
delimiter ;;
CREATE PROCEDURE `SlicecageSend`(in machineids BIGINT,in returnvalue varchar(255),in flag int,out messagedate varchar(255))
BEGIN
	#理片笼请求存储过程
	#判断是进片还是出片还是进出片请求
	#发送数据：玻璃编号，理片笼架子
	DECLARE a int DEFAULT 0;
	DECLARE dates varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	set messagedate=messagedates;
	
	
		call log_msg(@log_task,"[SlicecageSend]");


-- select flag;

select machine_type_id,work_id into @mtid,@work_groups from gmms_machine where id=machineids;

-- select @work_groups;
	
select file_name into @engineering_id from gmms_raw_file where operator=@work_groups and state='已领取';

select @engineering_id;

select var into @var from gmms_slicecage_pattern where groups=@work_groups;

-- select @var;
-- select flag;


	call log_key_value(@log_task,"@var",@var);
		call log_key_value(@log_task,"flag",flag);
	
	
if @var='1' then
		
	if flag=1 then
				call input(machineids,@发送字,@发送ID,@格子,@err);
				call intTo2ByteHex10_16_4(@发送字,@returnvalue);
				call intTo2ByteHex10_16_4(@发送ID,@typeid);
				call intTo2ByteHex10_16_4(@格子,@slicecage_rank);
				set dates=CONCAT(@returnvalue,@typeid,@slicecage_rank);
				call message(dates,'000a',@date);
				set messagedate=@date;
	#出片
	elseif flag=2 then
				if @work_groups=3 and @work_groups=2 and position("P" in @engineering_id)>0 then
					call output2(machineids,@发送字,@发送ID1,@发送ID2,@格子,@err);
				else
					call output(machineids,@发送字,@发送ID1,@发送ID2,@格子,@err);
				end if;
				
				select @发送字,@发送ID1,@发送ID2,@格子;
				call intTo2ByteHex10_16_4(@发送字,@returnvalue);
				call intTo2ByteHex10_16_4(@发送ID1,@galssid1);
				call intTo2ByteHex10_16_4(@发送ID2,@galssid2);
				call intTo2ByteHex10_16_4(@格子,@slicecage_rank);
				set dates=CONCAT(@returnvalue,'0000',@slicecage_rank,'0000',@galssid1,@galssid2);
				call message(dates,'000a',@date);
				set messagedate=@date;
				
	#进出片
	elseif flag=3 then
				select a;
				#先出后进
				if a=0 then
				  select a;
					set @kuneishu=0;
					set @counts=0;
					select 库内数 into @kuneishu from processcard where 工程号=@engineering_id order by 库内数 desc limit 1;
					select @work_groups;
					select count(*) into @counts from gmms_slicecage where state='空闲' and gourp=@work_groups;
					select @kuneishu;
					#判断理片笼里面是否有20片玻璃
					if @kuneishu>10 or @counts<=1 then
						if @work_groups=3 and @work_groups=2 and position("P" in @engineering_id)>0 then

					call output2(machineids,@发送字,@发送ID1,@发送ID2,@格子,@err);
				else
					call output(machineids,@发送字,@发送ID1,@发送ID2,@格子,@err);
				end if;
						call log_key_value(@log_task,"发送子",@发送字);
						call intTo2ByteHex10_16_4(@发送字,@returnvalue);
						call intTo2ByteHex10_16_4(@发送ID1,@galssid1);
						call intTo2ByteHex10_16_4(@发送ID2,@galssid2);
						call intTo2ByteHex10_16_4(@格子,@slicecage_rank);				
						set dates=CONCAT(@returnvalue,'0000',@slicecage_rank,'0000',@galssid1,@galssid2);
						call message(dates,'000a',@date);
						set messagedate=@date;
						select @date;
						call log_key_value(@log_task,"msg",@date);
						if @date is not null then 
							set a=1;
						end if;
					end if;
				end if;
		
				#进片
				if a=0 then
				select a;
					call input(machineids,@发送字,@发送ID,@格子,@err);
					call intTo2ByteHex10_16_4(@发送字,@returnvalue);
					call intTo2ByteHex10_16_4(@发送ID,@typeid);
					call intTo2ByteHex10_16_4(@格子,@slicecage_rank);
					set dates=CONCAT(@returnvalue,@typeid,@slicecage_rank);
					select @slicecage_rank;
					select @returnvalue;
					select @typeid;
					call message(dates,'000a',@date);
					set messagedate=@date;
				end if;
	
	end if;
		
elseif @var='3' then
		
	if flag=1 then
				call input(machineids,@发送字,@发送ID,@格子,@err);
				call intTo2ByteHex10_16_4(@发送字,@returnvalue);
				call intTo2ByteHex10_16_4(@发送ID,@typeid);
				call intTo2ByteHex10_16_4(@格子,@slicecage_rank);
				set dates=CONCAT(@returnvalue,@typeid,@slicecage_rank);
				call message(dates,'000a',@date);
				set messagedate=@date;
		
		
	#进出片
	elseif flag=3 then
		select @work_groups;
					call input(machineids,@发送字,@发送ID,@格子,@err);
					call intTo2ByteHex10_16_4(@发送字,@returnvalue);
					call intTo2ByteHex10_16_4(@发送ID,@typeid);
					call intTo2ByteHex10_16_4(@格子,@slicecage_rank);
					set dates=CONCAT(@returnvalue,@typeid,@slicecage_rank);
					call message(dates,'000a',@date);
					set messagedate=@date;
	
	end if;
		
elseif @var='2' then

-- 	select rank,type_id,galss_id,processcard_id,width,height into @rankid,@type_id,@galss_id,@processcardid,@width,@height from gmms_galss_task where work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id order by rank limit 1;
-- 	UPDATE gmms_galss_task SET work_procedure='理片',work_state='出库中' where rank=@rankid and engineering_id=@engineering_id;
-- 	UPDATE gmms_var set value=格子 where varname='last_pos' and groups=@work_groups;
-- 	update gmms_machine set remarks='3' where id=machineids;
-- 	
-- 	call intTo2ByteHex10_16_4(3,@returnvalue);
-- 	call intTo2ByteHex10_16_4(@galss_id,@typeid);
-- 	call intTo2ByteHex10_16_4(1,@slicecage_rank);
-- 	set dates=CONCAT(@returnvalue,@typeid,@slicecage_rank);
-- 	call message(dates,'000a',@date);
-- 	set messagedate=@date;
	
	
	select rank,type_id,galss_id,processcard_id,width,height into @rankid,@type_id,@galss_id,@processcardids,@width,@height from gmms_galss_task where work_procedure='理片' and work_state='等待中' and engineering_id=@engineering_id order by rank limit 1;
	select @processcardids;
	if @processcardids is not null then
	
	select id into @drop_id from gmms_drop_frame where processcard_id=@processcardids;
	select @drop_id;
	if @drop_id is not null then
	
		UPDATE gmms_galss_task SET work_procedure='理片',work_state='出库中' where rank=@rankid and engineering_id=@engineering_id;
		UPDATE gmms_var set value=1 where varname='last_pos' and groups=@work_groups;
		update gmms_machine set remarks='3' where id=machineids;
		call intTo2ByteHex10_16_4(3,@returnvalue);
		call intTo2ByteHex10_16_4(@galss_id,@typeid);
		call intTo2ByteHex10_16_4(1,@slicecage_rank);
		set dates=CONCAT(@returnvalue,@typeid,@slicecage_rank);
		call message(dates,'000a',@date);
		set messagedate=@date;
	else
		SELECT id,stops into @drop_pos,@stops from gmms_drop_frame where state='空架' and groups=@work_groups limit 1;
		select @drop_pos;
		if @drop_pos  is not null then 
			SELECT 流程卡,架号,总数,工程号 into @processcardid,@drop_frame,@processcard_count,@engineering_ids from processcard_ex where  流程卡=@processcardids;
			UPDATE gmms_drop_frame set state='使用中',drop_frame=@drop_frame,processcard_id=@processcardid,processcard_count=@processcard_count,engineering_id=@engineering_ids where id=@drop_pos;
			UPDATE gmms_galss_task SET work_procedure='理片',work_state='出库中',stop_position=@stops where rank=@rankid and engineering_id=@engineering_id;
			UPDATE gmms_var set value=1 where varname='last_pos' and groups=@work_groups;
			update gmms_machine set remarks='3' where id=machineids;
			
			call intTo2ByteHex10_16_4(3,@returnvalue);
			call intTo2ByteHex10_16_4(@galss_id,@typeid);
			call intTo2ByteHex10_16_4(1,@slicecage_rank);
			set dates=CONCAT(@returnvalue,@typeid,@slicecage_rank);
			call message(dates,'000a',@date);
			set messagedate=@date;
		end if;
	end if;	
	end if;
	
	
elseif @var='4' then
			if @work_groups=3 and @work_groups=2  and position("P" in @engineering_id)>0 then
					call output2(machineids,@发送字,@发送ID1,@发送ID2,@格子,@err);
				else
					call output(machineids,@发送字,@发送ID1,@发送ID2,@格子,@err);
				end if;
				call intTo2ByteHex10_16_4(@发送字,@returnvalue);
				call intTo2ByteHex10_16_4(@发送ID1,@galssid1);
				call intTo2ByteHex10_16_4(@发送ID2,@galssid2);
				call intTo2ByteHex10_16_4(@格子,@slicecage_rank);
				set dates=CONCAT(@returnvalue,'0000',@slicecage_rank,'0000',@galssid1,@galssid2);
				call message(dates,'000a',@date);
				set messagedate=@date;
		
end if;
	
	
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for SteelAfterCompound
-- ----------------------------
DROP PROCEDURE IF EXISTS `SteelAfterCompound`;
delimiter ;;
CREATE PROCEDURE `SteelAfterCompound`()
BEGIN
	#Routine body goes here...
	#一号线钢化后复合台
	DECLARE dates varchar(255);
	DECLARE messagedate varchar(255);
	select count(*),edg_width,edg_height,thickness,angle,marking_flag 
	into @counts,@width,@height,@thickness,@angle,@marking_flag from gmms_galss_task_2 
	where galss_id=rankids and work_state='待复合' and work_procedure='钢化后复合' and groups=@work_groups;
	
	#此处修改					
	if @angle=1 then 
		select gang_hou_zhuan into @z_angle from gmms_dongzuo_table where gmms_dongzuo_table.number=@marking_flag;
  else
		select gang_hou_bu_zhuan into @z_angle from gmms_dongzuo_table where gmms_dongzuo_table.number=@marking_flag;
	end if;
	
	call intTo2ByteHex10_16_4(@width,@widths);
	call intTo2ByteHex10_16_4(@height,@heights);
	call intTo2ByteHex10_16_4(@thickness,@thicknesss);
	call intTo2ByteHex10_16_4(@z_angle,@angles);
					
	if @heights!=null and @heights!='' and 
		 @widths!=null and @widths!='' and
		 @thicknesss!=null and @thicknesss!='' and
	   @angles!=null and @angles!='' 	
	then
		if @angle=1 or @angle=3 then
			set dates=CONCAT(@heights,@widths,@thicknesss,@angles,'000000000001');
		else
			set dates=CONCAT(@widths,@heights,@thicknesss,@angles,'000000000001');
		end if;					
		call message(dates,'000e',@date);
		set messagedate=@date;	
		update gmms_galss_task_2 set work_state='正在工作' 
		where  galss_id=rankids and work_state='待复合' and work_procedure='钢化后复合' and groups=@work_groups;	
	end if;	
		
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for StopFinshManual
-- ----------------------------
DROP PROCEDURE IF EXISTS `StopFinshManual`;
delimiter ;;
CREATE PROCEDURE `StopFinshManual`(in gId varchar(50))
BEGIN
-- 查询玻璃工程信息
select processcard_id into @processcard_id from gmms_galss_task where id=gId;

set @sRank_=null;
-- 查询下一个的落架顺序
select count(*) into @sRank_ From gmms_galss_task_stock where processcard_id =@processcard_id;

-- 更新玻璃状态
update gmms_galss_task set work_state='已完成',work_procedure='下片' ,stop_rank=@sRank_+1,finsh_time=now(),work_procedure_number=500,broken_state=999 where id=gId;

-- 插入玻璃
select count(*) into @c1 from gmms_galss_task_details where id = gId;
select count(*) into @c2 from gmms_galss_task_stock where id = gId;

if @c1=0 then
insert into gmms_galss_task_details select * from gmms_galss_task where id=gId;
end if;
if @c2=0 then
insert into gmms_galss_task_stock select * from gmms_galss_task where id=gId;


end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for Stops
-- ----------------------------
DROP PROCEDURE IF EXISTS `Stops`;
delimiter ;;
CREATE PROCEDURE `Stops`(in sendMessageHex varchar(255),in machineids BIGINT,out messagedate varchar(255))
BEGIN
	#下片机存储过程
	#找到当前机器标志位
	#查询机器逻辑表中对应的逻辑进行对应的步骤
	DECLARE machinedisposeids int DEFAULT 0;
	DECLARE ids int ;
	DECLARE plcfindid varchar(255);
	DECLARE dates varchar(255);
	DECLARE plcid varchar(255);
	DECLARE findplc varchar(255);
	DECLARE findmes varchar(255);
	DECLARE taskplc varchar(255);
	DECLARE taskmes varchar(255);
	DECLARE finsh_rankid BIGINT;
	DECLARE send_rankid BIGINT;
	DECLARE rankids varchar(255);
	DECLARE posunmes varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	
	set @begin_time = null;
	set @end_time = null;
	set @begin_time = SYSDATE();
	
	
	set messagedate=messagedates;
	set @stops=null;
	
	select machine_type_id,remarks into @mtid,@remarks from gmms_machine where id=machineids;
	
	set ids = cast(@remarks as UNSIGNED INTEGER);
	select stops into @stops from gmms_drop_frame where id=@remarks;
	
	select mf.flag into @完成标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='完成字';
	
	select mf.flag into @请求标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='请求字';
	
	select mf.flag into @发送标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='发送字';
	
	select mf.flag into @完成确认标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='完成确认字';
	
	if machineids=5 or machineids=8 then
		set @完成标志位=@完成标志位-1;
		set @完成确认标志位=@完成确认标志位-4;
	end if;
	
	set finsh_rankid = cast(@完成标志位 as UNSIGNED INTEGER)+1;
	set send_rankid = cast(@请求标志位 as UNSIGNED INTEGER)+1;
	
	set findplc = substring(sendMessageHex,(cast(@完成标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskplc = substring(sendMessageHex,(cast(@请求标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskmes = substring(sendMessageHex,(cast(@发送标志位 as UNSIGNED INTEGER)*4)+19,4);
	set findmes = substring(sendMessageHex,(cast(@完成确认标志位 as UNSIGNED INTEGER)*4)+19,4);
	set plcfindid = substring(sendMessageHex,(cast(8 as UNSIGNED INTEGER)*4)+19,4);
	
	
	
	select taskplc;
	select taskmes;
	select findplc;
	select findmes;
	select plcfindid;
	
	set rankids = conv(substring(sendMessageHex,(cast(send_rankid as UNSIGNED INTEGER)*4)+19,4),16,10);
	select rankids;

#完成逻辑查询

	select count(*),md.id into @完成数量,@完成逻辑编号 from gmms_machine_dispose md join gmms_machine_type mt on md.machine_type_id=mt.id where md.machine_type_id=@mtid  and md.plc_font_type='完成字' and md.mes_font_type='完成确认字' and md.plc_font_value=findplc and md.mes_font_value=findmes;

	
	
	#请求逻辑查询
	select count(*),md.id into @请求数量,@请求逻辑编号 from gmms_machine_dispose md join gmms_machine_type mt on md.machine_type_id=mt.id where md.machine_type_id=@mtid  and md.plc_font_type='请求字' and md.mes_font_type='发送字' and md.plc_font_value=taskplc and md.mes_font_value=taskmes;
	
	if machineids=5 then
		if @stops>0 then
				update gmms_machine set remarks=null where id=machineids;
				call intTo2ByteHex10_16_4(@stops,@id);
				call message(@id,'0024',@date);
				set messagedate=@date;
		end if;
	end if;
	
	
				
		select plcfindid,@完成数量,@请求数量,@stops,@remarks;
	if plcfindid="0001" then
				
				set dates=CONCAT("000000000000");	
				call message(dates,'001b',@date);
				set messagedate=@date;
	elseif @stops>0 then
		#界面点的
			update gmms_machine set remarks=null where id=machineids;
				call intTo2ByteHex10_16_4(@stops,@id);
				set dates=CONCAT("0001",@id);	
				call message(dates,'001b',@date);
				set messagedate=@date;
	#判断逻辑是否存在
	elseif @完成数量=1 or @请求数量=1  then
		#判断完成逻辑是否为空
		if @完成逻辑编号!=0 then
			set machinedisposeids=@完成逻辑编号;
			set rankids = conv(substring(sendMessageHex,(cast(finsh_rankid as UNSIGNED INTEGER)*4)+19,4),16,10);
		#判断请求逻辑是否为空
		elseif @请求逻辑编号!=0 then
			set machinedisposeids=@请求逻辑编号;
			set rankids = conv(substring(sendMessageHex,(cast(send_rankid as UNSIGNED INTEGER)*4)+19,4),16,10);
			
		end if;
		#查询当前执行逻辑进行判断
		select logic,return_value into @ExecutionLogic,@returnvalue from gmms_machine_dispose where id=machinedisposeids;
		if @ExecutionLogic='任务完成' then
				call 	StopsFinsh(sendMessageHex,machineids,@returnvalue,1,rankids,@messagedate);
				set messagedate=@messagedate;
		elseif @ExecutionLogic='完成破损' then
				call 	StopsFinsh(sendMessageHex,machineids,@returnvalue,2,rankids,@messagedate);
				set messagedate=@messagedate;		
 		elseif @ExecutionLogic='完成清空' then	

				set dates=CONCAT(@returnvalue,"0000");	
				if machineids=5 or machineids=8 then 
					call message(dates,'0014',@date);
				else
					call message(dates,'0018',@date);
				end if;
				set messagedate=@date;
				
				
		elseif @ExecutionLogic='任务请求' then
					call StopsSend(machineids,@returnvalue,rankids,@messagedate);
					set messagedate=@messagedate;
					select '请求';
				
 		elseif @ExecutionLogic='任务清空' then
				
				set dates=CONCAT(@returnvalue,"000000000000000000000000000000000000");
				call message(dates,'000a',@date);
				set messagedate=@date;

				
		end if;
	#根据发送过来的id查找任务发送过去
-- 	elseif rankids>0 then
-- -- 			set rankids = conv(substring(sendMessageHex,(cast(send_rankid as UNSIGNED INTEGER)*4)+19,4),16,10);
-- -- 			select rankids;
-- 		if  machineids=22 then
-- 			call StopsSend(machineids,@returnvalue,rankids,@messagedate);
-- 			set messagedate=@messagedate;
-- 		end if;
	end if;
set @end_time = SYSDATE();
#  机器人id等于75时是一号线下片机的任务
if machineids=75 then
	insert into A_JiXieHand(begin_time,end_time,content)values
	# @ExecutionLogic 做拼接将这个任务拼接记录
	(@begin_time,@end_time,CONCAT(IFNULL(@ExecutionLogic,''),':',IFNULL(messagedate,'')));
end if;


END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for StopsFinsh
-- ----------------------------
DROP PROCEDURE IF EXISTS `StopsFinsh`;
delimiter ;;
CREATE PROCEDURE `StopsFinsh`(in sendMessageHex varchar(255),in machineids BIGINT,in returnvalue varchar(255),in flag int,in rankid BIGINT,out messagedate varchar(255))
BEGIN
	#下片机完成存储过程
	#找到正在工作的任务进行完成
	DECLARE count1 int;
	DECLARE count2 int;
	DECLARE dates varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';

	
	set messagedate=messagedates;

	
	select machine_type_id,work_id into @mtid,@work_groups from gmms_machine where id=machineids;
	
	select file_name into @engineering_id from gmms_raw_file where operator=@work_groups and state='已领取';
	
	select rankid;
	
	select count(*),drop_frame,processcard_id,galss_type,id,stop_position into @counts,@drop_frame,@processcard_id,@galss_type,@id1,@stoppost from gmms_galss_task 
	where work_state='正在工作' and work_procedure='下片' and out_rank=rankid and engineering_id=@engineering_id;
	select stops,finsh_count,processcard_count into @id,@sum,@processcard_count from gmms_drop_frame where drop_frame=@drop_frame and processcard_id=@processcard_id and groups=@work_groups;
	
	select count(*) into @maxstoprank from gmms_galss_task_stock where id>(select min(id) from (select * from gmms_galss_task_stock order by id desc limit 40000) as tbmin) and processcard_id=@processcard_id;
	
	select @counts,1,flag;
	#任务完成
	if flag=1 then
		 
		if machineids=5 or machineids=8 then
			if @counts>0 then
				set count1=@maxstoprank+1;
				set count2=@sum+1;
				if @stoppost>12 and false then
					update gmms_galss_task set work_state='已完成',gc_stockid=@work_groups,stop_rank=count1,finsh_time=now(),work_procedure_number=500 
					where work_state='等待中' and work_procedure='机器人下片' and out_rank=rankid and engineering_id=@engineering_id;
				else
					update gmms_galss_task set work_state='已完成',gc_stockid=@work_groups,stop_rank=count1,finsh_time=now(),work_procedure_number=500 
					where work_state='正在工作' and work_procedure='下片' and out_rank=rankid and engineering_id=@engineering_id;
				end if;
				
				update gmms_drop_frame set finsh_count=count2 where processcard_id=@processcard_id and groups=@work_groups;
				select count(*) into @count8	from gmms_galss_task_stock where id=@id1;
				if @galss_type=0 and @count8=0 and @stoppost<13 then
					insert into gmms_galss_task_details select * from gmms_galss_task where engineering_id=@engineering_id and out_rank=rankid;
					insert into gmms_galss_task_stock select * from gmms_galss_task where engineering_id=@engineering_id and out_rank=rankid;
				end if;
				select count(*) into @findcount from gmms_galss_task where processcard_id=@processcard_id and engineering_id=@engineering_id and work_state='已完成' and work_procedure='下片';
				select count(*) into @posuncount from gmms_galss_task where processcard_id=@processcard_id and engineering_id=@engineering_id and work_state='破损';
				if @processcard_count=@findcount+@posuncount then
					call workmanships(@processcard_id);
				end if;
				if @processcard_count=count2 then
					call intTo2ByteHex10_16_4(@id,@ids);
					set dates=CONCAT(@ids,'0000',returnvalue);
					call message(dates,'0012',@date);
					set messagedate=@date;
				else
					call message(returnvalue,'0014',@date);
					set messagedate=@date;
				end if;
			else
				call message(returnvalue,'0014',@date);
				set messagedate=@date;
			end if;
		elseif machineids=75 then
			#新加机械手
			call y_RobotLengFinsh(flag,machineids,returnvalue,rankid,dates);
			set messagedate=dates;
		elseif machineids=17 or machineids=22 then 
			if @counts>0 then
				set count1=@maxstoprank+1;
				set count2=@sum+1;
 				update gmms_galss_task set work_state='已完成',gc_stockid=@work_groups,stop_rank=count1,finsh_time=now(),work_procedure_number=500 
				where work_state='正在工作' and work_procedure='下片' and out_rank=rankid and engineering_id=@engineering_id;
				update gmms_drop_frame set finsh_count=count2 where processcard_id=@processcard_id and groups=@work_groups;
				select count(*) into @count8	from gmms_galss_task_stock where id=@id1;
				if @galss_type=0 and @count8=0 then
					insert into gmms_galss_task_details select * from gmms_galss_task where engineering_id=@engineering_id and out_rank=rankid;
					insert into gmms_galss_task_stock select * from gmms_galss_task where engineering_id=@engineering_id and out_rank=rankid;
				end if;
				select count(*) into @findcount from gmms_galss_task where processcard_id=@processcard_id and engineering_id=@engineering_id and work_state='已完成' and work_procedure='下片';
				select count(*) into @posuncount from gmms_galss_task where processcard_id=@processcard_id and engineering_id=@engineering_id and work_state='破损';
				if @processcard_count=@findcount+@posuncount then
					call workmanships(@processcard_id);
				end if;
				if @processcard_count=count2 then
					call intTo2ByteHex10_16_4(@id,@ids);
					set dates=CONCAT(returnvalue,'00000000000000000001');
					call message(dates,'0018',@date);
					set messagedate=@date;
				else
					call message(returnvalue,'0018',@date);
					set messagedate=@date;
				end if;
			
	
			end if;
		end if;
		
	#任务破损
	elseif flag=2 then
	
		if @counts>0 then
			if machineids=5 or machineids=8 then
				update gmms_galss_task set work_state='破损',work_procedure_number=500 where work_state='正在工作' and work_procedure='下片' and out_rank=rankid and engineering_id=@engineering_id;
				call message(returnvalue,'0014',@date);
				set messagedate=@date;
			elseif machineids=17 or machineids=22 then 
				update gmms_galss_task set work_state='破损',work_procedure_number=500 where work_state='正在工作' and work_procedure='下片' and out_rank=rankid and engineering_id=@engineering_id;
				call message(returnvalue,'0018',@date);
				set messagedate=@date;
			end if;
			
	
		end if;
	
	end if;

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for StopsSend
-- ----------------------------
DROP PROCEDURE IF EXISTS `StopsSend`;
delimiter ;;
CREATE PROCEDURE `StopsSend`(in machineids BIGINT,in returnvalue varchar(255),in rankid BIGINT,out messagedate varchar(255))
BEGIN
	#下片机发送任务存储过程
	#找到等待中的任务，把下片位置，宽度，高度，厚度发送给plc
	DECLARE dates varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	set messagedate=messagedates;
	
	select machine_type_id,work_id into @mtid,@work_groups from gmms_machine where id=machineids;
	
	select file_name into @engineering_id from gmms_raw_file where operator=@work_groups and state='已领取';
	select @engineering_id;
	
	if machineids=17 or machineids=22 then
		
		select count(*) into @counts from gmms_galss_task where work_state='等待中' and work_procedure='下片' and out_rank=rankid and engineering_id=@engineering_id;
		select drop_frame,width,height,thickness,processcard_id,galss_type,color into @drop_frame,@width,@height,@thickness,@processcard_id,@galss_type,@color from gmms_galss_task where work_state='等待中' and work_procedure='下片' and out_rank=rankid and engineering_id=@engineering_id; 
		select stops,finsh_count into @id,@finsh_count from gmms_drop_frame where drop_frame=@drop_frame and processcard_id=@processcard_id and groups=@work_groups;
		select @id,@processcard_id,@counts;
		#判断是否有等待的任务
		
		if @counts=1 then		
			call intTo2ByteHex10_16_4(@width,@widths);
			call intTo2ByteHex10_16_4(@height,@heights);
			call intTo2ByteHex10_16_4(@thickness,@thicknesss);
			call intTo2ByteHex10_16_4(@id,@ids);
			call intTo2ByteHex10_16_4(rankid,@rankids);
			select @widths,@heights,@thicknesss,@ids;
			
			if machineids=17 or machineids=22 then 
		    select marking_flag into @z_mkk from gmms_galss_task  where work_state='等待中' and work_procedure='下片' and out_rank=rankid and engineering_id=@engineering_id; 
				select qing_xi_hou_zhua into @z_zhua from gmms_dongzuo_table where number=@z_mkk;
		    set @z_zhua=CONCAT("000",@z_zhua);
				if @galss_type=0 and @color!='F' then
					set dates=CONCAT(returnvalue,@rankids,'00000000',@ids,@heights,@widths,@thicknesss,@z_zhua);
				else
					set dates=CONCAT(returnvalue,@rankids,'00000000',@ids,@heights,@widths,@thicknesss,'0001');
				end if;	
			#	set dates=CONCAT(returnvalue,@rankids,'00000000',@ids,@heights,@widths,@thicknesss,'0001');
			else 		
				set dates=CONCAT(returnvalue,@rankids,'00000000',@ids,@heights,@widths,@thicknesss,'0001');
			end if;
			
			call message(dates,'000a',@date);				
			set messagedate=@date;
			if messagedate is not null then		
				update gmms_galss_task set work_state='正在工作' where work_state='等待中' and work_procedure='下片' and out_rank=rankid and 	engineering_id=@engineering_id;
			end if;
			
		end if;
	elseif machineids=75 then
		#新加机械手
		
		call y_RobotLeng(machineids,returnvalue,rankid,dates);
		set messagedate=dates;
	elseif machineids=5 then
		#一号线 下片机
		select count(*) into @counts from gmms_galss_task 
		where work_state='等待中' and work_procedure='下片'  and engineering_id=@engineering_id order by out_rank limit 0,1;
		
		select out_rank,stop_position into @out_rank1,@stop_position from gmms_galss_task 
		where work_state='正在工作' and work_procedure='下片'  and engineering_id=@engineering_id limit 0,1;
		
-- 		select id,drop_frame,width,height,thickness,processcard_id,out_rank into @gid_,@drop_frame,@width,@height,@thickness,@processcard_id,@out_rank from gmms_galss_task 
-- 		where work_state='等待中' and work_procedure='下片'  and engineering_id=@engineering_id order by out_rank limit 0,1;
		#修改   发送磨边后的宽高   下片机精度从0.1mm>1mm  2024/1/19
		select id,drop_frame,edg_width,edg_height,thickness,processcard_id,out_rank into @gid_,@drop_frame,@width,@height,@thickness,@processcard_id,@out_rank from gmms_galss_task 
		where work_state='等待中' and work_procedure='下片'  and engineering_id=@engineering_id order by out_rank limit 0,1;
		
		
		select stops,finsh_count into @id,@finsh_count from gmms_drop_frame 
		where drop_frame=@drop_frame and processcard_id=@processcard_id and groups=@work_groups;
		
		
		select processcard_id into @processcard_id1 from gmms_drop_frame where  stops=1 and groups=@work_groups;
		select processcard_id into @processcard_id2 from gmms_drop_frame where  stops=2 and groups=@work_groups;
		select count(*) into @sumprocesscardid1 from gmms_galss_task where processcard_id=@processcard_id1 and work_state!='破损' and engineering_id=@engineering_id;
		select count(*) into @sumprocesscardid2 from gmms_galss_task where processcard_id=@processcard_id2 and work_state!='破损' and engineering_id=@engineering_id;
		if @id=3 or @id=4 or @id=5 then
			set @id=3;
			if @sumprocesscardid1=0 then
				set @sumprocesscardid1=88;
			end if;	
			if @sumprocesscardid2=0 then
				set @sumprocesscardid2=88;
			end if;
		end if;
		#判断是否有等待的任务
		select @counts,@out_rank1,@stop_position,@engineering_id,@id;
		
		#set @counts=1,@out_rank=1,@width=934,@height=483,@thickness=5,@id=3,@sumprocesscardid1=0,@sumprocesscardid2=0;
		
		if @counts=1 then 
			if @out_rank is not null then 
					if @id=3 then
						update gmms_galss_task set work_state='等待中',work_procedure='机器人下片' 
						where work_state='等待中' and work_procedure='下片' and out_rank=@out_rank and engineering_id=@engineering_id;
					else
						update gmms_galss_task set work_state='正在工作' where work_state='等待中' and work_procedure='下片' and out_rank=@out_rank and engineering_id=@engineering_id;
					end if;		
					call intTo2ByteHex10_16_4(@out_rank,@out_ranks);
					call intTo2ByteHex10_16_4(@width,@widths);
					call intTo2ByteHex10_16_4(@height*10,@heights);
					call intTo2ByteHex10_16_4(@thickness*10,@thicknesss);
					call intTo2ByteHex10_16_4(@id,@ids);
					call intTo2ByteHex10_16_4(@sumprocesscardid1,@sumprocesscardids1);
					call intTo2ByteHex10_16_4(@sumprocesscardid2,@sumprocesscardids2);
					select @widths;
					select @heights;
					select @thicknesss;
					select @ids;
					set dates=CONCAT(returnvalue,@out_ranks,@ids,@widths,@heights,@thicknesss,@sumprocesscardids1,@sumprocesscardids2);
					call message(dates,'000a',@date);
					set messagedate=@date;
					insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,IFNULL(@id,0),NOW(),'发送下片机任务',IFNULL(@gid_,0),messagedate);
			end if;
		
		end if;
-- 		if (select software_connect from gmms_machine where id=5)=0 then
-- 			#set dates=CONCAT(returnvalue,'0001','0001','7fbc','3a98','003c');
-- 			set dates=CONCAT(returnvalue,'0001','0001','0ccf','5dc0','003c');
-- 			call message(dates,'000a',@date);
-- 			set messagedate=@date;
-- 			update gmms_machine set software_connect=10 where id=5;
-- 			insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,0,NOW(),'发送下片机测试任务',0,messagedate);
-- 		end if;
		
		if @counts=1 and false then
			if @out_rank1 is not null then
				if @stop_position=2 then
					update gmms_galss_task set work_state='正在工作' 
					where work_state='等待中' and work_procedure='下片' and out_rank=@out_rank and engineering_id=@engineering_id;												
					call intTo2ByteHex10_16_4(@out_rank,@out_ranks);
					call intTo2ByteHex10_16_4(@width*10,@widths);
					call intTo2ByteHex10_16_4(@height*10,@heights);
					call intTo2ByteHex10_16_4(@thickness*10,@thicknesss);
					call intTo2ByteHex10_16_4(@id,@ids);
					call intTo2ByteHex10_16_4(@sumprocesscardid1,@sumprocesscardids1);
					call intTo2ByteHex10_16_4(@sumprocesscardid2,@sumprocesscardids2);
					select @widths;
					select @heights;
					select @thicknesss;
					select @ids;
					set dates=CONCAT(returnvalue,@out_ranks,@ids,@widths,@heights,@thicknesss,@sumprocesscardids1,@sumprocesscardids2);
					call message(dates,'000a',@date);
					set messagedate=@date;
				end if;
			else
				select @counts,@out_rank1,@stop_position,@engineering_id ;
				update gmms_galss_task set work_state='正在工作' 
				where work_state='等待中' and work_procedure='下片' and out_rank=@out_rank and engineering_id=@engineering_id;
				call intTo2ByteHex10_16_4(@out_rank,@out_ranks);
				call intTo2ByteHex10_16_4(@width*10,@widths);
				call intTo2ByteHex10_16_4(@height*10,@heights);
				call intTo2ByteHex10_16_4(@thickness*10,@thicknesss);
				call intTo2ByteHex10_16_4(@id,@ids);
				call intTo2ByteHex10_16_4(@sumprocesscardid1,@sumprocesscardids1);
				call intTo2ByteHex10_16_4(@sumprocesscardid2,@sumprocesscardids2);
				select @widths;
				select @heights;
				select @thicknesss;
				select @ids;
				set dates=CONCAT(returnvalue,@out_ranks,@ids,@widths,@heights,@thicknesss,@sumprocesscardids1,@sumprocesscardids2);
				call message(dates,'000a',@date);
				set messagedate=@date;
			end if;
			
		end if;
	
	end if;

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for supplement_action
-- ----------------------------
DROP PROCEDURE IF EXISTS `supplement_action`;
delimiter ;;
CREATE PROCEDURE `supplement_action`(in action varchar(30),in ids text,out message varchar(255))
BEGIN
   DECLARE thick double;
	 DECLARE color varchar(30);
	 DECLARE p_id bigint;
	 set thick=0;
  set message="nothing";
 
	#Routine body goes here...
   if action="cancel" then
		   set @idx=0;
		   set @id="";
			while @id is not null do  
			  set @id=null;
				call getId(ids,@idx, @id);
			   set @idx=@idx+1;
				 if @id is not null then 
				    update supplement_liuchengkafua set state="撤销" where id=@id and state="未领取";
				 end if;			
			end while;
			set message="success";
	 end if;
	 
	 
	 if action="create" then 
	      set @id="";
			# 检查是否有不同磨系厚度
			set @idx=0;
			while @id is not null do
			  set @id=null;
				call getId(ids,@idx, @id);
			   set @idx=@idx+1;
				 if @id is not null then 
				     SET @thick=null;
						 SELECT houdu,yanse into @thick,@color from supplement_liuchengkafua where id=@id and state="未领取";
				     if @thick is null then 
						    set @id=null;
								set thick=null;
								set message="只有'未领取'的补片项才能创建补片工程";
							else 
						     if thick=0 then 
						         set thick=@thick;
									   set color=@color;
								  else 
									   if thick!=@thick then 
										   set @id=null;
											 set thick=null;
											 set message="只有相同厚度补片项才能创建工程";
										 end if;
						      end if;
						 end if;	 
				 end if;			
			 end while;
	    if thick is not null then 
			if thick>0 then 
					set @inc_id=1;
					set @firstcard=null;
				  set @gongcheng=null;
					set @gongcheng_id=null;
					call create_empty_raw_file("补片",@gongcheng_id, @gongcheng);
					set @id=1;
					set @idx=0;
				  	while @id is not null do
							set @id=null;
							call getId(ids,@idx, @id);
							if @id is not null then 
								set @shuliang=0;
							  SELECT liuchengkahao,kuan,gao,houdu,yanse,shuliang into @card,@kuan,@gao,@houdu,@yanse,@shuliang  from supplement_liuchengkafua where id=@id and state='未领取';			
							 	set @marking_flag=1;
								 if @kuan<@gao then 
								    set @tmpw=@kuan;
										set @kuan=@gao;
										set @gao=@tmpw;
										set @marking_flag=2;
								 end if;
							 
							 while @shuliang>0 do

							  if @firstcard is null then 
								 set @firstcard=@card;
								end if;
								 INSERT INTO  gmms_galss_task_buffer
								 (rank,type_id,galss_id,pattern_id,width,height
,thickness,color,edg_width,edg_height,chamfer_size,galss_type,edg_speed1,edg_speed2
,fine_throwing,work_state,work_procedure,slicecage_rank,drop_frame,slicecage_flag
,out_rank,start_time,finsh_time,stop_position,processcard_id,engineering_id
,broken_state,stop_rank,work_procedure_number,gc_number,gc_width,gc_height
,gc_stockid,marking_flag
)  
							 	 VALUES
								 (0,0,@inc_id,null,@kuan,@gao,@houdu,@yanse,@kuan,@gao,0,0,0,0
								 ,0,'待识别','测量','0','A1','0',
								 0,now(),null,0,@firstcard,@gongcheng,0,0,100,@gongcheng,0,0,0,@marking_flag
								 );
								set @inc_id=@inc_id+1;
							 
							   set @shuliang=@shuliang-1;
							 end while;
								 update supplement_liuchengkafua set state='已领取' where id=@id;
							    set @idx=@idx+1;
							end if;
						 
					  end while;
						
				
						call make_galss_task_typeid(@gongcheng);
						insert into gmms_pattern_task_buffer (rank,raw_type,raw_thickness,raw_width,raw_height,color,work_state,engineering_id,task_id)
						value (0,concat(thick,@color),thick,0,0,@color,'已完成',@gongcheng,0);
						
						set p_id=LAST_INSERT_ID();
						update gmms_galss_task_buffer set pattern_id=p_id where engineering_id=@gongcheng;
						
						
					set message="success";		
			 else 
			    set message="nopiece";
			end if;
			 end if;
	 end if;
	  
		
	 
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for task_changed
-- ----------------------------
DROP PROCEDURE IF EXISTS `task_changed`;
delimiter ;;
CREATE PROCEDURE `task_changed`(in _id bigint, in ow varchar(50),in os varchar(50),in nw varchar(50),in ns varchar(50) ,in work_seg varchar(50))
BEGIN
	#Routine body goes here...
	declare cr_w varchar(50);
	declare cr_st int;
	declare cr_ss int;
	declare done int default(0);
	#  日志状态查询游标
declare cur cursor for
		  	SELECT work_name,`status`,`segment_status` from gmms_glass_log_config where segment=work_seg  
				and work_proc=nw and work_status=ns order by  work_number asc;
				DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
		set @tm=now();		#日志时间
		set @ss=null;		  #存玻璃在该工艺段的全局状态
		set @wk=null;			#存当前工艺段的名称
		open cur;
		loop1: loop
				FETCH cur into cr_w,cr_st,cr_ss;
				if done=1 then 
					   LEAVE loop1;
				end if;
				# 更新开始时间和状态
				if cr_st=1 then 
						UPDATE gmms_glass_log  set `status`=cr_st ,begin_time=@tm where glass_id=_id and segment_name=work_seg and work_name=cr_w and freeze=false;
				end if;
				# 更新结束时间和状态
				if cr_st>=100 then 
						UPDATE gmms_glass_log  set `status`=cr_st ,end_time=@tm where glass_id=_id and segment_name=work_seg and work_name=				cr_w and freeze=false;
						end if;
				# 存最后一个工艺段全局状态
				if cr_ss is not null then 
					set @ss=cr_ss;
					set @wk=cr_w;
				end if;		
		end loop loop1;
		close cur;

			# 这是一个重置标记，要冻结就日志，创建新日志
			if @ss=0 then 
					UPDATE gmms_glass_log set freeze=true where glass_id=_id  and segment_name=work_seg; 
					call glass_log_new(_id,work_seg);
			end if;		
			if @ss=1 then 
						UPDATE gmms_glass_log  set `status`=@ss ,begin_time=@tm,work_name=@wk where glass_id=_id and segment_name=work_seg 						and  freeze=false and parent_id is null;
			end if;
			if @ss>=100 then 
						UPDATE gmms_glass_log  set `status`=@ss ,end_time=@tm,work_name=@wk where glass_id=_id and segment_name=work_seg 						and  freeze=false and parent_id is null;
			end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for TemperingDamaged
-- ----------------------------
DROP PROCEDURE IF EXISTS `TemperingDamaged`;
delimiter ;;
CREATE PROCEDURE `TemperingDamaged`(in Damagedid BIGINT,out count BIGINT,out msg varchar(255))
BEGIN
	#Routine body goes here...
	#钢化破损  传入任务ID  返回修改行数
	update gmms_galss_task_2 set work_state='破损' where id=Damagedid;
	
	INSERT INTO e_pierceds(processcard_id, type_num, pieces, pierced_type, pierced_cause, is_report, glass_id, create_time, process, line, gc_number, cs_number, team_name)
	select gt.processcard_id,gt.edg_speed2,1,null,null,0,gt.id,NOW(),'钢化',gt.groups,gt.engineering_id,0,guc.banzu from gmms_galss_task_2 as gt 
	INNER join gmms_user_current as guc on gt.groups=guc.chanxian 
	left join e_pierceds as et on gt.id=et.glass_id 
	where guc.gongyi='钢化' and gt.id=Damagedid and et.glass_id is null;
	
	select count(*) into @Damagedcount from gmms_galss_task_2 where id=Damagedid and work_state='破损'; 
	select count(*) into @count from gmms_galss_task_2 where id=Damagedid;
	set count=@Damagedcount;
	if @Damagedcount=1 then
		set msg='ok';
	elseif @count=0 then	
		set msg='不存在此id';
	elseif @count=1 and @Damagedcount=0 then
		set msg='failure';
	end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for TemperingRank
-- ----------------------------
DROP PROCEDURE IF EXISTS `TemperingRank`;
delimiter ;;
CREATE PROCEDURE `TemperingRank`(in sendMessageHex varchar(255),in machineids BIGINT,out messagedate varchar(255))
BEGIN
	#下片机存储过程
	#找到当前机器标志位
	#查询机器逻辑表中对应的逻辑进行对应的步骤
	DECLARE machinedisposeids int DEFAULT 0;
	DECLARE ids int ;
	DECLARE plcfindid varchar(255);
	DECLARE dates varchar(255);
	DECLARE plcid varchar(255);
	DECLARE findplc varchar(255);
	DECLARE findmes varchar(255);
	DECLARE taskplc varchar(255);
	DECLARE taskmes varchar(255);
	DECLARE finsh_rankid BIGINT;
	DECLARE send_rankid BIGINT;
	DECLARE rankids varchar(255);
	DECLARE posunmes varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	set messagedate=messagedates;
	
	
	select machine_type_id,work_id into @mtid,@remarks from gmms_machine where id=machineids;
	set ids = cast(@remarks as UNSIGNED INTEGER);
	
	select mf.flag into @完成标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='完成字';
	
	select mf.flag into @请求标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='请求字';
	
	select mf.flag into @发送标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='发送字';
	
	select mf.flag into @完成确认标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='完成确认字';
	
	set finsh_rankid = cast(@完成标志位 as UNSIGNED INTEGER)+1;
	set send_rankid = cast(@请求标志位 as UNSIGNED INTEGER)+1;
	
	select @完成标志位;
	
	set findplc = substring(sendMessageHex,(cast(@完成标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskplc = substring(sendMessageHex,(cast(@请求标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskmes = substring(sendMessageHex,(cast(@发送标志位 as UNSIGNED INTEGER)*4)+19,4);
	set findmes = substring(sendMessageHex,(cast(@完成确认标志位 as UNSIGNED INTEGER)*4)+19,4);
	
	select taskplc;
	select taskmes;
	select findplc;
	select findmes;
	
	set rankids = conv(substring(sendMessageHex,(cast(1 as UNSIGNED INTEGER)*4)+19,4),16,10);
	select rankids;

#完成逻辑查询
	select count(*),md.id into @完成数量,@完成逻辑编号 from gmms_machine_dispose md join gmms_machine_type mt on md.machine_type_id=mt.id where md.machine_type_id=@mtid  and md.plc_font_type='完成字' and md.mes_font_type='完成确认字' and md.plc_font_value=findplc and md.mes_font_value=findmes;
	
	#请求逻辑查询
	select count(*),md.id into @请求数量,@请求逻辑编号 from gmms_machine_dispose md join gmms_machine_type mt on md.machine_type_id=mt.id where md.machine_type_id=@mtid  and md.plc_font_type='请求字' and md.mes_font_type='发送字' and md.plc_font_value=taskplc and md.mes_font_value=taskmes;
	
	if taskplc='0000' and taskmes!='0000' then
		set dates=CONCAT('0000',"000000000000000000000000");
		call message(dates,'000a',@date);
		set messagedate=@date;
	elseif findplc='0000' and findmes!='0000' then
		set dates=CONCAT('0000',"0000");	
		call message(dates,'0014',@date);
		set messagedate=@date;
	else
		#判断逻辑是否存在
		if @完成数量=1 or @请求数量=1  then
			#判断完成逻辑是否为空
			if @完成逻辑编号!=0 then
				set machinedisposeids=@完成逻辑编号;
			#判断请求逻辑是否为空
			elseif @请求逻辑编号!=0 then
				set machinedisposeids=@请求逻辑编号;	
			end if;	
			#查询当前执行逻辑进行判断
			select logic,return_value into @ExecutionLogic,@returnvalue from gmms_machine_dispose where id=machinedisposeids;
			select @ExecutionLogic;
			select @returnvalue;
			if @ExecutionLogic='任务清空' then
				set dates=CONCAT(@returnvalue,"000000000000000000000000");
				call message(dates,'000a',@date);
				set messagedate=@date;
			elseif @ExecutionLogic='任务完成' then
				call y_RackFinsh(machineids,1,@returnvalue,@messagedate);
				#call 	TemperingRankFinsh_2(sendMessageHex,machineids,@returnvalue,1,@messagedate);
				set messagedate=@messagedate;			
			elseif @ExecutionLogic='任务失败' then	
				call y_RackFinsh(machineids,2,@returnvalue,@messagedate);
			elseif @ExecutionLogic='完成清空' then	
				set dates=CONCAT(@returnvalue,"0000");	
				call message(dates,'0014',@date);
				set messagedate=@date;
			elseif @ExecutionLogic='任务请求' then	
				call y_RackSend(machineids,@returnvalue,@messagedate);
				#call TemperingRankSend_2(machineids,@returnvalue,@messagedate);
				set messagedate=@messagedate;
				if messagedate is not null then
					insert into aaaa(Content,Sendtext)
					select '任务发送',messagedate;
				end if;					
			end if;
		end if;
	end if;


END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for TemperingRankFinsh_2
-- ----------------------------
DROP PROCEDURE IF EXISTS `TemperingRankFinsh_2`;
delimiter ;;
CREATE PROCEDURE `TemperingRankFinsh_2`(in sendMessageHex varchar(255),in machineids BIGINT,in returnvalue varchar(255),in flags int,out messagedate varchar(255))
BEGIN
	#下片机完成存储过程
	#找到正在工作的任务进行完成
	#移动料架
	if machineids=45 or machineids=69 then
		#钢化后的料架
		call y_RankTemperingAfterFinsh(machineids,returnvalue,messagedate);
	elseif machineids=51 or machineids=70 or machineids=72 or machineids=74 then
		call y_RankHollowFinsh(machineids,returnvalue,messagedate);
	elseif machineids=49 then
		call y_RankSlicecageFinsh(machineids,returnvalue,messagedate);		
	end if;

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for TemperingRankSend_2
-- ----------------------------
DROP PROCEDURE IF EXISTS `TemperingRankSend_2`;
delimiter ;;
CREATE PROCEDURE `TemperingRankSend_2`(in machineids BIGINT,in returnvalue varchar(255),out messagedate varchar(255))
BEGIN
	#移动料架
	
	if machineids=45 or machineids=69 then
		#钢化后的料架
		call y_RankTemperingAfter(machineids,returnvalue,messagedate);
	elseif machineids=51 or machineids=70 or machineids=72 or machineids=74 then	
		call y_RankHollowSend(machineids,returnvalue,messagedate);
 elseif machineids=49 then
		call y_RankSlicecageSend(machineids,returnvalue,messagedate);
	end if;
	
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for TemperingShelfFinsh_2
-- ----------------------------
DROP PROCEDURE IF EXISTS `TemperingShelfFinsh_2`;
delimiter ;;
CREATE PROCEDURE `TemperingShelfFinsh_2`(in sendMessageHex varchar(255),in machineids BIGINT,in returnvalue varchar(255),in flag int,out messagedate varchar(255))
BEGIN
	#仓储完成存储过程
	#找到正在工作的任务，判断是什么类型的仓储任务，分别进行什么处理
	DECLARE processcardid1 varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	set messagedate=messagedates;
	
	select work_id into @line from gmms_machine where id=machineids;
	
	select count(*) into @counts from gmms_shelf_task_2 where task_state='正在工作';
	select id,task_type,shelf_rack_id,load_rack,groups into @taskid,@task_type,@shelf_rack_id,@load_rack_id,@groups from gmms_shelf_task_2 where task_state='正在工作 'and line=@line limit 1;
	
	#任务完成
	if flag=1 then 
	select @counts;
	select @task_type;
		if @counts>0 then
		
			#仓位上片位之间类型的任务
			if @task_type='从仓位到钢化位' or @task_type='从钢化位到仓位' then
			
				if @task_type='从仓位到钢化位' then
				
					update gmms_shelf_rack_2 set state='正在使用' where number=@shelf_rack_id;
					update gmms_drop_frame_3 set flag='仓储完成',rank=@shelf_rack_id where stop_rank=@load_rack_id and groups=@groups;
					update gmms_shelf_task_2 set task_state='已完成',finsh_time=NOW() where id=@taskid;
					call message(returnvalue,'0014',@date);
					set messagedate=@date;
				else	
					select processcard_id,orientation into @processcard_id,@orientation from gmms_drop_frame_3 where stop_rank=@load_rack_id and groups=@groups;
					select @processcard_id;
					select @orientation;
					update gmms_shelf_rack_2 set state='使用中',processcard_id=@processcard_id,orientation=@orientation,mod_time=now(),sign=0 where number=@shelf_rack_id;
					update gmms_drop_frame_3 set flag=null,rank=null,state='空闲',processcard_id=null,drop_frame=null,engineering_id=null,finsh_count=0,processcard_count=0 where stop_rank=@load_rack_id and groups=@groups;
					update gmms_shelf_task_2 set task_state='已完成',finsh_time=NOW() where id=@taskid;
					call Temperingss(@processcard_id);
					call message(returnvalue,'0014',@date);
					set messagedate=@date;
				end if;
			
			
			
			elseif @task_type='从仓位到理片笼位' or @task_type='从理片笼位到仓位' then
			
				if @task_type='从仓位到理片笼位' then
					select processcard_id into @processcard_id from gmms_shelf_rack_2 where number=@shelf_rack_id;
					select count(*),engineering_id,drop_frame into @count1,@engineering_id,@drop_frame from gmms_galss_task_shelf where processcard_id=@processcard_id;
					update gmms_shelf_rack_2 set state='正在使用' where number=@shelf_rack_id;
					update gmms_drop_frame_4 set rank=@shelf_rack_id,processcard_id=@processcard_id,engineering_id=@engineering_id,state='使用中',drop_frame=@drop_frame,processcard_count=@count1,finsh_count=@count1,sign=1 where stop_rank=@load_rack_id and groups=10;
					insert into gmms_galss_task_slicecage select * from gmms_galss_task_shelf where processcard_id=@processcard_id;
					update gmms_galss_task_slicecage set rank=0 where processcard_id=@processcard_id;
					update gmms_shelf_task_2 set task_state='已完成',finsh_time=NOW() where id=@taskid;
					update gmms_machine set remarks=@load_rack_id where id=49;
					call message(returnvalue,'0014',@date);
					set messagedate=@date;
				else	
					select processcard_id into @processcard_id1 from gmms_shelf_rack_2 where number=@shelf_rack_id;
					update gmms_shelf_rack_2 set state='使用中',sign=2,mod_time=now() where number=@shelf_rack_id;
					update gmms_drop_frame_4 set flag=0,rank=null,state='空闲',processcard_id=null,drop_frame=null,engineering_id=null,finsh_count=0,processcard_count=0,sign=1 where stop_rank=@load_rack_id and groups=@groups;
					select count(*) into @count6 from gmms_galss_task_slicecage where processcard_id=@processcard_id1 and work_state='破损';
					delete from gmms_galss_task_slicecage where processcard_id=@processcard_id1;
					update gmms_shelf_task_2 set task_state='已完成',finsh_time=NOW() where id=@taskid;
					
					set processcardid1 = left(@processcard_id1,13);
					select count(*) into @count10 from gmms_shelf_rack_2 where sign=2 and  processcard_id like  CONCAT('%',processcardid1,'%');
					select count(*)  from gmms_shelf_rack_2 where  processcard_id like  CONCAT('%',processcardid1,'%');
					select count(*) into @count11 from gmms_liuchengka where current_frame like CONCAT('%',processcardid1,'%');
					select @count11;
					select @count10;
					if (@count11-@count10)=1 and @count6=0  then
					select @count11;
 						update gmms_shelf_rack_2 set sign=2 where  processcard_id like  CONCAT('%',processcardid1,'%');
					elseif @count6>0  then
						update gmms_shelf_rack_2 set sign=0 where  processcard_id like CONCAT('%',processcardid1,'%');
					end if;
					call message(returnvalue,'0014',@date);
						
					set messagedate=@date;
				end if;
			
			
			elseif @task_type='从仓位到中空位' or @task_type='从中空位到仓位' then
			
				if @task_type='从仓位到中空位' then
					select processcard_id into @processcard_id from gmms_shelf_rack_2 where number=@shelf_rack_id;
					
					select count(*),engineering_id,drop_frame into @count1,@engineering_id,@drop_frame from gmms_galss_task_shelf where processcard_id=@processcard_id;
					update gmms_shelf_rack_2 set state='正在使用' where number=@shelf_rack_id;

					select id into @id from gmms_drop_frame_4 where stop_rank=@load_rack_id and groups=@groups order by stops desc limit 0,1;
					
					update gmms_drop_frame_4 set rank=@shelf_rack_id,processcard_id=@processcard_id,engineering_id=@engineering_id,state='使用中',drop_frame=@drop_frame,processcard_count=@count1,finsh_count=@count1,sign=1 where id=@id;
					update gmms_shelf_task_2 set task_state='已完成',finsh_time=NOW() where id=@taskid;
					insert into gmms_galss_task_hollow  select * from gmms_galss_task_shelf where processcard_id =@processcard_id;
					
					

					call message(returnvalue,'0014',@date);
					set messagedate=@date;
				else	
					update gmms_shelf_rack_2 set state='空架',sign=0,processcard_id=null,mod_time=now() where number=@shelf_rack_id;
					update gmms_drop_frame_4 set flag=0,rank=null,state='空闲',processcard_id=null,drop_frame=null,engineering_id=null,finsh_count=0,processcard_count=0,sign=0 where stop_rank=@load_rack_id and groups=@groups and sign=4;
					update gmms_shelf_task_2 set task_state='已完成',finsh_time=NOW() where id=@taskid;
					
					
					call message(returnvalue,'0014',@date);
					set messagedate=@date;
				end if;
			
			
			elseif @task_type='从空仓位到仓位' or @task_type='从仓位到空仓位' then
				if @task_type='从仓位到空仓位' then
 					select processcard_id,orientation,sign into @processcard_id,@orientation,@sign from gmms_shelf_rack_2 where number=@shelf_rack_id;
 					update gmms_shelf_rack_2 set state='空闲',processcard_id=null,sign=@sign where number=@shelf_rack_id;
 					update gmms_shelf_rack_2 set state='使用中',processcard_id=@processcard_id,orientation=@orientation where number=@load_rack_id;
					update gmms_shelf_task_2 set task_state='已完成',finsh_time=NOW() where id=@taskid;
					call message(returnvalue,'0014',@date);
					set messagedate=@date;
				else
					select orientation,sign into @orientation,@sign from gmms_shelf_rack_2 where number=@shelf_rack_id;
 					update gmms_shelf_rack_2 set state='空闲' where number=@shelf_rack_id;
 					update gmms_shelf_rack_2 set state='空架',orientation=@orientation,sign=0 where number=@load_rack_id;
					update gmms_shelf_task_2 set task_state='已完成',finsh_time=NOW() where id=@taskid;
					call message(returnvalue,'0014',@date);
					set messagedate=@date;
				end if;
				
			elseif @task_type='从仓位到旋转位' or @task_type='从旋转位到仓位' then
				if @task_type='从仓位到旋转位' then
 					select processcard_id into @processcard_id from gmms_shelf_rack_2 where number=@shelf_rack_id;
					update gmms_drop_frame_2 set stops=@shelf_rack_id,processcard_id=@processcard_id where noodles=@load_rack_id and groups=20;
 					update gmms_shelf_rack_2 set state='正在使用' where number=@shelf_rack_id;
					update gmms_shelf_task_2 set task_state='已完成',finsh_time=NOW() where id=@taskid;
					call message(returnvalue,'0014',@date);
					set messagedate=@date;
				else
 					update gmms_drop_frame_2 set stops=null,processcard_id=null where noodles=@load_rack_id and groups=20;
					select orientation into @orientation from gmms_shelf_rack_2 where number=@shelf_rack_id;
					if @orientation='A' then
						set @orientation='B';
					elseif @orientation='B' then
						set @orientation='A';
					end if;
 					update gmms_shelf_rack_2 set state='使用中',orientation=@orientation where number=@shelf_rack_id;
					update gmms_shelf_task_2 set task_state='已完成',finsh_time=NOW() where id=@taskid;
					call message(returnvalue,'0014',@date);
					set messagedate=@date;
				end if;
				
			end if;
		else
			set messagedate='';
		end if;
		
		
		
	#任务失败，重新修改状态
	elseif flag=2 then

			update gmms_shelf_task_2 set task_state='等待中' where id=@taskid;
		
			call message(returnvalue,'0014',@date);
			set messagedate=@date;


	end if;

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for TemperingShelfSend_2
-- ----------------------------
DROP PROCEDURE IF EXISTS `TemperingShelfSend_2`;
delimiter ;;
CREATE PROCEDURE `TemperingShelfSend_2`(in machineids BIGINT,in machinedisposeids BIGINT,out messagedate varchar(255))
BEGIN
	#仓储请求存储过程
	#循环等待中的仓储任务，循环上片位，判断是什么类型任务分别进行处理
	DECLARE s int DEFAULT 0;
	DECLARE x int DEFAULT 0;
	DECLARE load_racks int DEFAULT 0;
	DECLARE orientations varchar(255) DEFAULT '';
	DECLARE dates varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	set messagedate=messagedates;
	
	select return_value into @returnvalue from gmms_machine_dispose where id=machinedisposeids;
	select work_id into @line from gmms_machine where id=machineids;
	

	set @task_type=null;
		select id,task_state,task_type,groups,shelf_rack_id,load_rack into @id,@task_state,@task_type,@groups,@shelf_rack_id,@load_rack from gmms_shelf_task_2 where task_state='等待中' and line=@line order by start_time limit 1;
		select count(*) into @count5 from gmms_shelf_task_2 where task_state='正在工作' and line=@line;
		if @count5=0 then
			if @task_type='从钢化位到仓位' then
				#判断上片位架子上面有玻璃，上片位是否启用
					select orientation into @orientation from gmms_drop_frame_3 where stops=@load_rack and groups=@groups;
					update gmms_shelf_task_2 set task_state='正在工作' where task_state='等待中' and id=@id;
					if @orientation='A' then
						set orientations='0001';
					else
						set orientations='0002';
					end if;
					if @shelf_rack_id=20 then
						set orientations='0001';
					end if;
					call intTo2ByteHex10_16_4(@shelf_rack_id,@shelfrackid);
					call intTo2ByteHex10_16_4(@load_rack,@loadrackid);
					
					set dates=CONCAT(@returnvalue,"0000",@loadrackid,@shelfrackid,orientations,'0001');
					call message(dates,'000a',@date);
					set messagedate=@date;
					
			
			elseif @task_type='从仓位到钢化位' then
				#判断上片位架子上面没有玻璃，上片位是否启用
					select orientation into @orientation from gmms_drop_frame_3 where stops=@load_rack and groups=@groups;
					update gmms_shelf_task_2 set task_state='正在工作' where task_state='等待中' and id=@id;
					if @orientation='A' then
						set orientations='0001';
					else
						set orientations='0002';
					end if;
					if @shelf_rack_id=20 then
						set orientations='0001';
					end if;
					call intTo2ByteHex10_16_4(@shelf_rack_id,@shelfrackid);
					call intTo2ByteHex10_16_4(@load_rack,@loadrackid);
					set dates=CONCAT(@returnvalue,"0000",@shelfrackid,@loadrackid,orientations,'0001');
					select dates;
					call message(dates,'000a',@date);
					set messagedate=@date;
					
			elseif @task_type='从仓位到理片笼位' then
	
					select orientation into @orientation from gmms_drop_frame_4 where stops=@load_rack and groups=@groups;
					update gmms_shelf_task_2 set task_state='正在工作' where task_state='等待中' and id=@id;
					if @orientation='A' then
						set orientations='0001';
					else
						set orientations='0002';
					end if;
					if @shelf_rack_id=21 then
						set orientations='0002';
					end if;
					call intTo2ByteHex10_16_4(@shelf_rack_id,@shelfrackid);
					call intTo2ByteHex10_16_4(@load_rack,@loadrackid);
					set dates=CONCAT(@returnvalue,"0000",@shelfrackid,@loadrackid,orientations,'0001');
					select dates;
					call message(dates,'000a',@date);
					set messagedate=@date;
					
			elseif @task_type='从理片笼位到仓位' then
					select orientation into @orientation from gmms_drop_frame_4 where stops=@load_rack and groups=@groups;
					update gmms_shelf_task_2 set task_state='正在工作' where task_state='等待中' and id=@id;
					if @orientation='A' then
						set orientations='0001';
					else
						set orientations='0002';
					end if;
					if @shelf_rack_id=21 then
						set orientations='0002';
					end if;
					call intTo2ByteHex10_16_4(@shelf_rack_id,@shelfrackid);
					call intTo2ByteHex10_16_4(@load_rack,@loadrackid);
					set dates=CONCAT(@returnvalue,"0000",@loadrackid,@shelfrackid,orientations,'0001');
					select dates;
					call message(dates,'000a',@date);
					set messagedate=@date;
					
					
			elseif @task_type='从仓位到空仓位' or @task_type='从空仓位到仓位' then
					select orientation into @orientation from gmms_shelf_rack_2 where number=@shelfrackid;
					update gmms_shelf_task_2 set task_state='正在工作' where task_state='等待中' and id=@id;
					update gmms_shelf_rack_2 set state='正在使用' where number=@shelf_rack_id or number=@load_rack;
					if @orientation='A' then
						set orientations='0001';
					else
						set orientations='0002';
					end if;
					if @shelf_rack_id=21 then
						set orientations='0002';
					elseif @shelf_rack_id=20 then
						set orientations='0001';
					end if;
					call intTo2ByteHex10_16_4(@shelf_rack_id,@shelfrackid);
					call intTo2ByteHex10_16_4(@load_rack,@loadrackid);
					set dates=CONCAT(@returnvalue,"0000",@shelfrackid,@loadrackid,orientations,'0001');
					select dates;
					call message(dates,'000a',@date);
					set messagedate=@date;
			
				
			elseif @task_type='从仓位到中空位' then
					select orientation into @orientation from gmms_shelf_rack_2 where number=@shelfrackid;
					update gmms_shelf_task_2 set task_state='正在工作' where task_state='等待中' and id=@id;
					update gmms_shelf_rack_2 set state='正在使用' where number=@shelfrackid;
					if @orientation='A' then
						set orientations='0001';
					else
						set orientations='0002';
					end if;
					if @shelf_rack_id=21 then
						set orientations='0002';
					elseif @shelf_rack_id=20 then
						set orientations='0001';
					end if;
					call intTo2ByteHex10_16_4(@shelf_rack_id,@shelfrackid);
					call intTo2ByteHex10_16_4(@load_rack,@loadrackid);
					set dates=CONCAT(@returnvalue,"0000",@shelfrackid,@loadrackid,orientations,'0001');
					select dates;
					call message(dates,'000a',@date);
					set messagedate=@date;	
					
			elseif @task_type='从中空位到仓位' then
					select orientation into @orientation from gmms_shelf_rack_2 where number=@shelfrackid;
					update gmms_shelf_task_2 set task_state='正在工作' where task_state='等待中' and id=@id;
					update gmms_shelf_rack_2 set state='正在使用' where number=@shelfrackid;
					if @orientation='A' then
						set orientations='0001';
					else
						set orientations='0002';
					end if;
					if @shelf_rack_id=21 then
						set orientations='0002';
					elseif @shelf_rack_id=20 then
						set orientations='0001';
					end if;
					call intTo2ByteHex10_16_4(@shelf_rack_id,@shelfrackid);
					call intTo2ByteHex10_16_4(@load_rack,@loadrackid);
					set dates=CONCAT(@returnvalue,"0000",@loadrackid,@shelfrackid,orientations,'0001');
					select dates;
					call message(dates,'000a',@date);
					set messagedate=@date;	
					
			elseif @task_type='从仓位到旋转位' or @task_type='从旋转位到仓位' then
					select orientation into @orientation from gmms_shelf_rack_2 where number=@shelfrackid;
					update gmms_shelf_task_2 set task_state='正在工作' where task_state='等待中' and id=@id;
					update gmms_shelf_rack_2 set state='正在使用' where number=@shelfrackid;
					if @orientation='A' then
						set orientations='0001';
					else
						set orientations='0002';
					end if;
					if @shelf_rack_id=21 then
						set orientations='0002';
					elseif @shelf_rack_id=20 then
						set orientations='0001';
					end if;
					call intTo2ByteHex10_16_4(@shelf_rack_id,@shelfrackid);
					call intTo2ByteHex10_16_4(@load_rack,@loadrackid);
					set dates=CONCAT(@returnvalue,"0000",@shelfrackid,@loadrackid,orientations,'0001');
					select dates;
					call message(dates,'000a',@date);
					set messagedate=@date;	
					
		
			
			end if;


	end if;

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for TemperingShelf_2
-- ----------------------------
DROP PROCEDURE IF EXISTS `TemperingShelf_2`;
delimiter ;;
CREATE PROCEDURE `TemperingShelf_2`(in sendMessageHex varchar(255),in machineids BIGINT,out messagedate varchar(255))
BEGIN
	#仓储存储过程
	#找到当前机器标志位
	#查询机器逻辑表中对应的逻辑进行对应的步骤
	DECLARE machinedisposeids int DEFAULT 0;
	DECLARE dates varchar(255);
	DECLARE findplc varchar(255);
	DECLARE findmes varchar(255);
	DECLARE taskplc varchar(255);
	DECLARE taskmes varchar(255);
	DECLARE processcardid1 varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	set messagedate=messagedates;
	
	select machine_type_id into @mtid from gmms_machine where id=machineids;
	
	
	
	select mf.flag into @完成标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='完成字';
	
	select mf.flag into @请求标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='请求字';
	
	select mf.flag into @发送标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='发送字';
	
	select mf.flag into @完成确认标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='完成确认字';
	
	set findplc = substring(sendMessageHex,(cast(@完成标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskplc = substring(sendMessageHex,(cast(@请求标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskmes = substring(sendMessageHex,(cast(@发送标志位 as UNSIGNED INTEGER)*4)+19,4);
	set findmes = substring(sendMessageHex,(cast(@完成确认标志位 as UNSIGNED INTEGER)*4)+19,4);
	select taskplc;
	select taskmes;
	select findplc;
	select findmes;

	#完成逻辑查询
	select count(*),md.id into @完成数量,@完成逻辑编号 from gmms_machine_dispose md join gmms_machine_type mt on md.machine_type_id=mt.id where md.machine_type_id=@mtid  and md.plc_font_type='完成字' and md.mes_font_type='完成确认字' and md.plc_font_value=findplc and md.mes_font_value=findmes;
	
	#请求逻辑查询
	select count(*),md.id into @请求数量,@请求逻辑编号 from gmms_machine_dispose md join gmms_machine_type mt on md.machine_type_id=mt.id where md.machine_type_id=@mtid  and md.plc_font_type='请求字' and md.mes_font_type='发送字' and md.plc_font_value=taskplc and md.mes_font_value=taskmes;
	#machineids=46,53,54

	#判断逻辑是否存在
	if @完成数量=1 or @请求数量=1 then
	
		#判断完成逻辑是否为空
		if @完成逻辑编号!=0 then
			set machinedisposeids=@完成逻辑编号;

		#判断请求逻辑是否为空
		elseif @请求逻辑编号!=0 then
			set machinedisposeids=@请求逻辑编号;
			
		end if;
		#查询当前执行逻辑进行判断
		select machinedisposeids;
		select logic,return_value,mes_font_value into @ExecutionLogic,@returnvalue,@mes_font_value from gmms_machine_dispose where id=machinedisposeids;
		select @returnvalue;
		select @ExecutionLogic;
		
		if @ExecutionLogic='任务完成' then
				call y_ShelfFinsh(machineids,1,@returnvalue,dates);
				set messagedate=dates;
 		elseif @ExecutionLogic='任务失败' then
				call y_ShelfFinsh(machineids,2,@returnvalue,dates);
				set messagedate=dates;
		elseif @ExecutionLogic='手动完成' then
				call y_ShelfFinsh(machineids,3,@returnvalue,dates);
				set messagedate=dates;
 		elseif @ExecutionLogic='完成清空' then
				select @returnvalue;
				call message(@returnvalue,'0014',@date);
				set messagedate=@date;
		elseif @ExecutionLogic='任务请求' then			
				call y_ShelfSend(machineids,@returnvalue,dates);
				call message(dates,'000a',@date);
				set messagedate=@date;
 		elseif @ExecutionLogic='任务清空' then
				set dates=CONCAT(@returnvalue,"00000000000000000000");
				call message(dates,'000a',@date);
				set messagedate=@date;
		else
				set messagedate='';
		end if;
	end if;
	
	
		
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for Temperingss
-- ----------------------------
DROP PROCEDURE IF EXISTS `Temperingss`;
delimiter ;;
CREATE PROCEDURE `Temperingss`(in processcard_id1 varchar(255))
BEGIN
DECLARE s int DEFAULT 1;
DECLARE x int DEFAULT 0;
DECLARE countpisce double DEFAULT 0;
DECLARE processcardid1 varchar(255);


-- 	set @processcard_id=null;
-- 	select number,orientation,processcard_id into @number,@orientation,@processcard_id from gmms_shelf_rack_2 where sign=0 and state='使用中' order by number limit 0,1;
-- 	select @processcard_id;
-- 	set processcardid1 = left(@processcard_id,13);
-- 	
-- 	
-- 
-- 	select count(*) into @count1 from gmms_shelf_rack_2 where sign=0 and  processcard_id like  CONCAT('%',processcardid1,'%');
-- 	select count(*) into @count21 from gmms_shelf_rack_2 where (sign=1 or sign=0) and  processcard_id like  CONCAT('%',processcardid1,'%');
-- 	select count(*) into @count20 from gmms_galss_task_shelf where work_state='已完成' and work_procedure='钢化后下片' and processcard_id like  CONCAT('%',processcardid1,'%') ;
-- 	select count(*) into @count11 from gmms_shelf_rack_2 where   processcard_id like  CONCAT('%',processcardid1,'%');
-- 	set countpisce=@count20/@count11;
-- 	select countpisce;
-- 	select @count1;
-- 	if @count1>1 or @count21>1 then
-- 		set @count2=0;
-- 		set @processcard_id1=null;
-- 		select processcard_id into @processcard_id1 from gmms_shelf_rack_2 where  processcard_id like  CONCAT('%',processcardid1,'%') and sign=2 limit 0,1;
-- 		if @processcard_id1 is null then
-- 			select processcard_id into @processcard_id1 from gmms_galss_task_shelf where processcard_id like  CONCAT('%',processcardid1,'%') GROUP BY processcard_id limit 0,1;
-- 		end if;
-- 		select @processcard_id1;
-- 		select count(*) into @count2 from gmms_galss_task_shelf where processcard_id =@processcard_id1;
-- 		select @count2;
-- 		if countpisce=@count2 then
-- 		while x<=@count2 do
-- 			select max(stop_rank) into @maxstoprank from gmms_galss_task_shelf where processcard_id =@processcard_id1;
-- 			select stop_rank,width,height,edg_speed2 into @stop_rank,@width,@height,@edg_speed2 from gmms_galss_task_shelf where processcard_id =@processcard_id1 order by stop_rank limit x,1;
-- -- 			select @stop_rank;
-- --  			select @width;
-- --  			select @height;
-- --  			select x;
-- 			while s<@count1 do
-- 
-- 				select processcard_id into @processcard_id3 from gmms_galss_task_shelf where processcard_id like  CONCAT('%',processcardid1,'%') GROUP BY processcard_id order by processcard_id limit s,1;
-- 				select count(*) into @count6 from gmms_galss_task_shelf where processcard_id =@processcard_id3;
-- 				#select @count6;
-- 				if @count2=@count6 then
-- -- 					select @edg_speed2;
-- -- 					select @stop_rank;
-- 
-- 					select count(*) into @count3 from gmms_galss_task_shelf where processcard_id =@processcard_id3  and stop_rank=@stop_rank and (edg_speed2=@edg_speed2 or (width=@width and height=@height));
-- 
-- 					#select @processcard_id3;
-- -- 					select @count3;
-- -- 					select @count9;
-- 					if @count3!=1 then
-- 						select @processcard_id1;
-- 						select @stop_rank;
-- 						select count(*) into @count4 from gmms_shelf_rack_2 where processcard_id =@processcard_id and state='使用中' and sign=0 ;
-- 						select @processcard_id;
-- 						if @count4=1 then
-- 							select number,orientation,processcard_id into @number,@orientation,@processcard_id2 from gmms_shelf_rack_2 where processcard_id =@processcard_id and state='使用中' and sign=0;
-- 							#select @number;
-- 							set @load_rank=null;
-- 							select stop_rank into @load_rank from gmms_drop_frame_4 where groups=10 and state='空闲' and flag=0 and orientation=@orientation;
-- 							select @load_rank;
-- 							if @load_rank is not null then
-- 								insert into gmms_shelf_task_2 (task_type,task_state,shelf_rack_id,load_rack,groups,start_time,line) values('从仓位到理片笼位','等待中',@number,@load_rank,10,now(),1);
-- 								select count(*) into @count5 from gmms_shelf_rack_2 where sign=0 and processcard_id like  CONCAT('%',processcardid1,'%');
-- 								if @count5=2 then
-- 									update gmms_shelf_rack_2 set sign=1 where processcard_id like  CONCAT('%',processcardid1,'%');
-- 								else 
-- 									update gmms_shelf_rack_2 set sign=1 where processcard_id =@processcard_id;
-- 								end if;
-- 								
-- 								update gmms_drop_frame_4 set flag=1 where stop_rank=@load_rank;
-- 							end if;
-- 						
-- 						end if;
-- 						
-- 						set s=10000;
-- 						set x=10000;
-- 					elseif @maxstoprank=@stop_rank then
--  								SELECT @maxstoprank;
--  								SELECT @stop_rank;	
-- 								select count(*) into @count5 from gmms_shelf_rack_2 where sign=0 and processcard_id like  CONCAT('%',processcardid1,'%');
-- 								if @count5=2 then
-- 									update gmms_shelf_rack_2 set sign=2 where processcard_id like  CONCAT('%',processcardid1,'%');
-- 								else 
-- 									update gmms_shelf_rack_2 set sign=2 where processcard_id =@processcard_id;
-- 								end if;
-- 					end if;
-- 				end if;
-- 				set s=s+1;
-- 			end while;
-- 			set s=0;
-- 			set x=x+1;
-- 		end while;
-- 		set x=0;
-- 		end if;
-- 
-- 	end if;
-- 	
	
	
	#set @processcard_id=null;
	#select number,orientation,processcard_id into @number,@orientation,@processcard_id from gmms_shelf_rack_2 where sign=0 and state='使用中' order by number limit 0,1;
-- 	
-- 	set @processcard_id=processcard_id1;
-- 	select @processcard_id;
-- 	#截取流程卡13位
-- 	set processcardid1 = left(@processcard_id,13);
-- 	
-- 	
-- 	#查询这个合片流程卡没有理片的流程卡数量
-- 	select count(*) into @count1 from gmms_shelf_rack_2 where sign=0 and  processcard_id like  CONCAT('%',processcardid1,'%');
-- 	select count(*) into @count21 from gmms_shelf_rack_2 where (sign=1 or sign=0) and  processcard_id like  CONCAT('%',processcardid1,'%');
-- 	#查询合片流程卡小片总数
-- 	select count(*) into @count20 from gmms_galss_task_shelf where work_state='已完成' and work_procedure='钢化后下片' and processcard_id like  CONCAT('%',processcardid1,'%') ;
-- 	#查询合片流程卡里有几个流程卡
-- 	select count(*) into @count11 from gmms_shelf_rack_2 where   processcard_id like  CONCAT('%',processcardid1,'%');
-- 	set countpisce=@count20/@count11;
-- 	select countpisce;
-- 	select @count1;
-- 	#判断没有理片的流程卡数量大于1
-- 	if @count1>0 or @count21>1 then
-- 		set @count2=0;
-- 		set @processcard_id1=null;
-- 		#查询合片流程卡里面已经理片的流程卡
-- 		select processcard_id into @processcard_id1 from gmms_shelf_rack_2 where  processcard_id like  CONCAT('%',processcardid1,'%') and sign=2 limit 0,1;
-- 		if @processcard_id1 is null then
-- 			#查询合片流程卡里不等于当前流程卡的流程卡
-- 			select processcard_id into @processcard_id1 from gmms_shelf_rack_2 where processcard_id like  CONCAT('%',processcardid1,'%') and state='使用中'  order by processcard_id limit 0,1;	
-- 		end if;
-- 		select processcard_id into @processcard_id from gmms_shelf_rack_2 where processcard_id like  CONCAT('%',processcardid1,'%') and state='使用中' and processcard_id!=@processcard_id1 and sign=0 order by processcard_id desc limit 0,1;	
-- 		select @processcard_id1;
-- 		select @processcard_id;
-- 		#查询当前流程卡的小片总数
-- 		select count(*) into @count2 from gmms_galss_task_shelf where processcard_id =@processcard_id1;
-- 		select @count2;
-- 		if countpisce=@count2 then
-- 				select @processcard_id1;
-- 				if @processcard_id1 is not null then
-- 						#查询两个流程卡的对比结果集
-- 						select GROUP_CONCAT(gtf.edg_speed2),GROUP_CONCAT(gtf.width),GROUP_CONCAT(gtf.height) into @stop_rank1,@width1,@height1 from (select edg_speed2,width,height from gmms_galss_task_shelf where processcard_id=@processcard_id1 order by stop_rank) as gtf;
-- 						select GROUP_CONCAT(gtf.edg_speed2),GROUP_CONCAT(gtf.width),GROUP_CONCAT(gtf.height) into @stop_rank2,@width2,@height2 from (select edg_speed2,width,height from gmms_galss_task_shelf where processcard_id=@processcard_id  order by stop_rank) as gtf;	
-- 						#判断是否相等
-- 						if @stop_rank1=@stop_rank2 then
-- 							#相等就把这个流程卡标记成2
-- 								select count(*) into @count5 from gmms_shelf_rack_2 where sign=2 and processcard_id like  CONCAT('%',processcardid1,'%');
-- 								select count(*) into @count11 from gmms_liuchengka where current_frame like CONCAT('%',processcardid1,'%');
-- 								if (@count11-@count5)=1 then
-- 									update gmms_shelf_rack_2 set sign=2 where processcard_id like  CONCAT('%',processcardid1,'%');
-- 								else 
-- 									update gmms_shelf_rack_2 set sign=2 where processcard_id =@processcard_id;
-- 								end if;
-- 						elseif @width1=@width2 and @height1=@height2 then
-- 								select count(*) into @count5 from gmms_shelf_rack_2 where sign=2 and processcard_id like  CONCAT('%',processcardid1,'%');
-- 								select count(*) into @count11 from gmms_liuchengka where current_frame like CONCAT('%',processcardid1,'%');
-- 								SELECT @count11;
-- 								if (@count11-@count5)=1 then
-- 									update gmms_shelf_rack_2 set sign=2 where processcard_id like  CONCAT('%',processcardid1,'%');
-- 								else 
-- 									update gmms_shelf_rack_2 set sign=2 where processcard_id =@processcard_id;
-- 								end if;
-- 						else
-- 							#不相等就去仓储拉架子去理片笼
-- 							select number,orientation,processcard_id into @number,@orientation,@processcard_id2 from gmms_shelf_rack_2 where processcard_id =@processcard_id and state='使用中' and sign=0;
-- 							set @load_rank=null;
-- 							select @processcard_id2;
-- 							select stop_rank into @load_rank from gmms_drop_frame_4 where groups=10 and state='空闲' and flag=0 and orientation=@orientation;
-- 							select @load_rank;
-- 							if @load_rank is not null then
-- 								insert into gmms_shelf_task_2 (task_type,task_state,shelf_rack_id,load_rack,groups,start_time,line) values('从仓位到理片笼位','等待中',@number,@load_rank,10,now(),1);
-- 								select count(*) into @count5 from gmms_shelf_rack_2 where sign=0 and processcard_id like  CONCAT('%',processcardid1,'%');
-- 								if @count5=2 then
-- 									update gmms_shelf_rack_2 set sign=1 where processcard_id =@processcard_id;
-- -- 									update gmms_shelf_rack_2 set sign=1 where processcard_id like  CONCAT('%',processcardid1,'%');
-- 								else 
-- 									update gmms_shelf_rack_2 set sign=1 where processcard_id =@processcard_id;
-- 								end if;
-- 								
-- 								update gmms_drop_frame_4 set flag=1 where stop_rank=@load_rank;
-- 							end if;
-- 								
-- 
-- 						end if;
-- 					end if;
-- 		
-- 		end if;
-- 
-- 	end if;
	
-- set @processcard_id=null;
-- select processcard_id into @processcard_id from gmms_galss_task_slicecage where work_procedure='理片' and work_state='已进库' limit 0,1;
-- select @processcard_id;
-- if @processcard_id is not null then
-- update gmms_galss_task_slicecage set stop_rank=0 where processcard_id=@processcard_id;
-- set processcardid2 = left(@processcard_id,13);
-- select count(*) into @count1 from gmms_galss_task_shelf where processcard_id=CONCAT(processcardid2,'/1');
-- while y<@count1 do
-- 	select width,height,stop_rank,gc_number into @width,@height,@stop_rank,@gc_number from gmms_galss_task_shelf where processcard_id=CONCAT(processcardid2,'/1') and stop_rank!=0 order by gc_number limit y,1;
-- 
-- 	select id into @id from gmms_galss_task_slicecage where gc_number=@gc_number and processcard_id=@processcard_id and stop_rank=0 limit 0,1;
-- 	if @id is not null then
-- 		update gmms_galss_task_slicecage set stop_rank=@stop_rank where id=@id;
-- 	end if;
-- set y=y+1;
-- end while;
-- end if;


END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for Temperingss_1
-- ----------------------------
DROP PROCEDURE IF EXISTS `Temperingss_1`;
delimiter ;;
CREATE PROCEDURE `Temperingss_1`()
BEGIN

	
		select count(*) into @count1 from gmms_shelf_rack_2 where number>=1 and number<=20 and state='空闲';
		select count(*) into @count2 from gmms_shelf_rack_2 where number>=1 and number<=20 and state='使用中' and processcard_id is not null;
		select count(*) into @count3 from gmms_shelf_rack_2 where number>=21 and number<=35 and state='空架';
		select count(*) into @count4 from gmms_shelf_rack_2 where number>=21 and number<=35 and state='空闲';
		
		if @count3>0 and @count1>0 then
			select number,orientation into @number,@orientation from gmms_shelf_rack_2 where number>=21 and number<=35 and state='空架' order by number limit 0,1;
			select number into @number1 from gmms_shelf_rack_2 where number>=1 and number<=20 and state='空闲' order by number limit 0,1;
			select count(*) into @count10 from gmms_shelf_task_2 where shelf_rack_id=@number and load_rack=@number1 and task_type='从仓位到仓位' and line=3 and (task_state='正在工作' or task_state='等待中');
				if @count10=0 and @number is not null then
					insert into gmms_shelf_task_2 (task_type,task_state,shelf_rack_id,load_rack,groups,start_time,line) values('从空仓位到仓位','等待中',@number,@number1,0,now(),3);
				end if;
		end if;
		
		
		if @count2>0 and @count4>0 then
			select number,orientation into @number,@orientation from gmms_shelf_rack_2 where number>=1 and number<=20 and state='使用中' and processcard_id is not null order by number limit 0,1;
			select number into @number1 from gmms_shelf_rack_2 where number>=21 and number<=35 and state='空闲' order by number limit 0,1;
			select count(*) into @count11 from gmms_shelf_task_2 where shelf_rack_id=@number and load_rack=@number1 and task_type='从仓位到仓位' and line=3 and (task_state='正在工作' or task_state='等待中');
				if @count11=0 and @number is not null then
					insert into gmms_shelf_task_2 (task_type,task_state,shelf_rack_id,load_rack,groups,start_time,line) values('从仓位到空仓位','等待中',@number,@number1,0,now(),3);
				end if;
		end if;
		
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for test
-- ----------------------------
DROP PROCEDURE IF EXISTS `test`;
delimiter ;;
CREATE PROCEDURE `test`(id long)
BEGIN

set @i=1;
while @i<=50 do
 INSERT INTO `gmms`.`gmms_yuliao_slicecage`(`slicecage_number`, `slicecage_group`, `slicecage_status`, `ban`) VALUES (@i, 2, NULL, 0);
  set @i=@i+1;
	end while;
end
;;
delimiter ;

-- ----------------------------
-- Procedure structure for timer_ontime
-- ----------------------------
DROP PROCEDURE IF EXISTS `timer_ontime`;
delimiter ;;
CREATE PROCEDURE `timer_ontime`(_name varchar(50) ,out result int)
BEGIN
	#Routine body goes here...
	    declare tm datetime DEFAULT null;
			select time_out into tm from gmms_timer where timer_name=_name;
			set result=-1;
			if tm is not null then 
			   if NOW()>tm then
						set result=1;
				 else
					  set result=0;
				 end if;
			 else
			 
				set	result=1;
			end if; 
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for timer_set
-- ----------------------------
DROP PROCEDURE IF EXISTS `timer_set`;
delimiter ;;
CREATE PROCEDURE `timer_set`(_name varchar(50),in add_second int)
BEGIN
	#Routine body goes here...
		DECLARE  tm1 datetime DEFAULT  null;
		DECLARE  tm2 datetime default null;
		set tm1=NOW();
		set tm2=TIMESTAMPADD(SECOND,add_second,NOW());
		
		update gmms_timer  set time_stamp=tm1 ,time_out=tm2 where timer_name=_name;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for Total_method
-- ----------------------------
DROP PROCEDURE IF EXISTS `Total_method`;
delimiter ;;
CREATE PROCEDURE `Total_method`(in sendMessageHex varchar(255),in machineids BIGINT,out messagedate varchar(255),out falgs varchar(255))
BEGIN
  declare msg varchar(255);
	set falgs=0;
	
	set @div_machine=machineids;
	
	set @z_now=now();
	
	update gmms_machine_message set message=sendMessageHex,mod_time=now() where machine_id=machineids;
	
-- 	call DoEvent(0,machineids,sendMessageHex);
	#Routine body goes here...
	#传入参数sendMessageHex：机器通讯信息，machineids：机器编号，输出参数messagedate：返回机器通讯信息
	#根据传过来的机器判断调用某个存储过程
	#1为上片，2为存储，4为测量，6为磨边,10为加速台，11为复合台，12为斜滚轮，15为下片机，16为输送台
	select machine_type_id into @typeid from gmms_machine where id=machineids;
	 UPDATE run_view SET tms=tms+1 where id=@typeid;
	if @typeid=1 then
		call Loads(sendMessageHex,machineids,@messagedate);
		set messagedate=@messagedate;
 	elseif @typeid=2 then
 		if machineids=46 or machineids=53 or machineids=54 then
		  set messagedate=null;
			
			
			call TemperingShelf_2(sendMessageHex,machineids,@messagedate);
			set messagedate=@messagedate;
			
		else
			call Shelf(sendMessageHex,machineids,@messagedate);
			set messagedate=@messagedate;
		end if;
-- 	elseif @typeid=3 then
-- 		call Loads(sendMessageHex,machineids,@messagedate);
-- 		set messagedate=@messagedate;
	elseif @typeid=4 then
		call Identify(sendMessageHex,machineids,@messagedate);
		set messagedate=@messagedate;
 	elseif @typeid=7 then
	   set msg:='开始 Slicecage';
		
 		call Slicecage(sendMessageHex,machineids,@messagedate,msg);
 		set messagedate=@messagedate;
		 UPDATE run_view SET msg=msg,t=now() where id=7;
 	elseif @typeid=6 then
 		call Edg(sendMessageHex,machineids,@messagedate);
 		set messagedate=@messagedate;
	elseif @typeid=10 then
		call Accelerate(sendMessageHex,machineids,@messagedate);
		set messagedate=@messagedate;
	elseif @typeid=11 then
		call Compound(sendMessageHex,machineids,@messagedate);
		set messagedate=@messagedate;
	elseif @typeid=12 then
		call Inclinedroller(sendMessageHex,machineids,@messagedate);
		set messagedate=@messagedate;
	elseif @typeid=15 then
		call Stops(sendMessageHex,machineids,@messagedate);
		set messagedate=@messagedate;
	elseif @typeid=16 then
		call Transportation(sendMessageHex,machineids,@messagedate);
		set messagedate=@messagedate;
	elseif @typeid=18 then
		call Horizontal(sendMessageHex,machineids,@messagedate,msg);
		set messagedate=@messagedate;
	elseif @typeid=17 then
		call Marking(sendMessageHex,machineids,@messagedate);
		set messagedate=@messagedate;
	elseif @typeid=19 then
		call RotatingRack_2(sendMessageHex,machineids,@messagedate);
		set messagedate=@messagedate;
	elseif @typeid=20 then
		call PendulumPiece_2(sendMessageHex,machineids,@messagedate);
		set messagedate=@messagedate;
	elseif @typeid=21 then
		call RobotHand_2(sendMessageHex,machineids,@messagedate,@falgs);
		set messagedate=@messagedate;
		set falgs=@falgs;
	elseif @typeid=22 then
		call TemperingRank(sendMessageHex,machineids,@messagedate);
		set messagedate=@messagedate;
	elseif @typeid=23 then
		call HollowSlicecage(sendMessageHex,machineids,@messagedate);
		set messagedate=@messagedate;
	end if;
	
   if (machineids=31) and  length(messagedate)>5 then 
		insert into io_log  (device_id,begin_time,end_time,read_message,send_message)
		values 
		(machineids,@z_now,now(),sendMessageHex,messagedate);
	
	end if;
--   if messagedate is not null and messagedate<>'' then
--     call DoEvent(1,machineids,messagedate);
--  end if;


END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for Transportation
-- ----------------------------
DROP PROCEDURE IF EXISTS `Transportation`;
delimiter ;;
CREATE PROCEDURE `Transportation`(in sendMessageHex varchar(255),in machineids BIGINT,out messagedate varchar(255))
BEGIN
	#输送台存储过程
	#找到当前机器标志位
	#查询机器逻辑表中对应的逻辑进行对应的步骤
	DECLARE machinedisposeids int DEFAULT 0;
	DECLARE dates varchar(255);
	DECLARE findplc varchar(255);
	DECLARE findmes varchar(255);
	DECLARE taskplc varchar(255);
	DECLARE taskmes varchar(255);
	DECLARE posunplc varchar(255);
	DECLARE posunmes varchar(255);
	DECLARE rankids varchar(255);
	DECLARE messagedates varchar(255) DEFAULT '';
	set messagedate=messagedates;
	
	select machine_type_id,work_id into @mtid,@work_groups from gmms_machine where id=machineids;
	
	select file_name into @engineering_id from gmms_raw_file where operator=@work_groups and state='已领取';
	
	
	
	select mf.flag into @完成标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='完成字';
	
	select mf.flag into @请求标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='请求字';
	
	select mf.flag into @发送标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='发送字';
	
	select mf.flag into @完成确认标志位  from gmms_machine_type mt join gmms_machine_font mf on mt.id=mf.machine_type_id where mf.machine_type_id=@mtid and mf.font_name='完成确认字';
	
	set findplc = substring(sendMessageHex,(cast(@完成标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskplc = substring(sendMessageHex,(cast(@请求标志位 as UNSIGNED INTEGER)*4)+19,4);
	set taskmes = substring(sendMessageHex,(cast(@发送标志位 as UNSIGNED INTEGER)*4)+19,4);
	set findmes = substring(sendMessageHex,(cast(@完成确认标志位 as UNSIGNED INTEGER)*4)+19,4);
	set rankids = conv(substring(sendMessageHex,(cast(@请求标志位+1 as UNSIGNED INTEGER)*4)+19,4),16,10);
	
 select taskplc;
 select taskmes;
	select findplc;
	select findmes;


	
	#请求逻辑查询
	select count(*),md.id into @请求数量,@请求逻辑编号 from gmms_machine_dispose md join gmms_machine_type mt on md.machine_type_id=mt.id where md.machine_type_id=@mtid  and md.plc_font_type='请求字' and md.mes_font_type='发送字' and md.plc_font_value=taskplc and md.mes_font_value=taskmes;


		if @请求逻辑编号!=0 then
			set machinedisposeids=@请求逻辑编号;
		end if;
		
		#查询当前执行逻辑进行判断
		select logic,return_value into @ExecutionLogic,@returnvalue from gmms_machine_dispose where id=machinedisposeids;
		
		if @ExecutionLogic='任务请求' then
				select count(*) into @counts from gmms_galss_task where engineering_id=@engineering_id and work_procedure='理片' and work_state='等待中' and rank=rankids;
				select rank into @rank from gmms_galss_task where engineering_id=@engineering_id and work_procedure='理片' and work_state='等待中' order by rank limit 0,1;
				if @counts=1 and @rank=rankids then
					#update gmms_galss_task set work_procedure_number=200 where engineering_id=@engineering_id and work_procedure='理片' and work_state='等待中' and rank=rankids;
					call message(@returnvalue,'000a',@messagedate);
 					set messagedate=@messagedate;
				end if;
 		elseif @ExecutionLogic='任务清空' then		
				set dates=CONCAT(@returnvalue,'0000');	
				call message(dates,'000a',@messagedate);
				set messagedate=@messagedate;
		end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for Update_raw
-- ----------------------------
DROP PROCEDURE IF EXISTS `Update_raw`;
delimiter ;;
CREATE PROCEDURE `Update_raw`(in line varchar(255),out banzuname varchar(255))
BEGIN
	#根據綫路獲取當前班組
	select banzu into banzuname from gmms_user_current where gongyi='磨边' and chanxian=line limit 0,1;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for web_alter_rank
-- ----------------------------
DROP PROCEDURE IF EXISTS `web_alter_rank`;
delimiter ;;
CREATE PROCEDURE `web_alter_rank`(_type varchar(50),_liuchengka varchar(50),_ids text,out _result int ,out _msg varchar(50))
BEGIN
	#Routine body goes here...
   declare zid bigint;
	 declare idx int;
	 declare zstr varchar(200);
	 set zid=0;
	 set idx=0;
set global log_bin_trust_function_creators = 1;
	 set zstr=get_str(_ids,idx);

	 set _msg="nothing";
	 #### 先检查玻璃是否和流程卡匹配，不匹配要报错，防止ID发错导致其他流程卡崩溃
	 while  zstr is not null do
	      set idx=idx+1;
		   SET zid=zstr;
			set @z_lk=null;
		
			select processcard_id into @z_lk from gmms_galss_task_buffer where id=zid LIMIT 1;

			if @z_lk is null then 
				set zstr=null;
			  set _msg="有玻璃不存在";
			else
				if @z_lk<>_liuchengka then 
						set zstr=null;
						set _msg="有玻璃流程卡不匹配";
				else
					  set zstr=get_str(_ids,idx);
		
				end if;
			end if;
	 end while;
	 
	 ###### 流程卡没问题 允许修改
	 if _msg="nothing" then 
			if _type="钢化前"  then
		
				update gmms_galss_task_stock set stop_rank=-1 where processcard_id=_liuchengka;
				 set idx=0;
				 set zstr=get_str(_ids,idx);
				 
				  while  zstr is not null do
					 set idx=idx+1;
	      SET zid=zstr;
			set @z_lk=null;
			select processcard_id into @z_lk from gmms_galss_task_stock where id=zid LIMIT 1;
			if @z_lk is null then 
			  
					insert into gmms_galss_task_stock (id,rank,type_id,galss_id,pattern_id,width,height,thickness,color,edg_width,edg_height,chamfer_size,galss_type,edg_speed1,edg_speed2,fine_throwing,work_state,work_procedure,slicecage_rank,drop_frame,slicecage_flag,out_rank,start_time,finsh_time,stop_position,processcard_id,engineering_id,broken_state,stop_rank,work_procedure_number,gc_number,gc_width,gc_height,gc_stockid,marking_flag) select id,rank,type_id,MOD(galss_id,10000),pattern_id,width,height,thickness,color,edg_width,edg_height,chamfer_size,galss_type,edg_speed1,edg_speed2,fine_throwing,'已完成','下片',slicecage_rank,drop_frame,slicecage_flag,out_rank,start_time,finsh_time,stop_position,processcard_id,engineering_id,broken_state,idx,work_procedure_number,gc_number,gc_width,gc_height,gc_stockid,marking_flag_ex from gmms_galss_task_buffer where id=zid;
			else
			
				update gmms_galss_task_stock set stop_rank=idx	 where id=zid;
			end if;
			  set zstr=get_str(_ids,idx);
	 end while;
			delete from gmms_galss_task_stock  where processcard_id=_liuchengka and stop_rank =-1;
				 
				 set _msg="ok";
			end if;
	 
	 end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for web_alter_rank_copy1
-- ----------------------------
DROP PROCEDURE IF EXISTS `web_alter_rank_copy1`;
delimiter ;;
CREATE PROCEDURE `web_alter_rank_copy1`(_type varchar(50),_liuchengka varchar(50),_ids text,out _result int ,out _msg varchar(50))
BEGIN
	#Routine body goes here...
   declare zid bigint;
	 declare idx int;
	 declare zstr varchar(200);
	 declare parentprocesscard_ varchar(200) DEFAULT (select parent_liuchengka from gmms_liuchengka where liuchengka=_liuchengka limit 0,1);
	 set zid=0;
	 set idx=0;
	 set zstr=get_str(_ids,idx);
	 set _msg="nothing";
	 #### 先检查玻璃是否和流程卡匹配，不匹配要报错，防止ID发错导致其他流程卡崩溃
	 while  zstr is not null do
	      set idx=idx+1;
		   SET zid=zstr;
			set @z_lk=null;
			select processcard_id into @z_lk from gmms_galss_task_buffer where id=zid LIMIT 1;
		
			if @z_lk is null then 
				set zstr=null;
			  set _msg="有玻璃不存在";
			else
				if @z_lk<>_liuchengka then 
						set zstr=null;
						set _msg="有玻璃流程卡不匹配";
				else
					  set zstr=get_str(_ids,idx);
		
				end if;
			end if;
	 end while;
	 
	 ###### 流程卡没问题 允许修改
	 if _msg="nothing" then 
			if _type="钢化后"  then
				update gmms_galss_task_shelf set stop_rank=-1 where processcard_id=_liuchengka;
				 set idx=0;
				 set zstr=get_str(_ids,idx);
				 
				  while  zstr is not null do
					 set idx=idx+1;
	      SET zid=zstr;
			set @z_lk=null;
			select processcard_id into @z_lk from gmms_galss_task_shelf where id=zid LIMIT 1;
			if @z_lk is null then 
					insert into gmms_galss_task_shelf (id,rank,type_id,galss_id,pattern_id,width,height,thickness,color,edg_width,edg_height,chamfer_size,galss_type,edg_speed1,edg_speed2,fine_throwing,work_state,work_procedure,slicecage_rank,drop_frame,slicecage_flag,out_rank,start_time,finsh_time,stop_position,processcard_id,engineering_id,broken_state,stop_rank,work_procedure_number,gc_number,gc_width,gc_height,gc_stockid,marking_flag) select id,rank,type_id,MOD(galss_id,10000),pattern_id,width,height,thickness,color,edg_width,edg_height,chamfer_size,galss_type,edg_speed1,edg_speed2,fine_throwing,'已完成','钢化后下片',slicecage_rank,drop_frame,slicecage_flag,0,start_time,finsh_time,0,processcard_id,engineering_id,broken_state,idx,work_procedure_number,parentprocesscard_,gc_width,gc_height,0,marking_flag_ex from gmms_galss_task_buffer where id=zid;
			else
			
				update gmms_galss_task_shelf set stop_rank=idx,work_state='已完成',work_procedure='钢化后下片' where id=zid and !(work_procedure='中空上片' and work_state='已完成');
			end if;
			  set zstr=get_str(_ids,idx);
	 end while;
			delete from gmms_galss_task_shelf  where processcard_id=_liuchengka and stop_rank =-1;				 
				 set _msg="ok";
			end if;
	 end if;
	 update gmms_galss_task_shelf set stop_position=0 where processcard_id=_liuchengka;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for workmanship
-- ----------------------------
DROP PROCEDURE IF EXISTS `workmanship`;
delimiter ;;
CREATE PROCEDURE `workmanship`(in line BIGINT)
BEGIN
	#Routine body goes here...
	DECLARE s int DEFAULT 1;
	DECLARE specificationss varchar(255) DEFAULT '';
	DECLARE engineering varchar(255) DEFAULT '';
	
	
	select file_name into @engineering_id from gmms_raw_file where state='已领取' and operator=line;
	set engineering= left(@engineering_id, 1);
  
	if engineering='P' then
		select count(aa.c) into @processcard_count from (select count(*) as c from gmms_galss_task where engineering_id=@engineering_id group by processcard_id,type_id) aa;
   
		while s<=@processcard_count  do
			select processcard_id,type_id into @processcard_id,@type_id from gmms_galss_task where engineering_id=@engineering_id group by processcard_id,type_id limit s,1;
			select count(*),edg_width,edg_height into @count5,@edg_width,@edg_height from gmms_galss_task where engineering_id=@engineering_id and processcard_id=@processcard_id and type_id=@type_id limit 0,1;
			select count(*) into @processcard_sum from gmms_galss_task where engineering_id=@engineering_id and processcard_id=@processcard_id and type_id=@type_id and work_procedure='下片' and work_state='已完成';
			set specificationss=CONCAT(@edg_width,"*",@edg_height);
			select count(*) into @count1 from gmms_workmanship where processcard_id=@processcard_id and specifications=specificationss;

			if @count1=0 then
				insert into gmms_workmanship (processcard_id,specifications,cuttingcount,edgingcount,processcard_count,starttime) values(@processcard_id,specificationss,@processcard_sum,@processcard_sum,@count5,now());
			else
				update gmms_workmanship set cuttingcount=@processcard_sum,edgingcount=@processcard_sum,processcard_count=@count5,starttime=now() where processcard_id=@processcard_id and specifications=specificationss;
			end if;
			select gy_gang into @gy_gang from gmms_liuchengka where liuchengka=@processcard_id;
			select gy_zhongkong into @gy_zhongkong from gmms_liuchengka where liuchengka=@processcard_id;
			if @gy_gang=1 then
				update gmms_workmanship set temperingcount=0 where processcard_id=@processcard_id and specifications=specificationss;
			end if;
			if @gy_zhongkong=1 then
				update gmms_workmanship set hollowcount=0 where processcard_id=@processcard_id and specifications=specificationss;
			end if;
			set s=s+1;
		end while;
	end if;
	

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for workmanships
-- ----------------------------
DROP PROCEDURE IF EXISTS `workmanships`;
delimiter ;;
CREATE PROCEDURE `workmanships`(in processcardid varchar(255))
BEGIN
	#Routine body goes here...
	DECLARE s int DEFAULT 0;
	DECLARE processcard_id_length int DEFAULT 0;
	DECLARE specificationss varchar(255) DEFAULT '';
	DECLARE engineering varchar(255) DEFAULT '';
	
	set processcard_id_length = char_length(processcardid);
	
	
	if processcard_id_length=13 then
	
		select count(aa.c) into @processcard_count from (select count(*) as c from gmms_galss_task_hollow where processcard_id like  CONCAT('%',processcardid,'%') group by processcard_id,type_id) aa;
		while s<@processcard_count  do
			select processcard_id,type_id into @processcard_id,@type_id from gmms_galss_task_hollow where processcard_id like  CONCAT('%',processcardid,'%') group by processcard_id,type_id limit s,1;
			
			select count(*),edg_width,edg_height into @count5,@edg_width,@edg_height from gmms_galss_task_hollow where processcard_id like  CONCAT('%',processcardid,'%') and processcard_id=@processcard_id and type_id=@type_id limit 0,1;
			
			select count(*) into @processcard_sum from gmms_galss_task_hollow where  processcard_id like  CONCAT('%',processcardid,'%') and processcard_id=@processcard_id and type_id=@type_id and work_procedure='中空上片' and work_state='已完成';
			
			set specificationss=CONCAT(@edg_width,"*",@edg_height);
			
			select count(*) into @count1 from gmms_workmanship where processcard_id=@processcard_id and specifications=specificationss;

			if @count1=1 then
				update gmms_workmanship set hollowcount=@processcard_sum where processcard_id=@processcard_id and specifications=specificationss;
				
			end if;
			
			set s=s+1;
		end while;
	elseif processcard_id_length=15 then
		select count(aa.c) into @processcard_count1 from (select count(*) as c from gmms_galss_task where processcard_id =processcardid group by processcard_id,type_id) aa;
		if @processcard_count1!=0 then
			
			while s<@processcard_count1  do
				select processcard_id,type_id into @processcard_id,@type_id from gmms_galss_task where processcard_id =processcardid group by processcard_id,type_id limit s,1;
				
				select count(*),edg_width,edg_height into @count5,@edg_width,@edg_height from gmms_galss_task where processcard_id =processcardid and processcard_id=@processcard_id and type_id=@type_id limit 0,1;
				
				select count(*) into @processcard_sum from gmms_galss_task where  processcard_id =processcardid and processcard_id=@processcard_id and type_id=@type_id and work_procedure='下片' and work_state='已完成';
				select @processcard_sum;
				
				set specificationss=CONCAT(@edg_width,"*",@edg_height);
				
				select count(*) into @count1 from gmms_workmanship where processcard_id=@processcard_id and specifications=specificationss;

				if @count1=0 then
					insert into gmms_workmanship (processcard_id,specifications,cuttingcount,edgingcount,processcard_count,starttime) values(@processcard_id,specificationss,@processcard_sum,@processcard_sum,@count5,now());
				else
					update gmms_workmanship set cuttingcount=@processcard_sum,edgingcount=@processcard_sum,processcard_count=@count5,starttime=now() where processcard_id=@processcard_id and specifications=specificationss;
				end if;
				select gy_gang into @gy_gang from gmms_liuchengka where liuchengka=@processcard_id;
				select gy_zhongkong into @gy_zhongkong from gmms_liuchengka where liuchengka=@processcard_id;
				if @gy_gang=1 then
					update gmms_workmanship set temperingcount=0 where processcard_id=@processcard_id and specifications=specificationss;
				end if;
				if @gy_zhongkong=1 then
					update gmms_workmanship set hollowcount=0 where processcard_id=@processcard_id and specifications=specificationss;
				end if;
				
				set s=s+1;
			end while;
		
		else
			
			select count(aa.c) into @processcard_count from (select count(*) as c from gmms_galss_task_2 where processcard_id =processcardid group by processcard_id,type_id) aa;
			while s<@processcard_count  do
				select processcard_id,type_id into @processcard_id,@type_id from gmms_galss_task_2 where processcard_id =processcardid group by processcard_id,type_id limit s,1;
				
				select count(*),edg_width,edg_height into @count5,@edg_width,@edg_height from gmms_galss_task_2 where processcard_id =processcardid and processcard_id=@processcard_id and type_id=@type_id limit 0,1;
				
				select count(*) into @processcard_sum from gmms_galss_task_2 where  processcard_id =processcardid and processcard_id=@processcard_id and type_id=@type_id and work_procedure='钢化后下片' and work_state='已完成';
				select @processcard_sum;
				
				set specificationss=CONCAT(@edg_width,"*",@edg_height);
				
				select count(*) into @count1 from gmms_workmanship where processcard_id=@processcard_id and specifications=specificationss;

				if @count1=1 then
					update gmms_workmanship set temperingcount=@processcard_sum where processcard_id=@processcard_id and specifications=specificationss;
					
				end if;
				
				set s=s+1;
			end while;
			
		end if;
	end if;
	
	

	

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for yuliao_create_task
-- ----------------------------
DROP PROCEDURE IF EXISTS `yuliao_create_task`;
delimiter ;;
CREATE PROCEDURE `yuliao_create_task`(_id bigint,out result int,out msg varchar(500))
BEGIN

   DECLARE m_count int DEFAULT(0);
   select count(	pattern_task_id) into m_count from gmms_yuliao_task where pattern_task_id =_id;
	 set msg="nothing";
	 set result=0;
	 if m_count=0 then 
	#Routine body goes here...
   insert into gmms_yuliao_task
   (
	  yuliao_id,
		pattern_task_id,
		state,
		lib_id,
		chanxian

	 )	 
	select c.id as gid,a.id as pid,0,null,b.operator from gmms_pattern_task a inner join gmms_raw_file b on a.raw_id=b.id
	inner join gmms_glass_yuliao c on c.gongchenghao=b.file_name and c.bantuxuhao=a.rank
	where a.id=_id and gao>300 and kuan>300;
	set result=100;
	set msg="ok";
	end if;

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for yuliao_input
-- ----------------------------
DROP PROCEDURE IF EXISTS `yuliao_input`;
delimiter ;;
CREATE PROCEDURE `yuliao_input`(in _yuliao_id bigint,in _target_id bigint,out result int,out msg varchar(200))
BEGIN
	#Routine body goes here...
	DECLARE d_count int default 0;
	DECLARE d_end bit default false;
	set result=0;
	set msg="nothing";
	
	if _target_id=0 then 
	
	   update gmms_yuliao_task set `state`=3 ,lib_id=null where yuliao_id=_yuliao_id;
		 set result=100;
		 set msg="ok";
	end if;
	
	
	SELECT count(*) into d_count from gmms_yuliao_task where lib_id=_target_id;
	if d_count>0 then 
	   set result=99;
		 set msg="库位已经被占用";
	end if;
	
	if result=0 then 
		 set	d_count=100;
		 select ban into d_count from gmms_yuliao_slicecage where id=_target_id;
		 if d_count<>0 then 
		   set result=99;
			 set msg="库位已经被占用";
		 end if;
	end if;
	
	if result=0 then 
	
	   update gmms_yuliao_task set `state`=1 ,lib_id=_target_id where yuliao_id=_yuliao_id;
		 set result=100;
		 set msg="ok";
	end if;
	
	
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for yuliao_output
-- ----------------------------
DROP PROCEDURE IF EXISTS `yuliao_output`;
delimiter ;;
CREATE PROCEDURE `yuliao_output`(in _yuliao_id bigint ,out result int,out msg varchar(255))
BEGIN
	#Routine body goes here...
		DECLARE d_count int default 0;
	DECLARE d_end bit default false;
	set msg="nothing";
	set d_count=null;
	SELECT lib_id into d_count from  gmms_yuliao_task where yuliao_id=_yuliao_id;
	if d_count is null then 
  	 set	d_end=true;
		set msg="无法出库：玻璃不在库中";
	end if;
	
	
	if d_end=false then 
		update gmms_yuliao_task set `state`=2 ,lib_id=null where yuliao_id=_yuliao_id;
		set msg="ok";
	end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for y_Diaozhuang
-- ----------------------------
DROP PROCEDURE IF EXISTS `y_Diaozhuang`;
delimiter ;;
CREATE PROCEDURE `y_Diaozhuang`(in RackId BIGINT,in lck varchar(50),in dropId BIGINT,out back_Num int,out back_Msg varchar(255))
BEGIN
		#吊装绑定架子  
		#参数：RackId 架子工位ID!    liuchengka流程卡号   dropId吊装工位ID
		#返回结果:back_Num 返回调用提示代码    back_Msg 返回调用提示内容
		#back_Num 失败=1 成功=2      back_Msg提示内容
		
		Declare isExistLck varchar(255) DEFAULT null;#此流程卡是否在其他架子上
		Declare isExistRack varchar(255) DEFAULT null; #吊装位上是否有架子
		#Declare isExistRackFlag varchar(255) DEFAULT null; #所需要的架子是否已经被其他任务占用
		Declare isUseRack varchar(255) DEFAULT null; #所需要的架子是否在某工位上
		Declare Rack_sign varchar(255) DEFAULT null;
		Declare Rack_falg varchar(255) DEFAULT null;
		
		#是否此流程卡已存在某一架子上
		select id into isExistLck from gmms_shelf_rack_2 where processcard_id=lck and id<42 limit 0,1;
		
		#是否此吊装位上有架子
		select rank into isExistRack from gmms_drop_frame_5
		where rank is not null and id=dropId and groups_=5;
		
		#此工位 信息
		select stop_rank,sign,flag into isUseRack,Rack_sign,Rack_falg from gmms_drop_frame_5
		where id=RackId and rank is not null and groups_=100 limit 0,1;
		
		if lck is null then
			set back_Num=1;
			set back_Msg=CONCAT(IFNULL(back_Msg,''),'流程卡号为空',IFNULL(lck,''));
		end if;
		if isExistLck is not null then
			set back_Num=1;
			set back_Msg=CONCAT(IFNULL(back_Msg,''),'此流程卡已存在',isExistLck,'号料架上; ');
		end if;
		if isExistRack is not null then
			set back_Num=1;		
			set back_Msg=CONCAT(IFNULL(back_Msg,''),'此吊装位上已有',isExistRack,'号料架; ');
		end if;
		if isUseRack is null then
			set back_Num=1;
			set back_Msg=CONCAT(IFNULL(back_Msg,''),'此库位无料架;');
		else
			if Rack_sign!=0 then
				set back_Num=1;
				set back_Msg=CONCAT(IFNULL(back_Msg,''),IFNULL(isUseRack,''),'库位被占用;');
			end if;
			if Rack_falg!=1 then
				set back_Num=1;
				set back_Msg=CONCAT(IFNULL(back_Msg,''),IFNULL(isUseRack,''),'库位被禁用;');
			end if;
		end if;
		if back_Msg is null then
		
			#(select processcard_id from gmms_galss_task_shelf where processcard_id='"+liuchengka+"' limit 0,1) 
			update gmms_drop_frame_5 as dr 
			left join gmms_drop_frame_5 as dr2 on dr.id=dropId and dr2.id=RackId
			left join gmms_shelf_rack_2 as sr on dr2.rank=sr.id
			set dr.rank=dr2.rank,dr.sign=0,dr2.sign=10,sr.processcard_id=lck
			where dr.id=dropId and dr2.id=RackId and dr.rank is null and dr2.rank is not null;
		
			insert into gmms_galss_task_shelf 
			(id,rank,type_id,galss_id,pattern_id,width,height,thickness,color,edg_width,edg_height,chamfer_size,
			galss_type,edg_speed1,edg_speed2,fine_throwing,work_state,work_procedure,slicecage_rank,drop_frame,
			slicecage_flag,out_rank,start_time,finsh_time,stop_position,processcard_id,engineering_id,broken_state,
			stop_rank,work_procedure_number,gc_number,gc_width,gc_height,gc_stockid,marking_flag) 
			select gt.id,gt.rank,gt.type_id,gt.galss_id,gt.pattern_id,gt.width,gt.height,gt.thickness,gt.color,gt.edg_width,gt.edg_height,
			gt.chamfer_size,gt.galss_type,gt.edg_speed1,gt.edg_speed2,gt.fine_throwing,'已完成','钢化后下片',gt.slicecage_rank,
			gt.drop_frame,gt.slicecage_flag,gt.out_rank,gt.start_time,gt.finsh_time,0,gt.processcard_id,gt.engineering_id,
			gt.broken_state,gt.stop_rank,gt.work_procedure_number,(select parent_liuchengka from gmms_liuchengka 
			where liuchengka=lck),gt.gc_width,gt.gc_height,gt.gc_stockid,gt.marking_flag 
			from gmms_galss_task_stock as gt left join gmms_galss_task_shelf as gs on gt.id=gs.id
			where gt.processcard_id=lck and gs.id is null;
			set back_Num=0;
			set back_Msg=CONCAT(IFNULL(back_Msg,''),lck,'流程卡绑定成功！');
		end if;
		
		
-- 		select * from gmms_drop_frame_5 as dr LEFT JOIN gmms_shelf_rack_2 as sr on dr.rank=sr.id 
-- 		where sr.processcard_id is not null and dr.id=dropId;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for y_optimize
-- ----------------------------
DROP PROCEDURE IF EXISTS `y_optimize`;
delimiter ;;
CREATE PROCEDURE `y_optimize`(in parentliuchengka varchar(255),in type int,out result int,out back_mag varchar(255))
BEGIN
	#y_optimize（父级流程卡）
	#优化  按架子上的顺序  与合片其他子流程卡进行优化  排序     
	DECLARE no_more_record DOUBLE DEFAULT 0;        #是否还有数据
	DECLARE typecount int DEFAULT 0;                 #玻璃类型
	DECLARE Carcount int DEFAULT 0;                 #流程卡数量
	DECLARE CarShefcount int DEFAULT 0;              #库内流程卡数量
	DECLARE Glasscount int DEFAULT 0;               #小片数量
	DECLARE count int DEFAULT 0;            		    #计数
	DECLARE mincount int DEFAULT 0;             	  #min计数
	DECLARE maxcount int DEFAULT 0;             	  #min计数
	DECLARE width_ int DEFAULT 0;             	  #min计数
	DECLARE height_ int DEFAULT 0;             	  #min计数
	DECLARE Resultmaxcount int DEFAULT 0;             	  #min计数
	DECLARE ResultmaxGalsscount int DEFAULT 0;             	  #min计数
	DECLARE Resultbegincount int DEFAULT 0;             	  #min计数
	DECLARE minglasscount int DEFAULT 0;             	  #min计数
	DECLARE liuchengka_ varchar(255) DEFAULT null;                 #流程卡
	#此合片任务 初始化  重新 优化  stop_position=0
	update gmms_galss_task_shelf 
	set stop_position=0,slicecage_rank=0,gc_stockid=0 
	where gc_number=parentliuchengka; 
	#库内流程卡玻璃类型数量
	select count(*) into typecount from 
		(select count(*) from gmms_galss_task_shelf 
		where gc_number=parentliuchengka and work_state='已完成' and work_procedure='钢化后下片' 
		group by edg_width,edg_height) as a;
	#库内流程卡数量	
	select count(*) into CarShefcount from 
		(select count(*) from gmms_galss_task_shelf 
		where gc_number=parentliuchengka and work_state='已完成' and work_procedure='钢化后下片' 
		group by processcard_id) as a;
		
	set Carcount= (select count(*) from gmms_liuchengka where parent_liuchengka=parentliuchengka);
	set Glasscount= (select piece_count from gmms_liuchengka where parent_liuchengka=parentliuchengka limit 0,1);
	#拿到最少的流程卡  的玻璃数
	select count(*) into minglasscount from gmms_galss_task_shelf 
	where gc_number=parentliuchengka and work_state='已完成' and work_procedure='钢化后下片' 
	group by processcard_id order by count(*) asc LIMIT 0,1;
	select CarShefcount,Carcount,count,typecount;
	#得到最少的流程卡号
	select processcard_id,count(*) from gmms_galss_task_shelf 
	where gc_number=parentliuchengka and work_state='已完成' and work_procedure='钢化后下片' 
	group by processcard_id order by count(*) asc limit 0,1;
	if CarShefcount=Carcount then
		#流程卡数=库内流程卡数
		#多尺寸循环优化	
		while Glasscount>=count and typecount>1 do
			set count=count+1;
			#取出  子流程卡里 同位置 同尺寸多的
			set width_=0;
			set height_=0;
			set mincount=0;	
			select edg_width,edg_height into width_,height_ from gmms_galss_task_shelf
			where gc_number=parentliuchengka and work_state='已完成' and work_procedure='钢化后下片' and stop_rank=count 
			group by edg_width,edg_height ORDER BY count(*) desc LIMIT 0,1;
		
			select min(stop_position)-1,MAX(stop_position)+1 into mincount,maxcount from gmms_galss_task_shelf 
			where gc_number=parentliuchengka and work_state='已完成' and work_procedure='钢化后下片';		
			call y_optimize_setrank(parentliuchengka,width_,height_,maxcount,mincount,Carcount);
		end while;
		set count=0;	
		while count<Carcount do
			select liuchengka into liuchengka_ from gmms_liuchengka where parent_liuchengka=parentliuchengka limit count,1;
			select max(gc_stockid),max(stop_rank) 
			into Resultmaxcount,ResultmaxGalsscount from gmms_galss_task_shelf
			where processcard_id=liuchengka_ and work_state='已完成' and work_procedure='钢化后下片';
			if typecount=1 then
				#一种尺寸
				select typecount,minglasscount;
				set @Resultrank=0;
				update gmms_galss_task_shelf 
				set stop_position=stop_rank
				where processcard_id=liuchengka_ and work_state='已完成' and work_procedure='钢化后下片';
				
				set @Resultrank=minglasscount+1;
				update gmms_galss_task_shelf	
				set gc_stockid=(@Resultrank:=@Resultrank-1) 
				where processcard_id=liuchengka_ and work_state='已完成' and work_procedure='钢化后下片' 
				order by stop_rank desc limit minglasscount;
			else
				if Resultmaxcount>0 then
					if type=1 then
						#缺片放里
						set @Resultrank=0;
						
						update gmms_galss_task_shelf set stop_position=(@Resultrank:=@Resultrank+1) 
						where processcard_id=liuchengka_ and work_state='已完成' and work_procedure='钢化后下片' order by gc_stockid asc;		
					elseif type=2 then
						#缺片放外
						update gmms_galss_task_shelf 
						set stop_position=gc_stockid 
						where processcard_id=liuchengka_ and work_state='已完成' and work_procedure='钢化后下片';
						set @Resultrank=Resultmaxcount;
						update gmms_galss_task_shelf set stop_position=(@Resultrank:=@Resultrank+1) 
						where processcard_id=liuchengka_ and work_state='已完成' and work_procedure='钢化后下片' and gc_stockid<1 order by gc_stockid asc;
					end if;		
				else
					update gmms_galss_task_shelf set stop_position=0
					where processcard_id=liuchengka_ and work_state='已完成' and work_procedure='钢化后下片';
				end if;
			end if;			
			set count=count+1;
		end while;
	end if;
	select * from gmms_galss_task_shelf 
			where gc_number=parentliuchengka and work_state='已完成' and work_procedure='钢化后下片'; 
	if (select count(*) from gmms_galss_task_shelf 
			where gc_number=parentliuchengka and work_state='已完成' and work_procedure='钢化后下片' and stop_position<1)>0
	then
		set result=0;
		set back_mag="失败";
	else
		set result=1;
		set back_mag="成功";
		update gmms_liuchengka set current_work_position='理片',current_status='等待中' 
		where parent_liuchengka=parentliuchengka and liuchengka in
		(select processcard_id from gmms_galss_task_shelf where gc_number=parentliuchengka and stop_position>0 group by processcard_id 
		HAVING GROUP_CONCAT(stop_rank order by stop_rank)!=GROUP_CONCAT(stop_position order by stop_rank));
		update gmms_liuchengka set current_work_position='理片',current_status='已完成' 
		where parent_liuchengka=parentliuchengka and liuchengka not in
		(select processcard_id from gmms_galss_task_shelf where gc_number=parentliuchengka and stop_position>0 group by processcard_id 
		HAVING GROUP_CONCAT(stop_rank order by stop_rank)!=GROUP_CONCAT(stop_position order by stop_rank));
		set @Resultrank=0;
		update gmms_galss_task_shelf 
		set galss_id=(@Resultrank:=@Resultrank+1) 
		where gc_number=parentliuchengka order by processcard_id asc,stop_position desc;
	end if;
	set @width=0;
	set @height=0;
	set mincount=0;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for y_optimize_GAI
-- ----------------------------
DROP PROCEDURE IF EXISTS `y_optimize_GAI`;
delimiter ;;
CREATE PROCEDURE `y_optimize_GAI`(in parentliuchengka varchar(255),in type int,out result int,out back_mag varchar(255))
BEGIN
	#y_optimize（父级流程卡）
	#优化  按架子上的顺序  与合片其他子流程卡进行优化  排序
	DECLARE typecount int DEFAULT 0;                 #玻璃类型数量
	DECLARE Carcount int DEFAULT 0;                 #流程卡数量
	DECLARE maxheight_ int DEFAULT 0;              #库内流程卡数量
	DECLARE CarShefcount int DEFAULT 0;              #库内流程卡数量
	
	
	
	
	if (select count(*) from gmms_liuchengka  where isyou='优化中' and liuchengka=parentliuchengka)=0 then
	#优化开始记录

	update gmms_liuchengka set isyou='优化中' 
	where liuchengka=parentliuchengka;
-- 	insert into gmms_galss_task_shelf_copy1 
-- 	select *,NOW(),'开始' from gmms_galss_task_shelf where gc_number=parentliuchengka;
			
	#此合片任务 初始化  重新 优化  stop_position=0
	update gmms_galss_task_shelf 
	set stop_position=0,slicecage_rank=0,out_rank=0 
	where gc_number=parentliuchengka; 
	#库内流程卡玻璃类型数量

	select count(*) into typecount from 
		(select count(*) from gmms_galss_task_shelf 
		where gc_number=parentliuchengka and work_state='已完成' and work_procedure='钢化后下片' 
		group by edg_width,edg_height) as a;
	#库内流程卡数量	
	select count(*) into CarShefcount from 
		(select count(*) from gmms_galss_task_shelf 
		where gc_number=parentliuchengka and work_state='已完成' and work_procedure='钢化后下片' 
		group by processcard_id) as a;
		#最大高度
	select MAX(height) into maxheight_ from gmms_galss_task_shelf 
		where gc_number=parentliuchengka and work_state='已完成' and work_procedure='钢化后下片';
		
	select child_count into Carcount from gmms_liuchengka where liuchengka=parentliuchengka;
	#玻璃全部到位
	if CarShefcount=Carcount and Carcount>0 then
		call y_optimize_not_same(parentliuchengka,Carcount,1);
	end if;
	#优化完成记录
-- 	insert into gmms_galss_task_shelf_copy1 
-- 	select *,NOW(),'完成' from gmms_galss_task_shelf where gc_number=parentliuchengka;
	if (select count(*) from gmms_galss_task_shelf 
			where gc_number=parentliuchengka and work_state='已完成' and work_procedure='钢化后下片' and stop_position<1)>0
	then
		set result=0;
		set back_mag="失败";
	elseif maxheight_>2000 then
		set result=1;
		set back_mag="成功";
		update gmms_liuchengka set current_work_position='理片',current_status='已完成' 
		where parent_liuchengka=parentliuchengka and liuchengka not in
		(select processcard_id from gmms_galss_task_shelf where gc_number=parentliuchengka and stop_position>0 group by processcard_id 
		HAVING GROUP_CONCAT(stop_rank order by stop_rank)!=GROUP_CONCAT(stop_position order by stop_rank));
		set @Resultrank=0;
		update gmms_galss_task_shelf 
		set galss_id=(@Resultrank:=@Resultrank+1) 
		where gc_number=parentliuchengka order by processcard_id asc,stop_position desc;
	else
		set result=1;
		set back_mag="成功";
		update gmms_liuchengka set current_work_position='理片',current_status='等待中' 
		where parent_liuchengka=parentliuchengka and liuchengka in
		(select processcard_id from gmms_galss_task_shelf where gc_number=parentliuchengka and stop_position>0 group by processcard_id 
		HAVING GROUP_CONCAT(stop_rank order by stop_rank)!=GROUP_CONCAT(stop_position order by stop_rank));
		
		update gmms_liuchengka set current_work_position='理片',current_status='已完成' 
		where parent_liuchengka=parentliuchengka and liuchengka not in
		(select processcard_id from gmms_galss_task_shelf where gc_number=parentliuchengka and stop_position>0 group by processcard_id 
		HAVING GROUP_CONCAT(stop_rank order by stop_rank)!=GROUP_CONCAT(stop_position order by stop_rank));
		set @Resultrank=0;
		update gmms_galss_task_shelf 
		set galss_id=(@Resultrank:=@Resultrank+1) 
		where gc_number=parentliuchengka order by processcard_id asc,stop_position desc;
	end if;
	update gmms_liuchengka set isyou='' 
	where liuchengka=parentliuchengka;
	end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for y_optimize_not_same
-- ----------------------------
DROP PROCEDURE IF EXISTS `y_optimize_not_same`;
delimiter ;;
CREATE PROCEDURE `y_optimize_not_same`(in parentliuchengka varchar(255),in CarCount int,in LocatPlace int)
BEGIN
	#不同尺寸优化-按片数最少的流程卡优化 （父级流程卡,子流程卡数量,摆放位置）
	#1.得到 父级流程卡下 片数最少的流程卡的片数 
	#2.理片优化不变stop_postion,中空优化根据最少片数N 从外往里数N个数 作为可配对数
	DECLARE Targetliuchengka_ varchar(255) DEFAULT null;  #目标流程卡
	DECLARE liuchengka_ varchar(255) DEFAULT null;        #子流程卡
	DECLARE minglasscount int DEFAULT 0;        					#父级流程卡下  最少片数子流程卡的片数
	DECLARE edgwidth_ int DEFAULT 0;             	  		  #目标高
	DECLARE edgheight_ int DEFAULT 0;             	  		#目标宽
	DECLARE CountA int DEFAULT 0;             	  					#循环次数
	DECLARE CountB int DEFAULT 0;             	  					#循环次数
	DECLARE CountP int DEFAULT 0;             	  					#匹配片数
	
	#拿到最少的流程卡的：玻璃数，流程卡号
	select count(*),processcard_id into minglasscount,Targetliuchengka_ from gmms_galss_task_shelf 
	where gc_number=parentliuchengka and work_state='已完成' and work_procedure='钢化后下片' 
	group by processcard_id order by count(*) asc,processcard_id asc LIMIT 0,1;
	
 	while CountB<Carcount do
 			set liuchengka_=null;
 			#子流程   去除破损重新排序
 			select liuchengka into liuchengka_ from gmms_liuchengka where parent_liuchengka=parentliuchengka limit CountB,1;		
 			set @Resultrank=0;
 			update gmms_galss_task_shelf 
 			set stop_rank=(@Resultrank:=@Resultrank+1)
 			where processcard_id=liuchengka_ and work_state='已完成' and work_procedure='钢化后下片'
 			order by stop_rank asc;
 			set @Resultrank=0;		
 			set CountB=CountB+1;		
 	end while;
 	set CountB=0;
 	set liuchengka_=null;
	#标记可以配对的玻璃
	while CountA<minglasscount do
		#得到目标流程卡从外 往里的尺寸
		set edgwidth_=0;
		set edgheight_=0;
		select edg_width,edg_height into edgwidth_,edgheight_ from gmms_galss_task_shelf 
		where gc_number=parentliuchengka and processcard_id=Targetliuchengka_ and work_state='已完成' and work_procedure='钢化后下片' 
		order by stop_rank desc limit CountA,1;
		set CountA=CountA+1;
		while CountB<Carcount do
			set liuchengka_=null;
			#子流程卡此尺寸 按架上顺序倒序取1
			select liuchengka into liuchengka_ from gmms_liuchengka where parent_liuchengka=parentliuchengka limit CountB,1;
 			select stop_rank,stop_position,processcard_id  from gmms_galss_task_shelf 
  			where gc_number=parentliuchengka and work_state='已完成' and work_procedure='钢化后下片' order by stop_position desc;
-- 			
 			select stop_rank,stop_position,processcard_id  from gmms_galss_task_shelf
 			where processcard_id=liuchengka_ and edg_width=edgwidth_ and edg_height=edgheight_ and stop_position=0
  		order by stop_rank desc;
			
			update gmms_galss_task_shelf 
			set stop_position=1000-CountA,out_rank=1000-CountA
			where processcard_id=liuchengka_ and edg_width=edgwidth_ and edg_height=edgheight_ and stop_position=0 
			and work_state='已完成' and work_procedure='钢化后下片'
			order by stop_rank desc limit 1;		
			set CountB=CountB+1;		
		end while;
		set CountB=0;
		set CountP=0;
		select count(*) into CountP from gmms_galss_task_shelf 
		where gc_number=parentliuchengka and work_state='已完成' and work_procedure='钢化后下片' and stop_position=1000-CountA;
		if CountP<CarCount and CountP>0 then
			update gmms_galss_task_shelf 
			set stop_position=0,out_rank=0
			where gc_number=parentliuchengka and work_state='已完成' and work_procedure='钢化后下片' and stop_position=1000-CountA;	
		end if;	
	end while;
	#select * from gmms_galss_task_shelf where gc_number=parentliuchengka order by stop_position desc;
	set CountB=0;
	#二次排序
	while CountB<Carcount do
		set liuchengka_=null;
		select liuchengka into liuchengka_ from gmms_liuchengka where parent_liuchengka=parentliuchengka limit CountB,1;
		
		set @Resultrank=0;
		update gmms_galss_task_shelf 
		set out_rank=(@Resultrank:=@Resultrank+1)
		where processcard_id=liuchengka_ and stop_position>0 and work_state='已完成' and work_procedure='钢化后下片'
		order by stop_position asc;
		
		set @Resultrank=0;
		update gmms_galss_task_shelf 
		set stop_position=(@Resultrank:=@Resultrank+1)
		where processcard_id=liuchengka_ and work_state='已完成' and work_procedure='钢化后下片'
		order by stop_position asc,stop_rank asc;
		
			
		set CountB=CountB+1;
	end while;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for y_optimize_same
-- ----------------------------
DROP PROCEDURE IF EXISTS `y_optimize_same`;
delimiter ;;
CREATE PROCEDURE `y_optimize_same`(in parentliuchengka varchar(255),in CarCount int)
BEGIN
	#同尺寸优化 （父级流程卡,子流程卡数量）
	#1.得到 父级流程卡下 片数最少的流程卡的片数
	#2.理片优化不变stop_postion,中空优化根据最少片数N 从外往里数N个数 作为可配对数
	DECLARE liuchengka_ varchar(255) DEFAULT null;        #子流程卡
	DECLARE minglasscount int DEFAULT 0;        					#父级流程卡下  最少片数子流程卡的片数
	DECLARE Count int DEFAULT 0;             	  					#循环次数
	
	#拿到最少的流程卡  的玻璃数
	select count(*) into minglasscount from gmms_galss_task_shelf 
	where gc_number=parentliuchengka and work_state='已完成' and work_procedure='钢化后下片' 
	group by processcard_id order by count(*) asc LIMIT 0,1;
	select minglasscount;
	while count<Carcount do
		set liuchengka_=null;
		select liuchengka into liuchengka_ from gmms_liuchengka where parent_liuchengka=parentliuchengka limit count,1;
		
		set @Resultrank=0;
		update gmms_galss_task_shelf 
		set stop_position=stop_rank
		where processcard_id=liuchengka_ and work_state='已完成' and work_procedure='钢化后下片';
		
		update gmms_galss_task_shelf	
		set out_rank=0
		where processcard_id=liuchengka_ and work_state='已完成' and work_procedure='钢化后下片';
		
		set @Resultrank=minglasscount+1;
		update gmms_galss_task_shelf	
		set out_rank=(@Resultrank:=@Resultrank-1) 
		where processcard_id=liuchengka_ and work_state='已完成' and work_procedure='钢化后下片' 
		order by stop_rank desc limit minglasscount;
		
		SET count=count+1;
	end while;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for y_optimize_setrank
-- ----------------------------
DROP PROCEDURE IF EXISTS `y_optimize_setrank`;
delimiter ;;
CREATE PROCEDURE `y_optimize_setrank`(in parentliuchengka varchar(255),in widths double,in heights double,in maxrank int,in minrank int,in Carcount int)
BEGIN
	#总优化 传入总流程卡,宽,高,顺序
	DECLARE no_more_record DOUBLE DEFAULT 0;  #是否还有数据
	DECLARE liuchengkas varchar(255);
 	DECLARE Actualcount int DEFAULT 0;
	#遍历   此合片流程卡内的 子片流程卡 
	DECLARE y_optimize_setrank CURSOR FOR SELECT liuchengka FROM gmms_liuchengka 
	where parent_liuchengka=parentliuchengka;
	DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_record = 1;
	OPEN y_optimize_setrank;
	FETCH y_optimize_setrank INTO liuchengkas;
		while no_more_record!=1 do
			
			update gmms_galss_task_shelf 
			set stop_position=maxrank,gc_stockid=maxrank
			where work_state='已完成' and work_procedure='钢化后下片' and processcard_id=liuchengkas 
			and stop_position=0 and edg_width=widths and edg_height=heights order by stop_rank ASC limit 1;
			
			set no_more_record=0;#重置  以防into 改变了 no_more_record值跳出游标			
			FETCH y_optimize_setrank INTO liuchengkas;
		end while;	
	#关闭游标
	CLOSE y_optimize_setrank;
	
	select count(*) into Actualcount from  gmms_galss_task_shelf   
	where gc_number=parentliuchengka and work_state='已完成' and work_procedure='钢化后下片' 
	and stop_position=maxrank and edg_width=widths and edg_height=heights;
	
	if Carcount!=Actualcount and Carcount!=0 then
		update gmms_galss_task_shelf  		
		set stop_position=minrank,gc_stockid=0
		where gc_number=parentliuchengka and  work_state='已完成' and work_procedure='钢化后下片' 
		and stop_position=maxrank and edg_width=widths and edg_height=heights;
	end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for y_RackFinsh
-- ----------------------------
DROP PROCEDURE IF EXISTS `y_RackFinsh`;
delimiter ;;
CREATE PROCEDURE `y_RackFinsh`(in machineids BIGINT,in flag BIGINT,in returnvalue varchar(255),out messagedate varchar(255))
BEGIN
	#料架完成
	DECLARE dates varchar(255);
	DECLARE groups1_ int DEFAULT 0;
	set @work_id=null;
	set @workingcount=null;
	set @sign=null;
	set @dropid=null;
	select work_id,remarks,flag into @work_id,@remarks,@flag1 from gmms_machine where id=machineids;
	if machineids=45 or machineids=69 then
		#钢化
		set groups1_=1;		
	elseif machineids=49 then
		#理片
		set groups1_=3;			
	elseif	machineids=51 or machineids=70 or machineids=72 or machineids=74	then
		#中空
		set groups1_=4;
	end if;
	
	select count(*) into @workingcount from gmms_drop_frame_5 where groups_=groups1_ and drop_groups=@work_id and (sign=2 or sign=5);
	select id,sign into @dropid,@sign from gmms_drop_frame_5 where groups_=groups1_ and drop_groups=@work_id and (sign=2 or sign=5);
	
	call message(returnvalue,'0014',@date);
	set messagedate=@date;
	
	if @workingcount=1 and messagedate is not null then
		if flag=1 then
			if @sign=2 then
				update gmms_drop_frame_5 set sign=3 where id=@dropid;
			elseif @sign=5 then
				update gmms_drop_frame_5 set sign=6 where id=@dropid;
			end if;
			insert into aaaa(MachineName,groups,data1,Content,Sendtext) values (machineids,@work_id,NOW(),'料架完成',messagedate);
		elseif flag=2 then
			if @sign=2 then
				update gmms_drop_frame_5 set sign=1 where id=@dropid;
			elseif @sign=5 then
				update gmms_drop_frame_5 set sign=4 where id=@dropid;
			end if;
			insert into aaaa(MachineName,groups,data1,Content,Sendtext) values (machineids,@work_id,NOW(),'料架失败',messagedate);		
		end if;		
-- 		call message(returnvalue,'0014',@date);
-- 	  set messagedate=@date;	
	end if;
	
	
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for y_RackSend
-- ----------------------------
DROP PROCEDURE IF EXISTS `y_RackSend`;
delimiter ;;
CREATE PROCEDURE `y_RackSend`(in machineids BIGINT,in returnvalue varchar(255),out messagedate varchar(255))
BEGIN
	#移动料架
	DECLARE dates varchar(255);
	DECLARE groups1_ int DEFAULT 0;
	set @work_id=null;
	set @workingcount=null;
	set @sign=null;
	set @dropid=null;
	
	select work_id,flag into @work_id,@flag1 from gmms_machine where id=machineids;
	if machineids=45 or machineids=69 then
		#钢化
		set groups1_=1;
		select id,stops,sign,stop_rank into @dropid,@dropstops,@sign,@stoprank from gmms_drop_frame_5 
		where groups_=groups1_ and drop_groups=@work_id and (sign=4 or sign=1) limit 0,1;
	elseif machineids=49 then
		#理片
		set groups1_=3;	
		select id,stops,sign,stop_rank into @dropid,@dropstops,@sign,@stoprank from gmms_drop_frame_5 
		where groups_=groups1_ and drop_groups=@work_id and (sign=4 or sign=1) limit 0,1;		
		select @work_id;
		select @dropid;
	elseif	machineids=51 or machineids=70 or machineids=72 or machineids=74	then
		#中空
		set groups1_=4;
		select df.id,df.stops,df.sign,df.stop_rank,st.sign into @dropid,@dropstops,@sign,@stoprank,@sign2 from gmms_drop_frame_5 as df 
		INNER JOIN  gmms_drop_frame_5 as st on df.stop_rank=st.stop_rank 
		where df.groups_=groups1_ and st.groups_=groups1_ 
		and df.drop_groups=@work_id and st.drop_groups=@work_id and df.stops<3 and (df.sign=4 or df.sign=1) and (st.sign=0 or st.sign=7)
		order by df.sign asc,df.stops desc limit 0,1;
		#3.4固定工位  
		update gmms_drop_frame_5 set sign=3 where groups_=groups1_ and drop_groups=@work_id and stops>2 and sign=1;
		update gmms_drop_frame_5 set sign=6 where groups_=groups1_ and drop_groups=@work_id and stops>2 and sign=4;
	end if;
	select machineids;
	#查看有没有正在工作的工位
	select count(*) into @workingcount from gmms_drop_frame_5 where groups_=groups1_ and drop_groups=@work_id and (sign=2 or sign=5);
	if @workingcount<1 and @dropid is not null and @dropstops is not null then
		if @sign=4 and (@sign2=0 or groups1_!=4) then
			update gmms_drop_frame_5 set sign=5 where id=@dropid;
			call intTo2ByteHex10_16_4(@dropstops,@stopss);
			set dates=CONCAT(returnvalue,"0000",@stopss,'0002');	
			call message(dates,'000a',@date);
			set messagedate=@date;
			insert into aaaa(MachineName,groups,data1,Content,Sendtext) values (machineids,@work_id,NOW(),'料架出',messagedate);
		elseif @sign=1 then
			update gmms_drop_frame_5 set sign=2 where id=@dropid;
			call intTo2ByteHex10_16_4(@dropstops,@stopss);
			set dates=CONCAT(returnvalue,"0000",@stopss,'0001');	
			call message(dates,'000a',@date);
			set messagedate=@date;	
			insert into aaaa(MachineName,groups,data1,Content,Sendtext) values (machineids,@work_id,NOW(),'料架进',messagedate);
		end if;
	end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for y_RankHollowFinsh
-- ----------------------------
DROP PROCEDURE IF EXISTS `y_RankHollowFinsh`;
delimiter ;;
CREATE PROCEDURE `y_RankHollowFinsh`(in machineids BIGINT,in returnvalue varchar(255),out messagedate varchar(255))
BEGIN
	#中空料架完成
	DECLARE dates varchar(255);
	set @work_id=null;
	set @workingcount=null;
	set @sign=null;
	set @dropid=null;
	select work_id,remarks,flag into @work_id,@remarks,@flag1 from gmms_machine where id=machineids;
	select count(*) into @workingcount from gmms_drop_frame_4 where groups=@work_id and (sign=2 or sign=5);
	select id,sign into @dropid,@sign from gmms_drop_frame_4 where groups=@work_id and (sign=2 or sign=5);
	if @workingcount=1 then
		if @sign=2 then
			update gmms_drop_frame_4 set sign=3 where id=@dropid;
		elseif @sign=5 then
			update gmms_drop_frame_4 set sign=6 where id=@dropid;
		end if;
		call message(returnvalue,'0014',@date);
	  set messagedate=@date;
	end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for y_RankHollowSend
-- ----------------------------
DROP PROCEDURE IF EXISTS `y_RankHollowSend`;
delimiter ;;
CREATE PROCEDURE `y_RankHollowSend`(in machineids BIGINT,in returnvalue varchar(255),out messagedate varchar(255))
BEGIN
	#中空料架
	DECLARE dates varchar(255);
	set @work_id=null;
	set @workingcount=null;
	set @sign=null;
	set @dropid=null;
	select work_id,remarks,flag into @work_id,@remarks,@flag1 from gmms_machine where id=machineids;
	select df.id,df.stops,df.sign,df.stop_rank into @dropid,@dropstops,@sign,@stoprank from gmms_drop_frame_4 as df 
	INNER JOIN  gmms_drop_frame_4 as st on df.stop_rank=st.stop_rank 
	where df.groups=@work_id and st.groups=@work_id and df.stops<3 and (df.sign=4 or df.sign=1) and st.sign=0 
	order by df.sign desc,df.stops desc;
	#3.4固定工位  
	update gmms_drop_frame_4 set sign=3 where groups=@work_id and stops>2 and sign=1;
	update gmms_drop_frame_4 set sign=6 where groups=@work_id and stops>2 and sign=4;
	
	#查看有没有正在工作的工位
	select count(*) into @workingcount from gmms_drop_frame_4 where groups=@work_id and (sign=2 or sign=5);
	if @workingcount<1 and @dropid is not null then
		if @sign=4 then
			update gmms_drop_frame_4 set sign=5 where id=@dropid;
			call intTo2ByteHex10_16_4(@dropstops,@stopss);
			set dates=CONCAT(@returnvalue,"0000",@stopss,'0002');	
			call message(dates,'000a',@date);
			set messagedate=@date;
		elseif @sign=1 then
			update gmms_drop_frame_4 set sign=2 where id=@dropid;
			call intTo2ByteHex10_16_4(@dropstops,@stopss);
			set dates=CONCAT(@returnvalue,"0000",@stopss,'0001');	
			call message(dates,'000a',@date);
			set messagedate=@date;
		end if;
	end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for y_RobotHollow
-- ----------------------------
DROP PROCEDURE IF EXISTS `y_RobotHollow`;
delimiter ;;
CREATE PROCEDURE `y_RobotHollow`(in machineids BIGINT,in returnvalue varchar(255),out messagedate varchar(255))
BEGIN
	#中空
	DECLARE dates varchar(255);
	DECLARE parentliuchengka_ varchar(255) DEFAULT null;
	DECLARE prcount_ int DEFAULT 0;  	#启用/禁用
	set @work_id=null;
	set @liuchengka=null;
	set @dropcount=null;
	set @id=null;
	set @color=null;
	set @processcount=null;
	set @stops=null;
	set @date=null;
	set @sign=null;
	set @processcard_id=null;
	set @cll=null;
	set @Occupieds=null;
	set @SuctionSheetMode=null;
	set @parent_liuchengka=null;
	select work_id,remarks,flag into @work_id,@remarks,@flag1 from gmms_machine where id=machineids;
	select @work_id;
	select lk.parent_liuchengka,lk.piece_count,lk2.Occupied,lk2.Suction_sheet_mode into @parent_liuchengka,@piececount,@Occupieds,@SuctionSheetMode from gmms_drop_frame_5 as dr5 
	left join gmms_shelf_rack_2 as sr on dr5.rank=sr.id
	left join gmms_liuchengka as lk on sr.processcard_id=lk.liuchengka
	left join gmms_liuchengka as lk2 on lk.parent_liuchengka=lk2.liuchengka
	where groups_=4 and drop_groups=@work_id and sr.id is not null and lk.liuchengka is not null and lk2.liuchengka is not null and
	lk2.current_status='正在工作' and lk2.current_work_position='中空' order by lk.liuchengka limit 0,1;

	if @parent_liuchengka is null then
		select lk.parent_liuchengka,lk2.Occupied into parentliuchengka_,@Occupieds from gmms_drop_frame_5 as dr5 
		left join gmms_shelf_rack_2 as sr on dr5.rank=sr.id
		left join gmms_liuchengka as lk on sr.processcard_id=lk.liuchengka
		left join gmms_liuchengka as lk2 on lk.parent_liuchengka=lk2.liuchengka
		where groups_=4 and drop_groups=@work_id and sr.id is not null and lk.liuchengka is not null and lk2.liuchengka is not null and
		lk2.current_status='等待中' and lk2.current_work_position='中空' order by lk.liuchengka limit 0,1;
		if @Occupieds=2 then
			#退库
			update gmms_drop_frame_5 as dr left join gmms_shelf_rack_2 as sr on dr.rank=sr.id
			left join gmms_liuchengka as lk on lk.liuchengka=sr.processcard_id
			set dr.sign=4
			where dr.rank is not null and sr.processcard_id is not null and lk.parent_liuchengka=parentliuchengka_ and dr.sign=3;
			#update gmms_liuchengka set current_status='等待中' where liuchengka=@parent_liuchengka;
		else
			update gmms_liuchengka set current_work_position='中空',current_status='正在工作' where liuchengka=parentliuchengka_;
		end if;		
	end if;
	
	#是否存在正在工作的任务  @workingcount
	select count(*) into @workingcount from gmms_galss_task_shelf 
	where work_state='正在工作' and work_procedure='中空上片' and gc_number=@parent_liuchengka;

	#吸片
	if @remarks=1 then
	if @workingcount<1 and @parent_liuchengka is not null then
		select @Occupieds;
		set @id=null;
		select @parent_liuchengka;
 		if @SuctionSheetMode=0 then
 			select id,galss_id,edg_width,edg_height,stop_rank,thickness,color,processcard_id,SUBSTR(processcard_id,LOCATE('/',processcard_id)+1) as cardrank 
 			into @id,@galss_id,@edg_width,@edg_height,@stop_rank,@thickness,@color,@processcard_id,@cardrank from gmms_galss_task_shelf 
 			where work_state='已完成' and work_procedure='钢化后下片' and gc_number=@parent_liuchengka and out_rank>0
 			order by out_rank desc,cardrank desc limit 0,1;
 		elseif @SuctionSheetMode=1 then
 			select id,galss_id,edg_width,edg_height,stop_rank,thickness,color,processcard_id,SUBSTR(processcard_id,LOCATE('/',processcard_id)+1) as cardrank 
 			into @id,@galss_id,@edg_width,@edg_height,@stop_rank,@thickness,@color,@processcard_id,@cardrank from gmms_galss_task_shelf 
 			where work_state='已完成' and work_procedure='钢化后下片' and gc_number=@parent_liuchengka and out_rank>0
 			order by out_rank desc,cardrank asc limit 0,1;
 		end if;
		
-- 		select id,galss_id,edg_width,edg_height,stop_rank,thickness,color,processcard_id,SUBSTR(processcard_id,LOCATE('/',processcard_id)+1) as cardrank 
-- 		into @id,@galss_id,@edg_width,@edg_height,@stop_rank,@thickness,@color,@processcard_id,@cardrank from gmms_galss_task_shelf 
-- 		where work_state='已完成' and work_procedure='钢化后下片' and gc_number=@parent_liuchengka and out_rank>0
-- 		order by out_rank desc,cardrank desc limit 0,1;
		
		if @id is null and @Occupieds=1 then
			select id,galss_id,edg_width,edg_height,stop_rank,thickness,color,processcard_id,SUBSTR(processcard_id,LOCATE('/',processcard_id)+1) as cardrank 
			into @id,@galss_id,@edg_width,@edg_height,@stop_rank,@thickness,@color,@processcard_id,@cardrank from gmms_galss_task_shelf 
			where work_state='已完成' and work_procedure='钢化后下片' and gc_number=@parent_liuchengka and stop_position>0
			order by galss_id asc limit 0,1;
		elseif @Occupieds=2 then
			#退库
			update gmms_drop_frame_5 as dr left join gmms_shelf_rack_2 as sr on dr.rank=sr.id
			left join gmms_liuchengka as lk on lk.liuchengka=sr.processcard_id
			set dr.sign=4
			where dr.rank is not null and sr.processcard_id is not null and lk.parent_liuchengka=@parent_liuchengka and dr.sign=3;
			update gmms_liuchengka set current_status='等待中' where liuchengka=@parent_liuchengka;
		end if;
		select @id;
		
		select IFNULL(max(stop_rank),0) into prcount_ from gmms_galss_task_shelf 
		where processcard_id=@processcard_id and out_rank>0 and work_state!='破损' and (groupsss is null or groupsss=@work_id);
		
		select stops,dr5.sign into @stops,@sign from gmms_drop_frame_5 as dr5 left join gmms_shelf_rack_2 as sr on dr5.rank=sr.id
		where sr.processcard_id=@processcard_id and sr.id is not null;
		select @stops;
		if (select count(*) from galss_C where color=@color)>0 then
			set @cll='0000';
		else
			set @cll='0001';
		end if;	
		if @id is not null and @cll is not null and @stops is not null and @sign=3 then					
			update gmms_galss_task_shelf set work_state='正在工作',work_procedure='中空上片',start_time=NOW() 
			where id=@id and work_procedure='钢化后下片';
			call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
			call intTo2ByteHex10_16_4(@stops,@stopss);
			call intTo2ByteHex10_16_4(@edg_width,@widths);
			call intTo2ByteHex10_16_4(@edg_height,@heights);
			call intTo2ByteHex10_16_4(@thickness,@thicknesss);
			call intTo2ByteHex10_16_4(@stop_rank,@stop_ranks);
			call intTo2ByteHex10_16_4(prcount_,@processcard_counts);								
			set dates=CONCAT(returnvalue,@galss_ids,@processcard_counts,'0000',@stopss,@widths,@heights,@thicknesss,@stop_ranks,@cll);
			call message(dates,'000a',@date);
			set messagedate=@date;
		end if;
	end if;	
	end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for y_RobotHollowFinsh
-- ----------------------------
DROP PROCEDURE IF EXISTS `y_RobotHollowFinsh`;
delimiter ;;
CREATE PROCEDURE `y_RobotHollowFinsh`(in flag int,in machineids BIGINT,in returnvalue varchar(255),out messagedate varchar(255))
BEGIN
	#中空完成
	DECLARE dates varchar(255);
	set @work_id=null;
	set @liuchengka=null;
	set @id=null;
	set @workingcount=null;
	set @processcard_id=null;
	set @workcount=null;
	set @dropid=null;	
	set @date=null;
	set @worked=null;
	select work_id,remarks,flag into @work_id,@remarks,@flag1 from gmms_machine where id=machineids;
	
	select lk.parent_liuchengka,lk.piece_count into @parent_liuchengka,@piececount from gmms_drop_frame_5 as dr5 
	left join gmms_shelf_rack_2 as sr on dr5.rank=sr.id
	left join gmms_liuchengka as lk on sr.processcard_id=lk.liuchengka
	left join gmms_liuchengka as lk2 on lk.parent_liuchengka=lk2.liuchengka
	where groups_=4 and drop_groups=@work_id and sr.id is not null and lk.liuchengka is not null and lk2.liuchengka is not null and
	lk2.current_status='正在工作' and lk2.current_work_position='中空' order by lk.liuchengka limit 0,1;
	
	select count(*) into @workingcount from gmms_galss_task_shelf 
	where work_state='正在工作' and work_procedure='中空上片' and gc_number = @parent_liuchengka;		
	if @workingcount=1 then
		select id,processcard_id into @id,@processcard_id from gmms_galss_task_shelf 
		where work_state='正在工作' and work_procedure='中空上片' and gc_number = @parent_liuchengka;
		if @id is not null then
			if flag=1 then
				update gmms_galss_task_shelf set work_state='已完成',finsh_time=NOW(),groupsss=@work_id where id=@id;
				call message(returnvalue,'0014',@date);
				set messagedate=@date;
				INSERT INTO e_record_hollow(processcard_id, glass_id, width, height, thinkess, color, sequence, create_time, line, team_name, type_num, is_report) 
						select gt.processcard_id,gt.id,gt.edg_width,gt.edg_height,gt.thickness,gt.color,gt.stop_rank,gt.finsh_time,@work_id,gt.banzu,gt.edg_speed2,1 from gmms_galss_task_shelf as gt 
						left join e_record_hollow as et on gt.id=et.glass_id 
						where et.glass_id is null and gt.id=@id and gt.work_state='已完成' and gt.work_procedure='中空上片';
			elseif flag=2 then
				update gmms_galss_task_shelf set work_state='已完成',work_procedure='钢化后下片' where id=@id;
				call message(returnvalue,'0014',@date);
				set messagedate=@date;
			elseif flag=3 then
				update gmms_galss_task_shelf set work_state='破损',groupsss=@work_id where id=@id;
				call message(returnvalue,'0014',@date);
				set messagedate=@date;
			end if;
			#查看此流程卡是否还有任务
			select count(*) into @workcount from gmms_galss_task_shelf 
			where processcard_id=@processcard_id and (work_state='已完成' or work_state='正在工作') and work_procedure='钢化后下片' and 
			out_rank>0;
			select count(*) into @worked from gmms_galss_task_shelf 
			where processcard_id=@processcard_id and (work_state='已完成' or work_state='正在工作') and work_procedure='钢化后下片';
			
			select dr5.id,stops into @dropid,@stops from gmms_drop_frame_5 as dr5 
			left join gmms_shelf_rack_2 as sr on dr5.rank=sr.id
			where sr.id is not null and sr.processcard_id=@processcard_id LIMIT 1;
			if @worked<1 then
			  if @dropid is not null and (@stops=1 or @stops=2) then
					update gmms_drop_frame_5 set sign=4 where id=@dropid;
				elseif @dropid is not null then
					update gmms_drop_frame_5 set sign=6 where id=@dropid;
				end if;
				if @worked=0 then
					update gmms_shelf_rack_2 as sr left join gmms_drop_frame_5 as dr on sr.id=dr.rank
					set sr.processcard_id=null
					where dr.rank is not null and dr.groups_=4 and dr.id=@dropid;
				end if;
			end if;
		end if;
	else
		call message(returnvalue,'0014',@date);
		set messagedate=@date;
	end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for y_RobotHollowSlicecage
-- ----------------------------
DROP PROCEDURE IF EXISTS `y_RobotHollowSlicecage`;
delimiter ;;
CREATE PROCEDURE `y_RobotHollowSlicecage`(in machineids BIGINT,in returnvalue varchar(255),out messagedate varchar(255))
BEGIN
	#中空理片机器手
	DECLARE dates varchar(255);
	DECLARE rankcount int DEFAULT 0;
	DECLARE signs_ int DEFAULT 0;
	DECLARE ranksumcount int DEFAULT 0;
	DECLARE maxpostoprank int DEFAULT 0;
	DECLARE stoprank int DEFAULT 0;
	DECLARE minstoprank int DEFAULT 0;
	DECLARE liuchengka_ varchar(255) DEFAULT null;
	DECLARE ischuku_ varchar(255) DEFAULT null;
	set @processcardid=null;
	set @id=null;
	set @sliceagecount=null;
	set @finshcount=null;
	set @piececount=null;
	set @dropid=null;
	set @outid=null;
  select remarks into @remarks from gmms_machine where id=machineids;
	#找到有任务的第一个工位,流程卡 默认B  A
	select dr5.id,lk.liuchengka,dr5.stops,lk.piece_count,dr5.sign,lk.ischu into @dropid,@processcardid,@stops,@piececount,signs_,ischuku_ from gmms_drop_frame_5 as dr5 
	left join gmms_shelf_rack_2 as sr on dr5.rank=sr.id
	left join gmms_liuchengka as lk on sr.processcard_id=lk.liuchengka
	where groups_=3 and drop_groups=1 and sr.id is not null and lk.liuchengka is not null and 
	lk.current_status='正在工作' and lk.current_work_position='理片';
  if @processcardid is null then
		select lk.liuchengka into liuchengka_ from gmms_drop_frame_5 as dr5 
		left join gmms_shelf_rack_2 as sr on dr5.rank=sr.id
		left join gmms_liuchengka as lk on sr.processcard_id=lk.liuchengka
		where groups_=3 and drop_groups=1 and dr5.sign=3 and sr.id is not null and lk.liuchengka is not null and 
		lk.current_status='等待中' and lk.current_work_position='理片' LIMIT 0,1;
		update gmms_liuchengka set current_work_position='理片',current_status='正在工作' where liuchengka=liuchengka_;
	end if;
	#未配对的数量,最小的玻璃顺序
	select count(*),min(stop_rank) into @sliceagecount,minstoprank from gmms_galss_task_shelf 
	where processcard_id=@processcardid and work_state='已完成' and work_procedure='钢化后下片' 
	and stop_position!=stop_rank and stop_position>0;
	
	#架子上的数量
	select count(*) into rankcount from gmms_galss_task_shelf 
	where processcard_id=@processcardid and ((work_state='已完成' and work_procedure='钢化后下片') or work_state='破损') and stop_position>0;
	
  #架子上的总数量
	select IFNULL(max(stop_rank),0) into ranksumcount from gmms_galss_task_shelf 
	where processcard_id=@processcardid and stop_position>0 and work_state!='破损';
	#优化结果中  架上最大的破损序号
	select IFNULL(max(stop_rank),0) into maxpostoprank from gmms_galss_task_shelf 
	where processcard_id=@processcardid and stop_position>0 and work_state='破损';
	
	select count(*) into @workcount from gmms_galss_task_shelf 
	where processcard_id=@processcardid and work_state='正在工作' and (work_procedure='理片笼上片' or work_procedure='理片笼下片');
	
	select @dropid,@workcount,@processcardid,signs_,@remarks,@sliceagecount,ischuku_;
	#and ranksumcount>=maxpostoprank
	if @dropid is not null and @workcount=0 and @processcardid is not null and signs_=3 and @remarks=1 then
		if @sliceagecount>0 and ischuku_ is null then
			#找到任务工位上 最外面的玻璃信息
			select id,galss_id,edg_width,edg_height,thickness,stop_rank,processcard_id,edg_speed2
			into @id,@galss_id,@edg_width,@edg_height,@thickness,stoprank,@processcard_id,@edg_speed2 from gmms_galss_task_shelf 
			where processcard_id=@processcardid and work_state='已完成' and work_procedure='钢化后下片'
			order by stop_rank desc limit 0,1;
			if @id is not null then				
				call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
				call intTo2ByteHex10_16_4(@stops,@stopss);
				call intTo2ByteHex10_16_4(@edg_width,@widths);
				call intTo2ByteHex10_16_4(@edg_height,@heights);
				call intTo2ByteHex10_16_4(@thickness,@thicknesss);
				call intTo2ByteHex10_16_4(stoprank,@stop_rankss);
				call intTo2ByteHex10_16_4(ranksumcount,@processcard_counts);
				if stoprank=minstoprank then
					set dates=CONCAT(returnvalue,@galss_ids,@processcard_counts,'0001',@stopss,@widths,@heights,@thicknesss,@stop_rankss,'0000');
				else
					set dates=CONCAT(returnvalue,@galss_ids,@processcard_counts,'0001',@stopss,@widths,@heights,@thicknesss,@stop_rankss,'0001');
				end if;
				if dates is not null then
					update gmms_galss_task_shelf set work_state='正在工作',work_procedure='理片笼上片' where id=@id;
				end if;
				
				call message(dates,'000a',@date);
				set messagedate=@date;
				insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,IFNULL(@id,0),NOW(),'发送理片机器人上片任务',"",messagedate);
			end if;			
		else
			update gmms_liuchengka set ischu='出库' where liuchengka=@processcardid and ischu='待出库';
			select id,galss_id,edg_width,edg_height,thickness,stop_rank,processcard_id
			into @outid,@galss_id,@edg_width,@edg_height,@thickness,stoprank,@processcard_id from gmms_galss_task_shelf 
			where processcard_id=@processcardid and work_state='已出库' and work_procedure='理片'
			order by stop_rank asc limit 0,1;
			if @outid is not null then				
				call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
				call intTo2ByteHex10_16_4(@stops,@stopss);
				call intTo2ByteHex10_16_4(@edg_width,@widths);
				call intTo2ByteHex10_16_4(@edg_height,@heights);
				call intTo2ByteHex10_16_4(@thickness,@thicknesss);
				call intTo2ByteHex10_16_4(stoprank,@stop_rankss);
				if (ranksumcount-1)=rankcount then
					set dates=CONCAT(returnvalue,@galss_ids,'00000002',@stopss,@widths,@heights,@thicknesss,@stop_rankss,'0000');
				else
					set dates=CONCAT(returnvalue,@galss_ids,'00000002',@stopss,@widths,@heights,@thicknesss,@stop_rankss,'0001');
				end if;
				if dates is not null then
					update gmms_galss_task_shelf set work_state='正在工作',work_procedure='理片笼下片' where id=@outid;		
				end if;
				call message(dates,'000a',@date);
				set messagedate=@date;
				insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,IFNULL(@outid,0),NOW(),'发送理片机器人下片任务',"",messagedate);
			end if;
			select '此时剩下玻璃片序相同，等待理片出片！';
		end if;
-- 	else
-- 		#标记  流程卡开始理片
-- 		update gmms_drop_frame_5 as dr5 
-- 		left join gmms_shelf_rack_2 as sr on dr5.rank=sr.id
-- 		left join gmms_liuchengka as lk on sr.processcard_id=lk.liuchengka
-- 		set lk.current_status='正在工作'
-- 		where groups_=3 and drop_groups=1 and sr.id is not null and lk.liuchengka is not null and 
-- 		lk.current_status='等待中' and lk.current_work_position='理片';
-- 	
end if;
	
	
	
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for y_RobotHollowSlicecageFinsh
-- ----------------------------
DROP PROCEDURE IF EXISTS `y_RobotHollowSlicecageFinsh`;
delimiter ;;
CREATE PROCEDURE `y_RobotHollowSlicecageFinsh`(in machineids BIGINT,in returnvalue varchar(255),in flag int,out messagedate varchar(255))
BEGIN
	DECLARE wordedcount int DEFAULT 0;  	#启用/禁用
	DECLARE posuncount int DEFAULT 0;  	#启用/禁用
	DECLARE zongcount int DEFAULT 0;  	#启用/禁用
	set @id=null;
	set @workingcount=null;
	set @work_procedure=null;
	set @processcardid=null;
	set @date=null;
	#理片完成
	#找到有任务的第一个工位,流程卡 默认B  A
	select dr5.id,lk.liuchengka,dr5.stops,lk.piece_count into @dropid,@processcardid,@stops,@piececount from gmms_drop_frame_5 as dr5 
	left join gmms_shelf_rack_2 as sr on dr5.rank=sr.id
	left join gmms_liuchengka as lk on sr.processcard_id=lk.liuchengka
	where groups_=3 and drop_groups=1 and sr.id is not null and lk.liuchengka is not null and 
	lk.current_status='正在工作' and lk.current_work_position='理片';
	
	select count(*) into @workingcount from gmms_galss_task_shelf 
	where processcard_id=@processcardid and work_state='正在工作' and (work_procedure='理片笼上片' or work_procedure='理片笼下片');
	
	select id,work_procedure,processcard_id,out_rank into @id,@work_procedure,@processcard_id,@outrank from gmms_galss_task_shelf 
	where processcard_id=@processcardid and work_state='正在工作' and (work_procedure='理片笼上片' or work_procedure='理片笼下片') limit 0,1;
	
	if @workingcount=1 and @id is not null then
	#得到正在工作的 任务数据	
		if flag=1 then
		#任务完成
			if @work_procedure='理片笼上片' then
				update gmms_galss_task_shelf set work_state='已完成' where id=@id;
			elseif @work_procedure='理片笼下片' then
				update gmms_galss_task_shelf set work_procedure='钢化后下片',work_state='已完成' where id=@id;
				
				select count(*) into wordedcount from gmms_galss_task_shelf 
				where processcard_id=@processcard_id and work_procedure='钢化后下片' and work_state='已完成' and stop_position>0 and stop_rank=stop_position;
				select count(*) into posuncount from gmms_galss_task_shelf 
				where processcard_id=@processcard_id and work_state='破损' and stop_position>0;
				select count(*) into zongcount from gmms_galss_task_shelf 
				where processcard_id=@processcard_id and stop_position>0;
				if wordedcount+posuncount=zongcount then
					#完成
					update gmms_drop_frame_5 set sign=4 where id=@dropid;
					update gmms_liuchengka set current_work_position='理片',current_status='已完成' where liuchengka=@processcardid;
				end if;	
			end if;
		elseif flag=2 then
		#任务失败
			if @work_procedure='理片笼上片' then
				update gmms_galss_task_shelf set work_state='已完成',work_procedure='钢化后下片' where id=@id;
			elseif @work_procedure='理片笼下片' then
				update gmms_galss_task_shelf set work_state='已出库',work_procedure='理片' where id=@id;		
			end if;
		end if;	
		call message(returnvalue,'0014',@date);
		set messagedate=@date;	
	end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for y_RobotLeng
-- ----------------------------
DROP PROCEDURE IF EXISTS `y_RobotLeng`;
delimiter ;;
CREATE PROCEDURE `y_RobotLeng`(in machineids BIGINT,in returnvalue varchar(255),in rankid BIGINT,out messagedate varchar(255))
BEGIN
	#中空
	DECLARE dates varchar(255);
	DECLARE filename_ varchar(255) DEFAULT null;
	DECLARE workcount_ int DEFAULT 0;  	#启用/禁用
	select machine_type_id,work_id into @mtid,@work_groups from gmms_machine where id=machineids;
	set @work_groups=1;
	select file_name into filename_ from gmms_raw_file where operator=@work_groups and state='已领取' limit 0,1;
	select count(*) into workcount_ from gmms_galss_task 
	where work_state='正在工作' and work_procedure='机器人下片' and engineering_id=filename_;
	
	select dr.stops,out_rank,gs.drop_frame,width,height,thickness,gs.processcard_id into @stops,@galss_id,@drop_frame,@width,@height,@thickness,@processcard_id from gmms_galss_task as gs
	left join gmms_drop_frame as dr on gs.processcard_id=dr.processcard_id
	where gs.work_state='等待中' and gs.work_procedure='机器人下片' and gs.engineering_id=filename_ and dr.processcard_id is not null
	order by out_rank asc limit 0,1; 
	
	select returnvalue,@galss_id,@stops,@width,@height,@thickness;
	if @stops is not null then
		call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
		call intTo2ByteHex10_16_4(@stops,@stopss);
		call intTo2ByteHex10_16_4(@width,@widths);
		call intTo2ByteHex10_16_4(@height,@heights);
		call intTo2ByteHex10_16_4(@thickness,@thicknesss);
		set dates=CONCAT(returnvalue,@galss_ids,"00000000",@stopss,@widths,@heights,@thicknesss);
		call message(dates,'000a',@date);
		set messagedate=@date;
		if messagedate is not null then
			update gmms_galss_task set work_state='正在工作' 
			where work_state='等待中' and work_procedure='机器人下片' and out_rank=@galss_id and processcard_id=@processcard_id limit 1;
			insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,"",NOW(),'发送机器人下片任务',"",messagedate);
		end if;
		#无任务
	end if;
-- 	set @galss_id=1,@stops=4,@edg_width=934,@edg_height=483,@thickness=5;
-- 	
-- 	call intTo2ByteHex10_16_4(@galss_id,@galss_ids);
-- 	call intTo2ByteHex10_16_4(@stops,@stopss);
-- 	call intTo2ByteHex10_16_4(@edg_width,@widths);
-- 	call intTo2ByteHex10_16_4(@edg_height,@heights);
-- 	call intTo2ByteHex10_16_4(@thickness,@thicknesss);
-- 	insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,"",NOW(),'发送下片任务',"",@stops);
-- 	set dates=CONCAT(returnvalue,@galss_ids,"00000000",@stopss,@widths,@heights,@thicknesss);
-- 	call message(dates,'000a',@date);
-- 	set messagedate=@date;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for y_RobotLengFinsh
-- ----------------------------
DROP PROCEDURE IF EXISTS `y_RobotLengFinsh`;
delimiter ;;
CREATE PROCEDURE `y_RobotLengFinsh`(in flag int,in machineids BIGINT,in returnvalue varchar(255),in rankid BIGINT,out messagedate varchar(255))
BEGIN
	#冷加工一线完成
	DECLARE dates varchar(255);
	DECLARE filename_ varchar(255) DEFAULT null;
	DECLARE count1 int;
	DECLARE count2 int;
	set @mtid=null;
	set @work_groups=null;
	set @processcard_id=null;
	set @drop_frame=null;
	set @galss_type=null;
	set @id1=null;
	set @id=null;
	set @sum=null;
	set @processcard_count=null;
	set @findcount=null;
	set @posuncount=null;
	set @maxstoprank=null;
	
	select machine_type_id,work_id into @mtid,@work_groups from gmms_machine where id=machineids;
	set @work_groups=1;
	select file_name into filename_ from gmms_raw_file where operator=@work_groups and state='已领取' limit 0,1;		

	select drop_frame,processcard_id,galss_type,id into @drop_frame,@processcard_id,@galss_type,@id1 from gmms_galss_task 
	where work_state='正在工作' and work_procedure='机器人下片' and out_rank=rankid and engineering_id=filename_ limit 0,1;
	
	#获取流程卡 总数：@processcard_count 完成数量 @sum
	select stops,finsh_count,processcard_count into @id,@sum,@processcard_count from gmms_drop_frame 
	where drop_frame=@drop_frame and processcard_id=@processcard_id and groups=@work_groups;
	#完成数量和破损数量
	select count(*) into @findcount from gmms_galss_task where processcard_id=@processcard_id and engineering_id=filename_ and work_state='已完成' and work_procedure='机器人下片';
	select count(*) into @posuncount from gmms_galss_task where processcard_id=@processcard_id and engineering_id=filename_ and work_state='破损';
	
	#获取最大的机器人下片位
	select count(*) into @maxstoprank from gmms_galss_task_stock where id>(select min(id) from (select * from gmms_galss_task_stock order by id desc limit 40000) as tbmin) and processcard_id=@processcard_id;
	
	select @work_groups,@drop_frame,filename_;
	if @drop_frame is not null then
		set count1=@maxstoprank+1;
		set count2=@sum+1;
		
		#满架下完 记录手机APP使用
		if @processcard_count=@findcount+@posuncount then
			call workmanships(@processcard_id);
		end if;
		
		if @processcard_count=count2 then
			call intTo2ByteHex10_16_4(@id,@ids);
			set dates=CONCAT(returnvalue,'00000000000000000001');
			call message(dates,'0018',@date);
			set messagedate=@date;
		else
			call message(returnvalue,'0018',@date);
			set messagedate=@date;
		end if;
			if messagedate is not null then
				update gmms_galss_task set work_state='已完成',work_procedure='下片',gc_stockid=@work_groups,stop_rank=count1,finsh_time=now(),work_procedure_number=500 
				where work_state='正在工作' and work_procedure='机器人下片' and out_rank=rankid and engineering_id=filename_ limit 1;
				update gmms_drop_frame set finsh_count=count2 where processcard_id=@processcard_id and groups=@work_groups;
				insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,"",NOW(),'发送机器人下片任务完成',"",messagedate);
				#非手动任务添加进机器人下片表
				if @galss_type=0 and (select count(*)	from gmms_galss_task_stock where id=@id1)=0 then
					insert into gmms_galss_task_details select * from gmms_galss_task where engineering_id=filename_ and id=@id1;
					insert into gmms_galss_task_stock select * from gmms_galss_task where engineering_id=filename_ and id=@id1;
				end if;
			end if;
	else
		select 1;
-- 		call message(returnvalue,'0018',@date);
-- 		set messagedate=@date;
		#insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,"",NOW(),'无任务发送机器人下片任务完成',"",messagedate);
	end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for y_RobotTemperingAfter
-- ----------------------------
DROP PROCEDURE IF EXISTS `y_RobotTemperingAfter`;
delimiter ;;
CREATE PROCEDURE `y_RobotTemperingAfter`(in rankid varchar(255),in machineids BIGINT,in returnvalue varchar(255),out messagedate varchar(255))
BEGIN
	#钢化后下片机械手
	DECLARE dates varchar(255);
	
	set @id=null;
	set @processcard_id=null;
	set @dropid=null;
	set @stops=null;
	set @workedcount=null;
	set @shelfracid=null;
	set @date=null;
	set @width=null;
	set @height=null;
	select work_id,remarks,flag into @work_id,@remarks,@flag1 from gmms_machine where id=machineids;

	select count(*) into @workingcount from gmms_galss_task_2 
	where work_state='正在工作' and work_procedure='钢化后下片' and groups=@work_id;
		
	select id,width,height,thickness,processcard_id,engineering_id,drop_frame 
	into @id,@width,@height,@thickness,@processcard_id,@engineering_id,@drop_frame from gmms_galss_task_2 
	where work_state='等待中' and work_procedure='钢化后下片' and groups=@work_id and galss_id=rankid limit 0,1;
	
	select count(*) into @workcount from gmms_galss_task_2 
	where work_state='等待中' and work_procedure='钢化后下片' and groups=@work_id and processcard_id=@processcard_id;
	select count(*) into @workedcount from gmms_galss_task_2 
	where work_state='已完成' and work_procedure='钢化后下片' and groups=@work_id and processcard_id=@processcard_id;
	
	#无工作中任务  查找是否存在此ID任务    正在工作的数量：@workingcount  等待中的数量：@workcount  已完成的数量：@workedcount
	if @workingcount=0 and @processcard_id is not null and @workcount>0 and @id is not null then					
		select stops into @stops from gmms_drop_frame_5 as dr5 left join gmms_shelf_rack_2 as sr on dr5.rank=sr.id
		where sr.processcard_id=@processcard_id and groups_=1 and drop_groups=@work_id and rank is not null;
		if @stops is not null then		
			set @workedcount=@workedcount+1;
			call intTo2ByteHex10_16_4(rankid,@galss_ids);
			call intTo2ByteHex10_16_4(@stops,@stopss);
			call intTo2ByteHex10_16_4(@width,@widths);
			call intTo2ByteHex10_16_4(@height,@heights);
			call intTo2ByteHex10_16_4(@thickness,@thicknesss);
			call intTo2ByteHex10_16_4(@workedcount,@finsh_counts);
			set dates=CONCAT(returnvalue,@galss_ids,'00000000',@stopss,@widths,@heights,@thicknesss,@finsh_counts);
			call message(dates,'000a',@date);
			set messagedate=@date;
			insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,@work_id,NOW(),'下片发送任务',@id,messagedate);
			update gmms_galss_task_2 set work_state='正在工作' where id=@id;
		else
			#自动绑架子  flag：1进入,2进入中,3到达指定位,4退出,5退出中,6等待退库
			select dr5.id,dr5.rank into @idrop,@shelfracid from gmms_drop_frame_5 as dr5 left join gmms_shelf_rack_2 as sr on dr5.rank=sr.id
			where  dr5.groups_=1 and dr5.drop_groups=@work_id and dr5.sign=3 and sr.processcard_id is null 
			and rank is not null and dr5.flag=1 limit 0,1;
			if @idrop is not null then
				update gmms_shelf_rack_2 set processcard_id=@processcard_id where id=@shelfracid;
				insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,@work_id,NOW(),'绑定新架子',rankid,@idrop);
			end if;
		end if;
	else 
		#当前正在工作
		select CONCAT('正在工作/此任务id不存在:',rankid);
	end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for y_RobotTemperingAfterFinsh
-- ----------------------------
DROP PROCEDURE IF EXISTS `y_RobotTemperingAfterFinsh`;
delimiter ;;
CREATE PROCEDURE `y_RobotTemperingAfterFinsh`(in flag int,in machineids BIGINT,in returnvalue varchar(255),out messagedate varchar(255))
BEGIN
	#钢化后下片机械手
	DECLARE dates varchar(255);
	DECLARE workedcounted INT DEFAULT 0; #记录总行数
	DECLARE workedcount INT DEFAULT 0;
	set @remarks=null;
	set @workingcount=null;
	set @id=null;
	set @rank=null;
	set @dropid=null;
	set @date=null;
	
	select work_id,remarks,flag into @work_id,@remarks,@flag1 from gmms_machine where id=machineids;
	
	select count(*) into @workingcount from gmms_galss_task_2
	where groups=@work_id and work_state='正在工作' and work_procedure='钢化后下片';
	
	if @workingcount=1 then
		select id,galss_id,processcard_id into @id,@galss_id,@processcardid from gmms_galss_task_2
		where groups=@work_id and work_state='正在工作' and work_procedure='钢化后下片' order by galss_id asc LIMIT 1;
		
		select count(*) into workedcount from gmms_galss_task_2
		where groups=@work_id and work_state='已完成' and work_procedure='钢化后下片' and processcard_id=@processcardid;
		
		select df.id,df.rank into @dropid,@rank from gmms_drop_frame_5 as df left join gmms_shelf_rack_2 as sr on df.rank=sr.id
		where sr.processcard_id=@processcardid and df.groups_=1 and df.drop_groups=@work_id;
		if flag=1 then
			set workedcount=workedcount+1;
-- 			update gmms_galss_task_2 set work_state='已完成',fine_throwing=@rank,stop_position=0,stop_rank=workedcount,finsh_time=NOW() 	
-- 			where id=@id;
-- 			
			update gmms_galss_task_2 as gt INNER join gmms_user_current as guc on gt.groups=guc.chanxian
			set gt.work_state='已完成',gt.fine_throwing=@rank,gt.stop_position=0,gt.stop_rank=workedcount,gt.finsh_time=NOW(),gt.banzu=guc.banzu
			where guc.gongyi='钢化' and gt.id=@id;
			
			insert into TemperingFinsh(machineid,glassid,Finshtime,stoprank,shelfrankid,processcard) 
			values (machineids,@id,NOW(),workedcount,@rank,@processcardid);	
		elseif flag=2 then
			update gmms_galss_task_2 set work_state='等待中',finsh_time=NOW() where id=@id;
		elseif flag=3 then
			update gmms_galss_task_2 set work_state='破损',finsh_time=NOW() where id=@id;
		end if;
		set workedcounted=(workedcount)+
		(select count(*) from gmms_galss_task_2 where groups=@work_id and work_state='破损' and processcard_id=@processcardid);
		
		select count(*) into @worksum from gmms_galss_task_2
		where groups=@work_id and processcard_id=@processcardid;
		if @worksum=workedcounted then
			update gmms_drop_frame_5 set sign=4 where id=@dropid;
		end if;
		call message(returnvalue,'0014',@date);
		set messagedate=@date;
		insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,@work_id,NOW(),'任务完成',@id,messagedate);
	end if;	
	
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for y_Shelf
-- ----------------------------
DROP PROCEDURE IF EXISTS `y_Shelf`;
delimiter ;;
CREATE PROCEDURE `y_Shelf`()
BEGIN
	DECLARE pro_id int DEFAULT 0;  
	DECLARE pro_id_outtime int DEFAULT 0;     #
	#半成品仓储   定时检索
	#1.检查 钢化后是否要补架子，
	#2.是否有需要理片的任务
	#3.是否有要去中空的任务
	#4.是否有要去旋转的任务
	select id into pro_id from y_pro_timer where pro_timer='Shelf' and is_enable='开启' and is_run='空闲' limit 0,1;
	select id into pro_id_outtime from y_pro_timer where  adddate(run_end_time, interval 90 second)<NOW() and is_enable='开启' and id=1 limit 0,1;
	if pro_id>0 or pro_id_outtime>0 then
		update y_pro_timer set is_run='运行中',run_begin_time=NOW() where id=1;
		call y_ShelfTempering(); #(钢化+旋转+理片+中空)退库任务，钢化补架+理片任务
		call y_Shelfcheck();     #移库任务

		call y_ShelfHollow(1);
		call y_ShelfHollow(2);
		call y_ShelfHollow(3);
		call y_ShelfHollow(4);
		call y_ShelfXz();
	
		call y_Shelfoptimize();
		update y_pro_timer set is_run='空闲',run_end_time=NOW() where id=1;
	end if;
	
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for y_ShelfAddHollow
-- ----------------------------
DROP PROCEDURE IF EXISTS `y_ShelfAddHollow`;
delimiter ;;
CREATE PROCEDURE `y_ShelfAddHollow`(in parentliuchengka_ varchar(255),in dropgroups BIGINT)
BEGIN
	#中空  仓储任务
	DECLARE isEnd DOUBLE DEFAULT 0;    #是否还有数据
	DECLARE liuchengka_ varchar(255) DEFAULT null;  #起始地
	DECLARE rank_ BIGINT DEFAULT 0;          #料架id
	DECLARE orientation_ varchar(255) DEFAULT null;  #面朝向
	
	DECLARE dropid_ int DEFAULT 0;  	#启用/禁用
	DECLARE drop_type_ varchar(255) DEFAULT null;  #起始地
	DECLARE stop_rank_ varchar(255) DEFAULT null;  #起始地
	DECLARE beginaddress_ varchar(255) DEFAULT null;  #起始地
	DECLARE endgroups_ BIGINT DEFAULT 0;     #目的线路
	DECLARE getgroups_ BIGINT DEFAULT 0;     #目的线路
	DECLARE CountCar_ BIGINT DEFAULT 0;     #目的线路
	DECLARE CountA_ BIGINT DEFAULT 0;     #目的线路
	DECLARE CountB_ BIGINT DEFAULT 0;     #目的线路
	DECLARE Carxuanze_ BIGINT DEFAULT 0;     #选择车子

	DECLARE ShelfAddHollow_cursor CURSOR FOR SELECT liuchengka,sr.id,sr.orientation FROM gmms_liuchengka as lk 
	left join gmms_shelf_rack_2 as sr on lk.liuchengka=sr.processcard_id
	where sr.processcard_id is not null and parent_liuchengka=parentliuchengka_;
	
	DECLARE CONTINUE HANDLER FOR NOT FOUND SET isEnd = 1;
	if dropgroups=1 then
		set Carxuanze_=1;
	elseif dropgroups=2 or dropgroups=3 or dropgroups=4 then
		set Carxuanze_=2;
	end if;
	OPEN ShelfAddHollow_cursor;
	FETCH ShelfAddHollow_cursor INTO liuchengka_,rank_,orientation_;
	
		select getgroups,child_count into getgroups_,CountCar_ from gmms_liuchengka where liuchengka=parentliuchengka_ LIMIT 0,1;
		#中空方向架数
	  select count(*) into CountA_ from gmms_drop_frame_5 as dr left join gmms_shelf_rack_2 as sr on dr.rank=sr.id
		left join gmms_liuchengka as lk on sr.processcard_id=lk.liuchengka
		where sr.id is not null and lk.liuchengka is not null and lk.parent_liuchengka=parentliuchengka_ and dr.stops>20;
		#钢化方向架数
		select count(*) into CountB_ from gmms_drop_frame_5 as dr left join gmms_shelf_rack_2 as sr on dr.rank=sr.id
		left join gmms_liuchengka as lk on sr.processcard_id=lk.liuchengka
		where sr.id is not null and lk.liuchengka is not null and lk.parent_liuchengka=parentliuchengka_ and dr.stops<21;
		while isEnd!=1 and Carxuanze_>0 and ((getgroups_=1 and CountB_=CountCar_  and CountB_>0) or(getgroups_!=1 and CountA_=CountCar_ and CountA_>0)) do
			#查找 和此流程卡使用面一样的  中空工位
			select stop_rank into beginaddress_ from gmms_drop_frame_5 where rank=rank_;
			select id,drop_type,stop_rank,all_groups into dropid_,drop_type_,stop_rank_,EndGroups_ from gmms_drop_frame_5 
			where groups_=4 and drop_groups=dropgroups and rank is null and sign=0 and orientation=orientation_ limit 0,1;
			if dropid_>0 then
				insert into gmms_shelf_tasks2(groups,tasktype,taskphase,taskstatus,starttime,beginaddress,endaddress,rackid,endgroups)
				select Carxuanze_,drop_type_,'出库','等待上车',NOW(),beginaddress_,stop_rank_,rank_,EndGroups_;
				update gmms_drop_frame_5 set sign=7 where id=dropid_;
				update gmms_drop_frame_5 as dr left join gmms_shelf_rack_2 as sr on dr.rank=sr.id
				set dr.sign=7 where sr.id is not null and sr.processcard_id=liuchengka_;
			end if;						
			set isEnd=0;#防止into 影响
			set dropid_=0;
			set stop_rank_=null;
			set endgroups_=0;
			set beginaddress_=0;
			set rank_=0;
			set orientation_=null;
			FETCH ShelfAddHollow_cursor INTO liuchengka_,rank_,orientation_;
		end while;
	
	#关闭游标
	CLOSE ShelfAddHollow_cursor;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for y_Shelfcheck
-- ----------------------------
DROP PROCEDURE IF EXISTS `y_Shelfcheck`;
delimiter ;;
CREATE PROCEDURE `y_Shelfcheck`()
BEGIN
	#钢化任务   +  中空+理片+旋转退库任务
	DECLARE isEnd DOUBLE DEFAULT 0;    #是否还有数据
	DECLARE id_ int DEFAULT 0;  	#启用/禁用
	DECLARE drop_type_ varchar(255) DEFAULT null;  #类型线路
	DECLARE stops_ varchar(255) DEFAULT null;  #在线库序号
	DECLARE stops2_ varchar(255) DEFAULT null;  #在线库序号2
	DECLARE stop_rank_ varchar(255) DEFAULT null;  #起始地
	DECLARE endgroups_ BIGINT DEFAULT 0;     #目的线路
	DECLARE rank_ BIGINT DEFAULT 0;          #料架id
	DECLARE sign_ BIGINT DEFAULT 0;          #料架id
	DECLARE rank2_ BIGINT DEFAULT 0;          #料架id2
	DECLARE orientation_ varchar(255) DEFAULT null;  #面朝向
	DECLARE processcardid_ varchar(255) DEFAULT null;  #流程卡
	DECLARE workstate_ varchar(255) DEFAULT null;  #工作状态
	DECLARE workprocedure_ varchar(255) DEFAULT null;  #工作阶段
	DECLARE parentliuchengkaws_ varchar(255) DEFAULT null;  #父级工作状态
	DECLARE parentliuchengkawp_ varchar(255) DEFAULT null;  #父级工作阶段
	DECLARE getgroups_ BIGINT DEFAULT 0;  #领取线路
	DECLARE parentliuchengka_ varchar(255) DEFAULT null;  #父级流程卡
	DECLARE back_mag_ varchar(255) DEFAULT null;  #是否优化成功
	#定义游标 
	DECLARE ShelfTempering_cursor CURSOR FOR 
	select dr.id,dr.drop_type,dr.stop_rank,dr.rank,dr.sign,dr.all_groups,dr.stops,sr.orientation,sr.processcard_id,
	lk.current_status,lk.current_work_position,lk2.getgroups,lk.parent_liuchengka,lk2.current_status,lk2.current_work_position from gmms_drop_frame_5 as dr 
	left join gmms_shelf_rack_2 as sr on dr.rank=sr.id 
	left join gmms_liuchengka as lk on sr.processcard_id=lk.liuchengka
	left join gmms_liuchengka as lk2 on lk.parent_liuchengka=lk2.liuchengka
	where dr.groups_=100 and dr.sign=0 and dr.flag=1;
	DECLARE CONTINUE HANDLER FOR NOT FOUND SET isEnd = 1;
	OPEN ShelfTempering_cursor;

	FETCH ShelfTempering_cursor 
	INTO id_,drop_type_,stop_rank_,rank_,sign_,EndGroups_,stops_,orientation_,processcardid_,workstate_,workprocedure_,getgroups_,parentliuchengka_,parentliuchengkaws_,parentliuchengkawp_;
		while isEnd!=1 do
			select sign into sign_ from gmms_drop_frame_5 where groups_=100 and stops=stops_;
			#判断此工位是否需要调度或者		
			if workprocedure_='理片' and workstate_='等待中' and sign_=0 then	
				if stops_<21 then
					#从1-20 调度到 21-41  已优化的理片任务
					select dr.stops,dr.rank into stops2_,rank2_ from gmms_drop_frame_5 as dr LEFT JOIN gmms_shelf_rack_2 as sr on dr.rank=sr.id
					left join gmms_liuchengka as lk on sr.processcard_id=lk.liuchengka
					left join gmms_liuchengka as lk2 on lk.parent_liuchengka=lk2.liuchengka
					where dr.groups_=100 and dr.sign=0 and dr.flag=1 and dr.stops>20 and dr.rank is not null and sr.processcard_id is not null
					and lk2.getgroups=1	limit 0,1;
					#查找 中空方向的 空架子 替换
					if stops2_=0 or stops2_ is null then
						select dr.stops,dr.rank into stops2_,rank2_ from gmms_drop_frame_5 as dr LEFT JOIN gmms_shelf_rack_2 as sr on dr.rank=sr.id
						left join gmms_liuchengka as lk on sr.processcard_id=lk.liuchengka
						left join gmms_liuchengka as lk2 on lk.parent_liuchengka=lk2.liuchengka
						where dr.groups_=100 and dr.sign=0 and dr.flag=1 and dr.stops>20 and dr.rank is not null and sr.processcard_id is null limit 0,1;
					end if;
					#查找 中空方向的 无领取+不需要理片的 替换
					if stops2_=0 or stops2_ is null then
						select dr.stops,dr.rank into stops2_,rank2_ from gmms_drop_frame_5 as dr LEFT JOIN gmms_shelf_rack_2 as sr on dr.rank=sr.id
						left join gmms_liuchengka as lk on sr.processcard_id=lk.liuchengka
						left join gmms_liuchengka as lk2 on lk.parent_liuchengka=lk2.liuchengka
						where dr.groups_=100 and dr.sign=0 and dr.flag=1 and dr.stops>20 and dr.rank is not null and sr.processcard_id is not null
						and lk2.getgroups is null and !(lk.current_work_position='理片' and lk.current_status='等待中') order by dr.id asc limit 0,1;
					end if;
					if stops2_>0 and (select count(*) from gmms_shelf_tasks2 where (rackid=rank_ or rackid=rank2_) and isrepeal='正常' and taskstatus!='已完成')=0 then
						insert into gmms_shelf_tasks2(groups,tasktype,taskphase,taskstatus,starttime,beginaddress,endaddress,rackid,endgroups)
						select 3,drop_type_,'移库','等待上车',NOW(),stops_,stops2_,rank_,150;
						insert into gmms_shelf_tasks2(groups,tasktype,taskphase,taskstatus,starttime,beginaddress,endaddress,rackid,endgroups)
						select 3,drop_type_,'移库','等待上车',NOW(),stops2_,stops_,rank2_,150;
						update gmms_drop_frame_5 as dr INNER join gmms_drop_frame_5 as dr1 
						set dr.sign=7,dr1.sign=7 where dr.stops=stops2_ and dr.groups_=100 and dr1.id=id_;
					end if;
				end if;
			elseif parentliuchengkawp_='中空' and parentliuchengkaws_='等待中' and getgroups_>0 and sign_=0 then
				if getgroups_=1 and stops_>20 then
					#从21-41 调度到 1-20   已领取的中空任务
					#找钢化方向  已领取 线路不等于1的
					select dr.stops,dr.rank into stops2_,rank2_ from gmms_drop_frame_5 as dr LEFT JOIN gmms_shelf_rack_2 as sr on dr.rank=sr.id
					left join gmms_liuchengka as lk on sr.processcard_id=lk.liuchengka
					left join gmms_liuchengka as lk2 on lk.parent_liuchengka=lk2.liuchengka
					where dr.groups_=100 and dr.sign=0 and dr.flag=1 and dr.stops<21 and dr.rank is not null and sr.processcard_id is not null
				  and lk2.getgroups>1 and lk2.getgroups is not null	limit 0,1;
					#找钢化方向 有流程卡的
					if stops2_=0 or stops2_ is null then
						select dr.stops,dr.rank into stops2_,rank2_ from gmms_drop_frame_5 as dr LEFT JOIN gmms_shelf_rack_2 as sr on dr.rank=sr.id
						left join gmms_liuchengka as lk on sr.processcard_id=lk.liuchengka
						left join gmms_liuchengka as lk2 on lk.parent_liuchengka=lk2.liuchengka
						where dr.groups_=100 and dr.sign=0 and dr.flag=1 and dr.stops<21 and dr.rank is not null and sr.processcard_id is not null and (lk2.getgroups is null or lk2.getgroups!=1)	limit 0,1;
					end if;
					#找钢化方向 有架子的	
					if stops2_=0 or stops2_ is null then
						select dr.stops,dr.rank into stops2_,rank2_ from gmms_drop_frame_5 as dr LEFT JOIN gmms_shelf_rack_2 as sr on dr.rank=sr.id
						left join gmms_liuchengka as lk on sr.processcard_id=lk.liuchengka
						left join gmms_liuchengka as lk2 on lk.parent_liuchengka=lk2.liuchengka
						where dr.groups_=100 and dr.sign=0 and dr.flag=1 and dr.stops<21 and dr.rank is not null and (lk2.getgroups is null or lk2.getgroups!=1) limit 0,1;
					end if;
					if stops2_>0 and (select count(*) from gmms_shelf_tasks2 where (rackid=rank_ or rackid=rank2_) and isrepeal='正常' and taskstatus!='已完成')=0 then
						insert into gmms_shelf_tasks2(groups,tasktype,taskphase,taskstatus,starttime,beginaddress,endaddress,rackid,endgroups)
						select 3,drop_type_,'移库','等待上车',NOW(),stops_,stops2_,rank_,200;
						insert into gmms_shelf_tasks2(groups,tasktype,taskphase,taskstatus,starttime,beginaddress,endaddress,rackid,endgroups)
						select 3,drop_type_,'移库','等待上车',NOW(),stops2_,stops_,rank2_,200;
						update gmms_drop_frame_5 as dr INNER join gmms_drop_frame_5 as dr1 
						set dr.sign=7,dr1.sign=7 where dr.stops=stops2_ and dr.groups_=100 and dr1.id=id_;
					end if;
				elseif (getgroups_=2 or getgroups_=3 or getgroups_=4) and stops_<21 and sign_=0 then
					#从1-20 调度到 21-41   已领取的中空任务
					#查找 中空方向的 已领的 1号中空线架子 替换
					select dr.stops,dr.rank into stops2_,rank2_ from gmms_drop_frame_5 as dr LEFT JOIN gmms_shelf_rack_2 as sr on dr.rank=sr.id
					left join gmms_liuchengka as lk on sr.processcard_id=lk.liuchengka
					left join gmms_liuchengka as lk2 on lk.parent_liuchengka=lk2.liuchengka
					where dr.groups_=100 and dr.sign=0 and dr.flag=1 and dr.stops>20 and dr.rank is not null and sr.processcard_id is not null
					and lk2.getgroups=1	limit 0,1;
					#查找 中空方向的 空架子 替换
					if stops2_=0 or stops2_ is null then
						select dr.stops,dr.rank into stops2_,rank2_ from gmms_drop_frame_5 as dr LEFT JOIN gmms_shelf_rack_2 as sr on dr.rank=sr.id
						left join gmms_liuchengka as lk on sr.processcard_id=lk.liuchengka
						left join gmms_liuchengka as lk2 on lk.parent_liuchengka=lk2.liuchengka
						where dr.groups_=100 and dr.sign=0 and dr.flag=1 and dr.stops>20 and dr.rank is not null and sr.processcard_id is null limit 0,1;
					end if;
					#查找 中空方向的 无领取+不需要理片的 替换
					if stops2_=0 or stops2_ is null then
						select dr.stops,dr.rank into stops2_,rank2_ from gmms_drop_frame_5 as dr LEFT JOIN gmms_shelf_rack_2 as sr on dr.rank=sr.id
						left join gmms_liuchengka as lk on sr.processcard_id=lk.liuchengka
						left join gmms_liuchengka as lk2 on lk.parent_liuchengka=lk2.liuchengka
						where dr.groups_=100 and dr.sign=0 and dr.flag=1 and dr.stops>20 and dr.rank is not null and sr.processcard_id is not null
						and lk2.getgroups is null and !(lk.current_work_position='理片' and lk.current_status='等待中') order by dr.id asc limit 0,1;
					end if;
					select stops2_;
					if stops2_>0 and (select count(*) from gmms_shelf_tasks2 where (rackid=rank_ or rackid=rank2_) and isrepeal='正常' and taskstatus!='已完成')=0 then
						insert into gmms_shelf_tasks2(groups,tasktype,taskphase,taskstatus,starttime,beginaddress,endaddress,rackid,endgroups)
						select 3,drop_type_,'移库','等待上车',NOW(),stops_,stops2_,rank_,200;
						insert into gmms_shelf_tasks2(groups,tasktype,taskphase,taskstatus,starttime,beginaddress,endaddress,rackid,endgroups)
						select 3,drop_type_,'移库','等待上车',NOW(),stops2_,stops_,rank2_,200;
						update gmms_drop_frame_5 as dr INNER join gmms_drop_frame_5 as dr1 
						set dr.sign=7,dr1.sign=7 where dr.stops=stops2_ and dr.groups_=100 and dr1.id=id_;
					end if;
				end if;
			elseif processcardid_ is null and rank_>0 and stops_>20 and sign_=0  then
				#21-41 空架 调度到  1-20						
				select dr.stops,dr.rank into stops2_,rank2_ from gmms_drop_frame_5 as dr 
				LEFT JOIN gmms_shelf_rack_2 as sr on dr.rank=sr.id			
				left join gmms_liuchengka as lk on sr.processcard_id=lk.liuchengka
				left join gmms_liuchengka as lk2 on lk.parent_liuchengka=lk2.liuchengka
				where dr.groups_=100 and dr.sign=0 and dr.flag=1 and dr.stops<21 and dr.rank is not null and sr.processcard_id is not null
		    and lk.liuchengka is not null and lk2.liuchengka is not null and lk2.current_work_position!='中空' limit 0,1;				
				if stops2_>0 and (select count(*) from gmms_shelf_tasks2 where (rackid=rank_ or rackid=rank2_) and isrepeal='正常' and taskstatus!='已完成')=0 then
					insert into gmms_shelf_tasks2(groups,tasktype,taskphase,taskstatus,starttime,beginaddress,endaddress,rackid,endgroups)
					select 3,drop_type_,'移库','等待上车',NOW(),stops_,stops2_,rank_,EndGroups_;
					insert into gmms_shelf_tasks2(groups,tasktype,taskphase,taskstatus,starttime,beginaddress,endaddress,rackid,endgroups)
					select 3,drop_type_,'移库','等待上车',NOW(),stops2_,stops_,rank2_,EndGroups_;
					update gmms_drop_frame_5 as dr INNER join gmms_drop_frame_5 as dr1 
					set dr.sign=7,dr1.sign=7 where dr.stops=stops2_ and dr.groups_=100 and dr1.id=id_;
				end if;
			end if;
			set isEnd=0;#防止into 影响
			set stops_=0;
			set stops2_=0;
			set rank2_=0;
			set stop_rank_=null;
			set endgroups_=0;
			set rank_=0;
			set orientation_=null;
			set drop_type_=null;
			set processcardid_=null;
			set parentliuchengka_=null;
			set workstate_=null;
			set workprocedure_=null;
			set getgroups_=0;
			FETCH ShelfTempering_cursor INTO id_,drop_type_,stop_rank_,rank_,sign_,EndGroups_,stops_,orientation_,processcardid_,workstate_,workprocedure_,getgroups_,parentliuchengka_,parentliuchengkaws_,parentliuchengkawp_;
		end while;
	
	#关闭游标
	CLOSE ShelfTempering_cursor;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for y_ShelfFinsh
-- ----------------------------
DROP PROCEDURE IF EXISTS `y_ShelfFinsh`;
delimiter ;;
CREATE PROCEDURE `y_ShelfFinsh`(in machineids BIGINT,in flag int,in returnvalue varchar(255),out messagedate varchar(255))
BEGIN
	#仓储完成
	DECLARE parentliuchengka_ varchar(255) DEFAULT null;    #父级流程卡
	DECLARE liuchengka_ varchar(255) DEFAULT null;    #子流程卡
	DECLARE Taskid_ bigint DEFAULT 0;    #车上架子任务id
	DECLARE Taskphase_ varchar(255) DEFAULT null;    #任务进/出/移库
	DECLARE Taskstatus_ varchar(255) DEFAULT null;    #任务状态
	DECLARE Tasktype_ varchar(255) DEFAULT null;    #任务类型
	DECLARE Beginaddress_ varchar(255) DEFAULT null;    #起始地
	DECLARE Endaddress_ varchar(255) DEFAULT null;    #目的地
	DECLARE orientation_ varchar(255) DEFAULT null;    #朝向
	DECLARE rackid_ bigint DEFAULT 0;    #车上架子任务id
	DECLARE zknotFinsh bigint DEFAULT 0;    #中空未完成数量
	DECLARE groups_ BIGINT DEFAULT  
	(select work_id from gmms_machine where id=machineids);  #线路
	DECLARE isWorking bigint DEFAULT 
	(select count(id) from gmms_shelf_tasks2 where groups=groups_ and (taskstatus='正在上车' or taskstatus='正在下车') and isrepeal='正常');   #是否正在工作

	if isWorking=1 then
		#有正在工作的任务
		select id,taskphase,taskstatus,tasktype,rackid,beginaddress,endaddress 
		into Taskid_,Taskphase_,Taskstatus_,Tasktype_,rackid_,Beginaddress_,Endaddress_ from gmms_shelf_tasks2 
		where groups=groups_ and (taskstatus='正在上车' or taskstatus='正在下车') and isrepeal='正常';	
						
		if flag=1 or flag=3 then
			if Taskstatus_='正在上车' then
				if Tasktype_='中空' and Taskphase_='入库' then
					update gmms_shelf_tasks2 set taskstatus='已上车',updatetime=NOW(),finshtype1=flag where id=Taskid_;
					update gmms_drop_frame_5 set rank=null,sign=0 where stop_rank=Beginaddress_ and rank is not null order by stops desc LIMIT 1;
				elseif Tasktype_='旋转' and Taskphase_='入库' then
					if Beginaddress_='99' then
						update gmms_shelf_tasks2 set taskstatus='已上车',updatetime=NOW(),finshtype1=flag where id=Taskid_;
						update gmms_drop_frame_5 set rank=null,sign=0 where stop_rank=98;
					elseif Beginaddress_='98' then
						update gmms_shelf_tasks2 set taskstatus='已上车',updatetime=NOW(),finshtype1=flag where id=Taskid_;
						update gmms_drop_frame_5 set rank=null,sign=0 where stop_rank=99;
					end if;					
				else
					update gmms_shelf_tasks2 set taskstatus='已上车',updatetime=NOW(),finshtype1=flag where id=Taskid_;
					update gmms_drop_frame_5 set rank=null,sign=0 where stop_rank=Beginaddress_;
				end if;			
			elseif Taskstatus_='正在下车' then				
				if Taskphase_='入库' then
					if Tasktype_='钢化' then
						select parent_liuchengka,liuchengka into parentliuchengka_,liuchengka_ from gmms_liuchengka 
						where liuchengka=(select processcard_id from gmms_shelf_rack_2 where id=rackid_ limit 0,1);
						insert into gmms_galss_task_shelf 
						(id,rank,type_id,galss_id,pattern_id,width,height,thickness,color,edg_width,edg_height,chamfer_size,
						galss_type,edg_speed1,edg_speed2,fine_throwing,work_state,work_procedure,slicecage_rank,drop_frame,
						slicecage_flag,out_rank,start_time,finsh_time,stop_position,processcard_id,engineering_id,broken_state,
						stop_rank,work_procedure_number,gc_number,gc_width,gc_height,gc_stockid,marking_flag) 
						select gt.id,gt.rank,gt.type_id,gt.galss_id,gt.pattern_id,gt.width,gt.height,gt.thickness,gt.color,gt.edg_width,gt.edg_height,
						gt.chamfer_size,gt.galss_type,gt.edg_speed1,gt.edg_speed2,gt.fine_throwing,gt.work_state,gt.work_procedure,gt.slicecage_rank,
						gt.drop_frame,gt.slicecage_flag,gt.out_rank,gt.start_time,gt.finsh_time,0,gt.processcard_id,gt.engineering_id,
						gt.broken_state,gt.stop_rank,gt.work_procedure_number,parentliuchengka_,gt.gc_width,gt.gc_height,gt.gc_stockid,gt.marking_flag 
						from gmms_galss_task_2 as gt left join gmms_galss_task_shelf as gs on gt.id=gs.id
						where gt.processcard_id=liuchengka_ and gt.work_state='已完成' and gt.work_procedure='钢化后下片' and gs.id is null;
						#钢化后的报工
						INSERT INTO e_record_tempering(processcard_id, glass_id, width, height, thinkess, color, sequence, create_time, line, team_name, type_num, is_report,rank,furnace) 
						select gt.processcard_id,gt.id,gt.edg_width,gt.edg_height,gt.thickness,gt.color,gt.stop_rank,gt.finsh_time,gt.groups,gt.banzu,gt.edg_speed2,1,gt.fine_throwing,gt.furnace from gmms_galss_task_2 as gt 
						left join e_record_tempering as et on gt.id=et.glass_id 
						where et.glass_id is null and gt.processcard_id=liuchengka_ and gt.work_state='已完成' and gt.work_procedure='钢化后下片';
						
-- 						delete gt from gmms_galss_task_2 as gt 
-- 						left join e_record_tempering as et on gt.id=et.glass_id 
-- 						where et.glass_id is not null and gt.processcard_id=liuchengka_;
						delete from gmms_galss_task_2 where processcard_id=liuchengka_;
						#钢化后的破损
-- 						INSERT INTO e_pierceds(processcard_id, type_num, pieces, pierced_type, pierced_cause, is_report, glass_id, create_time, process, line, gc_number, cs_number, team_name) 
-- 						select gs.processcard_id,gs.edg_speed2,1,'刺破类型','刺破原因',1,gs.id,NOW(),'钢化',gs.groups,gs.engineering_id,0,gs.banzu from gmms_galss_task_2 as gs left join E_Pierceds as ep on gs.id=ep.glass_id
-- 						where ep.glass_id is null and gs.processcard_id=liuchengka_ and gs.work_state='破损';
						
						update gmms_drop_frame_5 set rank=rackid_,sign=0,rankposition=Beginaddress_ where stop_rank=Endaddress_ limit 1;
						update gmms_liuchengka set isgang='已钢化' where liuchengka=liuchengka_;
					elseif Tasktype_='旋转' then						
						select orientation into orientation_ from gmms_shelf_rack_2 
						where id=rackid_ limit 0,1;
						if orientation_='A' then
							update gmms_shelf_rack_2 set orientation='B' where id=rackid_;
						elseif orientation_='B' then
							update gmms_shelf_rack_2 set orientation='A' where id=rackid_;
						end if;	
						update gmms_drop_frame_5 set rank=rackid_,sign=0,rankposition=Beginaddress_ where stop_rank=Endaddress_ limit 1;
					elseif Tasktype_='理片' then
						select liuchengka into liuchengka_ from gmms_liuchengka 
						where liuchengka=(select processcard_id from gmms_shelf_rack_2 where id=rackid_ limit 0,1);
						update gmms_liuchengka set ischu=null where liuchengka=liuchengka_;
						update gmms_drop_frame_5 set rank=rackid_,sign=0,rankposition=Beginaddress_ where stop_rank=Endaddress_ order by stops asc limit 1;
						
					elseif Tasktype_='中空' then
						#保存数据  然后删除库存表  zknotFinsh
						select liuchengka into liuchengka_ from gmms_liuchengka 
						where liuchengka=(select processcard_id from gmms_shelf_rack_2 where id=rackid_ limit 0,1);
						
						set zknotFinsh=(select count(*) from gmms_galss_task_shelf where processcard_id=liuchengka_ and !(work_state='已完成' and work_procedure='中空上片'));
						if zknotFinsh=0 then
							insert into gmms_galss_task_shelf_log  
							select gt.* from gmms_galss_task_shelf as gt left join gmms_galss_task_shelf_log as gs on gt.id=gs.id
							where gt.processcard_id=liuchengka_  and gs.id is null and 
							(select count(*) from gmms_galss_task_shelf where processcard_id=gt.processcard_id and !(work_state='已完成' and work_procedure='中空上片'))=0;
							delete from gmms_galss_task_shelf where processcard_id=liuchengka_;
						end if;
						update gmms_galss_task_shelf set stop_position=0 where processcard_id=liuchengka_;
						
						update gmms_drop_frame_5 set rank=rackid_,sign=0,rankposition=Beginaddress_ where stop_rank=Endaddress_ order by stops asc limit 1;
					else	
						update gmms_drop_frame_5 set rank=rackid_,sign=0,rankposition=Beginaddress_ where stop_rank=Endaddress_ order by stops asc limit 1;
					end if;					
				elseif Taskphase_='出库' then
					if Tasktype_='旋转' then
						update gmms_drop_frame_5 set rank=rackid_,sign=3,rankposition=Beginaddress_ where stop_rank=Endaddress_ limit 1;
					elseif Tasktype_='中空' then
						update gmms_drop_frame_5 set rank=rackid_,sign=1,rankposition=Beginaddress_ where stop_rank=Endaddress_ and (sign=7 or sign=0)  limit 1;
					else
						update gmms_drop_frame_5 set rank=rackid_,sign=1,rankposition=Beginaddress_ where stop_rank=Endaddress_  limit 1;
					end if;
				elseif Taskphase_='移库' then
					update gmms_drop_frame_5 set rank=rackid_,sign=0,rankposition=Beginaddress_ where stop_rank=Endaddress_ limit 1;
				end if;
				update gmms_shelf_tasks2 set taskstatus='已完成',endtime=NOW(),finshtype2=flag where id=Taskid_;			
			end if;
			#call message(returnvalue,'0014',messagedate);
		elseif flag=2 then
			if Taskstatus_='正在上车' then
				update gmms_shelf_tasks2 set taskstatus='等待上车',cardlocation=null where id=Taskid_;
			elseif Taskstatus_='正在下车' then
				update gmms_shelf_tasks2 set taskstatus='已上车' where id=Taskid_;
			end if;
			#call message(returnvalue,'0014',messagedate);
		end if;
	end if;
	call message(returnvalue,'0014',messagedate);
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for y_ShelfHollow
-- ----------------------------
DROP PROCEDURE IF EXISTS `y_ShelfHollow`;
delimiter ;;
CREATE PROCEDURE `y_ShelfHollow`(in groupss_ BIGINT)
BEGIN
	#中空任务   是否能生成仓储任务
	DECLARE isEnd DOUBLE DEFAULT 0;    #是否还有数据
	DECLARE CountA_S int DEFAULT 0;  	#启用/禁用
	DECLARE CountB_S int DEFAULT 0;  	#启用/禁用
	DECLARE CountA_D int DEFAULT 0;  	#启用/禁用
	DECLARE CountB_D int DEFAULT 0;  	#启用/禁用
	
	DECLARE parentliuchengka_ varchar(255) DEFAULT null;  #面朝向
	
	DECLARE id_ int DEFAULT 0;  	#启用/禁用
	DECLARE drop_type_ varchar(255) DEFAULT null;  #类型线路
	DECLARE stop_rank_ varchar(255) DEFAULT null;  #目的地
	DECLARE beginaddress_ varchar(255) DEFAULT null;  #起始地
	DECLARE endgroups_ BIGINT DEFAULT 0;     #目的线路
	DECLARE rank_ BIGINT DEFAULT 0;          #料架id
	DECLARE orientation_ varchar(255) DEFAULT null;  #面朝向
	DECLARE endstop_rank_ varchar(255) DEFAULT null;  #面朝向
	

	DECLARE childcount_ BIGINT DEFAULT 0;          #料架id
	#得到合片流程卡
	select ypl.zkliuchengka,lk.child_count into parentliuchengka_,childcount_ from y_parent_liuchengka as ypl 
	left join gmms_liuchengka as lk on ypl.zkliuchengka=lk.liuchengka
	where lk.liuchengka is not null and lk.current_work_position='中空' and lk.current_status='等待中' and 
	lk.getgroups=groupss_ order by lk.getdatatime asc limit 0,1;
	#得到合片流程卡   A/B面   架子数量
	select count(*) into CountA_S from gmms_drop_frame_5 as dr left join gmms_shelf_rack_2 as sr on dr.rank=sr.id
	left join gmms_liuchengka as lk on sr.processcard_id=lk.liuchengka
	where sr.id is not null and lk.liuchengka is not null and lk.parent_liuchengka=parentliuchengka_ and sr.orientation='A' and dr.sign=0;
	
	select count(*) into CountB_S from gmms_drop_frame_5 as dr left join gmms_shelf_rack_2 as sr on dr.rank=sr.id
	left join gmms_liuchengka as lk on sr.processcard_id=lk.liuchengka
	where sr.id is not null and lk.liuchengka is not null and lk.parent_liuchengka=parentliuchengka_ and sr.orientation='B' and dr.sign=0;
	#得到工位    A/B面   架子数量
	select count(stop_rank) into CountA_D from gmms_drop_frame_5 
	where groups_=4 and drop_groups=groupss_ and rank is null and sign=0 and flag=1 and orientation='A';
	select count(stop_rank) into CountB_D from gmms_drop_frame_5 
	where groups_=4 and drop_groups=groupss_ and rank is null and sign=0 and flag=1 and orientation='B';
	select CountA_D,CountB_D,CountA_S,CountB_S;
	#找到此合片流程卡 是否属于同一面
	if childcount_=2 and (CountA_S+CountB_S)=childcount_ then
		#两架合片
		if CountA_S=2 and CountA_D=2 then
			#两架都是A  且工位都是A
			call y_ShelfAddHollow(parentliuchengka_,groupss_);
		elseif CountB_S=2 and CountB_D=2 then
			#两架都是B  且工位都是B
			call y_ShelfAddHollow(parentliuchengka_,groupss_);
		elseif ( (CountB_D=2 and CountA_D<2 and CountA_S=2) or (CountA_D=2 and CountB_D<2 and CountB_S=2) ) and
			(select count(*) from gmms_drop_frame_5 where groups_=2 and sign=0)=2 and
			(select mode_type from y_mode_type where groups=groupss_ and number='ZKXZ0001')='自动' then
			#中空 1. B工位空闲2   A工位空闲<2  任务A面数2   2. A工位空闲2   B工位空闲<2  任务B面数2
			#找到此父级流程卡 中的一个架子去旋转
			select sr.id,dr.stop_rank into rank_,beginaddress_ from gmms_drop_frame_5 as dr 
			left join gmms_shelf_rack_2 as sr on dr.rank=sr.id
			left join gmms_liuchengka as lk on sr.processcard_id=lk.liuchengka
			where sr.id is not null and lk.liuchengka is not null 
			and lk.parent_liuchengka=parentliuchengka_ and dr.groups_=100 and dr.sign=0 limit 0,1;
		
			#空闲的旋转位
			select id,drop_type,stop_rank,all_groups 
			into id_,drop_type_,stop_rank_,EndGroups_ FROM gmms_drop_frame_5 
			where groups_=2 and rank is null and sign=0 and flag=1 limit 0,1;
			insert into gmms_shelf_tasks2(groups,tasktype,taskphase,taskstatus,starttime,beginaddress,endaddress,rackid,endgroups)
			select 3,drop_type_,'出库','等待上车',NOW(),beginaddress_,stop_rank_,rank_,300;
			
			update gmms_drop_frame_5 set sign=7 where id=id_ or rank=rank_;
		end if;
	elseif childcount_=3 and (CountA_S+CountB_S)=childcount_ then
		if ((CountA_S=2 and CountB_S=1) or (CountA_S=1 and CountB_S=2)) and CountA_D=2 and CountB_D=2 then 
			call y_ShelfAddHollow(parentliuchengka_,groupss_);
		end if;		
	elseif childcount_=4 and CountA_D=2 and CountB_D=2 then 
		#四架合片
		call y_ShelfAddHollow(parentliuchengka_,groupss_);
	end if;
	

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for y_Shelfoptimize
-- ----------------------------
DROP PROCEDURE IF EXISTS `y_Shelfoptimize`;
delimiter ;;
CREATE PROCEDURE `y_Shelfoptimize`()
BEGIN
	#仓储自动优化
	DECLARE isEnd DOUBLE DEFAULT 0;    #是否还有数据
	DECLARE Count_DZ DOUBLE DEFAULT 0;    #查找此父级流程卡在库内的数量
	DECLARE Count_S DOUBLE DEFAULT 0;    #查找此父级流程卡在库内的数量 
	DECLARE parentliuchengkaCount int DEFAULT 0;  	#此父级流程卡下 的子流程卡数量
	DECLARE zkliuchengka_ varchar(255) DEFAULT null;  #父级流程卡
	DECLARE isMatchCount_ varchar(255) DEFAULT null;  #是否有未优化的
	DECLARE isWorkingCount_ varchar(255) DEFAULT 0;  #是否有正在工作的
	#定义游标
	DECLARE emp_cursor CURSOR FOR SELECT zkliuchengka FROM y_parent_liuchengka;
	DECLARE CONTINUE HANDLER FOR NOT FOUND SET isEnd = 1;
	#打开游标
	OPEN emp_cursor;
	#使用游标（从游标中获取数据）
	FETCH emp_cursor INTO zkliuchengka_;
		while isEnd!=1 do
			set isMatchCount_=0;
			set parentliuchengkaCount=0;
			set Count_S=0;
			set Count_DZ=0;
			#查找此父级流程卡在库内的数量    
-- 			select count(*) into Count_S from gmms_drop_frame_5 as dr left join gmms_shelf_rack_2 as sr on dr.rank=sr.id
-- 			left join gmms_liuchengka as lk on sr.processcard_id=lk.liuchengka
-- 			where sr.id is not null and lk.liuchengka is not null and lk.parent_liuchengka=zkliuchengka_ and dr.groups_=100 and 
-- 			lk.current_status='已完成' and lk.current_work_position='创建';
			
			select count(*) into Count_S from gmms_drop_frame_5 as dr left join gmms_shelf_rack_2 as sr on dr.rank=sr.id
			left join gmms_liuchengka as lk on sr.processcard_id=lk.liuchengka
			where sr.id is not null and lk.liuchengka is not null and lk.parent_liuchengka=zkliuchengka_ and dr.groups_=100 and dr.sign=0;
			
			select count(*) into Count_DZ from gmms_drop_frame_5 as dr left join gmms_shelf_rack_2 as sr on dr.rank=sr.id
			left join gmms_liuchengka as lk on sr.processcard_id=lk.liuchengka
			where sr.id is not null and lk.liuchengka is not null and lk.parent_liuchengka=zkliuchengka_ and dr.groups_=5;
			#此父级流程卡下 的子流程卡数量
			select child_count into parentliuchengkaCount from gmms_liuchengka where liuchengka=zkliuchengka_;
			
			select count(*) into isMatchCount_ from gmms_galss_task_shelf 
			where gc_number=zkliuchengka_ and ((work_state='已完成' and work_procedure='钢化后下片' and stop_position<1) or (stop_position>0 and work_state='破损'));
			
			select count(*) into isWorkingCount_ from gmms_galss_task_shelf 
			where gc_number=zkliuchengka_ and work_state='正在工作';
			
			if Count_S=parentliuchengkaCount and parentliuchengkaCount>0 and isMatchCount_>0 and Count_DZ=0 and isWorkingCount_=0 then
				insert into A_youhua(processcard_id,count,youhuashijian) values (zkliuchengka_,isMatchCount_,NOW());
				call y_optimize_GAI(zkliuchengka_,1,@h,@l);
			end if;		
			set zkliuchengka_=null;
			set isEnd=0;#防止into 影响		
			FETCH emp_cursor INTO zkliuchengka_;			
		end while;
	
	#关闭游标
	CLOSE emp_cursor;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for y_ShelfSend
-- ----------------------------
DROP PROCEDURE IF EXISTS `y_ShelfSend`;
delimiter ;;
CREATE PROCEDURE `y_ShelfSend`(in machineids BIGINT,in returnvalue varchar(255),out messagedate varchar(255))
BEGIN
	#仓储发送
	DECLARE isEnd DOUBLE DEFAULT 0;    #是否还有数据
	DECLARE Carid_ bigint DEFAULT 0;    #车上架子任务id
	DECLARE CarEndgroups_ bigint DEFAULT 0;    #车上架子目的地线路
	DECLARE CarCount bigint DEFAULT 0;    #车上有几个架子
	DECLARE PastCardlocation bigint DEFAULT 0;    #车上架子在什么位置
	DECLARE PastBeginaddress_ varchar(255) DEFAULT null;    #车上架子目的地
	DECLARE PastEndaddress_ varchar(255) DEFAULT null;    #车上架子目的地
	DECLARE Pastisout_ bigint DEFAULT 0;    #车上架子目的地
	DECLARE Taskid_ bigint DEFAULT 0;  #任务id
	DECLARE Taskphases_ varchar(255) DEFAULT null;  #任务阶段
	DECLARE Taskstatus_ varchar(255) DEFAULT null;  #任务类型
	DECLARE Rackid_ int DEFAULT 0;  #料架id
	DECLARE Beginaddress_ varchar(255) DEFAULT null;  #起始地
	DECLARE Endaddress_ varchar(255) DEFAULT null;    #目的地
	DECLARE Endgroups_ varchar(255) DEFAULT null; #目的线路
	DECLARE Cardlocation_ bigint DEFAULT 0;     #小车位置
	DECLARE groups_ BIGINT DEFAULT  
	(select work_id from gmms_machine where id=machineids);  #线路
	DECLARE isWorking bigint DEFAULT 
	(select count(id) from gmms_shelf_tasks2 where groups=groups_ and (taskstatus='正在上车' or taskstatus='正在下车') and isrepeal='正常');   #是否正在工作
	DECLARE ShelfSend_cursor CURSOR FOR
	select id,taskphase,taskstatus,rackid,beginaddress,endgroups,endaddress FROM gmms_shelf_tasks2 
	where groups=groups_ and taskstatus='等待上车' and isrepeal='正常' order by endgroups desc, id asc;
	DECLARE CONTINUE HANDLER FOR NOT FOUND SET isEnd = 1;
	#打开游标
	OPEN ShelfSend_cursor;
	set CarCount=(select count(id) from gmms_shelf_tasks2 where groups=groups_ and taskstatus='已上车' and isrepeal='正常');
	select id,endgroups,cardlocation,beginaddress,endaddress,isout 
	into Carid_,CarEndgroups_,PastCardlocation,PastBeginaddress_,PastEndaddress_,Pastisout_ from gmms_shelf_tasks2 
	where groups=groups_ and taskstatus='已上车' and isrepeal='正常' limit 0,1;
	#使用游标（从游标中获取数据）
	set isEnd=0;
	FETCH ShelfSend_cursor INTO Taskid_,Taskphases_,Taskstatus_,Rackid_,Beginaddress_,Endgroups_,Endaddress_;					
	  if (CarCount=0 or CarCount=1) and Pastisout_=0 then
			#车上没有任务	
			while isEnd!=1 and isWorking=0 do
				if (Taskid_=(select id from gmms_shelf_tasks2 
										where groups=groups_ and endgroups=Endgroups_ and taskstatus='等待上车' and isrepeal='正常' limit 0,1) and CarEndgroups_=0) or
					 Taskid_=(select id from gmms_shelf_tasks2 
										where groups=groups_ and endgroups=CarEndgroups_ and taskstatus='等待上车' and isrepeal='正常' limit 0,1)
				then
					
					call intTo2ByteHex10_16_4(Beginaddress_,@target);
					#返回值+00000000+目标地+AB车+上下
					if groups_=1 then
						#钢化车 17/18/19/20/122工位 只能使用1拉
						if PastCardlocation=1 and 
							Beginaddress_!='17' and Beginaddress_!='18' and Beginaddress_!='19' and Beginaddress_!='20' and Beginaddress_!='122' and 
							Endaddress_!='17' and Endaddress_!='18' and Endaddress_!='19' and Endaddress_!='20' and Endaddress_!='122'	then
							set Cardlocation_=2;
							set messagedate=CONCAT(returnvalue,"00000000",@target,'0002','0001');
						elseif PastCardlocation!=1 then 
							set Cardlocation_=1;
							set messagedate=CONCAT(returnvalue,"00000000",@target,'0001','0001');
						end if;
					elseif groups_=2 then
						#中空车 21/22/23/131工位 只能使用2拉
	
						if PastCardlocation!=2 then
							set Cardlocation_=2;
							set messagedate=CONCAT(returnvalue,"00000000",@target,'0002','0001');
						elseif Beginaddress_!='21' and Beginaddress_!='22' and Beginaddress_!='23' and Beginaddress_!='131' and 
									 Endaddress_!='21' and Endaddress_!='22' and Endaddress_!='23' and Endaddress_!='131' then
							set Cardlocation_=1;
							set messagedate=CONCAT(returnvalue,"00000000",@target,'0001','0001');
						end if;
					elseif ((select state from gmms_machine where id=55)='允许' or (Endaddress_!=99 and Endaddress_!=98)) and groups_=3 then
						if PastCardlocation=1 then
							set Cardlocation_=2;
							set messagedate=CONCAT(returnvalue,"00000000",@target,'0002','0001');
						else
							set Cardlocation_=1;
							set messagedate=CONCAT(returnvalue,"00000000",@target,'0001','0001');
						end if;
					end if;	
					#select * from gmms_drop_frame_5 where stop_rank=Endaddress_ and rank is null 0,1;
					if messagedate is not null then
						if PastCardlocation>0 then
							update gmms_shelf_tasks2 set taskstatus='正在上车',cardlocation=Cardlocation_,isout=1 where id=Taskid_;
							insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,groups_,NOW(),'发送正在上车',Taskid_,messagedate);
					  else
							update gmms_shelf_tasks2 set taskstatus='正在上车',cardlocation=Cardlocation_ where id=Taskid_;		
							insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,groups_,NOW(),'发送正在上车',Taskid_,messagedate);
						end if;										
					end if;
				end if;			
				set isWorking=(select count(id) from gmms_shelf_tasks2 where groups=groups_ and (taskstatus='正在上车' or taskstatus='正在下车') and isrepeal='正常');	
				set isEnd=0;#防止into 影响		
				FETCH ShelfSend_cursor INTO Taskid_,Taskphases_,Taskstatus_,Rackid_,Beginaddress_,Endgroups_,Endaddress_;			
			end while;
		end if;
	#关闭游标
	CLOSE ShelfSend_cursor;
	if isWorking=0 and CarCount>0 then
		#下车
		call intTo2ByteHex10_16_4(PastEndaddress_,@target);
		call intTo2ByteHex10_16_4(PastCardlocation,@PastCardlocation);
		set messagedate=CONCAT(returnvalue,"00000000",@target,@PastCardlocation,'0002');
		if messagedate is not null then
			update gmms_shelf_tasks2 set taskstatus='正在下车' where id=Carid_;
			insert into aaaa(MachineName,groups,data1,Content,id,Sendtext) values (machineids,groups_,NOW(),'发送正在下车',Carid_,messagedate);
		end if;
	end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for y_ShelfTempering
-- ----------------------------
DROP PROCEDURE IF EXISTS `y_ShelfTempering`;
delimiter ;;
CREATE PROCEDURE `y_ShelfTempering`()
BEGIN
	#钢化任务   +  中空+理片+旋转退库任务
	DECLARE isEnd DOUBLE DEFAULT 0;    #是否还有数据
	DECLARE id_ int DEFAULT 0;  	#启用/禁用
	DECLARE drop_type_ varchar(255) DEFAULT null;  #类型线路
	DECLARE stops_ varchar(255) DEFAULT null;  #类型线路
	DECLARE stops2_ varchar(255) DEFAULT null;  #类型线路
	DECLARE flag_ int DEFAULT 0;  	#启用/禁用
	DECLARE sign_ int DEFAULT 0;    #标志
	DECLARE signout_ int DEFAULT 0;    #退回标志（出库前的库位）
	DECLARE stop_rank_ varchar(255) DEFAULT null;  #起始地
	DECLARE endstop_rank_ varchar(255) DEFAULT null;  #起始地
	DECLARE endgroups_ BIGINT DEFAULT 0;     #目的线路
	DECLARE rank_ BIGINT DEFAULT 0;          #料架id
	DECLARE rank2_ BIGINT DEFAULT 0;          #料架id
	DECLARE rankposition_ varchar(255) DEFAULT null;    #车上架子任务 出库前库位
	DECLARE orientation_ varchar(255) DEFAULT null;  #面朝向
	DECLARE orientation2_ varchar(255) DEFAULT null;  #面朝向
	#定义游标 
	DECLARE ShelfTempering_cursor CURSOR FOR SELECT id,drop_type,flag,sign,stop_rank,rank,all_groups,orientation,rankposition FROM gmms_drop_frame_5 where groups_<100;
	DECLARE CONTINUE HANDLER FOR NOT FOUND SET isEnd = 1;
	OPEN ShelfTempering_cursor;

	FETCH ShelfTempering_cursor INTO id_,drop_type_,flag_,sign_,stop_rank_,rank_,EndGroups_,orientation_,rankposition_;
		while isEnd!=1 do		
			if sign_=6 and rank_ is not null then
				#退库	 钢化+中空一线退库  1车
				if (drop_type_='钢化' or EndGroups_=5) and (select count(*) from gmms_shelf_tasks2 where rackid=rank_ and isrepeal='正常' and taskstatus!='已完成')=0 then
					insert into gmms_shelf_tasks2(groups,tasktype,taskphase,taskstatus,starttime,beginaddress,endaddress,rackid,endgroups)
					select 1,drop_type_,'入库','等待上车',NOW(),stop_rank_,rankposition_,rank_,EndGroups_;
					update gmms_drop_frame_5 set sign=7 where id=id_;
				elseif drop_type_='旋转' then
					if stop_rank_=99 then
						set stop_rank_=98;
					elseif stop_rank_=98 then
						set stop_rank_=99;
					end if;
					insert into gmms_shelf_tasks2(groups,tasktype,taskphase,taskstatus,starttime,beginaddress,endaddress,rackid,endgroups)
					select 3,drop_type_,'入库','等待上车',NOW(),stop_rank_,rankposition_,rank_,300;
					update gmms_drop_frame_5 set sign=7 where id=id_;
				elseif flag_=1 and (select count(*) from gmms_shelf_tasks2 where rackid=rank_ and isrepeal='正常' and taskstatus!='已完成')=0 then   #中空二，三，四线退库  理片车
					insert into gmms_shelf_tasks2(groups,tasktype,taskphase,taskstatus,starttime,beginaddress,endaddress,rackid,endgroups)
					select 2,drop_type_,'入库','等待上车',NOW(),stop_rank_,rankposition_,rank_,EndGroups_;
					update gmms_drop_frame_5 set sign=7 where id=id_;
				end if;				
			elseif sign_=0 and flag_=1 and rank_ is null  then
				if drop_type_='钢化' then
					#补架子   (只补1-20号架子)
					select dr.rank,dr.stops into rank_,stops_ from gmms_drop_frame_5 as dr 
					left join gmms_shelf_rack_2 as sr on dr.rank=sr.id 
					left join gmms_liuchengka as lk on sr.processcard_id=lk.liuchengka
					where groups_=100 and dr.stops<21 and dr.flag=1 and dr.sign=0 and dr.rank is not null
					and sr.processcard_id is null and sr.orientation=orientation_ limit 0,1;
					if rank_ is null and (select count(*) from gmms_drop_frame_5 where groups_=2 and sign=0)=2 then
						#旋转   无需要面的 空架    查找任意一空架进行旋转
						select dr.rank,dr.stops into rank_,stops_ from gmms_drop_frame_5 as dr 
						left join gmms_shelf_rack_2 as sr on dr.rank=sr.id
						where groups_=100 and dr.stops<21 and dr.flag=1 and dr.sign=0 and dr.rank is not null
						and sr.processcard_id is null limit 0,1;
						select stop_rank into endstop_rank_ from gmms_drop_frame_5 where groups_=2 and sign=0 limit 1;
						if rank_ is not null and (select count(*) from gmms_shelf_tasks2 where endaddress=endstop_rank_ and isrepeal='正常' and taskstatus!='已完成')=0 then
							insert into gmms_shelf_tasks2(groups,tasktype,taskphase,taskstatus,starttime,beginaddress,endaddress,rackid,endgroups)
							select 3,'旋转','出库','等待上车',NOW(),stops_,endstop_rank_,rank_,250;						
							update gmms_drop_frame_5 as dr INNER join gmms_drop_frame_5 as dr1 
							set dr.sign=7,dr1.sign=7 where dr.stop_rank=endstop_rank_ and dr1.stops=stops_ and dr1.groups_=100;
						end if;
					elseif rank_ is not null and (select count(*) from gmms_shelf_tasks2 where endaddress=stop_rank_ and isrepeal='正常' and taskstatus!='已完成')=0 then
						insert into gmms_shelf_tasks2(groups,tasktype,taskphase,taskstatus,starttime,beginaddress,endaddress,rackid,endgroups)
						select 1,drop_type_,'出库','等待上车',NOW(),stops_,stop_rank_,rank_,EndGroups_;						
						update gmms_drop_frame_5 as dr INNER join gmms_drop_frame_5 as dr1 
						set dr.sign=7,dr1.sign=7 where dr.id=id_ and dr1.rank=rank_;
					end if;	
				elseif drop_type_='理片' then	
					select dr.rank,dr.stops into rank_,stops_ from gmms_drop_frame_5 as dr 
					left join gmms_shelf_rack_2 as sr on dr.rank=sr.id 
					left join gmms_liuchengka as lk on sr.processcard_id=lk.liuchengka
					where groups_=100 and dr.stops>20 and dr.flag=1 and dr.sign=0 
					and lk.current_work_position='理片' and lk.current_status='等待中' and sr.orientation=orientation_ 
					order by lk.getdatatime asc limit 0,1;	
					if rank_ is not null and (select count(*) from gmms_shelf_tasks2 where endaddress=stop_rank_ and isrepeal='正常' and taskstatus!='已完成')=0 then
						insert into gmms_shelf_tasks2(groups,tasktype,taskphase,taskstatus,starttime,beginaddress,endaddress,rackid,endgroups)
						select 2,drop_type_,'出库','等待上车',NOW(),stops_,stop_rank_,rank_,EndGroups_;
						update gmms_drop_frame_5 as dr INNER join gmms_drop_frame_5 as dr1 
						set dr.sign=7,dr1.sign=7 where dr.id=id_ and dr1.rank=rank_;
					end if;
				end if;			 
			elseif sign_=0 and flag_=1 and rank_ is not null and drop_type_='吊装' then
					#起始库位
					select stops into stops_ from gmms_drop_frame_5 where groups_=100 and rank=rank_ and (sign=10 or sign=0) and flag=1 limit 0,1;
					select stops_;
					select stops_>20;
					if stops_>20 then
						insert into gmms_shelf_tasks2(groups,tasktype,taskphase,taskstatus,starttime,beginaddress,endaddress,rackid,endgroups)
						select 2,drop_type_,'出库','等待上车',NOW(),stops_,stop_rank_,rank_,EndGroups_;
						update gmms_drop_frame_5 as dr INNER join gmms_drop_frame_5 as dr1 
						set dr.sign=7,dr1.sign=7 where dr.id=id_ and dr1.rank=rank_;
					elseif stops_<21 and stops_>0 then 
						#查找 中空方向的 已领取的1号线任务  
						select dr.stops,dr.rank into stops2_,rank2_ from gmms_drop_frame_5 as dr LEFT JOIN gmms_shelf_rack_2 as sr on dr.rank=sr.id
						left join gmms_liuchengka as lk on sr.processcard_id=lk.liuchengka
						left join gmms_liuchengka as lk2 on lk.parent_liuchengka=lk2.liuchengka
						where dr.groups_=100 and dr.sign=0 and dr.flag=1 and dr.stops>20 and dr.rank is not null and sr.processcard_id is not null
						and lk2.getgroups=1	limit 0,1;
						#查找 中空方向的 空架子 替换
						if stops2_=0 or stops2_ is null then
							select dr.stops,dr.rank into stops2_,rank2_ from gmms_drop_frame_5 as dr LEFT JOIN gmms_shelf_rack_2 as sr on dr.rank=sr.id
							left join gmms_liuchengka as lk on sr.processcard_id=lk.liuchengka
							left join gmms_liuchengka as lk2 on lk.parent_liuchengka=lk2.liuchengka
							where dr.groups_=100 and dr.sign=0 and dr.flag=1 and dr.stops>20 and dr.rank is not null and sr.processcard_id is null limit 0,1;
						end if;
						#查找 中空方向的 无领取+不需要理片的 替换
						if stops2_=0 or stops2_ is null then
							select dr.stops,dr.rank into stops2_,rank2_ from gmms_drop_frame_5 as dr LEFT JOIN gmms_shelf_rack_2 as sr on dr.rank=sr.id
							left join gmms_liuchengka as lk on sr.processcard_id=lk.liuchengka
							left join gmms_liuchengka as lk2 on lk.parent_liuchengka=lk2.liuchengka
							where dr.groups_=100 and dr.sign=0 and dr.flag=1 and dr.stops>20 and dr.rank is not null and sr.processcard_id is not null
							and lk2.getgroups is null and !(lk.current_work_position='理片' and lk.current_status='等待中') order by dr.id asc limit 0,1;
						end if;
						if stops2_>0 and (select count(*) from gmms_shelf_tasks2 where rackid=rank_ and isrepeal='正常' and taskstatus!='已完成')=0 then
							insert into gmms_shelf_tasks2(groups,tasktype,taskphase,taskstatus,starttime,beginaddress,endaddress,rackid,endgroups)
							select 3,drop_type_,'移库','等待上车',NOW(),stops_,stops2_,rank_,400;
							insert into gmms_shelf_tasks2(groups,tasktype,taskphase,taskstatus,starttime,beginaddress,endaddress,rackid,endgroups)
							select 3,drop_type_,'移库','等待上车',NOW(),stops2_,stops_,rank2_,400;
							update gmms_drop_frame_5 as dr INNER join gmms_drop_frame_5 as dr1 
							set dr.sign=7,dr1.sign=7 where dr.stops=stops2_ and dr.groups_=100 and dr1.groups_=100 and dr1.stops=stops_;
						end if;
					end if;	
			end if;
			set isEnd=0;#防止into 影响
			set flag_=0;
			set sign_=0;
			set stops_=0;
			set stops2_=0;
			set stop_rank_=null;
			set endgroups_=0;
			set rank_=0;
			set rank2_=0;
			set orientation_=null;
			set orientation2_=null;
			set drop_type_=null;
			FETCH ShelfTempering_cursor INTO id_,drop_type_,flag_,sign_,stop_rank_,rank_,EndGroups_,orientation_,rankposition_;
		end while;
	
	#关闭游标
	CLOSE ShelfTempering_cursor;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for y_ShelfXz
-- ----------------------------
DROP PROCEDURE IF EXISTS `y_ShelfXz`;
delimiter ;;
CREATE PROCEDURE `y_ShelfXz`()
BEGIN
	#旋转任务 
	#1.查看是否两个旋转位都空  sign=0
	#2.查看3A 3B 的流程卡 创建旋转任务
	#3.查看1A 1B 的流程卡 创建旋转任务
	
	DECLARE parentliuchengka_ varchar(255) DEFAULT null;  #父级流程卡
	DECLARE orientation_ varchar(255) DEFAULT null;       #待旋转面朝向
	DECLARE rank_ varchar(255) DEFAULT null;       				#待旋转料架id
	DECLARE beginaddress_ varchar(255) DEFAULT null;      #待旋转料架所在库位编号
	
	DECLARE id_ BIGINT DEFAULT 0;                					#库位id
	DECLARE drop_type_ varchar(255) DEFAULT null;         #库位类型
	DECLARE stop_rank_ varchar(255) DEFAULT null;         #库位编号
	DECLARE EndGroups_ varchar(255) DEFAULT null;         #库位线路
	
	#两个旋转位都为空
	if (select count(*) from gmms_drop_frame_5 where groups_=2 and sign=0)=2 then
		#得到同一父级流程卡下 库内 相同面>2的 父级流程卡
		select lk.parent_liuchengka,sr.orientation into parentliuchengka_,orientation_ from gmms_drop_frame_5 as dr 
		left join gmms_shelf_rack_2 as sr on dr.rank=sr.id
		left join gmms_liuchengka as lk on sr.processcard_id=lk.liuchengka
		where sr.id is not null and lk.liuchengka is not null and groups_=100 and dr.sign=0
		and lk.liuchengka!=(select IFNULL(srd.processcard_id,"1") from gmms_drop_frame_5 as drd left join gmms_shelf_rack_2 as srd on drd.rank=srd.id where drd.id=501)
		group by lk.parent_liuchengka,sr.orientation having count(*)>2 limit 0,1;
		
		if parentliuchengka_ is null then
			#查看1A 1B 的流程卡    1.拿到需要旋转的父级流程卡  2.拿到那一架去旋转（需要根据是否领取）
			select parent_liuchengka into parentliuchengka_ from (
			select lk.parent_liuchengka,sr.orientation  from gmms_drop_frame_5 as dr 
			left join gmms_shelf_rack_2 as sr on dr.rank=sr.id
			left join gmms_liuchengka as lk on sr.processcard_id=lk.liuchengka
			left join gmms_liuchengka as lk2 on lk.parent_liuchengka=lk2.liuchengka
			where sr.id is not null and lk.liuchengka is not null and lk2.liuchengka is not null and dr.groups_=100 and dr.sign=0 and lk2.child_count=2
			and lk.liuchengka!=(select IFNULL(srd.processcard_id,"1") from gmms_drop_frame_5 as drd left join gmms_shelf_rack_2 as srd on drd.rank=srd.id where drd.id=501)	
			group by lk.parent_liuchengka,sr.orientation having count(*)=1) AS R group by parent_liuchengka having count(*)=2 LIMIT 0,1;
			
			select orientation into orientation_ from gmms_drop_frame_5 as dr 
			left join gmms_liuchengka as lk on dr.drop_groups=lk.getgroups
			where dr.groups_=4 and rank is null and flag=1 and sign=0 and liuchengka=parentliuchengka_ 
			group by orientation  having count(*)=2 limit 0,1;
			#中空  A工位空闲把B转成A    B工位空闲把A转成B
			if orientation_='A' then
				set orientation_='B';
			elseif orientation_='B' then
				set orientation_='A';
			end if;
			if orientation_ is null then
				#表示 1.此任务未领取 / 2.领取线路无空工位    （规则：按不缺片的方向为准，即缺片的去旋转）
				select sr.orientation into orientation_ from gmms_drop_frame_5 as dr 
				left join gmms_shelf_rack_2 as sr on dr.rank=sr.id
				left join gmms_liuchengka as lk on sr.processcard_id=lk.liuchengka
				where sr.id is not null and lk.liuchengka is not null and dr.groups_=100 and dr.sign=0
				and lk.parent_liuchengka=parentliuchengka_ 
				ORDER BY (select count(*) from gmms_galss_task_shelf where processcard_id=sr.processcard_id and (work_state='已完成' and work_procedure='钢化后下片')) asc limit 0,1;
			end if;
		end if;
		
		if parentliuchengka_ is not null and orientation_ is not null then
			#待旋转料架信息
			select sr.id,dr.stop_rank into rank_,beginaddress_ from gmms_drop_frame_5 as dr 
			left join gmms_shelf_rack_2 as sr on dr.rank=sr.id
			left join gmms_liuchengka as lk on sr.processcard_id=lk.liuchengka
			where sr.id is not null and lk.liuchengka is not null 
			and lk.parent_liuchengka=parentliuchengka_ and sr.orientation=orientation_ and groups_=100 and dr.sign=0 limit 0,1;
		
			#空闲的旋转位
			select id,drop_type,stop_rank,all_groups 
			into id_,drop_type_,stop_rank_,EndGroups_ FROM gmms_drop_frame_5 
			where groups_=2 and rank is null and sign=0 and flag=1 limit 0,1;
			#select CONCAT(rank_,',',beginaddress_,',',stop_rank_);
			insert into gmms_shelf_tasks2(groups,tasktype,taskphase,taskstatus,starttime,beginaddress,endaddress,rackid,endgroups)
			select 3,drop_type_,'出库','等待上车',NOW(),beginaddress_,stop_rank_,rank_,300;
			
			update gmms_drop_frame_5 set sign=7 where id=id_ or rank=rank_ and groups_=100;
		end if;
		
		
	end if;
	
	
	

END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for y_Shelf_copy1
-- ----------------------------
DROP PROCEDURE IF EXISTS `y_Shelf_copy1`;
delimiter ;;
CREATE PROCEDURE `y_Shelf_copy1`()
BEGIN
	DECLARE pro_id int DEFAULT 0;    #
	#半成品仓储   定时检索
	#1.检查 钢化后是否要补架子，
	#2.是否有需要理片的任务
	#3.是否有要去中空的任务
	#4.是否有要去旋转的任务
	select id into pro_id from y_pro_timer where pro_timer='Shelf' and is_enable='开启' and is_run='空闲' limit 0,1;
	select pro_id;
	if pro_id>0 then
		update y_pro_timer set is_run='运行中',run_begin_time=NOW() where id=pro_id;
		select '进';
		update y_pro_timer set is_run='空闲',run_end_time=NOW() where id=pro_id;
	end if;
END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for 测试入口
-- ----------------------------
DROP PROCEDURE IF EXISTS `测试入口`;
delimiter ;;
CREATE PROCEDURE `测试入口`()
BEGIN
	#Routine body goes here...
  call machine_state_begin_update();
	select @zzzz_update_state;
call machine_state_update_var(7,"工作状态","进库中");
call machine_state_update_var(7,"任务ID","1119");
select * from gmms_machine_state;
select @zzzz_update_state;
END
;;
delimiter ;

-- ----------------------------
-- Event structure for deleteshujujilu
-- ----------------------------
DROP EVENT IF EXISTS `deleteshujujilu`;
delimiter ;;
CREATE EVENT `deleteshujujilu`
ON SCHEDULE
EVERY '1' DAY STARTS '2023-02-28 08:39:00'
DO #call deleteshujujilu()
select 1
;;
delimiter ;

-- ----------------------------
-- Event structure for ShelfTasks
-- ----------------------------
DROP EVENT IF EXISTS `ShelfTasks`;
delimiter ;;
CREATE EVENT `ShelfTasks`
ON SCHEDULE
EVERY '5' SECOND STARTS '2023-03-09 21:43:00'
DO call y_Shelf()
;;
delimiter ;

-- ----------------------------
-- Triggers structure for table gmms_drop_frame
-- ----------------------------
DROP TRIGGER IF EXISTS `drop_change_trigger`;
delimiter ;;
CREATE TRIGGER `drop_change_trigger` AFTER UPDATE ON `gmms_drop_frame` FOR EACH ROW BEGIN
	set @oldk=OLD.processcard_id;
	set @olds=OLD.state;
	set @newk=NEW.processcard_id;
	set @news=NEW.state;
	if @oldk<>@newk or @olds<>@news  then 
		call liuchengka_change(@oldk,@olds,@newk,@news,NEW.stops,NEW.groups,1);
	end if;
END
;;
delimiter ;

-- ----------------------------
-- Triggers structure for table gmms_galss_task_interrupt
-- ----------------------------
DROP TRIGGER IF EXISTS `log_trigger_copy1`;
delimiter ;;
CREATE TRIGGER `log_trigger_copy1` BEFORE UPDATE ON `gmms_galss_task_interrupt` FOR EACH ROW if new.work_procedure<>old.work_procedure or old.work_state<>new.work_state  then 
	select operator into @z_line from gmms_raw_file where file_name=new.engineering_id;
	call log_glass(new.id,new.work_procedure,new.work_state,@z_line);
end if
;;
delimiter ;

SET FOREIGN_KEY_CHECKS = 1;
