#!/usr/bin/env bash
# scripts/seed-dev-data.sh — Populate dev/test DB with realistic-looking
# print-industry data covering everything wired in the frontend.
#
# Idempotent: every INSERT uses IGNORE on a unique key so repeated runs
# won't duplicate rows. Safe to re-run after any code change.
#
# Prerequisites:
#   1. .env.local with DB_HOST/DB_PORT/DB_SCHEMA/DB_USER/DB_PASSWORD
#   2. Schema must exist — i.e. Spring Boot has started at least once so
#      Flyway has applied V1 (creates tUser/tStaff/tPermissionCategory/
#      tUserPermission/tModule).
#
# What it seeds:
#   tStaff               — 20 employees with name, dept, staff number
#   tUser                — admin + 18 users, BCrypt(666666), linked to staff
#   tPermissionCategory  — 30 permission groups matching prototype's grid
#   tUserPermission      — common permissions assigned to non-admin users
#   tModule              — 6 root flows + 24 leaf modules (KPI tree + 系统设置)

set -euo pipefail

for p in /opt/homebrew/opt/mysql-client/bin /usr/local/opt/mysql-client/bin; do
  [ -d "$p" ] && case ":$PATH:" in *":$p:"*) ;; *) PATH="$p:$PATH" ;; esac
done

ENV_FILE="$(dirname "$0")/../.env.local"
[ -f "$ENV_FILE" ] || { echo "[seed-dev-data] ⚠️ .env.local 不存在（$ENV_FILE）" >&2; exit 1; }

set -a; . "$ENV_FILE"; set +a

# BCrypt(666666) cost 10. Spring's BCryptPasswordEncoder accepts $2a$/$2b$.
HASH='$2b$10$ZzbGP0yWo3QJkaiZqEgtAOQVrzrti911VqbY7FoethhGQFPV0/Oj6'

echo "[seed-dev-data] target: ${DB_USER}@${DB_HOST}:${DB_PORT}/${DB_SCHEMA}"

mysql -h"${DB_HOST}" -P"${DB_PORT}" -u"${DB_USER}" -p"${DB_PASSWORD}" "${DB_SCHEMA}" <<SQL
-- =====================================================================
-- tStaff — 20 印刷行业员工
-- =====================================================================
INSERT IGNORE INTO tStaff
  (sBrandsId, sSubsidiaryId, tCreateDate, sStaffNo, sStaffName, sDepartment, sCreatedBy, bDeleted) VALUES
('XLY','XLY','2024-01-15 09:00:00','S0001','管广飞','工艺技术','admin',0),
('XLY','XLY','2024-01-15 09:00:00','S0002','李斌','印刷车间','admin',0),
('XLY','XLY','2024-01-16 10:00:00','S0003','朱财喜','机修','admin',0),
('XLY','XLY','2024-01-16 10:00:00','S0004','林杰华','机务部','admin',0),
('XLY','XLY','2024-01-17 11:00:00','S0005','汪鑫','财务部','admin',0),
('XLY','XLY','2024-01-17 11:00:00','S0006','钱昉','装订车间','admin',0),
('XLY','XLY','2024-01-18 09:30:00','S0007','张冠飞','总经办公室','admin',0),
('XLY','XLY','2024-01-18 09:30:00','S0008','孟威','总务部','admin',0),
('XLY','XLY','2024-01-19 10:30:00','S0009','杭仁萍','供应链','admin',0),
('XLY','XLY','2024-01-19 10:30:00','S0010','王月','质量管理部','admin',0),
('XLY','XLY','2024-01-20 11:30:00','S0011','王宽明','模切车间','admin',0),
('XLY','XLY','2024-01-20 11:30:00','S0012','潘强','计划组','admin',0),
('XLY','XLY','2024-01-21 09:00:00','S0013','耿广东','样品开发','admin',0),
('XLY','XLY','2024-01-21 09:00:00','S0014','余涛','设计部','admin',0),
('XLY','XLY','2024-01-22 10:00:00','S0015','梁赵军','仓库','admin',0),
('XLY','XLY','2024-01-22 10:00:00','S0016','曹佳怡','工艺技术','admin',0),
('XLY','XLY','2024-01-23 11:00:00','S0017','陈思琪','印刷车间','admin',0),
('XLY','XLY','2024-01-23 11:00:00','S0018','张红英','机修','admin',0),
('XLY','XLY','2024-01-24 09:30:00','S0019','吕欣彦','财务部','admin',0),
('XLY','XLY','2024-01-24 09:30:00','S0020','陈雪婷','装订车间','admin',0);

-- =====================================================================
-- tUser — admin + 18 业务用户。所有密码默认 666666 (BCrypt).
-- bDeleted=1 on 梁赵军 to demonstrate the 作废 column.
-- =====================================================================
INSERT IGNORE INTO tUser
  (sBrandsId, sSubsidiaryId, tCreateDate, sUserNo, sUserName, iStaffId, sUserType, sLanguage,
   bCanModifyDocs, sPasswordHash, tLastLoginDate, sCreatedBy, bDeleted) VALUES
('XLY','XLY','2024-01-10 08:00:00','admin','admin', NULL,'超级管理员','zh',1,'$HASH','2026-04-30 09:55:00','system',0),
('XLY','XLY','2024-01-15 09:05:00','ggf','管广飞', (SELECT iIncrement FROM tStaff WHERE sStaffNo='S0001'),'超级管理员','zh',1,'$HASH','2026-04-29 16:32:11','admin',0),
('XLY','XLY','2024-01-15 09:06:00','lib','李斌', (SELECT iIncrement FROM tStaff WHERE sStaffNo='S0002'),'普通用户','zh',1,'$HASH','2026-04-29 14:20:08','admin',0),
('XLY','XLY','2024-01-16 10:05:00','zhucx','朱财喜',(SELECT iIncrement FROM tStaff WHERE sStaffNo='S0003'),'普通用户','zh',0,'$HASH','2026-04-28 09:11:42','admin',0),
('XLY','XLY','2024-01-16 10:06:00','ljh','林杰华',(SELECT iIncrement FROM tStaff WHERE sStaffNo='S0004'),'普通用户','zh',0,'$HASH','2026-04-29 11:55:30','admin',0),
('XLY','XLY','2024-01-17 11:05:00','wx','汪鑫',(SELECT iIncrement FROM tStaff WHERE sStaffNo='S0005'),'普通用户','zh',1,'$HASH','2026-04-30 08:42:15','admin',0),
('XLY','XLY','2024-01-17 11:06:00','qianb','钱昉',(SELECT iIncrement FROM tStaff WHERE sStaffNo='S0006'),'普通用户','zh',0,'$HASH','2026-04-25 13:22:00','admin',0),
('XLY','XLY','2024-01-18 09:35:00','zgf','张冠飞',(SELECT iIncrement FROM tStaff WHERE sStaffNo='S0007'),'超级管理员','zh',1,'$HASH','2026-04-30 09:18:55','admin',0),
('XLY','XLY','2024-01-18 09:36:00','mengw','孟威',(SELECT iIncrement FROM tStaff WHERE sStaffNo='S0008'),'普通用户','zh',0,'$HASH','2026-04-22 16:04:30','admin',0),
('XLY','XLY','2024-01-19 10:35:00','hangrp','杭仁萍',(SELECT iIncrement FROM tStaff WHERE sStaffNo='S0009'),'普通用户','zh',1,'$HASH','2026-04-29 17:33:12','admin',0),
('XLY','XLY','2024-01-19 10:36:00','wy','王月',(SELECT iIncrement FROM tStaff WHERE sStaffNo='S0010'),'普通用户','en',0,'$HASH','2026-04-28 10:15:00','admin',0),
('XLY','XLY','2024-01-20 11:35:00','wkm','王宽明',(SELECT iIncrement FROM tStaff WHERE sStaffNo='S0011'),'普通用户','zh',0,'$HASH','2026-04-30 07:55:40','admin',0),
('XLY','XLY','2024-01-20 11:36:00','pq','潘强',(SELECT iIncrement FROM tStaff WHERE sStaffNo='S0012'),'普通用户','zh',1,'$HASH','2026-04-29 15:48:20','admin',0),
('XLY','XLY','2024-01-21 09:05:00','ggd','耿广东',(SELECT iIncrement FROM tStaff WHERE sStaffNo='S0013'),'普通用户','zh',0,'$HASH','2026-04-26 11:11:11','admin',0),
('XLY','XLY','2024-01-21 09:06:00','yt','余涛',(SELECT iIncrement FROM tStaff WHERE sStaffNo='S0014'),'普通用户','zh',1,'$HASH','2026-04-30 08:30:25','admin',0),
('XLY','XLY','2024-01-22 10:05:00','lzj','梁赵军',(SELECT iIncrement FROM tStaff WHERE sStaffNo='S0015'),'普通用户','zh',0,'$HASH','2026-04-15 09:50:00','admin',1),
('XLY','XLY','2024-01-22 10:06:00','cjy','曹佳怡',(SELECT iIncrement FROM tStaff WHERE sStaffNo='S0016'),'普通用户','zh-TW',0,'$HASH','2026-04-29 13:45:00','admin',0),
('XLY','XLY','2024-01-23 11:05:00','csq','陈思琪',(SELECT iIncrement FROM tStaff WHERE sStaffNo='S0017'),'普通用户','zh',0,'$HASH','2026-04-28 14:12:00','admin',0),
('XLY','XLY','2024-01-23 11:06:00','zhy','张红英',(SELECT iIncrement FROM tStaff WHERE sStaffNo='S0018'),'普通用户','zh',0,'$HASH','2026-04-30 09:00:00','admin',0);

-- =====================================================================
-- tPermissionCategory — 30 permission groups (matches prototype's
-- PERMISSION_GROUPS list shown in UserDetail's permission grid).
-- All flat (iParentId=NULL) for simplicity.
-- =====================================================================
INSERT IGNORE INTO tPermissionCategory
  (sBrandsId, sSubsidiaryId, tCreateDate, sCategoryCode, sCategoryName, iParentId, iSortOrder, sCreatedBy, bDeleted) VALUES
('XLY','XLY','2024-01-10 08:00:00','PERM_DEFAULT_VISIBLE','默认显示(必选)',NULL,1,'system',0),
('XLY','XLY','2024-01-10 08:00:00','PERM_HIDE_PRICE','禁止查看价格',NULL,2,'system',0),
('XLY','XLY','2024-01-10 08:00:00','PERM_CS_FOLLOW','客服跟单',NULL,3,'system',0),
('XLY','XLY','2024-01-10 08:00:00','PERM_QUOTE_GROUP','报价组员工',NULL,4,'system',0),
('XLY','XLY','2024-01-10 08:00:00','PERM_MAT_CTRL','物控部员工',NULL,5,'system',0),
('XLY','XLY','2024-01-10 08:00:00','PERM_SUPPLY_PMC','供应链 PMC',NULL,6,'system',0),
('XLY','XLY','2024-01-10 08:00:00','PERM_VIEW_ORDER_PRICE','允许查看订单价格',NULL,7,'system',0),
('XLY','XLY','2024-01-10 08:00:00','PERM_STORAGE','储运部员工',NULL,8,'system',0),
('XLY','XLY','2024-01-10 08:00:00','PERM_EXT_SUPPLIER','外部供应商',NULL,9,'system',0),
('XLY','XLY','2024-01-10 08:00:00','PERM_QC_STAFF','品质部员工',NULL,10,'system',0),
('XLY','XLY','2024-01-10 08:00:00','PERM_TECH_CENTER','技术中心员工',NULL,11,'system',0),
('XLY','XLY','2024-01-10 08:00:00','PERM_MAINT_GROUP','机修组员工',NULL,12,'system',0),
('XLY','XLY','2024-01-10 08:00:00','PERM_PROD_PLANNING','生产部计划员工',NULL,13,'system',0),
('XLY','XLY','2024-01-10 08:00:00','PERM_OUTSOURCE','外发组员工',NULL,14,'system',0),
('XLY','XLY','2024-01-10 08:00:00','PERM_DIE_CUT','模烫车间',NULL,15,'system',0),
('XLY','XLY','2024-01-10 08:00:00','PERM_BINDING','装订车间',NULL,16,'system',0),
('XLY','XLY','2024-01-10 08:00:00','PERM_GLUE','粘接工车间',NULL,17,'system',0),
('XLY','XLY','2024-01-10 08:00:00','PERM_QC_MGMT','品质部管理',NULL,18,'system',0),
('XLY','XLY','2024-01-10 08:00:00','PERM_PREMIUM','精品车间',NULL,19,'system',0),
('XLY','XLY','2024-01-10 08:00:00','PERM_HR','人事组',NULL,20,'system',0),
('XLY','XLY','2024-01-10 08:00:00','PERM_STATS','统计组',NULL,21,'system',0),
('XLY','XLY','2024-01-10 08:00:00','PERM_MAINT_SUPER','机修主管',NULL,22,'system',0),
('XLY','XLY','2024-01-10 08:00:00','PERM_SAMPLE_DEV','样品开发部员工',NULL,23,'system',0),
('XLY','XLY','2024-01-10 08:00:00','PERM_DESIGN','设计开发',NULL,24,'system',0),
('XLY','XLY','2024-01-10 08:00:00','PERM_GM_OFFICE','总经办',NULL,25,'system',0),
('XLY','XLY','2024-01-10 08:00:00','PERM_FINANCE','财务部',NULL,26,'system',0),
('XLY','XLY','2024-01-10 08:00:00','PERM_SALES','销售员',NULL,27,'system',0),
('XLY','XLY','2024-01-10 08:00:00','PERM_PURCHASE','采购员',NULL,28,'system',0),
('XLY','XLY','2024-01-10 08:00:00','PERM_WAREHOUSE','仓库管理员',NULL,29,'system',0),
('XLY','XLY','2024-01-10 08:00:00','PERM_VOID','作废权限',NULL,30,'system',0);

-- =====================================================================
-- tUserPermission — 默认显示给所有非超管用户；按部门派几条相关权限
-- =====================================================================
-- 默认显示(必选) → 所有 active 非超管用户
INSERT IGNORE INTO tUserPermission
  (sBrandsId, sSubsidiaryId, tCreateDate, iUserId, iCategoryId, sCreatedBy)
SELECT 'XLY','XLY','2024-02-01 09:00:00', u.iIncrement, c.iIncrement, 'admin'
  FROM tUser u, tPermissionCategory c
 WHERE u.sUserType='普通用户' AND u.bDeleted=0
   AND c.sCategoryCode='PERM_DEFAULT_VISIBLE';

-- 财务部用户 → 财务 + 允许查看订单价格
INSERT IGNORE INTO tUserPermission
  (sBrandsId, sSubsidiaryId, tCreateDate, iUserId, iCategoryId, sCreatedBy)
SELECT 'XLY','XLY','2024-02-01 09:00:00', u.iIncrement, c.iIncrement, 'admin'
  FROM tUser u
  JOIN tStaff s ON s.iIncrement = u.iStaffId AND s.sDepartment='财务部'
  JOIN tPermissionCategory c ON c.sCategoryCode IN ('PERM_FINANCE','PERM_VIEW_ORDER_PRICE');

-- 印刷车间 / 机修 → 物控部员工
INSERT IGNORE INTO tUserPermission
  (sBrandsId, sSubsidiaryId, tCreateDate, iUserId, iCategoryId, sCreatedBy)
SELECT 'XLY','XLY','2024-02-01 09:00:00', u.iIncrement, c.iIncrement, 'admin'
  FROM tUser u
  JOIN tStaff s ON s.iIncrement = u.iStaffId AND s.sDepartment IN ('印刷车间','机修')
  JOIN tPermissionCategory c ON c.sCategoryCode='PERM_MAT_CTRL';

-- 装订车间 → 装订车间
INSERT IGNORE INTO tUserPermission
  (sBrandsId, sSubsidiaryId, tCreateDate, iUserId, iCategoryId, sCreatedBy)
SELECT 'XLY','XLY','2024-02-01 09:00:00', u.iIncrement, c.iIncrement, 'admin'
  FROM tUser u
  JOIN tStaff s ON s.iIncrement = u.iStaffId AND s.sDepartment='装订车间'
  JOIN tPermissionCategory c ON c.sCategoryCode='PERM_BINDING';

-- 模切车间 → 模烫车间
INSERT IGNORE INTO tUserPermission
  (sBrandsId, sSubsidiaryId, tCreateDate, iUserId, iCategoryId, sCreatedBy)
SELECT 'XLY','XLY','2024-02-01 09:00:00', u.iIncrement, c.iIncrement, 'admin'
  FROM tUser u
  JOIN tStaff s ON s.iIncrement = u.iStaffId AND s.sDepartment='模切车间'
  JOIN tPermissionCategory c ON c.sCategoryCode='PERM_DIE_CUT';

-- 质量管理部 → 品质部员工 + 品质部管理
INSERT IGNORE INTO tUserPermission
  (sBrandsId, sSubsidiaryId, tCreateDate, iUserId, iCategoryId, sCreatedBy)
SELECT 'XLY','XLY','2024-02-01 09:00:00', u.iIncrement, c.iIncrement, 'admin'
  FROM tUser u
  JOIN tStaff s ON s.iIncrement = u.iStaffId AND s.sDepartment='质量管理部'
  JOIN tPermissionCategory c ON c.sCategoryCode IN ('PERM_QC_STAFF','PERM_QC_MGMT');

-- 设计部 / 样品开发 → 设计开发 / 样品开发部员工
INSERT IGNORE INTO tUserPermission
  (sBrandsId, sSubsidiaryId, tCreateDate, iUserId, iCategoryId, sCreatedBy)
SELECT 'XLY','XLY','2024-02-01 09:00:00', u.iIncrement, c.iIncrement, 'admin'
  FROM tUser u
  JOIN tStaff s ON s.iIncrement = u.iStaffId
  JOIN tPermissionCategory c ON
       (s.sDepartment='设计部'   AND c.sCategoryCode='PERM_DESIGN')
    OR (s.sDepartment='样品开发' AND c.sCategoryCode='PERM_SAMPLE_DEV');

-- =====================================================================
-- tModule — KPI 流程作业单 (5 主流程) + 系统设置
-- Use sProcedureName as natural key (it has uk_procedure_name).
-- Insert roots first, then children referencing parents via subquery.
-- =====================================================================

-- 1. 估价管理流程
INSERT IGNORE INTO tModule
  (sBrandsId, sSubsidiaryId, tCreateDate, sDisplayType, sProcedureName, sModuleType,
   sManageDeptEn, bShowPermission, sModuleNameZh, iParentId, iSortOrder, sCreatedBy, bDeleted) VALUES
('XLY','XLY','2024-01-10 08:00:00','前端业务','Sp_QuoteFlow','KPI流程','Pricing',1,'估价管理流程',NULL,1,'admin',0);
INSERT IGNORE INTO tModule (sBrandsId,sSubsidiaryId,tCreateDate,sDisplayType,sProcedureName,sModuleType,sManageDeptEn,bShowPermission,sModuleNameZh,iParentId,iSortOrder,sCreatedBy,bDeleted) VALUES
('XLY','XLY','2024-01-10 08:00:00','前端业务','Sp_QuoteCreate','单据','Pricing',1,'01/04 【新增】新报价单',                (SELECT iIncrement FROM (SELECT iIncrement FROM tModule WHERE sProcedureName='Sp_QuoteFlow') AS x),1,'admin',0),
('XLY','XLY','2024-01-10 08:00:00','前端业务','Sp_QuoteAudit','单据','Pricing',1,'02/04 审核报价单->客户确认',           (SELECT iIncrement FROM (SELECT iIncrement FROM tModule WHERE sProcedureName='Sp_QuoteFlow') AS x),2,'admin',0),
('XLY','XLY','2024-01-10 08:00:00','前端业务','Sp_QuoteCustConfirm','单据','Pricing',1,'03/04 客户确认->二次确认',         (SELECT iIncrement FROM (SELECT iIncrement FROM tModule WHERE sProcedureName='Sp_QuoteFlow') AS x),3,'admin',0),
('XLY','XLY','2024-01-10 08:00:00','前端业务','Sp_QuoteToOrder','单据','Pricing',1,'04/04 报价单->销售订单',                (SELECT iIncrement FROM (SELECT iIncrement FROM tModule WHERE sProcedureName='Sp_QuoteFlow') AS x),4,'admin',0),
('XLY','XLY','2024-01-10 08:00:00','前端业务','Sp_QuoteToImpose','单据','Pricing',1,'04/04 报价单->转拼版单',                (SELECT iIncrement FROM (SELECT iIncrement FROM tModule WHERE sProcedureName='Sp_QuoteFlow') AS x),5,'admin',0),
('XLY','XLY','2024-01-10 08:00:00','前端业务','Sp_QuoteSupervisorAudit','单据','Pricing',1,'04/07 主管审核报价单',           (SELECT iIncrement FROM (SELECT iIncrement FROM tModule WHERE sProcedureName='Sp_QuoteFlow') AS x),6,'admin',0),
('XLY','XLY','2024-01-10 08:00:00','前端业务','Sp_QuoteSalesConfirm','单据','Pricing',1,'05/07 业务确认报价单',                (SELECT iIncrement FROM (SELECT iIncrement FROM tModule WHERE sProcedureName='Sp_QuoteFlow') AS x),7,'admin',0);

-- 2. 订单生产流程
INSERT IGNORE INTO tModule (sBrandsId,sSubsidiaryId,tCreateDate,sDisplayType,sProcedureName,sModuleType,sManageDeptEn,bShowPermission,sModuleNameZh,iParentId,iSortOrder,sCreatedBy,bDeleted) VALUES
('XLY','XLY','2024-01-10 08:00:00','前端业务','Sp_OrderFlow','KPI流程','Production',1,'订单生产流程',NULL,2,'admin',0);
INSERT IGNORE INTO tModule (sBrandsId,sSubsidiaryId,tCreateDate,sDisplayType,sProcedureName,sModuleType,sManageDeptEn,bShowPermission,sModuleNameZh,iParentId,iSortOrder,sCreatedBy,bDeleted) VALUES
('XLY','XLY','2024-01-10 08:00:00','前端业务','Sp_OrderCreate','单据','Production',1,'01/03 销售订单->生产任务单',        (SELECT iIncrement FROM (SELECT iIncrement FROM tModule WHERE sProcedureName='Sp_OrderFlow') AS x),1,'admin',0),
('XLY','XLY','2024-01-10 08:00:00','前端业务','Sp_OrderDispatch','单据','Production',1,'02/03 任务单分派',                  (SELECT iIncrement FROM (SELECT iIncrement FROM tModule WHERE sProcedureName='Sp_OrderFlow') AS x),2,'admin',0),
('XLY','XLY','2024-01-10 08:00:00','前端业务','Sp_OrderComplete','单据','Production',1,'03/03 任务单完成',                   (SELECT iIncrement FROM (SELECT iIncrement FROM tModule WHERE sProcedureName='Sp_OrderFlow') AS x),3,'admin',0);

-- 3. 自动拼版流程
INSERT IGNORE INTO tModule (sBrandsId,sSubsidiaryId,tCreateDate,sDisplayType,sProcedureName,sModuleType,sManageDeptEn,bShowPermission,sModuleNameZh,iParentId,iSortOrder,sCreatedBy,bDeleted) VALUES
('XLY','XLY','2024-01-10 08:00:00','前端业务','Sp_PanelFlow','KPI流程','Imposition',1,'自动拼版流程',NULL,3,'admin',0);
INSERT IGNORE INTO tModule (sBrandsId,sSubsidiaryId,tCreateDate,sDisplayType,sProcedureName,sModuleType,sManageDeptEn,bShowPermission,sModuleNameZh,iParentId,iSortOrder,sCreatedBy,bDeleted) VALUES
('XLY','XLY','2024-01-10 08:00:00','前端业务','Sp_PanelCreate','单据','Imposition',1,'01/02 拼版任务生成',                 (SELECT iIncrement FROM (SELECT iIncrement FROM tModule WHERE sProcedureName='Sp_PanelFlow') AS x),1,'admin',0),
('XLY','XLY','2024-01-10 08:00:00','前端业务','Sp_PanelAudit','单据','Imposition',1,'02/02 拼版审核',                       (SELECT iIncrement FROM (SELECT iIncrement FROM tModule WHERE sProcedureName='Sp_PanelFlow') AS x),2,'admin',0);

-- 4. 销售送货流程
INSERT IGNORE INTO tModule (sBrandsId,sSubsidiaryId,tCreateDate,sDisplayType,sProcedureName,sModuleType,sManageDeptEn,bShowPermission,sModuleNameZh,iParentId,iSortOrder,sCreatedBy,bDeleted) VALUES
('XLY','XLY','2024-01-10 08:00:00','前端业务','Sp_ShipFlow','KPI流程','Logistics',1,'销售送货流程',NULL,4,'admin',0);
INSERT IGNORE INTO tModule (sBrandsId,sSubsidiaryId,tCreateDate,sDisplayType,sProcedureName,sModuleType,sManageDeptEn,bShowPermission,sModuleNameZh,iParentId,iSortOrder,sCreatedBy,bDeleted) VALUES
('XLY','XLY','2024-01-10 08:00:00','前端业务','Sp_ShipCreate','单据','Logistics',1,'01/02 发货单创建',                       (SELECT iIncrement FROM (SELECT iIncrement FROM tModule WHERE sProcedureName='Sp_ShipFlow') AS x),1,'admin',0),
('XLY','XLY','2024-01-10 08:00:00','前端业务','Sp_ShipConfirm','单据','Logistics',1,'02/02 签收确认',                          (SELECT iIncrement FROM (SELECT iIncrement FROM tModule WHERE sProcedureName='Sp_ShipFlow') AS x),2,'admin',0);

-- 5. 物料采购流程
INSERT IGNORE INTO tModule (sBrandsId,sSubsidiaryId,tCreateDate,sDisplayType,sProcedureName,sModuleType,sManageDeptEn,bShowPermission,sModuleNameZh,iParentId,iSortOrder,sCreatedBy,bDeleted) VALUES
('XLY','XLY','2024-01-10 08:00:00','前端业务','Sp_PurchFlow','KPI流程','Purchasing',1,'物料采购流程',NULL,5,'admin',0);
INSERT IGNORE INTO tModule (sBrandsId,sSubsidiaryId,tCreateDate,sDisplayType,sProcedureName,sModuleType,sManageDeptEn,bShowPermission,sModuleNameZh,iParentId,iSortOrder,sCreatedBy,bDeleted) VALUES
('XLY','XLY','2024-01-10 08:00:00','前端业务','Sp_PurchRequest','单据','Purchasing',1,'01/03 采购申请',                       (SELECT iIncrement FROM (SELECT iIncrement FROM tModule WHERE sProcedureName='Sp_PurchFlow') AS x),1,'admin',0),
('XLY','XLY','2024-01-10 08:00:00','前端业务','Sp_PurchOrder','单据','Purchasing',1,'02/03 采购订单',                          (SELECT iIncrement FROM (SELECT iIncrement FROM tModule WHERE sProcedureName='Sp_PurchFlow') AS x),2,'admin',0),
('XLY','XLY','2024-01-10 08:00:00','前端业务','Sp_PurchReceive','单据','Purchasing',1,'03/03 入库确认',                         (SELECT iIncrement FROM (SELECT iIncrement FROM tModule WHERE sProcedureName='Sp_PurchFlow') AS x),3,'admin',0);

-- 6. 系统设置
INSERT IGNORE INTO tModule (sBrandsId,sSubsidiaryId,tCreateDate,sDisplayType,sProcedureName,sModuleType,sManageDeptEn,bShowPermission,sModuleNameZh,iParentId,iSortOrder,sCreatedBy,bDeleted) VALUES
('XLY','XLY','2024-01-10 08:00:00','系统配置','Sp_SysFlow','系统','SysAdmin',0,'系统设置',NULL,99,'admin',0);
INSERT IGNORE INTO tModule (sBrandsId,sSubsidiaryId,tCreateDate,sDisplayType,sProcedureName,sModuleType,sManageDeptEn,bShowPermission,sModuleNameZh,iParentId,iSortOrder,sCreatedBy,bDeleted) VALUES
('XLY','XLY','2024-01-10 08:00:00','系统配置','Sp_SysUserList','单据','SysAdmin',0,'用户列表',                                  (SELECT iIncrement FROM (SELECT iIncrement FROM tModule WHERE sProcedureName='Sp_SysFlow') AS x),1,'admin',0),
('XLY','XLY','2024-01-10 08:00:00','系统配置','Sp_SysModuleConfig','单据','SysAdmin',0,'系统模块配置',                            (SELECT iIncrement FROM (SELECT iIncrement FROM tModule WHERE sProcedureName='Sp_SysFlow') AS x),2,'admin',0),
('XLY','XLY','2024-01-10 08:00:00','系统配置','Sp_SysRole','单据','SysAdmin',0,'角色管理',                                       (SELECT iIncrement FROM (SELECT iIncrement FROM tModule WHERE sProcedureName='Sp_SysFlow') AS x),3,'admin',0),
('XLY','XLY','2024-01-10 08:00:00','系统配置','Sp_SysMenu','单据','SysAdmin',0,'菜单配置',                                       (SELECT iIncrement FROM (SELECT iIncrement FROM tModule WHERE sProcedureName='Sp_SysFlow') AS x),4,'admin',0),
('XLY','XLY','2024-01-10 08:00:00','系统配置','Sp_SysLog','单据','SysAdmin',0,'操作日志',                                        (SELECT iIncrement FROM (SELECT iIncrement FROM tModule WHERE sProcedureName='Sp_SysFlow') AS x),5,'admin',0);

SELECT
  (SELECT COUNT(*) FROM tStaff)              AS staff_rows,
  (SELECT COUNT(*) FROM tUser)               AS user_rows,
  (SELECT COUNT(*) FROM tPermissionCategory) AS perm_cat_rows,
  (SELECT COUNT(*) FROM tUserPermission)     AS user_perm_rows,
  (SELECT COUNT(*) FROM tModule)             AS module_rows;
SQL

echo "[seed-dev-data] OK"
