![]()
文字列結合にする方法で解決した☺️
func (b *userQueryBuilder) JoinsSubQuery(companyID value.ID, userIDs []value.ID) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
// 配列を文字列に変換
ids := make([]string, len(userIDs))
for i, id := range userIDs {
ids[i] = fmt.Sprintf("'%s'", id)
}
userIDsStr := strings.Join(ids, ",")
subQuery := fmt.Sprintf(
"SELECT u.id as user_id, "+
// ユーザテンプレート対象外数
"COUNT(DISTINCT at.id) as user_template_is_calculation_target_false_count, "+
// ユーザアカウントテンプレート対象外数
"COUNT(DISTINCT aet.id) as user_account_template_is_calculation_target_false_count "+
"FROM users AS u "+
"LEFT JOIN user_templates AS at "+
"ON u.company_id = '%[1]s' AND u.company_id = at.company_id AND "+
"at.is_calculation_target = 0 AND at.is_complete = 0 AND "+
"at.deleted_at IS NULL AND u.deleted_at IS NULL AND "+
"u.specific_item_join_key = at.specific_item_join_key AND "+
"u.specific_item_join_key != '%[2]s' "+
"LEFT JOIN user_account_templates AS aet "+
"ON u.company_id = '%[1]s' AND u.company_id = aet.company_id AND "+
"aet.is_calculation_target = 0 AND aet.is_complete = 0 AND "+
"aet.deleted_at IS NULL AND u.deleted_at IS NULL AND "+
"u.specific_item_join_key = aet.specific_item_join_key AND "+
"aet.specific_item_join_key != '%[2]s' "+
"WHERE u.company_id = '%[1]s' AND u.deleted_at IS NULL "+
"AND u.id IN (%[3]s) "+
"GROUP BY u.id",
companyID, BlankSpecificItemJoinKey, userIDsStr)
return db.Joins(fmt.Sprintf("LEFT JOIN (%s) AS false_template_counts ON false_template_counts.user_id = u.id", subQuery))
}
}





