123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- package dao
- import (
- "demo/internal/model"
- "fmt"
- "github.com/pkg/errors"
- "github.com/siddontang/go/log"
- "strconv"
- "strings"
- "sync"
- "time"
- )
- func (d *Dao) AddFactory(arg model.Factory, recall []int64) error {
- row := d.db.NewSession()
- defer row.Close()
- row.Begin()
- c := time.Now().Format("20060102")
- count1, err := row.Where(" batch like ? ", "%"+fmt.Sprintf("EB%s", c)+"%").Count(&model.Factory{})
- if err != nil {
- err = errors.Wrap(err, "AddFactory-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("EB%s", c)
- // //count1 = count1 + 1
- num := 3 - len(fmt.Sprintf("%d", count1))
- for n := 0; n < num; n++ {
- batch = fmt.Sprintf("%s0", batch)
- }
- batch = fmt.Sprintf("%s%d", batch, count1)
- //}
- arg.Batch = batch
- arg.Recall = int64(len(recall))
- _, err = row.InsertOne(&arg)
- if err != nil {
- err = errors.Wrap(err, "AddFactory-db-error")
- log.Error(err)
- row.Rollback()
- return err
- }
- err = row.Commit()
- if err != nil {
- err = errors.Wrap(err, "AddFactory-db-error")
- log.Error(err)
- row.Rollback()
- return err
- }
- row.Begin()
- now := time.Now()
- if arg.Genre == 0 {
- //count, err := row.Where(" code like ? ", "%"+fmt.Sprintf("bh%s", c)+"%").Count(&model.NeckRingListing{})
- //if err != nil {
- // err = errors.Wrap(err, "AddFactory-db-error")
- // log.Error(err)
- // row.Rollback()
- // return err
- //}
- var wg sync.WaitGroup
- dataList := make([]*model.NeckRingListing, 0)
- for i := 0; i < int(arg.Count); i++ {
- wg.Add(1)
- go func() {
- defer wg.Done()
- insertarg := &model.NeckRingListing{
- Pastureid: arg.Pastureid,
- Code: 0,
- //Status: 0,
- PurchaseDate: now,
- CreateDate: now,
- FactoryId: arg.Id,
- }
- dataList = append(dataList, insertarg)
- }()
- }
- wg.Wait()
- _, err := row.Insert(dataList)
- if err != nil {
- err = errors.Wrap(err, "AddFactory-db-error")
- log.Error(err)
- row.Rollback()
- return err
- }
- _, err = row.Exec(` update t_nz_jbqid SET REGIST_NUM_2 = id WHERE (CREATETIME = ?) `, now.Format("2006-01-02 15:04:05"))
- if err != nil {
- err = errors.Wrap(err, "AddFactory-db-error")
- log.Error(err)
- row.Rollback()
- return err
- }
- } else {
- //TODO 置换
- deliveryandrecallList := make([]*model.DeliveryAndRecall, 0)
- for _, recallId := range recall {
- var deliveryandrecall model.DeliveryAndRecall
- deliveryandrecall.FactoryId = arg.Id
- deliveryandrecall.Status = 0
- deliveryandrecall.RecallId = recallId
- deliveryandrecallList = append(deliveryandrecallList, &deliveryandrecall)
- }
- _, err := row.Insert(&deliveryandrecallList)
- if err != nil {
- err = errors.Wrap(err, "AddFactory-db-error")
- log.Error(err)
- row.Rollback()
- return err
- }
- count, err := row.In("factoryid", recall).And("status = 1").Count(new(model.DeliveryAndRecall))
- if err != nil {
- err = errors.Wrap(err, "AddFactory-db-error")
- log.Error(err)
- row.Rollback()
- return err
- }
- dataList := make([]*model.NeckRingListing, 0)
- for i := 0; i < int(count); i++ {
- //wg.Add(1)
- //go func() {
- // defer wg.Done()
- insertarg := &model.NeckRingListing{
- Pastureid: arg.Pastureid,
- Code: 0,
- Recall: 0,
- PurchaseDate: now,
- CreateDate: now,
- FactoryId: arg.Id,
- }
- //go func() {
- //_, err := row.Insert(insertarg)
- //if err != nil {
- // err = errors.Wrap(err, "AddFactory-db-error")
- // log.Error(err)
- // row.Rollback()
- // return err
- //}
- dataList = append(dataList, insertarg)
- //}()
- }
- _, err = row.Insert(dataList)
- if err != nil {
- err = errors.Wrap(err, "AddFactory-db-error")
- log.Error(err)
- row.Rollback()
- return err
- }
- //for _, item := range dataList {
- // item.Code = item.Id
- // _, err = row.Cols("REGIST_NUM_2").Where("id = ?", item.Id).Update(item)
- // if err != nil {
- // err = errors.Wrap(err, "AddFactory-db-error")
- // log.Error(err)
- // row.Rollback()
- // return err
- // }
- //}
- _, err = row.Exec(` update t_nz_jbqid SET REGIST_NUM_2 = id WHERE (CREATETIME = ?) `, now.Format("2006-01-02 15:04:05"))
- if err != nil {
- err = errors.Wrap(err, "AddFactory-db-error")
- log.Error(err)
- row.Rollback()
- return err
- }
- //wg.Wait()
- var recallstr []string
- for _, r := range recall {
- recallstr = append(recallstr, strconv.FormatInt(r, 10))
- }
- _, err = row.Exec(fmt.Sprintf(`update t_nz_jbqid set recall = 2 where id in(
- select recallid from deliveryandrecall where factoryid in(%s) and status = 1) and recall = 1 `, strings.Join(recallstr, ",")))
- if err != nil {
- err = errors.Wrap(err, "AddFactory-db-error")
- log.Error(err)
- row.Rollback()
- return err
- }
- }
- err = row.Commit()
- if err != nil {
- err = errors.Wrap(err, "AddFactory-db-error")
- log.Error(err)
- row.Rollback()
- return err
- }
- return nil
- }
- func (d *Dao) GetFactory(index, pagesize int64, pastureid, batch string, genre, startCount, endCount int64, createName, startDate, endDate string, recallID 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 genre != -1 {
- rows.Where(" f.genre = ? ", genre)
- }
- 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 startDate != "" && endDate != "" {
- rows.Where(" f.createtime between ? and ? ", startDate, endDate)
- }
- if pastureid != "" {
- rows.Where(" f.FARM_UUID = ? ", pastureid)
- }
- if recallID > 0 {
- rows.Where("f.id in(select recallid from deliveryandrecall where factoryid = ? and status = 1)", recallID)
- }
- 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, "GetFactory-db-error")
- log.Error(err)
- return dataList, count, err
- }
- return dataList, count, nil
- }
|