package services import ( "context" "fmt" "github.com/tealeg/xlsx" "hongze/hongze_task/models" "hongze/hongze_task/utils" "os" "strings" "time" ) //客户 func ImportCompany() { var err error defer func() { if err != nil { fmt.Println("err:", err.Error()) } }() fileName := "./company_0717.xlsx" xlFile, err := xlsx.OpenFile(fileName) if err != nil { fmt.Printf("open failed: %s\n", err) } n := 0 startDate := "" endDate := "" dateType := 3 permissionMap := make(map[string]int) permissionMap["宏观经济"] = 1 permissionMap["有色"] = 2 permissionMap["原油"] = 3 permissionMap["橡胶"] = 4 permissionMap["钢材"] = 5 permissionMap["煤炭"] = 6 permissionMap["甲醇"] = 7 permissionMap["沥青"] = 8 permissionMap["PTA"] = 9 permissionMap["MEG"] = 10 permissionMap["聚烯烃"] = 11 permissionMap["铁矿"] = 12 permissionMap["苯乙烯"] = 13 permissionMap["织造终端"] = 14 for _, sheet := range xlFile.Sheet { rows := sheet.MaxRow for i := 0; i < rows; i++ { if i > 2 { row := sheet.Row(i) cells := row.Cells realName := cells[1].String() realName = strings.Trim(realName, " ") realName = strings.Replace(realName, " ", "", -1) mobile := cells[2].String() mobile = strings.Trim(mobile, " ") mobile = strings.Replace(mobile, " ", "", -1) fmt.Println(mobile) companyName := cells[6].String() fmt.Println(companyName) companyName = strings.Trim(companyName, " ") companyName = strings.Replace(companyName, " ", "", -1) company, err := models.GetCompanyByName(companyName) if err != nil { if utils.ErrNoRow() == err.Error() { utils.FileLog.Info("客户不存在%s", companyName) fmt.Println("GetCompanyByName Err:" + err.Error()) return } else { fmt.Println("GetCompanyByName Err:" + err.Error()) return } } var companyId int if company == nil { sellsId := 0 sellsName := "" if sellsId <= 0 { fmt.Println("sellsId <= 0") return } if startDate == "" || endDate == "" { fmt.Println("startDate,endDate is empty") return } fmt.Println("客户不存在,", companyName) utils.FileLog.Info("客户不存在 %s", companyName) newCompany := new(models.Company) newCompany.CompanyName = companyName newCompany.CompanyType = 2 newCompany.Enabled = 1 newCompany.CreatedTime = time.Now() newCompany.LastUpdatedTime = time.Now() newCompany.Sort = 1 newCompany.SellsId = sellsId newCompany.Seller = sellsName newCompany.CompanyBelong = "ficc" newCompany.StartDate = startDate newCompany.EndDate = endDate newCompany.FirstStartDate = startDate newCompany.FirstEndDate = endDate newCompany.DateType = dateType newId, err := models.AddCompany(newCompany) if err != nil { fmt.Println("新增公司失败,Err:" + err.Error()) return } companyId = int(newId) //新增权限 return } companyId = company.CompanyId if companyId > 0 { wxUser, err := models.GetWxUserByMobile(mobile) if err != nil && err.Error() != utils.ErrNoRow() { fmt.Println("GetWxUserByMobile Err:" + err.Error()) return } if wxUser == nil { //用户不存在,新增 fmt.Println("AddWxUser") email := "" fmt.Println(company.CompanyId, mobile, realName) err = models.AddWxUser(company.CompanyId, mobile, realName, email) if err != nil { fmt.Println("AddWxUser Err:" + err.Error()) return } } else { fmt.Println("用户存在:mobile:", mobile) fmt.Println(company.CompanyId, mobile, realName) } fmt.Println(companyName, company.CompanyId) } else { fmt.Println("companyId<=0") } n++ } } } fmt.Println("len:", n) } //客户 func ImportCompanyUsers() { var err error defer func() { if err != nil { fmt.Println("err:", err.Error()) } }() fileName := "./company_user_1127.xlsx" xlFile, err := xlsx.OpenFile(fileName) if err != nil { fmt.Printf("open failed: %s\n", err) } n := 0 for _, sheet := range xlFile.Sheet { rows := sheet.MaxRow for i := 0; i < rows; i++ { if i > 2 { row := sheet.Row(i) cells := row.Cells realName := cells[1].String() realName = strings.Trim(realName, " ") realName = strings.Replace(realName, " ", "", -1) mobile := cells[2].String() mobile = strings.Trim(mobile, " ") mobile = strings.Replace(mobile, " ", "", -1) email := cells[5].String() email = strings.Trim(email, " ") email = strings.Replace(email, " ", "", -1) fmt.Println(mobile) var companyId int companyId = 5140 if companyId > 0 { wxUser, err := models.GetWxUserByMobile(mobile) if err != nil && err.Error() != utils.ErrNoRow() { fmt.Println("GetWxUserByMobile Err:" + err.Error()) return } if wxUser == nil { //用户不存在,新增 fmt.Println("AddWxUser") err = models.AddWxUser(companyId, mobile, realName, email) if err != nil { fmt.Println("AddWxUser Err:" + err.Error()) return } } else { fmt.Println("用户存在:mobile:", mobile, realName) } } else { fmt.Println("companyId<=0") } n++ } } } fmt.Println("len:", n) } // 导入客户信用码 func ImportCompanyCreditCode() { var err error defer func() { if err != nil { fmt.Println("err:", err.Error()) } }() fileName := "./客户社会信用码.xlsx" xlFile, err := xlsx.OpenFile(fileName) if err != nil { fmt.Printf("open failed: %s\n", err) } n := 0 for _, sheet := range xlFile.Sheet { rows := sheet.MaxRow for i := 0; i < rows; i++ { if i > 2 { row := sheet.Row(i) cells := row.Cells companyName := cells[0].String() companyName = strings.Trim(companyName, " ") companyName = strings.Replace(companyName, " ", "", -1) creditCode := cells[4].String() creditCode = strings.Trim(creditCode, " ") creditCode = strings.Replace(creditCode, " ", "", -1) industryName := cells[5].String() industryName = strings.Trim(industryName, " ") industryName = strings.Replace(industryName, " ", "", -1) companyItem, err := models.GetCompanyByName(companyName) if err != nil { fmt.Println("GetCompanyByName Err:" + err.Error()) continue } if companyItem == nil { fmt.Println("companyItem is nil") } count, err := models.GetCompanyCountByCreditCode(creditCode) if err != nil { fmt.Println("GetCompanyCountByCreditCode Err:" + err.Error()) continue } if count > 0 { fmt.Println("creditCode exist") continue } else { industryItem, err := models.GetCompanyIndustryByName(industryName) if err != nil { fmt.Println("GetCompanyIndustryByName Err:" + err.Error()) return } fmt.Println(industryItem.IndustryId, industryItem.IndustryName) err = models.ModifyCompanyCreditCode(companyItem.CompanyId, creditCode) if err != nil { fmt.Println("Err:" + err.Error()) return } err = models.ModifyCompanyIndustry(industryItem.IndustryId, companyItem.CompanyId, industryName) if err != nil { fmt.Println("Err:" + err.Error()) return } } fmt.Println(companyName, creditCode, industryName) } } } fmt.Println("len:", n) } // IncrementCompany 增量客户 func IncrementCompany(cont context.Context) (err error) { defer func() { if err != nil { fmt.Println("crete IncrementCompany err:", err.Error()) utils.FileLog.Info(fmt.Sprintf("crete IncrementCompany err: %s", err.Error())) } }() //endTime := time.Now().Format(utils.FormatDateTime) startTime := time.Now().AddDate(0, 0, -7).Format(utils.FormatDate) endTime := time.Now().Format(utils.FormatDate) _, items, err := models.GetIncrementCompanyProductUpdateLogList(startTime, endTime) if err != nil { return } title := "增量客户记录" //创建excel file := xlsx.NewFile() sheet, err := file.AddSheet(title) if err != nil { return err } //标头 rowTitle := sheet.AddRow() cellA := rowTitle.AddCell() cellA.Value = "客户名称" cellB := rowTitle.AddCell() cellB.Value = "社会信用码" cellC := rowTitle.AddCell() cellC.Value = "客户类型" cellD := rowTitle.AddCell() cellD.Value = "客户状态" cellE := rowTitle.AddCell() cellE.Value = "所属销售" for _, item := range items { row := sheet.AddRow() cellA := row.AddCell() cellA.Value = item.CompanyName cellB := row.AddCell() cellB.Value = item.CreditCode cellC := row.AddCell() productName := `` switch item.ProductId { case 1: productName = "ficc" case 2: productName = `权益` } cellC.Value = productName cellD := row.AddCell() cellD.Value = item.CurrStatus cellE := row.AddCell() cellE.Value = item.SellerName } savePath := "increment_company_" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx" err = file.Save("./" + savePath) if err != nil { return } //发送邮件 fmt.Println("start send email") //收取邮件的人 emailUser := "kwhuang@hzinsights.com;csun@hzinsights.com;sphuang@hzinsights.com;pyan@hzinsights.com" sendResult := utils.SendEmailByHongze(title, "增量客户记录,你好,上周的增量客户记录列表见附件。", emailUser, savePath, title+".xlsx") if sendResult { os.Remove(savePath) } //fmt.Println("send result:", sendResult) fmt.Println("end send email") return nil }