supplier.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. log "github.com/sirupsen/logrus"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "youngee_b_api/db"
  10. "youngee_b_api/model/gorm_model"
  11. "youngee_b_api/model/http_model"
  12. )
  13. var Supplier *supplier
  14. type supplier struct {
  15. }
  16. // CreateSupplier 创建younggee平台用户账号与服务商信息
  17. func (*supplier) CreateSupplier(ctx context.Context, phone string) (*http_model.RegisterData, error) {
  18. // 1. 创建YG平台用户
  19. user := gorm_model.YounggeeUser{
  20. Phone: phone,
  21. User: "1003",
  22. Username: phone,
  23. Password: "1003",
  24. RealName: "",
  25. Role: "6",
  26. Email: "",
  27. LastLoginTime: time.Now().UTC().Local(),
  28. }
  29. userId, err := db.CreateUser(ctx, user)
  30. if err != nil {
  31. log.Infof("[CreateEnterpriseUser] fail,err:%+v", err)
  32. return nil, err
  33. } else {
  34. // 2. 创建服务商信息
  35. supplier := &gorm_model.YoungeeSupplier{
  36. SupplierName: phone,
  37. PhoneNumber: phone,
  38. UserId: *userId,
  39. }
  40. supplierId, err := db.CreateSupplier(ctx, *supplier)
  41. fmt.Println(supplierId)
  42. if err != nil {
  43. log.Infof("[CreateEnterpriseUser] fail,err:%+v", err)
  44. return nil, err
  45. }
  46. res := &http_model.RegisterData{
  47. UserID: *userId,
  48. }
  49. return res, nil
  50. }
  51. }
  52. // GetSupplierIncomeList 查询服务商收入列表
  53. func (*supplier) GetSupplierIncomeList(ctx context.Context, req *http_model.FullSProjectIncomeListRequest) (*http_model.FullSProjectIncomeData, error) {
  54. // 1. 查询
  55. supplierIncome, total, err := db.GetSupplierIncomeList(ctx, req)
  56. if err != nil {
  57. return nil, nil
  58. }
  59. // 2. 整理数据格式
  60. var sProjectIncomeData *http_model.FullSProjectIncomeData
  61. sProjectIncomeData = &http_model.FullSProjectIncomeData{}
  62. sProjectIncomeData.SProjectIncomeList = supplierIncome
  63. sProjectIncomeData.Total = total
  64. return sProjectIncomeData, nil
  65. }
  66. // CreateSupplierInvoice 创建服务商发票
  67. func (*supplier) CreateSupplierInvoice(ctx context.Context, req *http_model.CreateSupplierInvoiceRequest) error {
  68. // 1. 数据转换
  69. var invoiceData *gorm_model.YounggeeSupplierInvoice
  70. invoiceData = &gorm_model.YounggeeSupplierInvoice{
  71. SupplierId: req.SupplierId,
  72. InvoiceStatus: 1,
  73. IncomeIds: req.IncomeIds,
  74. }
  75. if req.SOperatorType == 1 {
  76. invoiceData.SOperatorType = 1
  77. invoiceData.SOperator = req.SupplierId
  78. } else if req.SOperatorType == 2 {
  79. invoiceData.SOperatorType = 2
  80. invoiceData.SOperator = req.SubAccountId
  81. }
  82. // 2. 插入数据库
  83. err := db.CreateSupplierInvoice(ctx, invoiceData)
  84. if err != nil {
  85. return err
  86. }
  87. return nil
  88. }
  89. // UpdateSupplierInvoice 更新服务商发票
  90. func (*supplier) UpdateSupplierInvoice(ctx context.Context, req *http_model.UpdateSupplierInvoiceRequest) error {
  91. // 1. 数据转换
  92. var invoiceData *gorm_model.YounggeeSupplierInvoice
  93. var currentTime time.Time
  94. currentTime = time.Now()
  95. invoiceData = &gorm_model.YounggeeSupplierInvoice{
  96. InvoiceId: req.InvoiceId,
  97. InvoiceUrl: req.InvoiceUrl,
  98. Company: req.Company,
  99. UploadInvoiceTime: &currentTime,
  100. }
  101. // 2. 更新
  102. err := db.UpdateSupplierInvoice(ctx, invoiceData)
  103. if err != nil {
  104. return err
  105. }
  106. // 3. 更新服务商收入状态
  107. incomeIds, dbErr := db.GetIncomeIdsByInvoiceId(ctx, req.InvoiceId)
  108. if dbErr != nil {
  109. return dbErr
  110. }
  111. if incomeIds == "" {
  112. return nil
  113. }
  114. strSlice := strings.Split(incomeIds, ",")
  115. intSlice := make([]int, len(strSlice))
  116. for i, s := range strSlice {
  117. num, err := strconv.Atoi(s)
  118. if err != nil {
  119. fmt.Println("转换错误:", err)
  120. return err
  121. }
  122. intSlice[i] = num
  123. }
  124. updateSupplierIncomeErr := db.UpdateSupplierIncomeStatus(ctx, intSlice, 3)
  125. if updateSupplierIncomeErr != nil {
  126. return updateSupplierIncomeErr
  127. }
  128. return nil
  129. }
  130. // UpdateSupplierIncomeStatus 修改服务商收入状态
  131. func (*supplier) UpdateSupplierIncomeStatus(ctx context.Context, incomeIds string, incomeStatus int) error {
  132. // 1. 转换incomeIds为数组
  133. strSlice := strings.Split(incomeIds, ",")
  134. intSlice := make([]int, len(strSlice))
  135. for i, s := range strSlice {
  136. num, err := strconv.Atoi(s)
  137. if err != nil {
  138. fmt.Println("转换错误:", err)
  139. return err
  140. }
  141. intSlice[i] = num
  142. }
  143. // 2. 修改数据库中数据
  144. updateSupplierIncomeErr := db.UpdateSupplierIncomeStatus(ctx, intSlice, incomeStatus)
  145. if updateSupplierIncomeErr != nil {
  146. return updateSupplierIncomeErr
  147. }
  148. return nil
  149. }
  150. // GetSupplierInvoiceList 查找服务商发票列表
  151. func (*supplier) GetSupplierInvoiceList(ctx context.Context, req *http_model.SupplierInvoiceListRequest) (*http_model.SupplierInvoiceListData, error) {
  152. // 1. 查询服务商发票信息
  153. supplierInvoiceList, total, err := db.GetInvoiceListBySupplierId(ctx, req.SupplierId, req.InvoiceStatus, 4, req.PageSize, req.PageNum)
  154. if err != nil {
  155. return nil, err
  156. }
  157. // 2. 根据发票中的incomeIds去查找任务及其服务费收入
  158. var supplierInvoiceData *http_model.SupplierInvoiceListData
  159. supplierInvoiceData = &http_model.SupplierInvoiceListData{}
  160. for _, supplierInvoice := range supplierInvoiceList {
  161. var supplierInvoiceInfo *http_model.SupplierInvoiceInfo
  162. supplierInvoiceInfo = &http_model.SupplierInvoiceInfo{}
  163. // 2.1. 基础信息填入
  164. supplierInvoiceInfo.UploadInvoiceTime = supplierInvoice.UploadInvoiceTime
  165. supplierInvoiceInfo.InvoiceUrl = supplierInvoice.InvoiceUrl
  166. supplierInvoiceInfo.AgreeTime = supplierInvoice.AgreeTime
  167. supplierInvoiceInfo.RejectTime = supplierInvoice.RejectTime
  168. supplierInvoiceInfo.Company = supplierInvoice.Company
  169. supplierInvoiceInfo.SOperator = supplierInvoice.SOperator
  170. supplierInvoiceInfo.FailReason = supplierInvoice.FailReason
  171. // 2.2. 任务及其收入信息填入
  172. incomeIds := supplierInvoice.IncomeIds
  173. strSlice := strings.Split(incomeIds, ",")
  174. intSlice := make([]int, len(strSlice))
  175. for i, s := range strSlice {
  176. num, err := strconv.Atoi(s)
  177. if err != nil {
  178. fmt.Println("转换错误:", err)
  179. return nil, err
  180. }
  181. intSlice[i] = num
  182. }
  183. for _, incomeId := range intSlice {
  184. var sTaskInfo *http_model.STaskInfo
  185. sTaskInfo = &http_model.STaskInfo{}
  186. currIncome, incomeErr := db.GetIncomeInfoByIncomeId(ctx, incomeId)
  187. if incomeErr != nil {
  188. return nil, incomeErr
  189. }
  190. sTaskInfo.ServiceCharge = currIncome.SupplierChargeActual
  191. if currIncome.IncomeType == 1 {
  192. sTaskInfo.Id = currIncome.SProjectID
  193. } else if currIncome.IncomeType == 3 {
  194. sTaskInfo.Id = currIncome.SLocalLifeID
  195. }
  196. supplierInvoiceInfo.STaskInfo = append(supplierInvoiceInfo.STaskInfo, sTaskInfo)
  197. }
  198. supplierInvoiceData.SupplierInvoiceList = append(supplierInvoiceData.SupplierInvoiceList, supplierInvoiceInfo)
  199. }
  200. supplierInvoiceData.Total = total
  201. return supplierInvoiceData, nil
  202. }
  203. // GetSupplierToWithdrawList 服务商待提现列表
  204. func (*supplier) GetSupplierToWithdrawList(ctx context.Context, req *http_model.SupplierToWithdrawListRequest) (*http_model.SupplierToWithdrawListData, error) {
  205. // 1. 查询服务商发票信息
  206. supplierInvoiceList, total, err := db.GetInvoiceListBySupplierId(ctx, req.SupplierId, 3, 1, req.PageSize, req.PageNum)
  207. if err != nil {
  208. return nil, err
  209. }
  210. // 2. 根据发票中的incomeIds去查找任务及其服务费收入
  211. var supplierInvoiceData *http_model.SupplierToWithdrawListData
  212. supplierInvoiceData = &http_model.SupplierToWithdrawListData{}
  213. for _, supplierInvoice := range supplierInvoiceList {
  214. var supplierInvoiceInfo *http_model.SupplierToWithdrawInfo
  215. supplierInvoiceInfo = &http_model.SupplierToWithdrawInfo{}
  216. // 2.1. 基础信息填入
  217. supplierInvoiceInfo.AgreeTime = supplierInvoice.AgreeTime
  218. supplierInvoiceInfo.Company = supplierInvoice.Company
  219. // 2.2. 任务及其收入信息填入
  220. incomeIds := supplierInvoice.IncomeIds
  221. strSlice := strings.Split(incomeIds, ",")
  222. intSlice := make([]int, len(strSlice))
  223. for i, s := range strSlice {
  224. num, err := strconv.Atoi(s)
  225. if err != nil {
  226. fmt.Println("转换错误:", err)
  227. return nil, err
  228. }
  229. intSlice[i] = num
  230. }
  231. for _, incomeId := range intSlice {
  232. var sTaskInfo *http_model.STaskInfo
  233. sTaskInfo = &http_model.STaskInfo{}
  234. currIncome, incomeErr := db.GetIncomeInfoByIncomeId(ctx, incomeId)
  235. if incomeErr != nil {
  236. return nil, incomeErr
  237. }
  238. sTaskInfo.ServiceCharge = currIncome.SupplierChargeActual
  239. supplierInvoiceInfo.Amount += currIncome.SupplierChargeActual
  240. if currIncome.IncomeType == 1 {
  241. sTaskInfo.Id = currIncome.SProjectID
  242. } else if currIncome.IncomeType == 3 {
  243. sTaskInfo.Id = currIncome.SLocalLifeID
  244. }
  245. supplierInvoiceInfo.STaskInfo = append(supplierInvoiceInfo.STaskInfo, sTaskInfo)
  246. }
  247. supplierInvoiceData.ToWithdrawList = append(supplierInvoiceData.ToWithdrawList, supplierInvoiceInfo)
  248. }
  249. supplierInvoiceData.Total = total
  250. return supplierInvoiceData, nil
  251. }
  252. // CreateSupplierWithdraw TODO
  253. // CreateSupplierWithdraw 创建服务商提现信息
  254. func (*supplier) CreateSupplierWithdraw(ctx context.Context, req *http_model.CreateSupplierWithdrawRequest) error {
  255. // 1. 数据整理
  256. var supplierWithdrawInfoList []*gorm_model.YounggeeSupplierWithdraw
  257. for _, withdrawInfo := range req.CreateSupplierWithdrawList {
  258. var supplierWithdrawInfo *gorm_model.YounggeeSupplierWithdraw
  259. supplierWithdrawInfo = &gorm_model.YounggeeSupplierWithdraw{}
  260. // 1.1. 接口传入信息填入
  261. supplierWithdrawInfo.SupplierId = req.SupplierId
  262. supplierWithdrawInfo.WithdrawStatus = 1
  263. supplierWithdrawInfo.BankName = withdrawInfo.BankName
  264. supplierWithdrawInfo.BankNumber = withdrawInfo.BankNumber
  265. // 1.2. 查找服务商信息填入
  266. // 1.3. 回票信息填入
  267. supplierWithdrawInfoList = append(supplierWithdrawInfoList, supplierWithdrawInfo)
  268. }
  269. // 2. 数据库插入
  270. err := db.CreateSupplierWithdraw(ctx, supplierWithdrawInfoList)
  271. if err != nil {
  272. return err
  273. }
  274. return nil
  275. }