package dao import ( "demo/internal/model" "fmt" "github.com/pkg/errors" "github.com/siddontang/go/log" "time" ) func (d *Dao) AddRecall(arg model.Factory, factory []int64, REGISTNUM2 []string) error { row := d.db.NewSession() defer row.Close() row.Begin() c := time.Now().Format("20060102") count1, err := row.Where(" batch like ? ", "%"+fmt.Sprintf("RB%s", c)+"%").Count(&model.Factory{}) if err != nil { err = errors.Wrap(err, "AddRecall-db-error") log.Error(err) row.Rollback() return err } count1 = count1 + 1 var batch string //for i := 0; i <= int(count1); i++ { batch = fmt.Sprintf("RB%s", c) // //count1 = count1 + 1 num := 3 - len(fmt.Sprintf("%d", count1)) for n := 0; n < num; n++ { batch = fmt.Sprintf("%s0", batch) } neckRingList := make([]*model.NeckRingListing, 0) err = row.In("REGIST_NUM_2", REGISTNUM2).Find(&neckRingList) if err != nil { err = errors.Wrap(err, "AddRecall-db-error") log.Error(err) row.Rollback() return err } factorymap := make(map[int64]int64) for _, n := range neckRingList { factorymap[n.FactoryId] = 1 } arg.Batch = batch arg.Count = int64(len(REGISTNUM2)) arg.Recall = int64(len(factorymap)) _, err = row.Insert(&arg) if err != nil { err = errors.Wrap(err, "AddRecall-db-error") log.Error(err) row.Rollback() return err } deliveryandrecallList := make([]*model.DeliveryAndRecall, 0) for _, item := range neckRingList { var deliveryandrecall model.DeliveryAndRecall deliveryandrecall.FactoryId = arg.Id deliveryandrecall.Status = 1 deliveryandrecall.RecallId = item.FactoryId deliveryandrecall.REGISTNUM2 = item.Code deliveryandrecallList = append(deliveryandrecallList, &deliveryandrecall) } _, err = row.Insert(&deliveryandrecallList) if err != nil { err = errors.Wrap(err, "AddRecall-db-error") log.Error(err) row.Rollback() return err } row.Cols("recall").In("REGIST_NUM_2", REGISTNUM2).Update(&model.NeckRingListing{Recall: 1}) err = row.Commit() if err != nil { err = errors.Wrap(err, "AddRecall-db-error") log.Error(err) row.Rollback() return err } return nil } func (d *Dao) GetRecall(index, pagesize int64, pastureid, batch string, startCount, endCount int64, createName, startDate, endDate string, factoryID, replacement int64) ([]*model.FactoryList, int64, error) { rows := d.db.NewSession() defer rows.Close() rows.Table("factory").Alias("f") rows.Join("inner", []string{"t_nz_farm", "fa"}, "f.FARM_UUID=fa.UUID") if batch != "" { rows.Where(" f.batch like ? ", "%"+batch+"%") } if index != 0 && pagesize != 0 { first := (index - 1) * pagesize rows.Limit(int(pagesize), int(first)) } if startCount > 0 { rows.Where(" f.count >= ? ", startCount) } if endCount > 0 { rows.Where(" f.count <= ? ", endCount) } if createName != "" { rows.Where(" f.createname like ? ", "%"+createName+"%") } if pastureid != "" { rows.Where(" f.FARM_UUID = ? ", pastureid) } if startDate != "" && endDate != "" { rows.Where(" f.createtime between ? and ? ", startDate, endDate) } if factoryID > 0 { rows.Where("f.id in(select recallid from deliveryandrecall where factoryid = ? and status = 0)", factoryID) } if replacement == 1 { rows.Where("( select count(1) from deliveryandrecall where recallid = f.id) =0 ") } rows.Where("f.genre = 2") dataList := make([]*model.FactoryList, 0) count, err := rows.Desc("f.createtime").FindAndCount(&dataList) if err != nil { err = errors.Wrap(err, "GetRecall-db-error") log.Error(err) return dataList, count, err } return dataList, count, nil }