package cmd import ( "github.com/go-resty/resty/v2" "github.com/mizuki1412/go-core-kit/class/exception" "github.com/mizuki1412/go-core-kit/init/initkit" "github.com/mizuki1412/go-core-kit/library/commonkit" "github.com/mizuki1412/go-core-kit/service/configkit" "github.com/mizuki1412/go-core-kit/service/logkit" "github.com/spf13/cast" "github.com/spf13/cobra" "github.com/tidwall/gjson" "github.com/xuri/excelize/v2" "strconv" "time" ) func init() { rootCmd.AddCommand(supplyCmd) defFlagsSupply(supplyCmd) } var supplyCmd = &cobra.Command{ Use: "supply", Short: "Batch processing operations of the supply", Run: func(cmd *cobra.Command, args []string) { initkit.BindFlags(cmd) handleList() genExcelFile() }, } func defFlagsSupply(cmd *cobra.Command) { cmd.Flags().String("authorization", "", "*Specify the authorization") cmd.Flags().String("cookie", "", "*Specify the cookie") } var supplyClient = resty.New().SetRetryCount(5).SetRetryWaitTime(10 * time.Second) var exchangeArr []*exchange type exchange struct { changeNo string inProject string outProject string goodsName string unit string productsNums int realPrice float64 remainderRealAmount float64 inOut string note string } // 获取调拨列表信息 func handleList() { //supplyClient := resty.New().SetRetryCount(5).SetRetryWaitTime(10 * time.Second) resp, err := supplyClient.R(). SetHeaders(map[string]string{ "Authorization": "Bearer " + cast.ToString(configkit.Get("authorization", "")), "Content-Type": "application/json;charset=UTF-8", "Cookie": cast.ToString(configkit.Get("cookie", "")), }).SetBody(map[string]string{ //"beginTime": "2024-01-01 0:00:00", //"endTime": "2024-10-10 00:00:00", "pageNum": "1", "pageSize": "3500", "type": "out", }).Post("https://inneruscm.chinaunicom.cn:10003/api/biz-service-order/outer/v1.0/bizChange/findChangeOrderForPage") if err != nil || resp.IsError() || gjson.Get(resp.String(), "success").Bool() != true { panic(exception.New("获取调拨(调出)列表失败")) } data := gjson.Get(resp.String(), "data.records").Array() for _, v := range data { outProject := v.Get("outProject").String() inProject := v.Get("inProject").String() //if strings.Contains(outProject, "西湖宽带") || strings.Contains(outProject, "西湖公众") || strings.Contains(outProject, "西湖区公众") || strings.Contains(outProject, "西湖商务") { // if strings.Contains(inProject, "西湖宽带") || strings.Contains(inProject, "西湖公众") || strings.Contains(inProject, "西湖区公众") || strings.Contains(inProject, "西湖商务") { // continue // } //} //if strings.Contains(outProject, "西湖零星") || strings.Contains(inProject, "西湖零星") { // continue //} id := v.Get("id").String() changeNo := v.Get("changeNo").String() note := v.Get("note").String() _ = commonkit.RecoverFuncWrapper(func() { getDetail(id, "out", changeNo, inProject, outProject, note) }) } resp, err = supplyClient.R(). SetHeaders(map[string]string{ "Authorization": "Bearer " + cast.ToString(configkit.Get("authorization", "")), "Content-Type": "application/json;charset=UTF-8", "Cookie": cast.ToString(configkit.Get("cookie", "")), }).SetBody(map[string]string{ //"beginTime": "2024-01-01 0:00:00", //"endTime": "2024-10-10 00:00:00", "pageNum": "1", "pageSize": "3500", "type": "in", }).Post("https://inneruscm.chinaunicom.cn:10003/api/biz-service-order/outer/v1.0/bizChange/findChangeOrderForPage") if err != nil || resp.IsError() || gjson.Get(resp.String(), "success").Bool() != true { panic(exception.New("获取调拨(调入)列表失败")) } data = gjson.Get(resp.String(), "data.records").Array() for _, v := range data { outProject := v.Get("outProject").String() inProject := v.Get("inProject").String() //if strings.Contains(outProject, "西湖宽带") || strings.Contains(outProject, "西湖公众") || strings.Contains(outProject, "西湖区公众") || strings.Contains(outProject, "西湖商务") { // if strings.Contains(inProject, "西湖宽带") || strings.Contains(inProject, "西湖公众") || strings.Contains(inProject, "西湖区公众") || strings.Contains(inProject, "西湖商务") { // continue // } //} //if strings.Contains(outProject, "西湖零星") || strings.Contains(inProject, "西湖零星") { // continue //} id := v.Get("id").String() changeNo := v.Get("changeNo").String() note := v.Get("note").String() _ = commonkit.RecoverFuncWrapper(func() { getDetail(id, "in", changeNo, inProject, outProject, note) }) } } func getDetail(id, inOut, changeNo, inProject, outProject, note string) { //time.Sleep(200 * time.Millisecond) resp, err := supplyClient.R(). SetHeaders(map[string]string{ "Authorization": "Bearer " + cast.ToString(configkit.Get("authorization", "")), "Content-Type": "application/json;charset=UTF-8", "Cookie": cast.ToString(configkit.Get("cookie", "")), }).SetBody(map[string]string{ "id": id, "type": inOut, }).Post("https://inneruscm.chinaunicom.cn:10003/api/biz-service-order/outer/v1.0/bizChange/findChangeOrderDetailed") if err != nil || resp.IsError() || gjson.Get(resp.String(), "success").Bool() != true { panic(exception.New("获取详情失败! " + changeNo)) } detailList := gjson.Get(resp.String(), "data.changeOrderProduct").Array() for _, v := range detailList { goodsName := v.Get("goodsName").String() unit := v.Get("unit").String() productsNums := v.Get("productsNums").String() realPrice := v.Get("realPrice").String() remainderRealAmount := v.Get("remainderRealAmount").String() ex := new(exchange) ex.changeNo = changeNo ex.inProject = inProject ex.outProject = outProject ex.goodsName = goodsName ex.unit = unit ex.productsNums = cast.ToInt(productsNums) ex.realPrice = cast.ToFloat64(realPrice) ex.remainderRealAmount = cast.ToFloat64(remainderRealAmount) ex.note = note if inOut == "in" { ex.inOut = "调入" } else if inOut == "out" { ex.inOut = "调出" } else { ex.inOut = "null" } exchangeArr = append(exchangeArr, ex) } logkit.Info("获取数据...") } func genExcelFile() { f := excelize.NewFile() // 设置表头 headers := []string{"序号", "调拨单号", "物资名称", "单位", "数量", "单价", "总价", "调出项目", "调入项目", "调入调出"} for i, header := range headers { cell, _ := excelize.CoordinatesToCellName(i+1, 1) f.SetCellValue("Sheet1", cell, header) } // 填充数据 for i, ex := range exchangeArr { row := i + 2 // 数据从第2行开始 f.SetCellValue("Sheet1", "A"+strconv.Itoa(row), i+1) // 序号 f.SetCellValue("Sheet1", "B"+strconv.Itoa(row), ex.changeNo) // 调拨单号 f.SetCellValue("Sheet1", "C"+strconv.Itoa(row), ex.goodsName) // 物资名称 f.SetCellValue("Sheet1", "D"+strconv.Itoa(row), ex.unit) // 单位 f.SetCellValue("Sheet1", "E"+strconv.Itoa(row), ex.productsNums) // 数量 f.SetCellValue("Sheet1", "F"+strconv.Itoa(row), ex.realPrice) // 单价 f.SetCellValue("Sheet1", "G"+strconv.Itoa(row), ex.remainderRealAmount) // 总价 f.SetCellValue("Sheet1", "H"+strconv.Itoa(row), ex.outProject) // 调出项目 f.SetCellValue("Sheet1", "I"+strconv.Itoa(row), ex.inProject) // 调入项目 f.SetCellValue("Sheet1", "J"+strconv.Itoa(row), ex.inOut) f.SetCellValue("Sheet1", "K"+strconv.Itoa(row), ex.note) // 调入调出 } // 保存文件 if err := f.SaveAs("/Users/leo/Documents/erp调拨-all.xlsx"); err != nil { panic(exception.New("保存文件失败...")) } }