Commit 2ddaa705 by Joneq

解决协程不及时关闭的问题

parent f0337fa7
package main package main
var( var(
currentScore float32 currentScore float32
totalScore float32 totalScore [7]float32
) )
...@@ -13,8 +10,6 @@ var( ...@@ -13,8 +10,6 @@ var(
//公司信息计算 //公司信息计算
func CompanyInfo() { func CompanyInfo() {
totalScore = 0
EstablishmentTime() EstablishmentTime()
CompanyNature() CompanyNature()
ChangeslegalInFiveYears() ChangeslegalInFiveYears()
...@@ -22,8 +17,22 @@ func CompanyInfo() { ...@@ -22,8 +17,22 @@ func CompanyInfo() {
AnnualTaxRate() AnnualTaxRate()
lawsuitsInPastTwoYars() lawsuitsInPastTwoYars()
NumberOfSocial() NumberOfSocial()
var(
sumScore float32 = 0
joinNum float32 = 0
)
//查找出所有不是0的分值
for i :=0;i < len(totalScore) ; i++ {
if totalScore[i] != 0 {
joinNum += 1
sumScore += totalScore[i] * 0.5
}
}
//经过权重计算之后的总分值
sumScore = sumScore / joinNum
insertScoreMsg.FinalScore ,insertScoreMsg.TotalScore,insertScoreMsg.CurrentScore = totalScore,totalScore,totalScore insertScoreMsg.FinalScore ,insertScoreMsg.TotalScore,insertScoreMsg.CurrentScore = sumScore,sumScore,sumScore
insertScoreMsg.Weight = 50 insertScoreMsg.Weight = 50
setInserScoreChanFooter(1,0,1) setInserScoreChanFooter(1,0,1)
} }
...@@ -42,7 +51,7 @@ func EstablishmentTime() { ...@@ -42,7 +51,7 @@ func EstablishmentTime() {
currentScore = 0 currentScore = 0
} }
totalScore += currentScore totalScore[0]= currentScore
//设置分数 //设置分数
insertScoreMsg.FinalScore ,insertScoreMsg.TotalScore,insertScoreMsg.CurrentScore = currentScore,currentScore,currentScore insertScoreMsg.FinalScore ,insertScoreMsg.TotalScore,insertScoreMsg.CurrentScore = currentScore,currentScore,currentScore
...@@ -62,7 +71,8 @@ func CompanyNature() { ...@@ -62,7 +71,8 @@ func CompanyNature() {
currentScore = 1 currentScore = 1
} }
totalScore += currentScore totalScore[1]= currentScore
//设置分数 //设置分数
insertScoreMsg.FinalScore ,insertScoreMsg.TotalScore,insertScoreMsg.CurrentScore = currentScore,currentScore,currentScore insertScoreMsg.FinalScore ,insertScoreMsg.TotalScore,insertScoreMsg.CurrentScore = currentScore,currentScore,currentScore
...@@ -83,7 +93,8 @@ func ChangeslegalInFiveYears() { ...@@ -83,7 +93,8 @@ func ChangeslegalInFiveYears() {
currentScore = 0 currentScore = 0
} }
totalScore += currentScore totalScore[2]= currentScore
setInserScoreChanFooter(1,1,10) setInserScoreChanFooter(1,1,10)
} }
...@@ -101,7 +112,8 @@ func RegisteredCapital() { ...@@ -101,7 +112,8 @@ func RegisteredCapital() {
currentScore = 1 currentScore = 1
} }
totalScore += currentScore totalScore[3]= currentScore
setInserScoreChanFooter(1,1,11) setInserScoreChanFooter(1,1,11)
} }
...@@ -117,7 +129,8 @@ func AnnualTaxRate() { ...@@ -117,7 +129,8 @@ func AnnualTaxRate() {
currentScore = 1 currentScore = 1
} }
totalScore += currentScore totalScore[4]= currentScore
setInserScoreChanFooter(1,1,12) setInserScoreChanFooter(1,1,12)
} }
...@@ -131,7 +144,8 @@ func lawsuitsInPastTwoYars() { ...@@ -131,7 +144,8 @@ func lawsuitsInPastTwoYars() {
currentScore = 2 currentScore = 2
} }
totalScore += currentScore totalScore[5]= currentScore
setInserScoreChanFooter(1,1,13) setInserScoreChanFooter(1,1,13)
} }
...@@ -146,7 +160,8 @@ func NumberOfSocial() { ...@@ -146,7 +160,8 @@ func NumberOfSocial() {
currentScore = 0 currentScore = 0
} }
totalScore += currentScore totalScore[6]= currentScore
setInserScoreChanFooter(1,1,14) setInserScoreChanFooter(1,1,14)
} }
......
...@@ -10,6 +10,7 @@ import ( ...@@ -10,6 +10,7 @@ import (
"kaopu-server/internal/model" "kaopu-server/internal/model"
"kaopu-server/internal/service" "kaopu-server/internal/service"
"strconv" "strconv"
"sync"
"time" "time"
) )
...@@ -18,13 +19,13 @@ var ( ...@@ -18,13 +19,13 @@ var (
//获取的公司信息 //获取的公司信息
companyData []map[string]string companyData []map[string]string
//插入分值的通道,限制一千,超过等待 //插入分值的通道,限制一千,超过等待
socreRecordChan = make(chan model.Scores, 1000) socreRecordChan = make(chan model.Scores)
//分数的数据 //分数的数据
insertScoreMsg model.Scores insertScoreMsg model.Scores
//公司的详细数据 //公司的详细数据
companyBasicInfo map[string]string companyBasicInfo map[string]string
//设置等待
isOver = make(chan int) wg sync.WaitGroup
) )
...@@ -32,7 +33,10 @@ var ( ...@@ -32,7 +33,10 @@ var (
func main() { func main() {
//获取config
getConfig() getConfig()
//设置协程数量
wg.Add(1)
go insertRecord() go insertRecord()
svc := service.New() svc := service.New()
...@@ -46,18 +50,16 @@ func main() { ...@@ -46,18 +50,16 @@ func main() {
insertScoreMsg.ComCreditsId = stringTurnInt64(v["id"]) insertScoreMsg.ComCreditsId = stringTurnInt64(v["id"])
companyBasicInfo,_ = svc.GetCompanyBaseInfoByCid(ctx,insertScoreMsg.ComCreditsId) companyBasicInfo,_ = svc.GetCompanyBaseInfoByCid(ctx,insertScoreMsg.ComCreditsId)
CompanyInfo() CompanyInfo()
//EnterPriseCredit() EnterPriseCredit()
//HistoryBussiness() //HistoryBussiness()
//PersonalCredit() //PersonalCredit()
//AddScore() //AddScore()
} }
//赋值完关闭通道
close(socreRecordChan)
// 判断所有协程是否退出 wg.Wait()
for range isOver {
close(isOver)
}
} }
...@@ -78,19 +80,18 @@ func getConfig() { ...@@ -78,19 +80,18 @@ func getConfig() {
//协程增加分值记录 //协程增加分值记录
func insertRecord() { func insertRecord() {
for { ctx := context.Background()
for insertScoreMsg := range socreRecordChan{
//声明service层 //声明service层
connect := dao.New() connect := dao.New()
ctx := context.Background()
// 从通道中拿出消息 //插入数据
insertScoreMsg := <- socreRecordChan
insertId,err := connect.InsertScores(ctx,insertScoreMsg) insertId,err := connect.InsertScores(ctx,insertScoreMsg)
fmt.Print(err) fmt.Print(err)
fmt.Print(insertId) fmt.Print(insertId)
//插入数据
}
isOver <- 1 }
defer wg.Done()
} }
//设置插入的分数头 //设置插入的分数头
......
...@@ -281,3 +281,33 @@ ...@@ -281,3 +281,33 @@
[2019/11/25 19:37:02.573] [INFO] [/usr/local/var/www/ichunt/kaopu-server/cmd/static/main.go:70] caipu-server start [2019/11/25 19:37:02.573] [INFO] [/usr/local/var/www/ichunt/kaopu-server/cmd/static/main.go:70] caipu-server start
[2019/11/25 19:46:54.194] [INFO] [/usr/local/var/www/ichunt/kaopu-server/cmd/static/main.go:74] caipu-server start [2019/11/25 19:46:54.194] [INFO] [/usr/local/var/www/ichunt/kaopu-server/cmd/static/main.go:74] caipu-server start
[2019/11/25 19:48:42.586] [INFO] [/usr/local/var/www/ichunt/kaopu-server/cmd/static/main.go:74] caipu-server start [2019/11/25 19:48:42.586] [INFO] [/usr/local/var/www/ichunt/kaopu-server/cmd/static/main.go:74] caipu-server start
[2019/11/25 19:52:31.401] [INFO] [/usr/local/var/www/ichunt/kaopu-server/cmd/static/main.go:75] caipu-server start
[2019/11/25 19:54:53.134] [INFO] [/usr/local/var/www/ichunt/kaopu-server/cmd/static/main.go:75] caipu-server start
[2019/11/25 19:55:21.030] [INFO] [/usr/local/var/www/ichunt/kaopu-server/cmd/static/main.go:76] caipu-server start
[2019/11/25 19:56:50.837] [INFO] [/usr/local/var/www/ichunt/kaopu-server/cmd/static/main.go:75] caipu-server start
[2019/11/25 19:59:02.549] [INFO] [/usr/local/var/www/ichunt/kaopu-server/cmd/static/main.go:75] caipu-server start
[2019/11/25 19:59:10.735] [INFO] [/usr/local/var/www/ichunt/kaopu-server/cmd/static/main.go:75] caipu-server start
[2019/11/25 19:59:20.506] [INFO] [/usr/local/var/www/ichunt/kaopu-server/cmd/static/main.go:72] caipu-server start
[2019/11/25 19:59:55.539] [INFO] [/usr/local/var/www/ichunt/kaopu-server/cmd/static/main.go:76] caipu-server start
[2019/11/25 20:00:13.941] [INFO] [/usr/local/var/www/ichunt/kaopu-server/cmd/static/main.go:76] caipu-server start
[2019/11/25 20:00:58.563] [INFO] [/usr/local/var/www/ichunt/kaopu-server/cmd/static/main.go:76] caipu-server start
[2019/11/25 20:02:25.883] [INFO] [/usr/local/var/www/ichunt/kaopu-server/cmd/static/main.go:76] caipu-server start
[2019/11/26 09:59:52.203] [INFO] [/usr/local/var/www/ichunt/kaopu-server/cmd/static/main.go:76] caipu-server start
[2019/11/26 10:00:47.411] [INFO] [/usr/local/var/www/ichunt/kaopu-server/cmd/static/main.go:76] caipu-server start
[2019/11/26 10:02:52.480] [INFO] [/usr/local/var/www/ichunt/kaopu-server/cmd/static/main.go:76] caipu-server start
[2019/11/26 10:03:37.917] [INFO] [/usr/local/var/www/ichunt/kaopu-server/cmd/static/main.go:76] caipu-server start
[2019/11/26 10:13:09.595] [INFO] [/usr/local/var/www/ichunt/kaopu-server/cmd/static/main.go:75] caipu-server start
[2019/11/26 10:13:32.899] [INFO] [/usr/local/var/www/ichunt/kaopu-server/cmd/static/main.go:75] caipu-server start
[2019/11/26 10:20:01.059] [INFO] [/usr/local/var/www/ichunt/kaopu-server/cmd/static/main.go:73] caipu-server start
[2019/11/26 10:21:18.273] [INFO] [/usr/local/var/www/ichunt/kaopu-server/cmd/static/main.go:74] caipu-server start
[2019/11/26 10:25:36.417] [INFO] [/usr/local/var/www/ichunt/kaopu-server/cmd/static/main.go:73] caipu-server start
[2019/11/26 10:26:24.076] [INFO] [/usr/local/var/www/ichunt/kaopu-server/cmd/static/main.go:73] caipu-server start
[2019/11/26 10:53:56.988] [INFO] [/usr/local/var/www/ichunt/kaopu-server/cmd/static/main.go:78] caipu-server start
[2019/11/26 10:55:53.465] [INFO] [/usr/local/var/www/ichunt/kaopu-server/cmd/static/main.go:78] caipu-server start
[2019/11/26 10:59:33.908] [INFO] [/usr/local/var/www/ichunt/kaopu-server/cmd/static/main.go:70] caipu-server start
[2019/11/26 11:04:01.646] [INFO] [/usr/local/var/www/ichunt/kaopu-server/cmd/static/main.go:70] caipu-server start
[2019/11/26 11:04:35.443] [INFO] [/usr/local/var/www/ichunt/kaopu-server/cmd/static/main.go:69] caipu-server start
[2019/11/26 11:05:16.839] [INFO] [/usr/local/var/www/ichunt/kaopu-server/cmd/static/main.go:69] caipu-server start
[2019/11/26 11:11:13.459] [INFO] [/usr/local/var/www/ichunt/kaopu-server/cmd/static/main.go:69] caipu-server start
[2019/11/26 11:20:03.328] [INFO] [/usr/local/var/www/ichunt/kaopu-server/cmd/static/main.go:70] caipu-server start
[2019/11/26 11:22:26.430] [INFO] [/usr/local/var/www/ichunt/kaopu-server/cmd/static/main.go:76] caipu-server start
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment