invoice_service.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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, _ := dao.InvoiceRecordDao{}.GetBillList(param.EnterpriseId, param.SubAccountId, param.BillStatus, param.Page, param.PageSize)
  121. //if err != nil {
  122. // result = vo.ResultVO{
  123. // Page: param.Page,
  124. // PageSize: param.PageSize,
  125. // Total: total,
  126. // Data: reInvoiceRecords,
  127. // }
  128. // return result, err
  129. //}
  130. for _, invoiceRecord := range invoiceRecords {
  131. var creatorName string
  132. if invoiceRecord.SubAccountId == 0 {
  133. enterprise, err := dao.EnterpriseDao{}.GetEnterprise(invoiceRecord.EnterpriseID)
  134. if err == nil && enterprise != nil {
  135. creatorName = enterprise.BusinessName
  136. }
  137. } else {
  138. subAccount, err := dao.SubAccountDao{}.GetSubAccount(invoiceRecord.SubAccountId)
  139. if err == nil && subAccount != nil {
  140. creatorName = subAccount.SubAccountName
  141. }
  142. }
  143. var taskNumber int
  144. // 将 JSON 字符串解码为二维数组
  145. var arrayData [3][]string
  146. err := json.Unmarshal([]byte(invoiceRecord.TaskIds), &arrayData)
  147. if err != nil {
  148. fmt.Println("Error unmarshaling JSON:", err)
  149. return result, err
  150. }
  151. for _, array := range arrayData {
  152. taskNumber += len(array)
  153. }
  154. reInvoiceRecord := &vo.ReInvoiceRecord{
  155. BillingId: invoiceRecord.BillingId,
  156. InvoiceAmount: invoiceRecord.InvoiceAmount,
  157. CreatorName: creatorName,
  158. SubmitAt: invoiceRecord.SubmitAt.Format("2006-01-02 15:04:05"),
  159. InvoiceBody: invoiceRecord.InvoiceBody,
  160. InvoiceType: invoiceRecord.InvoiceType,
  161. Status: invoiceRecord.Status,
  162. TaskNumber: int64(taskNumber),
  163. InvoiceUrl: invoiceRecord.InvoiceUrl,
  164. }
  165. if param.BillStatus == 2 {
  166. reInvoiceRecord.BillingAt = invoiceRecord.BillingAt.Format("2006-01-02 15:04:05")
  167. }
  168. reInvoiceRecords = append(reInvoiceRecords, reInvoiceRecord)
  169. }
  170. result = vo.ResultVO{
  171. Page: param.Page,
  172. PageSize: param.PageSize,
  173. Total: total,
  174. Data: reInvoiceRecords,
  175. }
  176. return result, nil
  177. }
  178. // 可开票账单
  179. func (s InvoiceService) GetBillableList(param *vo.InvoiceBillListParam) (vo.ResultVO, error) {
  180. if param.Page <= 0 {
  181. param.Page = 1
  182. }
  183. if param.PageSize <= 0 {
  184. param.PageSize = 10
  185. }
  186. var result vo.ResultVO
  187. var reBillableInfos []*vo.ReBillableInfo
  188. var billableSelections []*entity.SelectionInfo
  189. var billableProjects []*entity.Project
  190. // 电商带货
  191. billableSelections, _, _ = dao.InvoiceRecordDao{}.GetBillableSelectionList(param.EnterpriseId, param.SubAccountId, param.Page, param.PageSize)
  192. // 品牌种草
  193. billableProjects, _, _ = dao.InvoiceRecordDao{}.GetBillableProjectList(param.EnterpriseId, param.SubAccountId, param.Page, param.PageSize)
  194. // 本地生活
  195. // 汇总结果
  196. for _, billableSelection := range billableSelections {
  197. // 获取商品详情字段
  198. var creatorName string
  199. var productName string
  200. var productPrice float64
  201. var mainImage string
  202. if billableSelection.SubAccountId == 0 {
  203. enterprise, err := dao.EnterpriseDao{}.GetEnterprise(billableSelection.EnterpriseID)
  204. if err == nil && enterprise != nil {
  205. creatorName = enterprise.BusinessName
  206. }
  207. } else {
  208. subAccount, err := dao.SubAccountDao{}.GetSubAccount(billableSelection.SubAccountId)
  209. if err == nil && subAccount != nil {
  210. creatorName = subAccount.SubAccountName
  211. }
  212. }
  213. product, err := dao.ProductDAO{}.GetProductByID(billableSelection.ProductID)
  214. if err == nil && product != nil {
  215. productName = product.ProductName
  216. productPrice = product.ProductPrice
  217. }
  218. mainImage, err = dao.ProductPhotoDAO{}.GetMainPhotoByProductID(billableSelection.ProductID)
  219. // 电商带货汇总
  220. reBillableInfo := &vo.ReBillableInfo{
  221. ProductId: billableSelection.ProductID,
  222. MainImage: mainImage,
  223. ProductName: productName,
  224. ProductPrice: productPrice,
  225. Platform: billableSelection.Platform,
  226. CreatorName: creatorName,
  227. TaskType: "电商带货",
  228. BillableAmount: billableSelection.SettlementAmount,
  229. TaskId: billableSelection.SelectionID,
  230. }
  231. reBillableInfos = append(reBillableInfos, reBillableInfo)
  232. }
  233. for _, billableProject := range billableProjects {
  234. // 获取商品详情字段
  235. var creatorName string
  236. var productName string
  237. var productPrice float64
  238. var mainImage string
  239. if billableProject.SubAccountId == 0 {
  240. enterprise, err := dao.EnterpriseDao{}.GetEnterprise(billableProject.EnterpriseID)
  241. if err == nil && enterprise != nil {
  242. creatorName = enterprise.BusinessName
  243. }
  244. } else {
  245. subAccount, err := dao.SubAccountDao{}.GetSubAccount(billableProject.SubAccountId)
  246. if err == nil && subAccount != nil {
  247. creatorName = subAccount.SubAccountName
  248. }
  249. }
  250. product, err := dao.ProductDAO{}.GetProductByID(billableProject.ProductID)
  251. if err == nil && product != nil {
  252. productName = product.ProductName
  253. productPrice = product.ProductPrice
  254. }
  255. mainImage, err = dao.ProductPhotoDAO{}.GetMainPhotoByProductID(billableProject.ProductID)
  256. // 品牌种草汇总
  257. reBillableInfo := &vo.ReBillableInfo{
  258. ProductId: billableProject.ProductID,
  259. MainImage: mainImage,
  260. ProductName: productName,
  261. ProductPrice: productPrice,
  262. Platform: billableProject.ProjectPlatform,
  263. CreatorName: creatorName,
  264. TaskType: "品牌种草",
  265. BillableAmount: billableProject.SettlementAmount,
  266. TaskId: billableProject.ProjectId,
  267. }
  268. reBillableInfos = append(reBillableInfos, reBillableInfo)
  269. }
  270. // 本地生活
  271. startIndex := (param.Page - 1) * param.PageSize
  272. endIndex := startIndex + param.PageSize
  273. // 分页
  274. if startIndex >= len(reBillableInfos) {
  275. result = vo.ResultVO{
  276. Page: param.Page,
  277. PageSize: param.PageSize,
  278. Total: int64(len(reBillableInfos)),
  279. Data: nil,
  280. }
  281. return result, nil
  282. }
  283. if endIndex > len(reBillableInfos) {
  284. endIndex = len(reBillableInfos)
  285. }
  286. result = vo.ResultVO{
  287. Page: param.Page,
  288. PageSize: param.PageSize,
  289. Total: int64(len(reBillableInfos)),
  290. Data: reBillableInfos[startIndex:endIndex],
  291. }
  292. return result, nil
  293. }