invoice_service.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. package service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "time"
  6. "youngee_b_api/app/consts"
  7. "youngee_b_api/app/dao"
  8. "youngee_b_api/app/entity"
  9. "youngee_b_api/app/util"
  10. "youngee_b_api/app/vo"
  11. )
  12. type InvoiceService struct{}
  13. // 设置默认开票抬头
  14. func (s InvoiceService) UpdateInvoiceDefault(param *vo.InvoiceDefaultParam) (*int64, error) {
  15. var err error
  16. invoiceInfo, err := dao.InvoiceInfoDao{}.SelectDefault(param.EnterpriseId, param.InvoiceType, 1)
  17. if err != nil {
  18. return nil, err
  19. }
  20. if invoiceInfo != nil && invoiceInfo.InvoiceID != 0 {
  21. err := dao.InvoiceInfoDao{}.Delete(invoiceInfo.InvoiceID)
  22. if err != nil {
  23. return nil, err
  24. }
  25. }
  26. if param.HeadType == 0 {
  27. param.HeadType = 1
  28. }
  29. enterprise, err1 := dao.EnterpriseDao{}.GetEnterpriseInfo(param.EnterpriseId)
  30. if err1 != nil {
  31. return nil, err1
  32. }
  33. invoiceInfoAdd := entity.InvoiceInfo{
  34. EnterpriseID: param.EnterpriseId,
  35. InvoiceType: param.InvoiceType,
  36. HeadType: consts.GetHeadType(param.HeadType),
  37. InvoiceHeader: enterprise.BusinessName,
  38. TaxCode: enterprise.TaxNumber,
  39. RegisteredAddress: enterprise.Address,
  40. RegisteredPhone: param.RegisteredPhone,
  41. Bank: param.Bank,
  42. BankCardNumber: param.BankCardNumber,
  43. IsDefault: 1,
  44. UpdateAt: time.Now(),
  45. }
  46. err = dao.InvoiceInfoDao{}.Insert(&invoiceInfoAdd)
  47. if err != nil {
  48. return nil, err
  49. }
  50. return &invoiceInfoAdd.InvoiceID, nil
  51. }
  52. // 获取默认开票抬头
  53. func (s InvoiceService) GetInvoiceDefault(param *vo.InvoiceDefaultParam) (*vo.ReInvoiceInfo, error) {
  54. invoiceInfo, err := dao.InvoiceInfoDao{}.SelectDefault(param.EnterpriseId, param.InvoiceType, 1)
  55. if err != nil {
  56. return nil, err
  57. }
  58. reInvoiceInfo := &vo.ReInvoiceInfo{
  59. InvoiceHeader: invoiceInfo.InvoiceHeader,
  60. TaxCode: invoiceInfo.TaxCode,
  61. RegisteredAddress: invoiceInfo.RegisteredAddress,
  62. RegisteredPhone: invoiceInfo.RegisteredPhone,
  63. Bank: invoiceInfo.Bank,
  64. BankCardNumber: invoiceInfo.BankCardNumber,
  65. IsDefault: invoiceInfo.IsDefault,
  66. }
  67. return reInvoiceInfo, nil
  68. }
  69. // 确认开票
  70. func (s InvoiceService) BillInvoice(param *vo.InvoiceBillParam) (*string, error) {
  71. var err error
  72. billingId := util.GenerateDateRelatedUUID(16)
  73. taskIds := param.TaskIds
  74. // 将二维数组转换为 JSON 字符串
  75. jsonData, _ := json.Marshal(taskIds)
  76. // 将 []byte 转换为 string
  77. taskIdsString := string(jsonData)
  78. invoiceRecordAdd := entity.InvoiceRecord{
  79. BillingId: billingId,
  80. EnterpriseID: param.EnterpriseId,
  81. SubAccountId: param.SubAccountId,
  82. InvoiceAmount: param.InvoiceAmount,
  83. InvoiceBody: param.InvoiceBody,
  84. InvoiceContent: param.InvoiceContent,
  85. InvoiceType: param.InvoiceType,
  86. InvoiceHeader: param.InvoiceHead,
  87. TaxCode: param.TaxCode,
  88. RegisteredAddress: param.RegisteredAddress,
  89. RegisteredPhone: param.RegisteredPhone,
  90. Bank: param.Bank,
  91. BankCardNumber: param.BankCardNumber,
  92. TaskIds: taskIdsString,
  93. Status: 1,
  94. SubmitAt: time.Now(),
  95. }
  96. err = dao.InvoiceRecordDao{}.Insert(&invoiceRecordAdd)
  97. if err != nil {
  98. return nil, err
  99. }
  100. // 更新选品表的开票状态字段
  101. // 电商带货
  102. err = dao.SelectionInfoDAO{}.UpdateInvoiceStatus(taskIds[0])
  103. if err != nil {
  104. return nil, err
  105. }
  106. // 品牌种草
  107. err = dao.ProjectDAO{}.UpdateInvoiceStatus(taskIds[1])
  108. if err != nil {
  109. return nil, err
  110. }
  111. // 本地生活
  112. return &billingId, nil
  113. }
  114. // 开票记录
  115. func (s InvoiceService) GetBillList(param *vo.InvoiceBillListParam) (vo.ResultVO, error) {
  116. if param.Page <= 0 {
  117. param.Page = 1
  118. }
  119. if param.PageSize <= 0 {
  120. param.PageSize = 10
  121. }
  122. var result vo.ResultVO
  123. var reInvoiceRecords []*vo.ReInvoiceRecord
  124. invoiceRecords, total, _ := dao.InvoiceRecordDao{}.GetBillList(param.EnterpriseId, param.SubAccountId, param.BillStatus, param.Page, param.PageSize)
  125. //if err != nil {
  126. // result = vo.ResultVO{
  127. // Page: param.Page,
  128. // PageSize: param.PageSize,
  129. // Total: total,
  130. // Data: reInvoiceRecords,
  131. // }
  132. // return result, err
  133. //}
  134. for _, invoiceRecord := range invoiceRecords {
  135. var creatorName string
  136. if invoiceRecord.SubAccountId == 0 {
  137. enterprise, err := dao.EnterpriseDao{}.GetEnterprise(invoiceRecord.EnterpriseID)
  138. if err == nil && enterprise != nil {
  139. creatorName = enterprise.BusinessName
  140. }
  141. } else {
  142. subAccount, err := dao.SubAccountDao{}.GetSubAccount(invoiceRecord.SubAccountId)
  143. if err == nil && subAccount != nil {
  144. creatorName = subAccount.SubAccountName
  145. }
  146. }
  147. var taskNumber int
  148. // 将 JSON 字符串解码为二维数组
  149. var arrayData [3][]string
  150. err := json.Unmarshal([]byte(invoiceRecord.TaskIds), &arrayData)
  151. if err != nil {
  152. fmt.Println("Error unmarshaling JSON:", err)
  153. return result, err
  154. }
  155. for _, array := range arrayData {
  156. taskNumber += len(array)
  157. }
  158. reInvoiceRecord := &vo.ReInvoiceRecord{
  159. BillingId: invoiceRecord.BillingId,
  160. InvoiceAmount: invoiceRecord.InvoiceAmount,
  161. CreatorName: creatorName,
  162. SubmitAt: invoiceRecord.SubmitAt.Format("2006-01-02 15:04:05"),
  163. InvoiceBody: invoiceRecord.InvoiceBody,
  164. InvoiceType: invoiceRecord.InvoiceType,
  165. Status: invoiceRecord.Status,
  166. TaskNumber: int64(taskNumber),
  167. InvoiceUrl: invoiceRecord.InvoiceUrl,
  168. }
  169. if param.BillStatus == 2 {
  170. reInvoiceRecord.BillingAt = invoiceRecord.BillingAt.Format("2006-01-02 15:04:05")
  171. }
  172. reInvoiceRecords = append(reInvoiceRecords, reInvoiceRecord)
  173. }
  174. result = vo.ResultVO{
  175. Page: param.Page,
  176. PageSize: param.PageSize,
  177. Total: total,
  178. Data: reInvoiceRecords,
  179. }
  180. return result, nil
  181. }
  182. // 可开票账单
  183. func (s InvoiceService) GetBillableList(param *vo.InvoiceBillListParam) (vo.ResultVO, error) {
  184. if param.Page <= 0 {
  185. param.Page = 1
  186. }
  187. if param.PageSize <= 0 {
  188. param.PageSize = 10
  189. }
  190. var result vo.ResultVO
  191. var reBillableInfos []*vo.ReBillableInfo
  192. var billableSelections []*entity.SelectionInfo
  193. var billableProjects []*entity.Project
  194. // 电商带货
  195. billableSelections, _, _ = dao.InvoiceRecordDao{}.GetBillableSelectionList(param.EnterpriseId, param.SubAccountId, param.Page, param.PageSize)
  196. // 品牌种草
  197. billableProjects, _, _ = dao.InvoiceRecordDao{}.GetBillableProjectList(param.EnterpriseId, param.SubAccountId, param.Page, param.PageSize)
  198. // 本地生活
  199. // 汇总结果
  200. for _, billableSelection := range billableSelections {
  201. // 获取商品详情字段
  202. var creatorName string
  203. var productName string
  204. var productPrice float64
  205. var mainImage string
  206. if billableSelection.SubAccountId == 0 {
  207. enterprise, err := dao.EnterpriseDao{}.GetEnterprise(billableSelection.EnterpriseID)
  208. if err == nil && enterprise != nil {
  209. creatorName = enterprise.BusinessName
  210. }
  211. } else {
  212. subAccount, err := dao.SubAccountDao{}.GetSubAccount(billableSelection.SubAccountId)
  213. if err == nil && subAccount != nil {
  214. creatorName = subAccount.SubAccountName
  215. }
  216. }
  217. product, err := dao.ProductDAO{}.GetProductByID(billableSelection.ProductID)
  218. if err == nil && product != nil {
  219. productName = product.ProductName
  220. productPrice = product.ProductPrice
  221. }
  222. mainImage, err = dao.ProductPhotoDAO{}.GetMainPhotoByProductID(billableSelection.ProductID)
  223. // 电商带货汇总
  224. reBillableInfo := &vo.ReBillableInfo{
  225. ProductId: billableSelection.ProductID,
  226. MainImage: mainImage,
  227. ProductName: productName,
  228. ProductPrice: productPrice,
  229. Platform: billableSelection.Platform,
  230. CreatorName: creatorName,
  231. TaskType: "电商带货",
  232. BillableAmount: billableSelection.SettlementAmount,
  233. TaskId: billableSelection.SelectionID,
  234. }
  235. reBillableInfos = append(reBillableInfos, reBillableInfo)
  236. }
  237. for _, billableProject := range billableProjects {
  238. // 获取商品详情字段
  239. var creatorName string
  240. var productName string
  241. var productPrice float64
  242. var mainImage string
  243. if billableProject.SubAccountId == 0 {
  244. enterprise, err := dao.EnterpriseDao{}.GetEnterprise(billableProject.EnterpriseID)
  245. if err == nil && enterprise != nil {
  246. creatorName = enterprise.BusinessName
  247. }
  248. } else {
  249. subAccount, err := dao.SubAccountDao{}.GetSubAccount(billableProject.SubAccountId)
  250. if err == nil && subAccount != nil {
  251. creatorName = subAccount.SubAccountName
  252. }
  253. }
  254. product, err := dao.ProductDAO{}.GetProductByID(billableProject.ProductID)
  255. if err == nil && product != nil {
  256. productName = product.ProductName
  257. productPrice = product.ProductPrice
  258. }
  259. mainImage, err = dao.ProductPhotoDAO{}.GetMainPhotoByProductID(billableProject.ProductID)
  260. // 品牌种草汇总
  261. reBillableInfo := &vo.ReBillableInfo{
  262. ProductId: billableProject.ProductID,
  263. MainImage: mainImage,
  264. ProductName: productName,
  265. ProductPrice: productPrice,
  266. Platform: billableProject.ProjectPlatform,
  267. CreatorName: creatorName,
  268. TaskType: "品牌种草",
  269. BillableAmount: billableProject.SettlementAmount,
  270. TaskId: billableProject.ProjectId,
  271. }
  272. reBillableInfos = append(reBillableInfos, reBillableInfo)
  273. }
  274. // 本地生活
  275. startIndex := (param.Page - 1) * param.PageSize
  276. endIndex := startIndex + param.PageSize
  277. // 分页
  278. if startIndex >= len(reBillableInfos) {
  279. result = vo.ResultVO{
  280. Page: param.Page,
  281. PageSize: param.PageSize,
  282. Total: int64(len(reBillableInfos)),
  283. Data: nil,
  284. }
  285. return result, nil
  286. }
  287. if endIndex > len(reBillableInfos) {
  288. endIndex = len(reBillableInfos)
  289. }
  290. result = vo.ResultVO{
  291. Page: param.Page,
  292. PageSize: param.PageSize,
  293. Total: int64(len(reBillableInfos)),
  294. Data: reBillableInfos[startIndex:endIndex],
  295. }
  296. return result, nil
  297. }