Commit e72a37a4 by huangchengyi

1.0

parent 382520b7
......@@ -6,12 +6,14 @@ import (
"kaopu-server/internal/service"
"github.com/bilibili/kratos/pkg/conf/paladin"
"kaopu-server/internal/logic"
"kaopu-server/internal/dao"
"fmt"
"os"
"strconv"
"sync"
)
func main() {
//获取输入参数s
var mod int;
......@@ -20,21 +22,10 @@ func main() {
if err := paladin.Init(); err != nil {
panic(err)
}
model_dong := dao.Model_dong
mod_name := dao.Model_dong[mod]
mod_name_log := dao.Model_dong_log[mod]
//模块对应的值
var model_dong = map[int]string{
0:"所有",
35:"逾期金额率",
36:"发生逾期笔数率",
37:"平均逾期支付时间(天)",
38:"平均付款时间(天)",
39:"正常收款笔数率",
40:"近6个月账期变化率",
41:"交货及时率",
42:"退货率",
43:"近6个月逾期变化率",
44:"近6个月参保人数变化率",
}
_ ,ok := model_dong[mod]
if !ok {
fmt.Println("模块名称输入错误!")
......@@ -42,8 +33,10 @@ func main() {
}
ctx := context.Background() //上下文
if mod >0 {
runTask(mod,ctx)
svc := service.New()
svc.DongRun(ctx,mod,""); //运行main
}else{
var wg sync.WaitGroup
......@@ -51,14 +44,18 @@ func main() {
for k,_ := range model_dong{
// 开启协程
wg.Add(1)
go func(k int) {
go func(k int,ctx context.Context) {
fmt.Println("--开始协程:"+strconv.Itoa(k))
logic.Loginfo("统计开始--"+mod_name,mod_name_log);
runTask(k,ctx) //运行任务
svc := service.New()
svc.DongRun(ctx,mod,""); //运行main
fmt.Println("--结束协程:"+strconv.Itoa(k))
logic.Loginfo("统计结束--"+mod_name,mod_name_log);
defer wg.Add(-1)
}(k)
}(k,ctx)
}
wg.Wait()
......@@ -66,22 +63,4 @@ func main() {
}
}
/*
* 根据类型分别跑任务
*/
func runTask(mod int,ctx context.Context) {
svc := service.New()
switch mod{
case 35:
logic.Loginfo("逾期率统计开始","_yuqilv");
svc.CountYuqiRate(ctx,mod,"");
case 36:
logic.Loginfo("发生逾期笔数率统计开始","_yuqibi");
svc.CountYuqiBi(ctx,mod,"");
default:
return
}
return
}
......@@ -35,8 +35,11 @@ type (
//---静态结束
//---动态开始
CountYuqiRate(ctx context.Context,project_id int,erp_company_code string) (r []map[string]interface{},err error) //计算逾期率
CountYuqiBi(ctx context.Context,project_id int,erp_company_code string) (r []map[string]interface{},err error) //计算逾期笔数率
DongMain(ctx context.Context,project_id int,erp_company_code string) (r []map[string]interface{},err error) //动态模型调用主函数
DongYuqiRate(ctx context.Context,y map[string]string,modelItems []map[string]string,project_id int ) (zhi float64,fen float64,err error)//计算逾期率
DongYuqiBi(ctx context.Context,y map[string]string,modelItems []map[string]string,project_id int ) (zhi float64,fen float64,err error)//计算逾期笔数率
//CountYuqiRate(ctx context.Context,project_id int,erp_company_code string) (r []map[string]interface{},err error) //计算逾期率
//CountYuqiBi(ctx context.Context,project_id int,erp_company_code string) (r []map[string]interface{},err error) //计算逾期笔数率
//---动态结束
}
)
......
package dao
import (
"context"
"fmt"
"kaopu-server/internal/logic"
"kaopu-server/internal/model"
"strconv"
)
//存放动态模型所有常量
const (
_sql1= "select m.full_score,m.weight,m.max_score,p.id,p.pid from lie_model m,lie_project p where m.project_id=p.id and p.id=%s" //查询项目配置
_sql2_1 = "select id,erp_company_code,company_name from lie_com_credits ddd order by id asc " //查询公司所有信息
_sql2_2 = "select id,erp_company_code,company_name from lie_com_credits where erp_company_code = '%s' order by id asc " //查询单个公司所有信息
)
//项目对应的id
var Model_dong = map[int]string{
35:"逾期金额率",
36:"发生逾期笔数率",
37:"平均逾期支付时间(天)",
38:"平均付款时间(天)",
39:"正常收款笔数率",
40:"近6个月账期变化率",
41:"交货及时率",
42:"退货率",
43:"近6个月逾期变化率",
44:"近6个月参保人数变化率",
}
//动态模型对应的日志文件名称
var Model_dong_log = map[int]string{
35:"_yuqilv",
36:"_yuqibi",
37:"",
38:"",
39:"",
40:"",
41:"",
42:"",
43:"",
44:"",
}
//动态模型对应梯度单位
var Model_dong_unit = map[int]float64{
35: 0.01, //百分比 等于乘以 0.01
36: 0.01,
}
//计算逾期金额率:延期支付时间超过平均账期的金额/收款总额:
func (d *dao) DongMain(ctx context.Context,project_id int,erp_company_code string) (r []map[string]interface{},err error) {
var (
final_score,zhi float64
score_one float64 =0; //分数
_sql2 string; //
mod_name string = Model_dong[project_id] //统计动态模型名称
log_pre string = Model_dong_log[project_id] //日志文件名称
)
//查询逾期率项目配置
sql_str1 := fmt.Sprintf(_sql1,strconv.Itoa(project_id))
yuqilv_project,err := d.QueryOne(ctx,1,sql_str1)
if yuqilv_project == nil {
logic.Loginfo("不存在该项目配置lie_model ,project_id:"+strconv.Itoa(project_id),log_pre)
return
}
weight,_ := strconv.ParseFloat(yuqilv_project["weight"],64)
//查询逾期率项目详情
sql_str4 := fmt.Sprintf(_sql4,strconv.Itoa(project_id))
modelItems,err := d.QueryMany(ctx,1,sql_str4)
if modelItems == nil {
logic.Loginfo("不存在该项目配置详情lie_model_items,project_id:"+strconv.Itoa(project_id),log_pre)
return
}
//查询当前申请的所有公司
if erp_company_code == "" {
_sql2 = _sql2_1
}else {
_sql2 =fmt.Sprintf(_sql2_2,erp_company_code) //查询一个公司
}
company_info,err := d.QueryMany(ctx,1,_sql2);
if err != nil {
fmt.Println("\r\nerror:1004",err.Error())
logic.Loginfo("错误"+err.Error(),log_pre)
return
}
for _,v := range company_info{ //循环当前公司,计算各个公司对应分数
if v["erp_company_code"] != "" {
//调用公共动态算法去查对应得分
switch project_id{
case 35:
zhi,score_one,err = d.DongYuqiRate(ctx,v,modelItems,project_id)
case 36:
zhi,score_one,err = d.DongYuqiRate(ctx,v,modelItems,project_id)
default:
continue;
}
if score_one == 0 {
fmt.Print(mod_name+"不存在对应梯度,erp_company_code:"+v["erp_company_code"]+" zhi:"+logic.MyFloat64ToStr(zhi)+"\r\n")
continue;
}
//拼接最终得分,权重*单位*分数
final_score = weight*Model_dong_unit[project_id]*score_one
var scores model.Scores //插入分数结构体
scores.Type=2;
scores.ProjectPid = logic.MyInt8(yuqilv_project["pid"]);
scores.ProjectId = logic.MyInt8(yuqilv_project["id"]);
scores.TotalScore = score_one;
scores.CurrentScore = score_one;
scores.Weight= weight;
scores.FinalScore = final_score;
scores.ComCreditsId = logic.MyInt64(v["id"]);
insertid,err := d.InsertScores(ctx,scores)
if err != nil {
logic.Loginfo("错误:"+err.Error(),log_pre)
fmt.Print("错误:"+err.Error(),insertid)
}
log1 := "统计"+mod_name+"成功--公司编码:"+v["erp_company_code"]+"计算得分:"+logic.MyFloat64ToStr(score_one)+" 最终得分:"+logic.MyFloat64ToStr(final_score);
fmt.Print(log1)
logic.Loginfo(log1,log_pre)
}
}
logic.Loginfo("统计"+mod_name+"完成",log_pre)
return
}
package dao
import (
"context"
"fmt"
"kaopu-server/internal/logic"
"strconv"
)
const (
_sql3 = "select sum(delay_amount)/sum(order_amount) yuqilv from lie_basic_detail where erp_company_code='%s' " //基础数据
_sql4 = "select * from lie_model_items where project_id=%s " //查询统计项目梯度详情
_sql5 = "select count(1) yuqibi from lie_basic_detail where delay_amount>0 and erp_company_code='%s'" //发生逾期笔数
_sql6 = "select count(1) allbi from lie_basic_detail where return_amount>0 and erp_company_code='%s'" //收款总笔数
)
/*
@param zhi 对应梯度 率 或天
@param fen 分数,最终对应哪个梯度得分
*/
//计算逾期金额率:延期支付时间超过平均账期的金额/收款总额:
func (d *dao) DongYuqiRate(ctx context.Context,y map[string]string,modelItems []map[string]string,project_id int) (zhi float64,fen float64,err error) {
var (
score_one float64 =0; //分数
mod_name string = Model_dong[project_id] //统计动态模型名称
log_pre string = Model_dong_log[project_id] //日志文件名称
)
//查询逾期率
oneyu,_ := d.QueryOne(ctx,2,fmt.Sprintf(_sql3,y["erp_company_code"]))
if oneyu["yuqilv"] == "" || oneyu == nil { //没有结果
log1 := "公司编码:"+y["erp_company_code"]+"逾期金额率为空,跳过";
fmt.Print(log1)
logic.Loginfo("统计--"+mod_name+log1,log_pre)
return
}
fmt.Print("\r\n公司编码:"+y["erp_company_code"]+mod_name+"为:"+oneyu["yuqilv"])
//查询逾期率对应分数
yuqilv,_ := strconv.ParseFloat(oneyu["yuqilv"],64)
for _,y := range modelItems{
ladder_range_min := logic.MyFloat64(y["ladder_range_min"])
ladder_range_max := logic.MyFloat64(y["ladder_range_max"])
if (ladder_range_min <= yuqilv && yuqilv < ladder_range_max ) {
score_one = logic.MyFloat64(y["score"]) //得到的分数
}
//fmt.Print("\r\n\r\n :",ladder_range_min,ladder_range_max,yuqilv)
}
zhi = yuqilv
fen = score_one
return
}
//发生逾期笔数率: 发生逾期笔数/收款总笔数
func (d *dao) DongYuqiBi(ctx context.Context,y map[string]string,modelItems []map[string]string,project_id int) (zhi float64,fen float64,err error) {
var (
score_one float64 =0; //分数
mod_name string = Model_dong[project_id] //统计动态模型名称
log_pre string = Model_dong_log[project_id] //日志文件名称
)
//查询逾期笔数
yuqibi,_ := d.QueryOne(ctx,2,fmt.Sprintf(_sql5,y["erp_company_code"]))
if yuqibi["yuqibi"] == "0" { //没有结果
log1 := "公司编码:"+y["erp_company_code"]+"没有逾期笔数,跳过";
fmt.Print(log1)
logic.Loginfo("统计--"+mod_name+log1,log_pre)
}
//查询逾期笔数
allbi,_ := d.QueryOne(ctx,2,fmt.Sprintf(_sql6,y["erp_company_code"]))
birate := logic.MyFloat64(yuqibi["yuqibi"])/logic.MyFloat64(allbi["allbi"])
//计算分数
for _,y := range modelItems{
ladder_range_min := logic.MyFloat64(y["ladder_range_min"])
ladder_range_max := logic.MyFloat64(y["ladder_range_max"])
if (ladder_range_min <= birate && birate < ladder_range_max ) {
score_one = logic.MyFloat64(y["score"]) //得到的分数
}
}
zhi = birate
fen = score_one
return
}
......@@ -7,13 +7,8 @@ import (
"context"
)
//计算逾期率
func (s *Service) CountYuqiRate(ctx context.Context,project_id int,erp_company_code string) (res []map[string]interface{}, err error) {
res, err = s.dao.CountYuqiRate(ctx,project_id,erp_company_code)
//动态模型服务
func (s *Service) DongRun(ctx context.Context,project_id int,erp_company_code string) (res []map[string]interface{}, err error) {
res, err = s.dao.DongMain(ctx,project_id,erp_company_code)
return
}
//计算逾期笔数率
func (s *Service) CountYuqiBi(ctx context.Context,project_id int,erp_company_code string) (res []map[string]interface{}, err error) {
res, err = s.dao.CountYuqiBi(ctx,project_id,erp_company_code)
return
}
\ No newline at end of file
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