bidsection_dao.go 1.9 KB

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