package main
import (
"backend-service-api-scripts/utils"
"database/sql"
"flag"
"fmt"
"log"
"os"
)
var (
db *sql.DB = utils.ConnectData() // 这里的连接数据库需要根据数据库本身的情况进行更改
courseId int
companyId int
outputFile string
inputFile string
h bool
)
func init() {
flag.BoolVar(&h, "h", false, "帮助")
flag.IntVar(&courseId, "c", 0, "(必传值)课程ID:\n\tA课:111\n\tB课:222\n\tC课:333")
flag.IntVar(&companyId, "C", 0, "(必传值)企业ID")
flag.StringVar(&inputFile, "i", "inputFile.txt", "需要查询的电话列表文件")
flag.StringVar(&outputFile, "o", "outputFile.txt", "查询电话对应的班级号文件")
flag.Usage = usage
}
func usage() {
fmt.Fprintf(os.Stderr, `View class number by phone
Usage: viewClassNumber [-c courseId] [-C companyId] [-i inputFile] [-o outputFile]
Options:
`)
flag.PrintDefaults()
}
type UserClassesInfo struct {
ClasseNo sql.NullString
}
func getUserGroupId(phoneList <-chan string, courseId, companyId int) {
for {
phone, ok := <-phoneList
if !ok {
break
}
row := db.QueryRow("SELECT classes_no FROM classes.classes_info WHERE phone=? AND course_id=? AND company_id=?", phone, courseId, companyId)
u := UserClassesInfo{}
if err := row.Scan(&u.ClasseNo); err != nil {
log.Println(err)
}
info := fmt.Sprintf("%v\t%v\n", phone, u.ClasseNo.String)
utils.WriteFile(outputFile, &info)
}
}
func main() {
flag.Parse()
if h || companyId == 0 || courseId == 0 {
flag.Usage()
return
}
phoneList := make(chan string, 500)
utils.ReadFile(inputFile, phoneList)
getUserGroupId(phoneList, courseId, companyId)
}
标签:outputFile,companyId,courseId,sql,golang,flag,使用,inputFile
From: https://www.cnblogs.com/cqblog/p/17799591.html