supplier.go 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/issue9/conv"
  6. log "github.com/sirupsen/logrus"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "youngee_b_api/db"
  11. "youngee_b_api/model/gorm_model"
  12. "youngee_b_api/model/http_model"
  13. )
  14. var Supplier *supplier
  15. type supplier struct {
  16. }
  17. // CreateSupplier 创建younggee平台用户账号与服务商用户信息
  18. func (*supplier) CreateSupplier(ctx context.Context, phone string) (*http_model.RegisterData, error) {
  19. // 1. 创建YG平台用户
  20. userInfo := gorm_model.YounggeeUser{
  21. Phone: phone,
  22. User: "1003",
  23. Username: phone,
  24. Password: "1003",
  25. RealName: "",
  26. Role: "6",
  27. Email: "",
  28. LastLoginTime: time.Now().UTC().Local(),
  29. }
  30. userId, createUserErr := db.CreateUser(ctx, userInfo)
  31. if createUserErr != nil {
  32. log.Infof("[CreateSupplierUser] fail,err:%+v", createUserErr)
  33. return nil, createUserErr
  34. } else {
  35. // 2. 创建服务商信息
  36. supplierInfo := &gorm_model.YoungeeSupplier{
  37. SupplierName: phone,
  38. PhoneNumber: phone,
  39. UserId: *userId,
  40. }
  41. supplierId, createSupplierErr := db.CreateSupplier(ctx, *supplierInfo)
  42. fmt.Println(supplierId)
  43. if createSupplierErr != nil {
  44. log.Infof("[CreateSupplierUser] fail,err:%+v", createSupplierErr)
  45. return nil, createSupplierErr
  46. }
  47. res := &http_model.RegisterData{
  48. UserID: *userId,
  49. }
  50. return res, nil
  51. }
  52. }
  53. // GetSupplierIncomeList 查询服务商收入列表
  54. func (*supplier) GetSupplierIncomeList(ctx context.Context, req *http_model.FullSProjectIncomeListRequest) (*http_model.FullSProjectIncomeData, error) {
  55. var sProjectIncomeData *http_model.FullSProjectIncomeData
  56. sProjectIncomeData = &http_model.FullSProjectIncomeData{}
  57. // 1. 查询
  58. supplierIncome, total, err := db.GetSupplierIncomeList(ctx, req.PageSize, req.PageNum, req.SupplierId, req.IncomeStatus)
  59. if err != nil {
  60. return nil, nil
  61. }
  62. // 2. 补充种草/本地生活任务信息
  63. if supplierIncome != nil {
  64. sProjectIncomeData.Total = total
  65. for _, income := range supplierIncome {
  66. // 2.1. 种草任务基本信息
  67. if income.IncomeType == 1 {
  68. var sProjectInfo *http_model.FullSProjectIncomeListResponse
  69. sProjectInfo = &http_model.FullSProjectIncomeListResponse{}
  70. sProjectData, sProjectErr := db.GetSProjectDetail(ctx, income.SProjectID)
  71. if sProjectErr != nil {
  72. log.Infof("[GetSProjectDetail] fail,err:%+v", sProjectErr)
  73. return nil, sProjectErr
  74. }
  75. if sProjectData != nil {
  76. sProjectInfo.SProjectId = sProjectData.SProjectId
  77. sProjectInfo.IncomeId = income.IncomeID
  78. sProjectInfo.IncomeType = income.IncomeType
  79. sProjectInfo.SProjectId = sProjectData.SProjectId
  80. sProjectInfo.ProjectName = sProjectData.ProjectName
  81. sProjectInfo.ProjectPlatform = sProjectData.ProjectPlatform
  82. sProjectInfo.ServiceCharge = sProjectData.ServiceCharge
  83. sProjectInfo.ServiceChargeSettle = sProjectData.ServiceChargeSettle
  84. sProjectInfo.RecruitNum = sProjectData.RecruitNum
  85. sProjectInfo.SettleNum = sProjectData.SettleNum
  86. sProjectInfo.FinishTime = conv.MustString(sProjectData.FinishTime)
  87. // 2.2. 商品基本信息
  88. productInfo, productErr := db.GetProductByID(ctx, sProjectData.ProductId)
  89. if productErr != nil {
  90. log.Infof("[GetProductByID] fail,err:%+v", productErr)
  91. return nil, productErr
  92. }
  93. if productInfo != nil {
  94. sProjectInfo.ProductName = productInfo.ProductName
  95. sProjectInfo.ProductPrice = productInfo.ProductPrice
  96. }
  97. // 2.3. 商品图片信息
  98. productPhotoInfo, productPhotoErr := db.GetProductPhotoByProductID(ctx, sProjectData.ProductId)
  99. if productPhotoErr != nil {
  100. log.Infof("[GetProductPhotoByProductID] fail,err:%+v", productPhotoErr)
  101. return nil, productPhotoErr
  102. }
  103. if productPhotoInfo != nil {
  104. for _, photo := range productPhotoInfo {
  105. fmt.Println(photo)
  106. if photo.Symbol == 1 {
  107. sProjectInfo.ProductPhotoSymbol = 1
  108. sProjectInfo.ProductPhotoUrl = photo.PhotoUrl
  109. sProjectInfo.ProductPhotoUid = photo.PhotoUid
  110. }
  111. }
  112. }
  113. }
  114. }
  115. // 2.1. 本地生活任务基本信息
  116. if income.IncomeType == 2 {
  117. var sLocalInfo *http_model.FullSProjectIncomeListResponse
  118. sLocalInfo = &http_model.FullSProjectIncomeListResponse{}
  119. sLocalData, sLocalErr := db.GetSLocalLifeDetail(ctx, income.SLocalID)
  120. if sLocalErr != nil {
  121. log.Infof("[GetSLocalLifeDetail] fail,err:%+v", sLocalErr)
  122. return nil, sLocalErr
  123. }
  124. if sLocalData != nil {
  125. sLocalInfo.IncomeId = income.IncomeID
  126. sLocalInfo.IncomeType = income.IncomeType
  127. sLocalInfo.SLocalId = sLocalData.SLocalId
  128. sLocalInfo.LocalName = sLocalData.LocalName
  129. sLocalInfo.LocalPlatform = sLocalData.LocalPlatform
  130. sLocalInfo.ServiceCharge = sLocalData.ServiceCharge
  131. sLocalInfo.ServiceChargeSettle = sLocalData.ServiceChargeSettle
  132. sLocalInfo.RecruitNum = sLocalData.RecruitNum
  133. sLocalInfo.SettleNum = sLocalData.SettleNum
  134. sLocalInfo.FinishTime = conv.MustString(sLocalData.FinishTime)
  135. // 2.2. 门店基本信息
  136. storeInfo, storeErr := db.FindStoreById(ctx, sLocalData.StoreId)
  137. if storeErr != nil {
  138. log.Infof("[GetProductByID] fail,err:%+v", storeErr)
  139. return nil, storeErr
  140. }
  141. if storeInfo != nil {
  142. sLocalInfo.StoreName = storeInfo.StoreName
  143. }
  144. // 2.3. 门店图片信息
  145. storePhotoInfo, storePhotoErr := db.GetStorePhotoByStoreID(ctx, sLocalData.StoreId)
  146. if storePhotoErr != nil {
  147. log.Infof("[GetProductPhotoByProductID] fail,err:%+v", storePhotoErr)
  148. return nil, storePhotoErr
  149. }
  150. if storePhotoInfo != nil {
  151. for _, photo := range storePhotoInfo {
  152. fmt.Println(photo)
  153. if photo.Symbol == 1 {
  154. sLocalInfo.StoreMainPhotoSymbol = 1
  155. sLocalInfo.StoreMainPhotoUrl = photo.PhotoUrl
  156. sLocalInfo.StoreMainPhotoUid = photo.PhotoUid
  157. }
  158. }
  159. }
  160. }
  161. }
  162. }
  163. } else {
  164. sProjectIncomeData.Total = 0
  165. }
  166. return sProjectIncomeData, nil
  167. }
  168. // CreateSupplierInvoice 创建服务商发票
  169. func (*supplier) CreateSupplierInvoice(ctx context.Context, req *http_model.CreateSupplierInvoiceRequest) error {
  170. // 1. 数据转换
  171. var invoiceData *gorm_model.YounggeeSupplierInvoice
  172. invoiceData = &gorm_model.YounggeeSupplierInvoice{
  173. SupplierId: req.SupplierId,
  174. InvoiceStatus: 1,
  175. IncomeIds: req.IncomeIds,
  176. }
  177. if req.SOperatorType == 1 {
  178. invoiceData.SOperatorType = 1
  179. invoiceData.SOperator = req.SupplierId
  180. } else if req.SOperatorType == 2 {
  181. invoiceData.SOperatorType = 2
  182. invoiceData.SOperator = req.SubAccountId
  183. }
  184. // 2. 插入数据库
  185. err := db.CreateSupplierInvoice(ctx, invoiceData)
  186. if err != nil {
  187. return err
  188. }
  189. return nil
  190. }
  191. // UpdateSupplierInvoice 更新服务商发票
  192. func (*supplier) UpdateSupplierInvoice(ctx context.Context, req *http_model.UpdateSupplierInvoiceRequest) error {
  193. // 1. 数据转换
  194. var invoiceData *gorm_model.YounggeeSupplierInvoice
  195. var currentTime time.Time
  196. currentTime = time.Now()
  197. invoiceData = &gorm_model.YounggeeSupplierInvoice{
  198. InvoiceId: req.InvoiceId,
  199. InvoiceUrl: req.InvoiceUrl,
  200. Company: req.Company,
  201. UploadInvoiceTime: &currentTime,
  202. }
  203. // 2. 更新
  204. err := db.UpdateSupplierInvoice(ctx, invoiceData)
  205. if err != nil {
  206. return err
  207. }
  208. // 3. 更新服务商收入状态
  209. incomeIds, dbErr := db.GetIncomeIdsByInvoiceId(ctx, req.InvoiceId)
  210. if dbErr != nil {
  211. return dbErr
  212. }
  213. if incomeIds == "" {
  214. return nil
  215. }
  216. strSlice := strings.Split(incomeIds, ",")
  217. intSlice := make([]int, len(strSlice))
  218. for i, s := range strSlice {
  219. num, err := strconv.Atoi(s)
  220. if err != nil {
  221. fmt.Println("转换错误:", err)
  222. return err
  223. }
  224. intSlice[i] = num
  225. }
  226. updateSupplierIncomeErr := db.UpdateSupplierIncomeStatus(ctx, intSlice, 3)
  227. if updateSupplierIncomeErr != nil {
  228. return updateSupplierIncomeErr
  229. }
  230. return nil
  231. }
  232. // UpdateSupplierIncomeStatus 修改服务商收入状态
  233. func (*supplier) UpdateSupplierIncomeStatus(ctx context.Context, incomeIds string, incomeStatus int) error {
  234. // 1. 转换incomeIds为数组
  235. strSlice := strings.Split(incomeIds, ",")
  236. intSlice := make([]int, len(strSlice))
  237. for i, s := range strSlice {
  238. num, err := strconv.Atoi(s)
  239. if err != nil {
  240. fmt.Println("转换错误:", err)
  241. return err
  242. }
  243. intSlice[i] = num
  244. }
  245. // 2. 修改数据库中数据
  246. updateSupplierIncomeErr := db.UpdateSupplierIncomeStatus(ctx, intSlice, incomeStatus)
  247. if updateSupplierIncomeErr != nil {
  248. return updateSupplierIncomeErr
  249. }
  250. return nil
  251. }
  252. // GetSupplierInvoiceList 查找服务商发票列表
  253. func (*supplier) GetSupplierInvoiceList(ctx context.Context, req *http_model.SupplierInvoiceListRequest) (*http_model.SupplierInvoiceListData, error) {
  254. // 1. 查询服务商发票信息
  255. supplierInvoiceList, total, err := db.GetInvoiceListBySupplierId(ctx, req.SupplierId, req.InvoiceStatus, 4, req.PageSize, req.PageNum)
  256. if err != nil {
  257. return nil, err
  258. }
  259. // 2. 根据发票中的incomeIds去查找任务及其服务费收入
  260. var supplierInvoiceData *http_model.SupplierInvoiceListData
  261. supplierInvoiceData = &http_model.SupplierInvoiceListData{}
  262. for _, supplierInvoice := range supplierInvoiceList {
  263. var supplierInvoiceInfo *http_model.SupplierInvoiceInfo
  264. supplierInvoiceInfo = &http_model.SupplierInvoiceInfo{}
  265. // 2.1. 基础信息填入
  266. supplierInvoiceInfo.UploadInvoiceTime = supplierInvoice.UploadInvoiceTime
  267. supplierInvoiceInfo.InvoiceUrl = supplierInvoice.InvoiceUrl
  268. supplierInvoiceInfo.AgreeTime = supplierInvoice.AgreeTime
  269. supplierInvoiceInfo.RejectTime = supplierInvoice.RejectTime
  270. supplierInvoiceInfo.Company = supplierInvoice.Company
  271. supplierInvoiceInfo.SOperator = supplierInvoice.SOperator
  272. supplierInvoiceInfo.FailReason = supplierInvoice.FailReason
  273. // 2.2. 任务及其收入信息填入
  274. incomeIds := supplierInvoice.IncomeIds
  275. strSlice := strings.Split(incomeIds, ",")
  276. intSlice := make([]int, len(strSlice))
  277. for i, s := range strSlice {
  278. num, err := strconv.Atoi(s)
  279. if err != nil {
  280. fmt.Println("转换错误:", err)
  281. return nil, err
  282. }
  283. intSlice[i] = num
  284. }
  285. for _, incomeId := range intSlice {
  286. var sTaskInfo *http_model.STaskInfo
  287. sTaskInfo = &http_model.STaskInfo{}
  288. currIncome, incomeErr := db.GetIncomeInfoByIncomeId(ctx, incomeId)
  289. if incomeErr != nil {
  290. return nil, incomeErr
  291. }
  292. sTaskInfo.ServiceCharge = currIncome.ServiceChargeSettle
  293. supplierInvoiceInfo.Amount += currIncome.ServiceChargeSettle
  294. if currIncome.IncomeType == 1 {
  295. sTaskInfo.Id = currIncome.SProjectID
  296. } else if currIncome.IncomeType == 3 {
  297. sTaskInfo.Id = currIncome.SLocalID
  298. }
  299. supplierInvoiceInfo.STaskInfo = append(supplierInvoiceInfo.STaskInfo, sTaskInfo)
  300. }
  301. supplierInvoiceData.SupplierInvoiceList = append(supplierInvoiceData.SupplierInvoiceList, supplierInvoiceInfo)
  302. }
  303. supplierInvoiceData.Total = total
  304. return supplierInvoiceData, nil
  305. }
  306. // GetSupplierToWithdrawList 服务商待提现列表
  307. func (*supplier) GetSupplierToWithdrawList(ctx context.Context, req *http_model.SupplierToWithdrawListRequest) (*http_model.SupplierToWithdrawListData, error) {
  308. // 1. 判断服务商类型
  309. var supplierInvoiceData *http_model.SupplierToWithdrawListData
  310. supplierInvoiceData = &http_model.SupplierToWithdrawListData{}
  311. supplierInfo, supplierErr := db.GetSupplierById(ctx, req.SupplierId)
  312. if supplierErr != nil {
  313. return nil, supplierErr
  314. }
  315. if supplierInfo != nil {
  316. // 企业服务商
  317. if supplierInfo.SupplierType == 2 {
  318. // 查询企业服务商发票信息
  319. supplierInvoiceList, total, supplierInvoiceErr := db.GetInvoiceListBySupplierId(ctx, req.SupplierId, 3, 1, req.PageSize, req.PageNum)
  320. if supplierInvoiceErr != nil {
  321. return nil, supplierInvoiceErr
  322. }
  323. if supplierInvoiceList != nil {
  324. for _, supplierInvoice := range supplierInvoiceList {
  325. // 2. 根据发票中的incomeIds去查找任务及其服务费收入
  326. var supplierInvoiceInfo *http_model.SupplierToWithdrawInfo
  327. supplierInvoiceInfo = &http_model.SupplierToWithdrawInfo{}
  328. // supplierInvoiceInfo.Amount = 0.0
  329. // 2.1. 基础信息填入
  330. supplierInvoiceInfo.AgreeTime = supplierInvoice.AgreeTime
  331. supplierInvoiceInfo.Company = supplierInvoice.Company
  332. supplierInvoiceInfo.SupplierType = supplierInfo.SupplierType
  333. // 2.2. 任务及其收入信息填入
  334. incomeIds := supplierInvoice.IncomeIds
  335. strSlice := strings.Split(incomeIds, ",")
  336. intSlice := make([]int, len(strSlice))
  337. for i, s := range strSlice {
  338. num, err := strconv.Atoi(s)
  339. if err != nil {
  340. fmt.Println("转换错误:", err)
  341. return nil, err
  342. }
  343. intSlice[i] = num
  344. }
  345. for _, incomeId := range intSlice {
  346. var sTaskInfo *http_model.STaskInfo
  347. sTaskInfo = &http_model.STaskInfo{}
  348. currIncome, incomeErr := db.GetIncomeInfoByIncomeId(ctx, incomeId)
  349. if incomeErr != nil {
  350. return nil, incomeErr
  351. }
  352. sTaskInfo.ServiceCharge = currIncome.ServiceChargeSettle
  353. supplierInvoiceInfo.Amount += currIncome.ServiceChargeSettle
  354. if currIncome.IncomeType == 1 {
  355. sTaskInfo.Id = currIncome.SProjectID
  356. } else if currIncome.IncomeType == 3 {
  357. sTaskInfo.Id = currIncome.SLocalID
  358. }
  359. supplierInvoiceInfo.STaskInfo = append(supplierInvoiceInfo.STaskInfo, sTaskInfo)
  360. }
  361. supplierInvoiceData.ToWithdrawList = append(supplierInvoiceData.ToWithdrawList, supplierInvoiceInfo)
  362. }
  363. supplierInvoiceData.Total = total
  364. }
  365. } else if supplierInfo.SupplierType == 1 {
  366. // 个人服务商
  367. // 查询个人服务商收入信息
  368. supplierIncomeList, supplierIncomeTotal, supplierIncomeErr := db.GetSupplierIncomeList(ctx, req.PageSize, req.PageNum, req.SupplierId, 5)
  369. if supplierIncomeErr != nil {
  370. return nil, supplierIncomeErr
  371. }
  372. if supplierIncomeList != nil {
  373. supplierInvoiceData.Total = supplierIncomeTotal
  374. for _, supplierIncome := range supplierIncomeList {
  375. var supplierInvoiceInfo *http_model.SupplierToWithdrawInfo
  376. supplierInvoiceInfo = &http_model.SupplierToWithdrawInfo{}
  377. supplierInvoiceInfo.SupplierType = supplierInfo.SupplierType
  378. supplierInvoiceInfo.IncomeId = supplierIncome.IncomeID
  379. supplierInvoiceInfo.Amount = supplierIncome.ServiceChargeSettle
  380. supplierInvoiceInfo.AmountActual = supplierIncome.ServiceChargeSettle * 0.05
  381. var sTaskInfo *http_model.STaskInfo
  382. sTaskInfo = &http_model.STaskInfo{}
  383. sTaskInfo.ServiceCharge = supplierIncome.ServiceChargeSettle
  384. if supplierIncome.IncomeType == 1 {
  385. sTaskInfo.Id = supplierIncome.SProjectID
  386. } else if supplierIncome.IncomeType == 3 {
  387. sTaskInfo.Id = supplierIncome.SLocalID
  388. }
  389. supplierInvoiceInfo.STaskInfo = append(supplierInvoiceInfo.STaskInfo, sTaskInfo)
  390. supplierInvoiceData.ToWithdrawList = append(supplierInvoiceData.ToWithdrawList, supplierInvoiceInfo)
  391. }
  392. }
  393. }
  394. }
  395. return supplierInvoiceData, nil
  396. }
  397. // CreateSupplierWithdraw 创建服务商提现信息
  398. func (*supplier) CreateSupplierWithdraw(ctx context.Context, req *http_model.CreateSupplierWithdrawRequest) error {
  399. var supplierWithdrawInfoList []*gorm_model.YounggeeSupplierWithdraw
  400. // 1. 判断服务商类型
  401. supplierInfo, supplierErr := db.GetSupplierById(ctx, req.SupplierId)
  402. if supplierErr != nil {
  403. return supplierErr
  404. }
  405. if supplierInfo != nil {
  406. if supplierInfo.SupplierType == 1 {
  407. // 1.1. 个人服务商
  408. supplierPaymentInfo, supplierPaymentErr := db.GetSupplierPaymentInfoById(ctx, supplierInfo.SupplierId)
  409. if supplierPaymentErr != nil {
  410. return supplierPaymentErr
  411. }
  412. if supplierPaymentInfo != nil {
  413. for _, withdrawInfo := range req.IncomeIds {
  414. var supplierWithdrawInfo *gorm_model.YounggeeSupplierWithdraw
  415. supplierWithdrawInfo = &gorm_model.YounggeeSupplierWithdraw{}
  416. // 1.2.1. 接口传入信息填入
  417. supplierWithdrawInfo.SupplierId = req.SupplierId
  418. supplierWithdrawInfo.WithdrawStatus = 2
  419. supplierWithdrawInfo.BankName = supplierPaymentInfo.BankName
  420. supplierWithdrawInfo.BankNumber = supplierPaymentInfo.BankNumber
  421. var currentTime time.Time
  422. currentTime = time.Now()
  423. supplierWithdrawInfo.SupplyTime = &currentTime
  424. supplierWithdrawInfo.WithdrawAmount = 0.0
  425. // 1.2.2. 查找服务商信息填入
  426. supplierWithdrawInfo.Name = supplierInfo.Name
  427. supplierWithdrawInfo.Phone = supplierPaymentInfo.Phone
  428. // 1.2.3. 收入信息填入
  429. currIncome, incomeInfoErr := db.GetIncomeInfoByIncomeId(ctx, withdrawInfo)
  430. if incomeInfoErr != nil {
  431. return incomeInfoErr
  432. }
  433. if currIncome != nil {
  434. supplierWithdrawInfo.WithdrawAmount += currIncome.ServiceChargeSettle
  435. }
  436. supplierWithdrawInfo.AmountPayable = supplierWithdrawInfo.WithdrawAmount
  437. supplierWithdrawInfoList = append(supplierWithdrawInfoList, supplierWithdrawInfo)
  438. }
  439. }
  440. } else if supplierInfo.SupplierType == 2 {
  441. // 1.2. 机构服务商
  442. supplierPaymentInfo, supplierPaymentErr := db.GetSupplierPaymentInfoById(ctx, supplierInfo.SupplierId)
  443. if supplierPaymentErr != nil {
  444. return supplierPaymentErr
  445. }
  446. if supplierPaymentInfo != nil {
  447. for _, withdrawInfo := range req.InvoiceIds {
  448. var supplierWithdrawInfo *gorm_model.YounggeeSupplierWithdraw
  449. supplierWithdrawInfo = &gorm_model.YounggeeSupplierWithdraw{}
  450. // 1.2.1. 接口传入信息填入
  451. supplierWithdrawInfo.SupplierId = req.SupplierId
  452. supplierWithdrawInfo.WithdrawStatus = 2
  453. supplierWithdrawInfo.BankName = supplierPaymentInfo.BankName
  454. supplierWithdrawInfo.BankNumber = supplierPaymentInfo.BankNumber
  455. var currentTime time.Time
  456. currentTime = time.Now()
  457. supplierWithdrawInfo.SupplyTime = &currentTime
  458. supplierWithdrawInfo.WithdrawAmount = 0.0
  459. // 1.2.2. 查找服务商信息填入
  460. supplierWithdrawInfo.Name = supplierInfo.Name
  461. supplierWithdrawInfo.Phone = supplierInfo.PhoneNumber
  462. supplierWithdrawInfo.Company = supplierInfo.CompanyName
  463. // 1.2.3. 收入信息填入
  464. incomeIds, incomeErr := db.GetIncomeIdsByInvoiceId(ctx, withdrawInfo)
  465. if incomeErr != nil {
  466. return incomeErr
  467. }
  468. if incomeIds != "" {
  469. strSlice := strings.Split(incomeIds, ",")
  470. intSlice := make([]int, len(strSlice))
  471. for i, s := range strSlice {
  472. num, err := strconv.Atoi(s)
  473. if err != nil {
  474. fmt.Println("转换错误:", err)
  475. return err
  476. }
  477. intSlice[i] = num
  478. }
  479. for _, incomeId := range intSlice {
  480. currIncome, incomeInfoErr := db.GetIncomeInfoByIncomeId(ctx, incomeId)
  481. if incomeInfoErr != nil {
  482. return incomeInfoErr
  483. }
  484. if currIncome != nil {
  485. supplierWithdrawInfo.WithdrawAmount += currIncome.ServiceChargeSettle
  486. }
  487. }
  488. supplierWithdrawInfo.AmountPayable = supplierWithdrawInfo.WithdrawAmount
  489. }
  490. supplierWithdrawInfoList = append(supplierWithdrawInfoList, supplierWithdrawInfo)
  491. }
  492. }
  493. }
  494. }
  495. // 2. 数据库插入
  496. err := db.CreateSupplierWithdraw(ctx, supplierWithdrawInfoList)
  497. if err != nil {
  498. return err
  499. }
  500. return nil
  501. }
  502. // GetSupplierWithdrawList 服务商提现列表
  503. func (*supplier) GetSupplierWithdrawList(ctx context.Context, req *http_model.SupplierWithdrawListRequest) (*http_model.SupplierWithdrawListData, error) {
  504. var supplierWithdrawListData *http_model.SupplierWithdrawListData
  505. supplierWithdrawListData = &http_model.SupplierWithdrawListData{}
  506. // 1. 根据服务商ID和提现状态去查找提现信息列表
  507. supplierWithdrawList, total, supplierWithdrawErr := db.GetSupplierWithdrawList(ctx, req.PageSize, req.PageNum, req.SupplierId, req.WithdrawStatus)
  508. if supplierWithdrawErr != nil {
  509. return nil, supplierWithdrawErr
  510. }
  511. if supplierWithdrawList != nil {
  512. supplierWithdrawListData.Total = total
  513. supplierInfo, supplierErr := db.GetSupplierById(ctx, req.SupplierId)
  514. if supplierErr != nil {
  515. return nil, supplierErr
  516. }
  517. if supplierInfo != nil {
  518. if supplierInfo.SupplierId == 1 {
  519. // 2. 个人服务商
  520. var supplierWithdrawInfo *http_model.SupplierWithdrawInfo
  521. supplierWithdrawInfo = &http_model.SupplierWithdrawInfo{}
  522. for _, withdrawInfo := range supplierWithdrawList {
  523. IncomeId, IncomeIdErr := strconv.Atoi(withdrawInfo.IncomeIds)
  524. if IncomeIdErr != nil {
  525. return nil, IncomeIdErr
  526. }
  527. incomeInfo, incomeErr := db.GetIncomeInfoByIncomeId(ctx, IncomeId)
  528. if incomeErr != nil {
  529. return nil, incomeErr
  530. }
  531. if incomeInfo != nil {
  532. var sTaskInfo *http_model.STaskInfo
  533. sTaskInfo = &http_model.STaskInfo{}
  534. if incomeInfo.IncomeType == 1 {
  535. sTaskInfo.Id = incomeInfo.SProjectID
  536. sTaskInfo.ServiceCharge = incomeInfo.ServiceChargeSettle
  537. } else if incomeInfo.IncomeType == 3 {
  538. sTaskInfo.Id = incomeInfo.SLocalID
  539. sTaskInfo.ServiceCharge = incomeInfo.ServiceChargeSettle
  540. }
  541. supplierWithdrawInfo.STaskInfo = append(supplierWithdrawInfo.STaskInfo, sTaskInfo)
  542. }
  543. supplierWithdrawInfo.SupplierType = 1
  544. supplierWithdrawInfo.SupplierWithdrawId = withdrawInfo.SupplierWithdrawId
  545. supplierWithdrawInfo.WithdrawAmount = withdrawInfo.WithdrawAmount
  546. supplierWithdrawInfo.AmountPayable = withdrawInfo.AmountPayable
  547. supplierWithdrawInfo.Name = withdrawInfo.Name
  548. supplierWithdrawInfo.IDNumber = withdrawInfo.IDNumber
  549. supplierWithdrawInfo.BankName = withdrawInfo.BankName
  550. supplierWithdrawInfo.BankNumber = withdrawInfo.BankNumber
  551. supplierWithdrawInfo.Phone = withdrawInfo.Phone
  552. // supplierWithdrawInfo.Company = withdrawInfo.Company
  553. supplierWithdrawInfo.SupplyTime = conv.MustString(withdrawInfo.SupplyTime)
  554. supplierWithdrawInfo.AgreeTime = conv.MustString(withdrawInfo.AgreeTime)
  555. supplierWithdrawInfo.RejectTime = conv.MustString(withdrawInfo.RejectTime)
  556. supplierWithdrawInfo.FailReason = withdrawInfo.FailReason
  557. supplierWithdrawListData.WithdrawList = append(supplierWithdrawListData.WithdrawList, supplierWithdrawInfo)
  558. }
  559. } else if supplierInfo.SupplierId == 2 {
  560. // 3. 企业服务商
  561. for _, withdrawInfo := range supplierWithdrawList {
  562. var supplierWithdrawInfo *http_model.SupplierWithdrawInfo
  563. supplierWithdrawInfo = &http_model.SupplierWithdrawInfo{}
  564. InvoiceId, InvoiceIdErr := strconv.Atoi(withdrawInfo.InvoiceIds)
  565. if InvoiceIdErr != nil {
  566. return nil, InvoiceIdErr
  567. }
  568. incomeIds, incomeIdsErr := db.GetIncomeIdsByInvoiceId(ctx, InvoiceId)
  569. if incomeIdsErr != nil {
  570. return nil, incomeIdsErr
  571. }
  572. if incomeIds != "" {
  573. strSlice := strings.Split(incomeIds, ",")
  574. intSlice := make([]int, len(strSlice))
  575. for i, s := range strSlice {
  576. num, err := strconv.Atoi(s)
  577. if err != nil {
  578. fmt.Println("转换错误:", err)
  579. return nil, err
  580. }
  581. intSlice[i] = num
  582. }
  583. for _, incomeId := range intSlice {
  584. var sTaskInfo *http_model.STaskInfo
  585. sTaskInfo = &http_model.STaskInfo{}
  586. currIncome, incomeInfoErr := db.GetIncomeInfoByIncomeId(ctx, incomeId)
  587. if incomeInfoErr != nil {
  588. return nil, incomeInfoErr
  589. }
  590. if currIncome != nil {
  591. if currIncome.IncomeType == 1 {
  592. sTaskInfo.Id = currIncome.SProjectID
  593. sTaskInfo.ServiceCharge = currIncome.ServiceChargeSettle
  594. } else if currIncome.IncomeType == 3 {
  595. sTaskInfo.Id = currIncome.SLocalID
  596. sTaskInfo.ServiceCharge = currIncome.ServiceChargeSettle
  597. }
  598. supplierWithdrawInfo.STaskInfo = append(supplierWithdrawInfo.STaskInfo, sTaskInfo)
  599. }
  600. }
  601. }
  602. supplierWithdrawInfo.SupplierType = 1
  603. supplierWithdrawInfo.SupplierWithdrawId = withdrawInfo.SupplierWithdrawId
  604. supplierWithdrawInfo.WithdrawAmount = withdrawInfo.WithdrawAmount
  605. supplierWithdrawInfo.AmountPayable = withdrawInfo.AmountPayable
  606. supplierWithdrawInfo.Name = withdrawInfo.Name
  607. supplierWithdrawInfo.IDNumber = withdrawInfo.IDNumber
  608. supplierWithdrawInfo.BankName = withdrawInfo.BankName
  609. supplierWithdrawInfo.BankNumber = withdrawInfo.BankNumber
  610. supplierWithdrawInfo.Phone = withdrawInfo.Phone
  611. supplierWithdrawInfo.Company = withdrawInfo.Company
  612. supplierWithdrawInfo.SupplyTime = conv.MustString(withdrawInfo.SupplyTime)
  613. supplierWithdrawInfo.AgreeTime = conv.MustString(withdrawInfo.AgreeTime)
  614. supplierWithdrawInfo.RejectTime = conv.MustString(withdrawInfo.RejectTime)
  615. supplierWithdrawInfo.FailReason = withdrawInfo.FailReason
  616. supplierWithdrawListData.WithdrawList = append(supplierWithdrawListData.WithdrawList, supplierWithdrawInfo)
  617. }
  618. }
  619. }
  620. }
  621. return supplierWithdrawListData, nil
  622. }
  623. func (*supplier) GetSupplierAmountBillList(ctx context.Context, req *http_model.SupplierAmountBillListRequest) (*http_model.SupplierAmountBillData, error) {
  624. return nil, nil
  625. }
  626. // GetManageInvoiceInfo 查找后台回票信息
  627. func (*supplier) GetManageInvoiceInfo(ctx context.Context, req *http_model.ManageInvoiceInfoRequest) (*http_model.ManageInvoiceInfoData, error) {
  628. var invoiceInfo *http_model.ManageInvoiceInfoData
  629. invoiceInfo = &http_model.ManageInvoiceInfoData{}
  630. manageInvoiceInfo, manageInvoiceErr := db.GetManageInvoice(ctx)
  631. if manageInvoiceErr != nil {
  632. return nil, manageInvoiceErr
  633. }
  634. if manageInvoiceInfo != nil {
  635. invoiceInfo.InvoiceInfoID = manageInvoiceInfo.InvoiceInfoID
  636. invoiceInfo.Address = manageInvoiceInfo.Address
  637. invoiceInfo.Phone = manageInvoiceInfo.Phone
  638. invoiceInfo.BankNumber = manageInvoiceInfo.BankNumber
  639. invoiceInfo.EnterpriseName = manageInvoiceInfo.EnterpriseName
  640. invoiceInfo.ProjectNameToChoose = manageInvoiceInfo.ProjectNameToChoose
  641. invoiceInfo.BankName = manageInvoiceInfo.BankName
  642. }
  643. return invoiceInfo, nil
  644. }
  645. // GetWithdrawAmount 查找可提现、提现中、已提现金额
  646. func (*supplier) GetWithdrawAmount(ctx context.Context, req *http_model.WithdrawAmountRequest) (*http_model.WithdrawAmountData, error) {
  647. var amountInfo *http_model.WithdrawAmountData
  648. amountInfo = &http_model.WithdrawAmountData{}
  649. amountInfo.WithdrawAble = 0.0
  650. amountInfo.PendingWithdraw = 0.0
  651. amountInfo.Withdrawn = 0.0
  652. // 可提现
  653. platformConfirmingIncome, platformConfirmingErr := db.GetFullSupplierIncomeList(ctx, req.SupplierId, 5)
  654. if platformConfirmingErr != nil {
  655. return nil, platformConfirmingErr
  656. }
  657. if platformConfirmingIncome != nil {
  658. for _, income := range platformConfirmingIncome {
  659. amountInfo.WithdrawAble += income.ServiceChargeSettle
  660. }
  661. }
  662. // 提现中
  663. pendingWithdrawIncome, pendingWithdrawErr := db.GetFullSupplierIncomeList(ctx, req.SupplierId, 7)
  664. if pendingWithdrawErr != nil {
  665. return nil, pendingWithdrawErr
  666. }
  667. if pendingWithdrawIncome != nil {
  668. for _, income := range pendingWithdrawIncome {
  669. amountInfo.PendingWithdraw += income.ServiceChargeSettle
  670. }
  671. }
  672. // 已经提现
  673. withdrawnIncome, withdrawnErr := db.GetFullSupplierIncomeList(ctx, req.SupplierId, 8)
  674. if withdrawnErr != nil {
  675. return nil, withdrawnErr
  676. }
  677. if withdrawnIncome != nil {
  678. for _, income := range withdrawnIncome {
  679. amountInfo.Withdrawn += income.ServiceChargeSettle
  680. }
  681. }
  682. return amountInfo, nil
  683. }
  684. // GetSupplierBillAmount 服务商账单 总余额、可提现金额
  685. func (*supplier) GetSupplierBillAmount(ctx context.Context, req *http_model.SupplierAmountRequest) (*http_model.SupplierBillAmountData, error) {
  686. var incomeData *http_model.SupplierBillAmountData
  687. incomeData = &http_model.SupplierBillAmountData{}
  688. incomeData.FullAmount = 0.0
  689. incomeData.Settle = 0.0
  690. // 1. 个人服务商
  691. if req.SupplierType == 1 {
  692. supplierIncome, supplierIncomeErr := db.GetFullSupplierIncomeList(ctx, req.SupplierId, 5)
  693. if supplierIncomeErr != nil {
  694. return nil, supplierIncomeErr
  695. }
  696. if supplierIncome != nil {
  697. for _, income := range supplierIncome {
  698. incomeData.FullAmount += income.ServiceChargeSettle
  699. }
  700. incomeData.Settle = incomeData.FullAmount
  701. }
  702. } else if req.SupplierType == 2 {
  703. // 2. 企业服务商
  704. // 可提现
  705. supplierIncome, supplierIncomeErr := db.GetFullSupplierIncomeList(ctx, req.SupplierId, 5)
  706. if supplierIncomeErr != nil {
  707. return nil, supplierIncomeErr
  708. }
  709. if supplierIncome != nil {
  710. for _, income := range supplierIncome {
  711. incomeData.Settle += income.ServiceChargeSettle
  712. }
  713. incomeData.FullAmount = incomeData.Settle
  714. }
  715. // 可回票
  716. supplierToInvoice, supplierToInvoiceErr := db.GetFullSupplierIncomeList(ctx, req.SupplierId, 1)
  717. if supplierToInvoiceErr != nil {
  718. return nil, supplierToInvoiceErr
  719. }
  720. if supplierToInvoice != nil {
  721. for _, invoice := range supplierToInvoice {
  722. incomeData.FullAmount += invoice.ServiceChargeSettle
  723. }
  724. }
  725. }
  726. // fmt.Println(incomeData)
  727. return incomeData, nil
  728. }
  729. // GetWithdrawPaymentInfo 查找提现收款信息
  730. func (*supplier) GetWithdrawPaymentInfo(ctx context.Context, req *http_model.WithdrawPaymentInfoRequest) (*http_model.WithdrawPaymentInfoData, error) {
  731. var paymentInfo *http_model.WithdrawPaymentInfoData
  732. paymentInfo = &http_model.WithdrawPaymentInfoData{}
  733. // 1. 个人服务商
  734. if req.SupplierType == 1 {
  735. // 1.1. 个人认证信息
  736. supplierInfo, supplierErr := db.GetSupplierById(ctx, req.SupplierId)
  737. if supplierErr != nil {
  738. return nil, supplierErr
  739. }
  740. if supplierInfo != nil {
  741. paymentInfo.Name = supplierInfo.Name
  742. paymentInfo.IDNumber = supplierInfo.IdNumber
  743. }
  744. // 1.2. 提现收款信息查询
  745. supplierPaymentInfo, supplierPaymentErr := db.GetSupplierPaymentInfoById(ctx, req.SupplierId)
  746. if supplierPaymentErr != nil {
  747. return nil, supplierPaymentErr
  748. }
  749. if supplierPaymentInfo != nil {
  750. paymentInfo.Tag = 2
  751. paymentInfo.Phone = supplierPaymentInfo.Phone
  752. paymentInfo.SupplierType = supplierPaymentInfo.SupplierType
  753. paymentInfo.BankName = supplierPaymentInfo.BankName
  754. paymentInfo.BankNumber = supplierPaymentInfo.BankNumber
  755. } else {
  756. paymentInfo.Tag = 1
  757. }
  758. } else if req.SupplierType == 2 {
  759. // 2. 机构服务商
  760. // 2.1. 机构认证信息
  761. supplierInfo, supplierErr := db.GetSupplierById(ctx, req.SupplierId)
  762. if supplierErr != nil {
  763. return nil, supplierErr
  764. }
  765. if supplierInfo != nil {
  766. paymentInfo.Company = supplierInfo.CompanyName
  767. }
  768. // 2.2. 提现收款信息查询
  769. supplierPaymentInfo, supplierPaymentErr := db.GetSupplierPaymentInfoById(ctx, req.SupplierId)
  770. if supplierPaymentErr != nil {
  771. return nil, supplierPaymentErr
  772. }
  773. if supplierPaymentInfo != nil {
  774. paymentInfo.Tag = 2
  775. paymentInfo.PaymentInfoID = supplierPaymentInfo.PaymentInfoID
  776. paymentInfo.SupplierType = supplierPaymentInfo.SupplierType
  777. paymentInfo.BankName = supplierPaymentInfo.BankName
  778. paymentInfo.BankNumber = supplierPaymentInfo.BankNumber
  779. } else {
  780. paymentInfo.Tag = 1
  781. }
  782. }
  783. return paymentInfo, nil
  784. }
  785. // UpdateWithdrawPaymentInfo 更新提现收款信息
  786. func (*supplier) UpdateWithdrawPaymentInfo(ctx context.Context, req *http_model.UpdateWithdrawPaymentInfoRequest) error {
  787. var paymentInfo *gorm_model.SupplierPaymentInfo
  788. paymentInfo = &gorm_model.SupplierPaymentInfo{}
  789. paymentInfo.PaymentInfoID = req.PaymentInfoId
  790. paymentInfo.SupplierID = req.SupplierID
  791. paymentInfo.Phone = req.Phone
  792. paymentInfo.BankName = req.BankName
  793. paymentInfo.BankNumber = req.BankNumber
  794. paymentInfo.Name = req.Name
  795. paymentInfo.IDNumber = req.IDNumber
  796. paymentInfo.Company = req.Company
  797. updatePaymentInfoErr := db.UpdateSupplierPayment(ctx, paymentInfo)
  798. if updatePaymentInfoErr != nil {
  799. return updatePaymentInfoErr
  800. }
  801. return nil
  802. }
  803. // CreateWithdrawPaymentInfo 创建提现收款信息
  804. func (*supplier) CreateWithdrawPaymentInfo(ctx context.Context, req *http_model.CreateWithdrawPaymentInfoRequest) error {
  805. // 1. 个人服务商
  806. if req.SupplierType == 1 {
  807. var paymentInfo *gorm_model.SupplierPaymentInfo
  808. paymentInfo = &gorm_model.SupplierPaymentInfo{}
  809. paymentInfo.SupplierID = req.SupplierID
  810. paymentInfo.Phone = req.Phone
  811. paymentInfo.BankName = req.BankName
  812. paymentInfo.BankNumber = req.BankNumber
  813. paymentInfo.Name = req.Name
  814. paymentInfo.IDNumber = req.IDNumber
  815. paymentInfo.SupplierType = req.SupplierType
  816. createPaymentInfoErr := db.CreateSupplierPayment(ctx, paymentInfo)
  817. if createPaymentInfoErr != nil {
  818. return createPaymentInfoErr
  819. }
  820. } else if req.SupplierType == 2 {
  821. // 2. 机构服务商
  822. var paymentInfo *gorm_model.SupplierPaymentInfo
  823. paymentInfo = &gorm_model.SupplierPaymentInfo{}
  824. paymentInfo.SupplierType = req.SupplierType
  825. paymentInfo.SupplierID = req.SupplierID
  826. paymentInfo.BankName = req.BankName
  827. paymentInfo.BankNumber = req.BankNumber
  828. paymentInfo.Name = req.Name
  829. paymentInfo.IDNumber = req.IDNumber
  830. paymentInfo.Company = req.Company
  831. createPaymentInfoErr := db.CreateSupplierPayment(ctx, paymentInfo)
  832. if createPaymentInfoErr != nil {
  833. return createPaymentInfoErr
  834. }
  835. }
  836. return nil
  837. }
  838. // GetSupplierInvoiceAmount 可回发票、待传发票、平台确认中、已回发票金额
  839. func (*supplier) GetSupplierInvoiceAmount(ctx context.Context, req *http_model.InvoiceAmountRequest) (*http_model.InvoiceAmountData, error) {
  840. var invoiceAmount *http_model.InvoiceAmountData
  841. invoiceAmount = &http_model.InvoiceAmountData{}
  842. invoiceAmount.InvoiceReturned = 0.0
  843. invoiceAmount.PendingUpload = 0.0
  844. invoiceAmount.CanUpload = 0.0
  845. invoiceAmount.PlatformConfirming = 0.0
  846. // 可回发票
  847. supplierIncome, supplierIncomeErr := db.GetFullSupplierIncomeList(ctx, req.SupplierId, 1)
  848. if supplierIncomeErr != nil {
  849. return nil, supplierIncomeErr
  850. }
  851. if supplierIncome != nil {
  852. for _, income := range supplierIncome {
  853. invoiceAmount.CanUpload += income.ServiceChargeSettle
  854. }
  855. }
  856. // 待传发票
  857. pendingUploadIncome, pendingUploadErr := db.GetFullSupplierIncomeList(ctx, req.SupplierId, 2)
  858. if pendingUploadErr != nil {
  859. return nil, pendingUploadErr
  860. }
  861. if pendingUploadIncome != nil {
  862. for _, income := range pendingUploadIncome {
  863. invoiceAmount.PendingUpload += income.ServiceChargeSettle
  864. }
  865. }
  866. // 平台确认中
  867. platformConfirmingIncome, platformConfirmingErr := db.GetFullSupplierIncomeList(ctx, req.SupplierId, 3)
  868. if platformConfirmingErr != nil {
  869. return nil, platformConfirmingErr
  870. }
  871. if platformConfirmingIncome != nil {
  872. for _, income := range platformConfirmingIncome {
  873. invoiceAmount.PlatformConfirming += income.ServiceChargeSettle
  874. }
  875. }
  876. // 已回票
  877. InvoiceReturnedIncome, InvoiceReturnedErr := db.GetFullSupplierIncomeList(ctx, req.SupplierId, 5)
  878. if InvoiceReturnedErr != nil {
  879. return nil, InvoiceReturnedErr
  880. }
  881. if InvoiceReturnedIncome != nil {
  882. for _, income := range InvoiceReturnedIncome {
  883. invoiceAmount.InvoiceReturned += income.ServiceChargeSettle
  884. }
  885. }
  886. return invoiceAmount, nil
  887. }