bidsection_dao.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. ok, err := d.engine.Get(data)
  29. if ok && 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. _, err = session.Insert(data)
  68. if err != nil {
  69. session.Rollback()
  70. return errors.New("创建标段联系出错-db")
  71. }
  72. err = session.Commit()
  73. if err != nil {
  74. session.Rollback()
  75. return errors.New("创建标段出错-db")
  76. }
  77. return nil
  78. }