supplier.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. var sProjectIncomeData *http_model.FullSProjectIncomeData
  55. sProjectIncomeData = &http_model.FullSProjectIncomeData{}
  56. // 1. 查询
  57. supplierIncome, total, err := db.GetSupplierIncomeList(ctx, req.PageSize, req.PageNum, req.SupplierId, req.IncomeStatus)
  58. if err != nil {
  59. return nil, nil
  60. }
  61. // 2. 补充种草/本地生活任务信息
  62. if supplierIncome != nil {
  63. sProjectIncomeData.Total = total
  64. } else {
  65. sProjectIncomeData.Total = 0
  66. }
  67. return sProjectIncomeData, nil
  68. }
  69. // CreateSupplierInvoice 创建服务商发票
  70. func (*supplier) CreateSupplierInvoice(ctx context.Context, req *http_model.CreateSupplierInvoiceRequest) error {
  71. // 1. 数据转换
  72. var invoiceData *gorm_model.YounggeeSupplierInvoice
  73. invoiceData = &gorm_model.YounggeeSupplierInvoice{
  74. SupplierId: req.SupplierId,
  75. InvoiceStatus: 1,
  76. IncomeIds: req.IncomeIds,
  77. }
  78. if req.SOperatorType == 1 {
  79. invoiceData.SOperatorType = 1
  80. invoiceData.SOperator = req.SupplierId
  81. } else if req.SOperatorType == 2 {
  82. invoiceData.SOperatorType = 2
  83. invoiceData.SOperator = req.SubAccountId
  84. }
  85. // 2. 插入数据库
  86. err := db.CreateSupplierInvoice(ctx, invoiceData)
  87. if err != nil {
  88. return err
  89. }
  90. return nil
  91. }
  92. // UpdateSupplierInvoice 更新服务商发票
  93. func (*supplier) UpdateSupplierInvoice(ctx context.Context, req *http_model.UpdateSupplierInvoiceRequest) error {
  94. // 1. 数据转换
  95. var invoiceData *gorm_model.YounggeeSupplierInvoice
  96. var currentTime time.Time
  97. currentTime = time.Now()
  98. invoiceData = &gorm_model.YounggeeSupplierInvoice{
  99. InvoiceId: req.InvoiceId,
  100. InvoiceUrl: req.InvoiceUrl,
  101. Company: req.Company,
  102. UploadInvoiceTime: &currentTime,
  103. }
  104. // 2. 更新
  105. err := db.UpdateSupplierInvoice(ctx, invoiceData)
  106. if err != nil {
  107. return err
  108. }
  109. // 3. 更新服务商收入状态
  110. incomeIds, dbErr := db.GetIncomeIdsByInvoiceId(ctx, req.InvoiceId)
  111. if dbErr != nil {
  112. return dbErr
  113. }
  114. if incomeIds == "" {
  115. return nil
  116. }
  117. strSlice := strings.Split(incomeIds, ",")
  118. intSlice := make([]int, len(strSlice))
  119. for i, s := range strSlice {
  120. num, err := strconv.Atoi(s)
  121. if err != nil {
  122. fmt.Println("转换错误:", err)
  123. return err
  124. }
  125. intSlice[i] = num
  126. }
  127. updateSupplierIncomeErr := db.UpdateSupplierIncomeStatus(ctx, intSlice, 3)
  128. if updateSupplierIncomeErr != nil {
  129. return updateSupplierIncomeErr
  130. }
  131. return nil
  132. }
  133. // UpdateSupplierIncomeStatus 修改服务商收入状态
  134. func (*supplier) UpdateSupplierIncomeStatus(ctx context.Context, incomeIds string, incomeStatus int) error {
  135. // 1. 转换incomeIds为数组
  136. strSlice := strings.Split(incomeIds, ",")
  137. intSlice := make([]int, len(strSlice))
  138. for i, s := range strSlice {
  139. num, err := strconv.Atoi(s)
  140. if err != nil {
  141. fmt.Println("转换错误:", err)
  142. return err
  143. }
  144. intSlice[i] = num
  145. }
  146. // 2. 修改数据库中数据
  147. updateSupplierIncomeErr := db.UpdateSupplierIncomeStatus(ctx, intSlice, incomeStatus)
  148. if updateSupplierIncomeErr != nil {
  149. return updateSupplierIncomeErr
  150. }
  151. return nil
  152. }
  153. // GetSupplierInvoiceList 查找服务商发票列表
  154. func (*supplier) GetSupplierInvoiceList(ctx context.Context, req *http_model.SupplierInvoiceListRequest) (*http_model.SupplierInvoiceListData, error) {
  155. // 1. 查询服务商发票信息
  156. supplierInvoiceList, total, err := db.GetInvoiceListBySupplierId(ctx, req.SupplierId, req.InvoiceStatus, 4, req.PageSize, req.PageNum)
  157. if err != nil {
  158. return nil, err
  159. }
  160. // 2. 根据发票中的incomeIds去查找任务及其服务费收入
  161. var supplierInvoiceData *http_model.SupplierInvoiceListData
  162. supplierInvoiceData = &http_model.SupplierInvoiceListData{}
  163. for _, supplierInvoice := range supplierInvoiceList {
  164. var supplierInvoiceInfo *http_model.SupplierInvoiceInfo
  165. supplierInvoiceInfo = &http_model.SupplierInvoiceInfo{}
  166. // 2.1. 基础信息填入
  167. supplierInvoiceInfo.UploadInvoiceTime = supplierInvoice.UploadInvoiceTime
  168. supplierInvoiceInfo.InvoiceUrl = supplierInvoice.InvoiceUrl
  169. supplierInvoiceInfo.AgreeTime = supplierInvoice.AgreeTime
  170. supplierInvoiceInfo.RejectTime = supplierInvoice.RejectTime
  171. supplierInvoiceInfo.Company = supplierInvoice.Company
  172. supplierInvoiceInfo.SOperator = supplierInvoice.SOperator
  173. supplierInvoiceInfo.FailReason = supplierInvoice.FailReason
  174. // 2.2. 任务及其收入信息填入
  175. incomeIds := supplierInvoice.IncomeIds
  176. strSlice := strings.Split(incomeIds, ",")
  177. intSlice := make([]int, len(strSlice))
  178. for i, s := range strSlice {
  179. num, err := strconv.Atoi(s)
  180. if err != nil {
  181. fmt.Println("转换错误:", err)
  182. return nil, err
  183. }
  184. intSlice[i] = num
  185. }
  186. for _, incomeId := range intSlice {
  187. var sTaskInfo *http_model.STaskInfo
  188. sTaskInfo = &http_model.STaskInfo{}
  189. currIncome, incomeErr := db.GetIncomeInfoByIncomeId(ctx, incomeId)
  190. if incomeErr != nil {
  191. return nil, incomeErr
  192. }
  193. sTaskInfo.ServiceCharge = currIncome.SupplierChargeActual
  194. if currIncome.IncomeType == 1 {
  195. sTaskInfo.Id = currIncome.SProjectID
  196. } else if currIncome.IncomeType == 3 {
  197. sTaskInfo.Id = currIncome.SLocalLifeID
  198. }
  199. supplierInvoiceInfo.STaskInfo = append(supplierInvoiceInfo.STaskInfo, sTaskInfo)
  200. }
  201. supplierInvoiceData.SupplierInvoiceList = append(supplierInvoiceData.SupplierInvoiceList, supplierInvoiceInfo)
  202. }
  203. supplierInvoiceData.Total = total
  204. return supplierInvoiceData, nil
  205. }
  206. // GetSupplierToWithdrawList 服务商待提现列表
  207. func (*supplier) GetSupplierToWithdrawList(ctx context.Context, req *http_model.SupplierToWithdrawListRequest) (*http_model.SupplierToWithdrawListData, error) {
  208. // 1. 查询服务商发票信息
  209. var supplierInvoiceData *http_model.SupplierToWithdrawListData
  210. supplierInvoiceData = &http_model.SupplierToWithdrawListData{}
  211. supplierInvoiceList, total, err := db.GetInvoiceListBySupplierId(ctx, req.SupplierId, 3, 1, req.PageSize, req.PageNum)
  212. if err != nil {
  213. return nil, err
  214. }
  215. if supplierInvoiceList != nil {
  216. // 企业服务商
  217. // 2. 根据发票中的incomeIds去查找任务及其服务费收入
  218. for _, supplierInvoice := range supplierInvoiceList {
  219. var supplierInvoiceInfo *http_model.SupplierToWithdrawInfo
  220. supplierInvoiceInfo = &http_model.SupplierToWithdrawInfo{}
  221. // 2.1. 基础信息填入
  222. supplierInvoiceInfo.AgreeTime = supplierInvoice.AgreeTime
  223. supplierInvoiceInfo.Company = supplierInvoice.Company
  224. // 2.2. 任务及其收入信息填入
  225. incomeIds := supplierInvoice.IncomeIds
  226. strSlice := strings.Split(incomeIds, ",")
  227. intSlice := make([]int, len(strSlice))
  228. for i, s := range strSlice {
  229. num, err := strconv.Atoi(s)
  230. if err != nil {
  231. fmt.Println("转换错误:", err)
  232. return nil, err
  233. }
  234. intSlice[i] = num
  235. }
  236. for _, incomeId := range intSlice {
  237. var sTaskInfo *http_model.STaskInfo
  238. sTaskInfo = &http_model.STaskInfo{}
  239. currIncome, incomeErr := db.GetIncomeInfoByIncomeId(ctx, incomeId)
  240. if incomeErr != nil {
  241. return nil, incomeErr
  242. }
  243. sTaskInfo.ServiceCharge = currIncome.SupplierChargeActual
  244. supplierInvoiceInfo.Amount += currIncome.SupplierChargeActual
  245. if currIncome.IncomeType == 1 {
  246. sTaskInfo.Id = currIncome.SProjectID
  247. } else if currIncome.IncomeType == 3 {
  248. sTaskInfo.Id = currIncome.SLocalLifeID
  249. }
  250. supplierInvoiceInfo.STaskInfo = append(supplierInvoiceInfo.STaskInfo, sTaskInfo)
  251. }
  252. supplierInvoiceData.ToWithdrawList = append(supplierInvoiceData.ToWithdrawList, supplierInvoiceInfo)
  253. }
  254. supplierInvoiceData.Total = total
  255. } else {
  256. // 个人服务商
  257. }
  258. return supplierInvoiceData, nil
  259. }
  260. // CreateSupplierWithdraw TODO
  261. // CreateSupplierWithdraw 创建服务商提现信息
  262. func (*supplier) CreateSupplierWithdraw(ctx context.Context, req *http_model.CreateSupplierWithdrawRequest) error {
  263. // 1. 数据整理
  264. var supplierWithdrawInfoList []*gorm_model.YounggeeSupplierWithdraw
  265. for _, withdrawInfo := range req.CreateCompanySupplierWithdraw {
  266. var supplierWithdrawInfo *gorm_model.YounggeeSupplierWithdraw
  267. supplierWithdrawInfo = &gorm_model.YounggeeSupplierWithdraw{}
  268. // 1.1. 接口传入信息填入
  269. supplierWithdrawInfo.SupplierId = req.SupplierId
  270. supplierWithdrawInfo.WithdrawStatus = 1
  271. supplierWithdrawInfo.BankName = withdrawInfo.BankName
  272. supplierWithdrawInfo.BankNumber = withdrawInfo.BankNumber
  273. // 1.2. 查找服务商信息填入
  274. // 1.3. 回票信息填入
  275. supplierWithdrawInfoList = append(supplierWithdrawInfoList, supplierWithdrawInfo)
  276. }
  277. // 2. 数据库插入
  278. err := db.CreateSupplierWithdraw(ctx, supplierWithdrawInfoList)
  279. if err != nil {
  280. return err
  281. }
  282. return nil
  283. }