stock_service.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package service
  2. import (
  3. "ulink-admin/frame"
  4. "ulink-admin/modules/stock/dao"
  5. "ulink-admin/modules/stock/models/model"
  6. "ulink-admin/modules/stock/models/req"
  7. "ulink-admin/pkg/base"
  8. "xorm.io/xorm"
  9. )
  10. type StockService struct {
  11. stockDao dao.StockDao
  12. stockDetailDao dao.StockDetailDao
  13. }
  14. // List 查询所有大盘走势业务方法
  15. func (this StockService) List(query *req.StockQuery, list interface{}) {
  16. this.stockDao.List(query, list)
  17. }
  18. // Page 查询大盘走势分页列表
  19. func (this StockService) Page(query *req.StockQuery) ([]*model.Stock, int64) {
  20. return this.stockDao.Page(query)
  21. }
  22. // Insert 添加大盘走势
  23. func (this StockService) Insert(stock *model.Stock) {
  24. //检查站点标识唯一性
  25. if this.stockDao.Exist(stock.TableName(), "create_date=? and type=?", stock.CreateDate, stock.Type) {
  26. frame.Throw(frame.BUSINESS_CODE, "当天记录已存在,新增失败")
  27. }
  28. base.Transaction(func(session *xorm.Session) {
  29. this.stockDao.Insert(stock)
  30. for _, detail := range stock.Details {
  31. detail.StockId = stock.Id
  32. }
  33. this.stockDetailDao.Insert(stock.Details)
  34. })
  35. }
  36. // Get 查询
  37. func (this StockService) Get(id int64) *model.Stock {
  38. model := &model.Stock{}
  39. this.stockDao.GetById(id, model)
  40. return model
  41. }
  42. // Delete 批量删除
  43. func (this StockService) Delete(list []int64) {
  44. base.Transaction(func(session *xorm.Session) {
  45. this.stockDao.Delete(&model.Stock{}, list)
  46. this.stockDetailDao.GetSession().In("stock_id", list).Delete(&model.StockDetail{})
  47. })
  48. }
  49. // Edit 修改
  50. func (this StockService) Edit(stock *model.Stock, cols []string) {
  51. if this.stockDao.Exist(stock.TableName(), "create_date=? and type=? and id!=?", stock.CreateDate, stock.Type, stock.Id) {
  52. frame.Throw(frame.BUSINESS_CODE, "当天记录已存在,新增失败")
  53. }
  54. base.Transaction(func(session *xorm.Session) {
  55. this.stockDao.Update(stock, cols...)
  56. for _, detail := range stock.Details {
  57. this.stockDetailDao.Update(detail)
  58. }
  59. })
  60. }