bidsection_dao.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * @description: 标段-数据库操作
  3. * @Author: CP
  4. * @Date: 2020-09-28 10:35:56
  5. * @FilePath: \construction_management\dao\bidsection_dao.go
  6. */
  7. package dao
  8. import (
  9. "errors"
  10. "time"
  11. "github.com/go-xorm/xorm"
  12. "go.mod/models"
  13. )
  14. //数据库操作引擎
  15. type BidsectionDao struct {
  16. engine *xorm.Engine
  17. }
  18. //获得一个DAO对象
  19. func NewBidsectionDao(engine *xorm.Engine) *BidsectionDao {
  20. return &BidsectionDao{
  21. engine: engine,
  22. }
  23. }
  24. //id获得数据
  25. func (d *BidsectionDao) Get(id int, projectId int) *models.CmBidsection {
  26. data := &models.CmBidsection{Id: id, ProjectId: projectId}
  27. //Get取到值后,会自动赋值到data中
  28. _, err := d.engine.Get(data)
  29. if err == nil {
  30. return data
  31. } else {
  32. data.Id = 0
  33. return data
  34. }
  35. }
  36. //更新
  37. func (d *BidsectionDao) Update(data *models.CmBidsection, columns []string) error {
  38. _, err := d.engine.Id(data.Id).MustCols(columns...).Update(data)
  39. return err
  40. }
  41. //创建
  42. func (d *BidsectionDao) Create(data *models.CmTree) error {
  43. session := d.engine.NewSession()
  44. defer session.Close()
  45. err := session.Begin()
  46. if err != nil {
  47. return errors.New("创建标段出错-db")
  48. }
  49. // 创建标段
  50. bidsection := &models.CmBidsection{}
  51. bidsection.ProjectId = data.ProjectId
  52. bidsection.Name = data.Name
  53. bidsection.CreateTime = time.Now()
  54. bidsection.DealTp = "0"
  55. bidsection.TotalPrice = "0"
  56. _, err = session.Insert(bidsection)
  57. if err != nil {
  58. session.Rollback()
  59. return errors.New("标段创建不正确")
  60. }
  61. // 创建标段在树结构中的联系
  62. data.BidsectionId = bidsection.Id
  63. data.ContractsIncome = "0"
  64. data.ContractsPaid = "0"
  65. data.ContractsPay = "0"
  66. data.ContractsReturned = "0"
  67. data.ContractDeductionTotal = "0"
  68. _, err = session.Insert(data)
  69. if err != nil {
  70. session.Rollback()
  71. return errors.New("创建标段关系 出错-db")
  72. }
  73. err = session.Commit()
  74. if err != nil {
  75. session.Rollback()
  76. return errors.New("创建标段出错-db")
  77. }
  78. return nil
  79. }