package dao import ( "demo/internal/model" "github.com/pkg/errors" "github.com/siddontang/go/log" ) func (d *Dao) AddManagement(arg model.Management, mnagementBh []*model.ManagementBh) error { row := d.db.NewSession() defer row.Close() row.Begin() _, err := row.Insert(&arg) if err != nil { err = errors.Wrap(err, "AddManagement-db-error") log.Error(err) row.Rollback() return err } for _, bh := range mnagementBh { bh.ManagementID = arg.Id _, err := row.Insert(bh) if err != nil { err = errors.Wrap(err, "AddManagement-db-error") log.Error(err) row.Rollback() return err } } err = row.Commit() if err != nil { err = errors.Wrap(err, "AddManagement-db-error") log.Error(err) row.Rollback() return err } return nil } func (d *Dao) GetManagement(index, pagesize int64, pastureid string, genre int64, startDate, endDate string) ([]*model.ManagementList, int64, error) { //rows := d.db.NewSession() rows := d.db.NewSession().Table(new(model.Management).TableName()).Alias("m") rows.Join("inner", []string{"t_nz_farm", "f"}, "m.FARM_UUID=f.UUID") rows.Select("m.*,f.FARM_NAME") defer rows.Close() //importdate if pastureid != "" { rows.Where("m.FARM_UUID = ?", pastureid) } if genre >= 0 { //0 售卖,1 返厂 2 置换 rows.Where("m.genre = ?", genre) } if startDate != "" && endDate != "" { rows.Where("m.importdate between ? and ? ", startDate, endDate) } if index != 0 && pagesize != 0 { first := (index - 1) * pagesize rows.Limit(int(pagesize), int(first)) } dataList := make([]*model.ManagementList, 0) count, err := rows.Desc("importdate").FindAndCount(&dataList) if err != nil { err = errors.Wrap(err, "GetManagement-db-error") log.Error(err) return dataList, count, err } return dataList, count, nil } func (d *Dao) GetManagementBh(index, pagesize, managementId int64) ([]*model.ManagementBh, int64, error) { rows := d.db.NewSession() defer rows.Close() rows.Where("managementid = ?", managementId) if index != 0 && pagesize != 0 { first := (index - 1) * pagesize rows.Limit(int(pagesize), int(first)) } dataList := make([]*model.ManagementBh, 0) count, err := rows.OrderBy("REGIST_NUM_2").FindAndCount(&dataList) if err != nil { err = errors.Wrap(err, "GetManagementBh-db-error") log.Error(err) return dataList, count, err } return dataList, count, nil }