invoice_service.go 9.2 KB

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