package service import ( "XT_New/models" "XT_New/utils" "fmt" "github.com/jinzhu/gorm" "math/rand" "time" ) //生成编号,规则为:"SH-"+4位随机数 func CreateCode() string { s := fmt.Sprintf("%04v", rand.New(rand.NewSource(time.Now().UnixNano())).Int63n(10000)) code := "SH-" + s return code } //查询编号是否重复 func FindStorehouseCode(orgid int64, code string) bool { var total int XTReadDB().Model(&models.Storehouse{}).Where(" storehouse_code = ? and user_org_id = ? and status = 1", code, orgid).Count(&total) if total > 0 { return true } else { return false } } //判断仓库的库存是否为零 true为零,false不为零 func IsStorehouseNil(id, orgid int64) bool { //根据 id gro_id 查为零的库存 var h, y int //判断耗材是否为零 XTReadDB().Model(&models.WarehousingInfo{}).Where("stock_count > 0 and status = 1 and org_id = ? and storehouse_id = ?", orgid, id).Count(&h) if h > 0 { return false } //判断药品是否为零 XTReadDB().Model(&models.XtDrugWarehouseInfo{}).Where("stock_max_number > 0 or stock_min_number > 0 ").Where(" org_id = ? and storehouse_id = ?", orgid, id).Count(&y) if y > 0 { return false } return true } //修改仓库状态 func UpdateStorehouseStatus(id int64) error { err := XTWriteDB().Exec("UPDATE xt_storehouse,(SELECT CASE storehouse_status WHEN 1 THEN 0 WHEN 0 THEN 1 END AS tt FROM xt_storehouse WHERE id=?)tmp SET storehouse_status = tmp.tt where id=?", id, id).Error return err } //删除仓库 func DeleteStorehouse(id int64) error { err := XTWriteDB().Model(&models.Storehouse{}).Where("id = ?", id).Update("status", 0).Error return err } //查询仓库名称是否重复 func IsStorehouseName(orgid int64, storehouse_name string) (bool, error) { var total int var tmp bool err := XTReadDB().Model(&models.Storehouse{}).Where("storehouse_name = ? and user_org_id = ? and status = 1", storehouse_name, orgid).Count(&total).Error if total > 0 { tmp = true //有重复的 } else { tmp = false } return tmp, err } //查询仓库地址是否重复 func IsStorehouseAddress(orgid int64, storehouse_address string) (bool, error) { var total int var tmp bool err := XTReadDB().Model(&models.Storehouse{}).Where("storehouse_address = ? and user_org_id = ? and status = 1", storehouse_address, orgid).Count(&total).Error if total > 0 { tmp = true //有重复的 } else { tmp = false } return tmp, err } //新增仓库 func AddStroehouse(storehouse models.Storehouse) error { tx := XTWriteDB().Begin() defer func() { if err != nil { tx.Rollback() } else { tx.Commit() } }() //创建仓库 err := tx.Create(&storehouse).Error if err != nil { return err } var id models.Storehouse //获取创建仓库的id err = tx.Model(&models.Storehouse{}).Select("id").Where("status = 1 and user_org_id = ?", storehouse.UserOrgId).Order("id desc").First(&id).Error if err != nil { return err } //判断是否存在仓库配置 boolean := IsStorehouseconfigXT(storehouse.UserOrgId, tx) if boolean == false { //创建仓库配置 err = CreateStorehouseConfigXT(storehouse.UserOrgId, id.ID, tx) } else { utils.ErrorLog("仓库配置已存在") } return err } //修改仓库 func UpdateStroehouse(storehouse models.Storehouse) error { err := XTWriteDB().Model(&models.Storehouse{}).Where("id = ? and status = 1", storehouse.ID).Updates( map[string]interface{}{ "storehouse_name": storehouse.StorehouseName, "storehouse_address": storehouse.StorehouseAddress, "storehouse_status": storehouse.StorehouseStatus, "mtime": storehouse.Mtime, }).Error return err } //查询一条仓库的信息 func GetOneStorehouse(id, orgid int64) (storehouse models.Storehouse, err error) { err = XTReadDB().Model(&models.Storehouse{}).Where("id = ? and user_org_id = ?", id, orgid).Find(&storehouse).Error return } //分页 func StorehouseList(page, limit, orgid int64, keyword string) (storehouse []models.Storehouse, total int64, err error) { db := XTReadDB().Model(&storehouse).Where("status = 1 and user_org_id = ?", orgid) offset := (page - 1) * limit if len(keyword) > 0 { keyword = "%" + keyword + "%" db = db.Where("storehouse_code like ? or storehouse_name like ? or storehouse_address like ? or storehouse_admin_name like ?", keyword, keyword, keyword, keyword) } err = db.Count(&total).Offset(offset).Order("id").Limit(limit).Find(&storehouse).Error return storehouse, total, err } //获取当前机构所有可用的仓库名字 func GetAllStorehouseName(orgid int64) (storehouse []models.Storehouse, err error) { err = XTReadDB().Model(&models.Storehouse{}).Where("storehouse_status = 1 and status = 1 and user_org_id = ?", orgid).Find(&storehouse).Error return } //根据机构id,生成一个默认仓库 func GetDefaultStorehouse(orgid int64) error { var code string for a := true; a == true; { code = CreateCode() tmp := FindStorehouseCode(orgid, code) //如果没有重复的编码结束循环 if tmp == false { a = false } } storehouse := models.Storehouse{ StorehouseCode: code, StorehouseName: "默认仓库", StorehouseAddress: "", StorehouseStatus: 1, UserOrgId: orgid, StorehouseAdminName: "admin", Status: 1, Ctime: time.Now().Unix(), } err := AddStroehouse(storehouse) return err } //查找该机构id是否有仓库存在,ture存在,false不存在 func IsStorehouse(orgid int64) bool { var total int XTReadDB().Model(&models.Storehouse{}).Where("user_org_id = ?", orgid).Count(&total) if total > 0 { return true } else { return false } } //查找该机构id是否有仓库配置存在,ture存在,false不存在 func IsStorehouseconfigXT(orgid int64, tx *gorm.DB) bool { var total int tx.Model(&models.StorehouseConfig{}).Where("user_org_id = ? and status = 1", orgid).Count(&total) if total > 0 { return true } else { return false } } // 根据机构id查询仓库配置 //storehouse_info:耗材 自动入库 的仓库id //storehouse_out_info:耗材 自动出库 的仓库id //drug_storehouse_info:药品 自动入库 的仓库id //drug_storehouse_out:药品 自动出库 的仓库id func FindStorehouseConfig(orgid int64) (storehouse_config models.StorehouseConfig, err error) { err = XTReadDB().Model(&models.StorehouseConfig{}).Where("status = 1 and user_org_id = ?", orgid).Find(&storehouse_config).Error return } //根据机构id,仓库id新建一个仓库配置 func CreateStorehouseConfig(orgid, id int64) (err error) { storeconfig := models.StorehouseConfig{ UserOrgId: orgid, StorehouseInfo: id, StorehouseOutInfo: id, DrugStorehouseInfo: id, DrugStorehouseOut: id, Status: 1, Ctime: time.Now().Unix(), } err = XTWriteDB().Create(&storeconfig).Error return } //根据机构id,仓库id新建一个仓库配置,带事务 func CreateStorehouseConfigXT(orgid, id int64, tx *gorm.DB) (err error) { storeconfig := models.StorehouseConfig{ UserOrgId: orgid, StorehouseInfo: id, StorehouseOutInfo: id, DrugStorehouseInfo: id, DrugStorehouseOut: id, Status: 1, Ctime: time.Now().Unix(), } err = tx.Create(&storeconfig).Error return } //更改耗材自动入库仓库 func UpdateInfo(orgid, id int64) (err error) { mtime := time.Now().Unix() err = XTWriteDB().Model(&models.StorehouseConfig{}).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"storehouse_info": id, "mtime": mtime}).Error return } //更改耗材自动出库仓库 func UpdateOutInfo(orgid, id int64) (err error) { mtime := time.Now().Unix() err = XTWriteDB().Model(&models.StorehouseConfig{}).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"storehouse_out_info": id, "mtime": mtime}).Error return } //更改药品自动入库仓库 func UpdateDrugInfo2(orgid, id int64) (err error) { mtime := time.Now().Unix() err = XTWriteDB().Model(&models.StorehouseConfig{}).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"drug_storehouse_info": id, "mtime": mtime}).Error return } //更改药品自动出库仓库 func UpdateDrugOut(orgid, id int64) (err error) { mtime := time.Now().Unix() err = XTWriteDB().Model(&models.StorehouseConfig{}).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"drug_storehouse_out": id, "mtime": mtime}).Error return } //根据id查询仓库名称 func FindStorehouseName(id int64) (storehouse models.Storehouse, err error) { err = XTReadDB().Model(&models.Storehouse{}).Where("id = ?", id).Find(&storehouse).Error return } //判断该仓库是否在仓库配置表中 func IsInConfig(orgid, id int64) bool { var total int XTReadDB().Model(&models.StorehouseConfig{}).Where("user_org_id = ? and status = 1", orgid).Where( "storehouse_info = ? or storehouse_out_info = ? or drug_storehouse_info = ? or drug_storehouse_out = ?", id, id, id, id).Count(&total) if total > 0 { return true //存在 } else { return false } } //兼容旧数据 func Byliinit() (err error) { tx := XTWriteDB().Begin() defer func() { if err != nil && err.Error() != "record not found" { tx.Rollback() } else { tx.Commit() } }() err = StoreReduceOrg(tx) if err != nil && err.Error() != "record not found" { return } err = StoreReduceConfig(tx) if err != nil && err.Error() != "record not found" { return } err = initxt_storehouse(tx) if err != nil && err.Error() != "record not found" { return } err = initxt_stock_flow(tx) if err != nil && err.Error() != "record not found" { return } err = initxt_drug_flow(tx) if err != nil && err.Error() != "record not found" { return } err = initdialysis_before_prepare(tx) if err != nil && err.Error() != "record not found" { return } err = initxt_automatic_reduce_detail(tx) if err != nil && err.Error() != "record not found" { return } err = initxt_drug_automatic_reduce_detail(tx) if err != nil && err.Error() != "record not found" { return } err = initxt_warehouse(tx) if err != nil && err.Error() != "record not found" { return } err = initxt_warehouse_info(tx) if err != nil && err.Error() != "record not found" { return } err = initxt_warehouse_out(tx) if err != nil && err.Error() != "record not found" { return } err = initxt_warehouse_out_info(tx) if err != nil && err.Error() != "record not found" { return } err = initxt_drug_warehouse(tx) if err != nil && err.Error() != "record not found" { return } err = initxt_drug_warehouse_info(tx) if err != nil && err.Error() != "record not found" { return } err = initxt_drug_warehouse_out(tx) if err != nil && err.Error() != "record not found" { return } err = initxt_drug_warehouse_out_info(tx) if err != nil && err.Error() != "record not found" { return } err = initxt_cancel_stock(tx) if err != nil && err.Error() != "record not found" { return } err = initxt_cancel_stock_info(tx) if err != nil && err.Error() != "record not found" { return } err = initxt_drug_cancel_stock(tx) if err != nil && err.Error() != "record not found" { return } err = initxt_drug_cancel_stock_info(tx) if err != nil && err.Error() != "record not found" { return } err = initxt_supplier_warehouse_info(tx) if err != nil && err.Error() != "record not found" { return } err = initxt_supplier_warehousing_info_order(tx) if err != nil && err.Error() != "record not found" { return } err = initxt_supplier_warehouse_out(tx) if err != nil && err.Error() != "record not found" { return } err = initxt_supplier_warehousing_out_order(tx) if err != nil && err.Error() != "record not found" { return } err = initxt_supplier_warehouse_cancel(tx) if err != nil && err.Error() != "record not found" { return } err = initxt_supplier_warehousing_cancel_order(tx) return } //根据xt_gobal_template 获取的机构id减去仓库表存在的机构id,把结果插入到仓库表 func StoreReduceOrg(tx *gorm.DB) (err error) { err = tx.Exec("INSERT INTO sgj_xt.xt_storehouse(storehouse_code,storehouse_name,user_org_id,ctime)" + "SELECT CONCAT(\"SH-\",FLOOR(RAND()*9000+1000)),\"默认仓库\",tmp.id,UNIX_TIMESTAMP()" + "FROM" + "(select t1.org_id as id FROM sgj_xt.xt_gobal_template as t1 LEFT JOIN " + "(SELECT distinct user_org_id as i FROM sgj_xt.xt_storehouse) as t2 on t1.org_id = t2.i WHERE t2.i is null and t1.status = 1)tmp").Error //err = tx.Exec("INSERT INTO sgj_xt.xt_storehouse(storehouse_code,storehouse_name,user_org_id,ctime)" + // "SELECT CONCAT(\"sh-\",FLOOR(RAND()*9000+1000)),\"默认仓库\",tmp.id,UNIX_TIMESTAMP()" + // "FROM" + // "(select t1.id FROM sgj_users.sgj_user_org as t1 LEFT JOIN " + // "(SELECT distinct user_org_id as i FROM sgj_xt.xt_storehouse) as t2 on t1.id = t2.i WHERE t2.i is null and t1.status != 0 and t1.import = 0)tmp").Error return } //查找仓库表的orgid 减去仓库配置表的orgid,把结果插入到仓库配置表 func StoreReduceConfig(tx *gorm.DB) (err error) { err = tx.Exec("INSERT INTO sgj_xt.xt_storehouse_config(user_org_id,storehouse_info,storehouse_out_info,drug_storehouse_info,drug_storehouse_out,ctime)" + "SELECT tmp.user_org_id,tmp.id,tmp.id,tmp.id,tmp.id,UNIX_TIMESTAMP()" + "FROM(select t1.id,t1.user_org_id FROM sgj_xt.xt_storehouse as t1 LEFT JOIN " + "(SELECT user_org_id as i FROM sgj_xt.xt_storehouse_config) as t2 on t1.user_org_id = t2.i WHERE t2.i is null and t1.status = 1)tmp").Error return } //初始化 xt_storehouse()把插入管理员的名字 func initxt_storehouse(tx *gorm.DB) (err error) { err = tx.Exec("UPDATE sgj_xt.xt_storehouse as t," + "(SELECT o.id,o.creator,u.name from sgj_users.sgj_user_org as o,sgj_users.sgj_user_admin as u," + "(SELECT DISTINCT org_id from sgj_xt.xt_gobal_template)as t " + "WHERE o.creator = u.id AND t.org_id = o.id )tmp " + "SET t.storehouse_admin_id = tmp.creator,t.storehouse_admin_name = tmp.name,t.mtime = UNIX_TIMESTAMP()" + "WHERE t.status = 1 and t.user_org_id = tmp.id").Error return } //初始化 xt_stock_flow(完成) func initxt_stock_flow(tx *gorm.DB) (err error) { err = tx.Exec("UPDATE sgj_xt.xt_stock_flow as t," + "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," + "(SELECT distinct user_org_id as orgid FROM sgj_xt.xt_stock_flow WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " + "WHERE t1.user_org_id = t2.orgid)tmp " + "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" + "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error return } //初始化 xt_drug_flow(完成) func initxt_drug_flow(tx *gorm.DB) (err error) { err = tx.Exec("UPDATE sgj_xt.xt_drug_flow as t," + "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," + "(SELECT distinct user_org_id as orgid FROM sgj_xt.xt_drug_flow WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " + "WHERE t1.user_org_id = t2.orgid)tmp " + "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" + "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error return } //初始化 dialysis_before_prepare(完成) func initdialysis_before_prepare(tx *gorm.DB) (err error) { err = tx.Exec("UPDATE sgj_xt.dialysis_before_prepare as t," + "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," + "(SELECT distinct user_org_id as orgid FROM sgj_xt.dialysis_before_prepare WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " + "WHERE t1.user_org_id = t2.orgid)tmp " + "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" + "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error return } //初始化 xt_automatic_reduce_detail(完成) func initxt_automatic_reduce_detail(tx *gorm.DB) (err error) { err = tx.Exec("UPDATE sgj_xt.xt_automatic_reduce_detail as t," + "(SELECT t1.user_org_id as orgid,t1.storehouse_out_info as id from sgj_xt.xt_storehouse_config as t1," + "(SELECT distinct org_id as orgid FROM sgj_xt.xt_automatic_reduce_detail WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " + "WHERE t1.user_org_id = t2.orgid)tmp " + "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" + "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error return } //初始化 xt_drug_automatic_reduce_detail(完成) func initxt_drug_automatic_reduce_detail(tx *gorm.DB) (err error) { err = tx.Exec("UPDATE sgj_xt.xt_drug_automatic_reduce_detail as t," + "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_out as id from sgj_xt.xt_storehouse_config as t1," + "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_automatic_reduce_detail WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " + "WHERE t1.user_org_id = t2.orgid)tmp " + "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" + "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error return } //初始化 xt_warehouse(完成) func initxt_warehouse(tx *gorm.DB) (err error) { err = tx.Exec("UPDATE sgj_xt.xt_warehouse as t," + "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," + "(SELECT distinct org_id as orgid FROM sgj_xt.xt_warehouse WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " + "WHERE t1.user_org_id = t2.orgid)tmp " + "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" + "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error return } //初始化 xt_warehouse_info(完成) func initxt_warehouse_info(tx *gorm.DB) (err error) { err = tx.Exec("UPDATE sgj_xt.xt_warehouse_info as t," + "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," + "(SELECT distinct org_id as orgid FROM sgj_xt.xt_warehouse_info WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " + "WHERE t1.user_org_id = t2.orgid)tmp " + "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" + "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error return } //初始化 xt_warehouse_out(完成) func initxt_warehouse_out(tx *gorm.DB) (err error) { err = tx.Exec("UPDATE sgj_xt.xt_warehouse_out as t," + "(SELECT t1.user_org_id as orgid,t1.storehouse_out_info as id from sgj_xt.xt_storehouse_config as t1," + "(SELECT distinct org_id as orgid FROM sgj_xt.xt_warehouse_out WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " + "WHERE t1.user_org_id = t2.orgid)tmp " + "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" + "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error return } //初始化 xt_warehouse_out_info(完成) func initxt_warehouse_out_info(tx *gorm.DB) (err error) { err = tx.Exec("UPDATE sgj_xt.xt_warehouse_out_info as t," + "(SELECT t1.user_org_id as orgid,t1.storehouse_out_info as id from sgj_xt.xt_storehouse_config as t1," + "(SELECT distinct org_id as orgid FROM sgj_xt.xt_warehouse_out_info WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " + "WHERE t1.user_org_id = t2.orgid)tmp " + "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" + "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error return } //初始化 xt_drug_warehouse(完成) func initxt_drug_warehouse(tx *gorm.DB) (err error) { err = tx.Exec("UPDATE sgj_xt.xt_drug_warehouse as t," + "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_info as id from sgj_xt.xt_storehouse_config as t1," + "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_warehouse WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " + "WHERE t1.user_org_id = t2.orgid)tmp " + "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" + "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error return } //初始化 xt_drug_warehouse_info(完成) func initxt_drug_warehouse_info(tx *gorm.DB) (err error) { err = tx.Exec("UPDATE sgj_xt.xt_drug_warehouse_info as t," + "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_info as id from sgj_xt.xt_storehouse_config as t1," + "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_warehouse_info WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " + "WHERE t1.user_org_id = t2.orgid)tmp " + "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" + "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error return } //初始化 xt_drug_warehouse_out(完成) func initxt_drug_warehouse_out(tx *gorm.DB) (err error) { err = tx.Exec("UPDATE sgj_xt.xt_drug_warehouse_out as t," + "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_out as id from sgj_xt.xt_storehouse_config as t1," + "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_warehouse_out WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " + "WHERE t1.user_org_id = t2.orgid)tmp " + "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" + "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error return } //初始化 xt_drug_warehouse_out_info(完成) func initxt_drug_warehouse_out_info(tx *gorm.DB) (err error) { err = tx.Exec("UPDATE sgj_xt.xt_drug_warehouse_out_info as t," + "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_out as id from sgj_xt.xt_storehouse_config as t1," + "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_warehouse_out_info WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " + "WHERE t1.user_org_id = t2.orgid)tmp " + "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" + "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error return } //初始化 xt_cancel_stock(完成) func initxt_cancel_stock(tx *gorm.DB) (err error) { err = tx.Exec("UPDATE sgj_xt.xt_cancel_stock as t," + "(SELECT t1.user_org_id as orgid,t1.storehouse_out_info as id from sgj_xt.xt_storehouse_config as t1," + "(SELECT distinct org_id as orgid FROM sgj_xt.xt_cancel_stock WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " + "WHERE t1.user_org_id = t2.orgid)tmp " + "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" + "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error return } //初始化 xt_cancel_stock_info(完成) func initxt_cancel_stock_info(tx *gorm.DB) (err error) { err = tx.Exec("UPDATE sgj_xt.xt_cancel_stock_info as t," + "(SELECT t1.user_org_id as orgid,t1.storehouse_out_info as id from sgj_xt.xt_storehouse_config as t1," + "(SELECT distinct org_id as orgid FROM sgj_xt.xt_cancel_stock_info WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " + "WHERE t1.user_org_id = t2.orgid)tmp " + "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" + "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error return } //初始化 xt_drug_cancel_stock(完成) func initxt_drug_cancel_stock(tx *gorm.DB) (err error) { err = tx.Exec("UPDATE sgj_xt.xt_drug_cancel_stock as t," + "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_out as id from sgj_xt.xt_storehouse_config as t1," + "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_cancel_stock WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " + "WHERE t1.user_org_id = t2.orgid)tmp " + "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" + "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error return } //初始化 xt_drug_cancel_stock_info(完成) func initxt_drug_cancel_stock_info(tx *gorm.DB) (err error) { err = tx.Exec("UPDATE sgj_xt.xt_drug_cancel_stock_info as t," + "(SELECT t1.user_org_id as orgid,t1.drug_storehouse_out as id from sgj_xt.xt_storehouse_config as t1," + "(SELECT distinct org_id as orgid FROM sgj_xt.xt_drug_cancel_stock_info WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " + "WHERE t1.user_org_id = t2.orgid)tmp " + "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" + "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.org_id = tmp.orgid").Error return } //初始化 xt_supplier_warehouse_info(完成) func initxt_supplier_warehouse_info(tx *gorm.DB) (err error) { err = tx.Exec("UPDATE sgj_xt.xt_supplier_warehouse_info as t," + "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," + "(SELECT distinct user_org_id as orgid FROM sgj_xt.xt_supplier_warehouse_info WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " + "WHERE t1.user_org_id = t2.orgid)tmp " + "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" + "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error return } //初始化 xt_supplier_warehousing_info_order(完成) func initxt_supplier_warehousing_info_order(tx *gorm.DB) (err error) { err = tx.Exec("UPDATE sgj_xt.xt_supplier_warehousing_info_order as t," + "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," + "(SELECT distinct user_org_id as orgid FROM sgj_xt.xt_supplier_warehousing_info_order WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " + "WHERE t1.user_org_id = t2.orgid)tmp " + "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" + "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error return } //初始化 xt_supplier_warehouse_out(完成) func initxt_supplier_warehouse_out(tx *gorm.DB) (err error) { err = tx.Exec("UPDATE sgj_xt.xt_supplier_warehouse_out as t," + "(SELECT t1.user_org_id as orgid,t1.storehouse_out_info as id from sgj_xt.xt_storehouse_config as t1," + "(SELECT distinct user_org_id as orgid FROM sgj_xt.xt_supplier_warehouse_out WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " + "WHERE t1.user_org_id = t2.orgid)tmp " + "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" + "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error return } //初始化 xt_supplier_warehousing_out_order(完成) func initxt_supplier_warehousing_out_order(tx *gorm.DB) (err error) { err = tx.Exec("UPDATE sgj_xt.xt_supplier_warehousing_out_order as t," + "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," + "(SELECT distinct user_org_id as orgid FROM sgj_xt.xt_supplier_warehousing_out_order WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " + "WHERE t1.user_org_id = t2.orgid)tmp " + "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" + "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error return } //初始化 xt_supplier_warehouse_cancel(完成) func initxt_supplier_warehouse_cancel(tx *gorm.DB) (err error) { err = tx.Exec("UPDATE sgj_xt.xt_supplier_warehouse_cancel as t," + "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," + "(SELECT distinct user_org_id as orgid FROM sgj_xt.xt_supplier_warehouse_cancel WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " + "WHERE t1.user_org_id = t2.orgid)tmp " + "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" + "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error return } //初始化 xt_supplier_warehousing_cancel_order(完成) func initxt_supplier_warehousing_cancel_order(tx *gorm.DB) (err error) { err = tx.Exec("UPDATE sgj_xt.xt_supplier_warehousing_cancel_order as t," + "(SELECT t1.user_org_id as orgid,t1.storehouse_info as id from sgj_xt.xt_storehouse_config as t1," + "(SELECT distinct user_org_id as orgid FROM sgj_xt.xt_supplier_warehousing_cancel_order WHERE storehouse_id is NULL or storehouse_id = 0 )t2 " + "WHERE t1.user_org_id = t2.orgid)tmp " + "SET t.storehouse_id = tmp.id,t.mtime = UNIX_TIMESTAMP()" + "WHERE (t.storehouse_id = 0 or t.storehouse_id is null) and t.user_org_id = tmp.orgid").Error return } //查询机构下的所有用户名 func GetuserName(orgid int64) (username []models.App_Role_byli, err error) { err = XTReadDB().Select("id,org_id,user_name").Where("org_id = ?", orgid).Find(&username).Error return }