|
@@ -0,0 +1,226 @@
|
|
|
|
+package main
|
|
|
|
+
|
|
|
|
+import (
|
|
|
|
+ "context"
|
|
|
|
+ _ "github.com/gogf/gf/contrib/drivers/mysql/v2"
|
|
|
|
+ "github.com/gogf/gf/v2/frame/g"
|
|
|
|
+ "github.com/gogf/gf/v2/util/gconv"
|
|
|
|
+ "github.com/google/uuid"
|
|
|
|
+ "strings"
|
|
|
|
+ "time"
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+func main() {
|
|
|
|
+ CreateRecord(context.TODO())
|
|
|
|
+ //fmt.Println(int(time.Now().Month()))
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+var (
|
|
|
|
+ deptMapping = `[
|
|
|
|
+ {"oldCode":"010000","show":"销售一部","newId":"58435"},
|
|
|
|
+ {"oldCode":"010100","show":"销售一部/电销部","newId":"26820"},
|
|
|
|
+ {"oldCode":"010101","show":"销售一部/电销部/电销一组","newId":"26821"},
|
|
|
|
+ {"oldCode":"010102","show":"销售一部/电销部/电销二组","newId":"26822"},
|
|
|
|
+ {"oldCode":"010103","show":"销售一部/电销部/电销三组","newId":"26823"},
|
|
|
|
+ {"oldCode":"010104","show":"销售一部/电销部/电销四组","newId":"26824"},
|
|
|
|
+ {"oldCode":"010200","show":"销售一部/大客户部","newId":"27275"},
|
|
|
|
+ {"oldCode":"010300","show":"销售一部/渠道部","newId":"-1"},
|
|
|
|
+ {"oldCode":"020000","show":"产品部","newId":"26920"},
|
|
|
|
+ {"oldCode":"020100","show":"产品部/售后服务组","newId":"58437"},
|
|
|
|
+ {"oldCode":"020200","show":"产品部/客户成功组","newId":"27777"},
|
|
|
|
+ {"oldCode":"030000","show":"销售二部","newId":"58416"},
|
|
|
|
+ {"oldCode":"040000","show":"运营部","newId":"27103"},
|
|
|
|
+ {"oldCode":"040100","show":"运营部/客户成功组","newId":"59005"},
|
|
|
|
+ {"oldCode":"040200","show":"运营部/运营组","newId":"59028"},
|
|
|
|
+ {"oldCode":"050000","show":"销售三部","newId":"27955"},
|
|
|
|
+ {"oldCode":"060000","show":"市场部","newId":"59010"},
|
|
|
|
+ {"oldCode":"060100","show":"市场部/市场组","newId":"59011"},
|
|
|
|
+ {"oldCode":"060200","show":"市场部/大客组","newId":"59017"},
|
|
|
|
+ {"oldCode":"070000","show":"数据事业部","newId":"59006"},
|
|
|
|
+ {"oldCode":"070100","show":"数据事业部/销售组","newId":"59012"},
|
|
|
|
+ {"oldCode":"070200","show":"数据事业部/售前与售后组","newId":"59007"}
|
|
|
|
+ ]`
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+type (
|
|
|
|
+ saleMoneyObj struct {
|
|
|
|
+ Data []struct {
|
|
|
|
+ Name string `json:"name"`
|
|
|
|
+ Money float64 `json:"money"`
|
|
|
|
+ } `json:"data"`
|
|
|
|
+ }
|
|
|
|
+ newDept struct {
|
|
|
|
+ Name string
|
|
|
|
+ Id int
|
|
|
|
+ }
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+// CreateRecord 已生效的业绩
|
|
|
|
+func CreateRecord(ctx context.Context) {
|
|
|
|
+ var (
|
|
|
|
+ codeDeptMapping = map[string]*newDept{}
|
|
|
|
+ insertArr []map[string]interface{}
|
|
|
|
+ )
|
|
|
|
+ for _, m := range gconv.Maps(deptMapping) {
|
|
|
|
+ codeDeptMapping[gconv.String(m["oldCode"])] = &newDept{
|
|
|
|
+ Name: gconv.String(m["show"]),
|
|
|
|
+ Id: gconv.Int(m["newId"]),
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ res, err := g.DB("release").Query(ctx, "SELECT order_status,order_code,pay_money,saleDep,salesperson_id,salesperson,saleMoney,is_backstage_order,sale_time,pay_time,refund_status FROM dataexport_order WHERE sale_time>'2024-01-01 00:00:00' and sale_time <'2024-09-10 00:00:00' ")
|
|
|
|
+ if err != nil {
|
|
|
|
+ panic(err)
|
|
|
|
+ }
|
|
|
|
+ for _, m := range res.List() {
|
|
|
|
+ var (
|
|
|
|
+ pay_money = gconv.Int(m["pay_money"])
|
|
|
|
+ is_backstage_order = gconv.Int(m["is_backstage_order"])
|
|
|
|
+ sale_time = gconv.String(m["sale_time"])
|
|
|
|
+ order_code = gconv.String(m["order_code"])
|
|
|
|
+ order_status = gconv.Int(m["order_status"])
|
|
|
|
+ refund_status = gconv.Int(m["refund_status"])
|
|
|
|
+ )
|
|
|
|
+ if pay_money <= 0 {
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+ if !(refund_status == 1 || refund_status == 2 || order_status == 1) {
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var (
|
|
|
|
+ statistics_time = gconv.String(m["pay_time"])
|
|
|
|
+ uuidStr = uuid.New().String()
|
|
|
|
+ reason = "回款成功" //协议归档
|
|
|
|
+ )
|
|
|
|
+ if is_backstage_order == 1 {
|
|
|
|
+ date, err := time.ParseInLocation(time.DateTime, sale_time, time.Local)
|
|
|
|
+ if err != nil {
|
|
|
|
+ g.Log().Printf(ctx, "格式化销售时间异常 %s %s", order_code, sale_time)
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+ if date.Month() <= 3 {
|
|
|
|
+ //用录入的业绩统计时间
|
|
|
|
+ statistics_time = sale_time
|
|
|
|
+ } else {
|
|
|
|
+ //管理后台创建的订单:协议归档时间和首笔回款时间的最小值。
|
|
|
|
+ res, _ := g.DB("release").GetOne(ctx, "SELECT * FROM (SELECT * FROM(SELECT contract_archive_time as t ,'0' as v FROM contract WHERE order_code=? and contract_archive_time is NOT NULL) as t1 UNION ALL SELECT * FROM(SELECT return_time as t ,'1' as v FROM return_money_record WHERE order_code=? AND state=1) as t2) as d ORDER BY t asc LIMIT 1", order_code, order_code)
|
|
|
|
+ if !res.IsEmpty() {
|
|
|
|
+ var (
|
|
|
|
+ tStr = gconv.String(res.Map()["t"])
|
|
|
|
+ v = gconv.String(res.Map()["v"])
|
|
|
|
+ )
|
|
|
|
+ if tStr != "" {
|
|
|
|
+ statistics_time = tStr
|
|
|
|
+ }
|
|
|
|
+ if v == "1" {
|
|
|
|
+ reason = "协议归档"
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if statistics_time == "" {
|
|
|
|
+ statistics_time = sale_time
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var (
|
|
|
|
+ salesperson_id = gconv.String(m["salesperson_id"])
|
|
|
|
+ salesperson = gconv.String(m["salesperson"])
|
|
|
|
+ saleDep = gconv.String(m["saleDep"])
|
|
|
|
+ )
|
|
|
|
+ if salesperson_id != "" {
|
|
|
|
+ if strings.Contains(salesperson, ",") {
|
|
|
|
+ sale := saleMoneyObj{}
|
|
|
|
+ gconv.Struct(gconv.String(m["saleMoney"]), &sale)
|
|
|
|
+ idArr := strings.Split(salesperson_id, ",")
|
|
|
|
+ personArr := strings.Split(salesperson, ",")
|
|
|
|
+ depArr := strings.Split(saleDep, ",")
|
|
|
|
+ if len(sale.Data)-1 != len(idArr) || len(idArr) != len(personArr) {
|
|
|
|
+ g.Log().Errorf(ctx, "%s 数据格式异常 %d %d %d %d", order_code, len(sale.Data)-1, len(idArr), len(personArr), len(depArr))
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+ for index, name := range personArr {
|
|
|
|
+ var (
|
|
|
|
+ depCode string
|
|
|
|
+ money int
|
|
|
|
+ )
|
|
|
|
+ if index > len(depArr)-1 {
|
|
|
|
+ depCode = depArr[0]
|
|
|
|
+ } else {
|
|
|
|
+ depCode = depArr[index]
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ dept, ok := codeDeptMapping[depCode]
|
|
|
|
+ if !ok {
|
|
|
|
+ g.Log().Errorf(ctx, "%s 未找到 %s 部门对应关系 ", order_code, depCode)
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+ for _, datum := range sale.Data {
|
|
|
|
+ if datum.Name == name {
|
|
|
|
+ money = gconv.Int(datum.Money * 100)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if money == 0 {
|
|
|
|
+ g.Log().Errorf(ctx, "%s 未找到 %s 对应业绩 ", order_code, name)
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+ insertArr = append(insertArr, map[string]interface{}{
|
|
|
|
+ "state": 1,
|
|
|
|
+ "ordercode": order_code,
|
|
|
|
+ "saler_Id": idArr[index],
|
|
|
|
+ "saler_name": name,
|
|
|
|
+ "saler_dept": dept.Name,
|
|
|
|
+ "saler_dept_id": dept.Id,
|
|
|
|
+ "change_value": money,
|
|
|
|
+ "money": money,
|
|
|
|
+ "group_uuid": uuidStr,
|
|
|
|
+ "operator": "系统自动",
|
|
|
|
+ "change_reason": reason,
|
|
|
|
+ "create_time": statistics_time,
|
|
|
|
+ "statistics_time": statistics_time,
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ dept, ok := codeDeptMapping[saleDep]
|
|
|
|
+ if !ok {
|
|
|
|
+ g.Log().Errorf(ctx, "%s 未找到 %s 部门对应关系 ", order_code, saleDep)
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+ insertArr = append(insertArr, map[string]interface{}{
|
|
|
|
+ "state": 1,
|
|
|
|
+ "ordercode": order_code,
|
|
|
|
+ "saler_Id": salesperson_id,
|
|
|
|
+ "saler_name": salesperson,
|
|
|
|
+ "saler_dept": dept.Name,
|
|
|
|
+ "saler_dept_id": dept.Id,
|
|
|
|
+ "change_value": pay_money,
|
|
|
|
+ "money": pay_money,
|
|
|
|
+ "group_uuid": uuidStr,
|
|
|
|
+ "operator": "系统自动",
|
|
|
|
+ "change_reason": reason,
|
|
|
|
+ "create_time": statistics_time,
|
|
|
|
+ "statistics_time": statistics_time,
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ //save
|
|
|
|
+ if len(insertArr) >= 100 {
|
|
|
|
+ SaveDb(ctx, insertArr)
|
|
|
|
+ insertArr = []map[string]interface{}{}
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if len(insertArr) > 0 {
|
|
|
|
+ SaveDb(ctx, insertArr)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func SaveDb(ctx context.Context, data []map[string]interface{}) {
|
|
|
|
+ _, err := g.DB().Save(ctx, "order_sale_record_release", data, 10)
|
|
|
|
+ if err != nil {
|
|
|
|
+ g.Log().Errorf(ctx, "插入数据异常 %v", err)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// WaitRecord 未生效的业绩
|
|
|
|
+func WaitRecord() {
|
|
|
|
+
|
|
|
|
+}
|