package dao import ( "demo/internal/model" "fmt" "github.com/pkg/errors" "github.com/siddontang/go/log" "strings" ) func (d *Dao) GetNeckRingListing(index, pagesize int64, code int64, barName, purchaseStart, purchaseEnd, manufactureStart, manufactureEnd string, status int64, cattle string, recallID, factoryID int64) ([]*model.NeckRingListingList, int64, error) { dataList := make([]*model.NeckRingListingList, 0) rows := d.db.NewSession().Table("t_nz_jbqid").Alias("j") rows.Join("inner", []string{"t_nz_farm", "f"}, "j.FARM_UUID=f.UUID") if recallID == 0 && factoryID == 0 { rows.Select(`j.id,j.FARM_UUID ,j.REGIST_NUM_2,j.BUY_DATE,j.ENTER_DATE,j.START_DATE,j.ISUSED as status,j.STOP_DATE,j.CREATETIME`) } else { rows.Join("left", []string{"factory", "fa"}, " fa.id = j.factoryid ") rows.Select(`j.id,j.FARM_UUID ,j.REGIST_NUM_2,j.BUY_DATE,j.ENTER_DATE,j.START_DATE,j.ISUSED as status,j.STOP_DATE,j.CREATETIME ,fa.createtime,fa.batch`) } defer rows.Close() if code != 0 { rows.Where("j.REGIST_NUM_2 = ? ", code) } if barName != "" || cattle != "" { rows.Join("left", []string{"t_nz_cow", "c"}, " j.FARM_UUID=c.FARM_UUID And j.REGIST_NUM_2=c.REGIST_NUM_2") rows.Join("left", []string{"t_jc_house", "h"}, " c.HOUSE_UUID=h.UUID ") } if barName != "" { rows.Where("h.HOUSE_NAME like ? ", "%"+barName+"%") } if purchaseStart != "" && purchaseEnd != "" { rows.Where(" j.BUY_DATE between ? and ? ", purchaseStart, purchaseEnd) } if manufactureStart != "" && manufactureEnd != "" { rows.Where(" j.ENTER_DATE between ? and ? ", manufactureStart, manufactureEnd) } if status > 0 { if status == 0 { rows.Where("j.STOP_DATE is not null ") } else { rows.Where("j.STOP_DATE is null ") } } if cattle != "" { rows.Where("c.COW_NUM = ? ", cattle) } if recallID > 0 { rows.Where("REGIST_NUM_2 in(select REGIST_NUM_2 from deliveryandrecall where factoryid = ? and status = 1)", recallID) } if factoryID > 0 { rows.Where("factoryid =? ", factoryID) } if index != 0 && pagesize != 0 { first := (index - 1) * pagesize rows.Limit(int(pagesize), int(first)) } count, err := rows.Desc("j.CREATETIME").FindAndCount(&dataList) if err != nil { err = errors.Wrap(err, "GetNeckRingListing-db-error") log.Error(err) return dataList, count, err } return dataList, count, nil } func (d *Dao) GetFactoryidByREGISTNUM2(REGISTNUM2 []string) ([]*model.NeckRingListing, error) { rows := d.db.NewSession() defer rows.Close() rows.In("REGIST_NUM_2", REGISTNUM2) dataList := make([]*model.NeckRingListing, 0) err := rows.Cols("factoryid").GroupBy("factoryid").Find(&dataList) if err != nil { err = errors.Wrap(err, "GetFactoryidByREGISTNUM2-db-error") log.Error(err) return dataList, err } return dataList, nil } func (d *Dao) GetHOUSENAME(codeList []string) ([]map[string]string, error) { rows := d.db.NewSession() dataList, err := rows.SQL(fmt.Sprintf(`select c.COW_NUM, h.HOUSE_NAME ,c.REGIST_NUM_2, c.FARM_UUID from t_nz_cow c JOIN t_jc_house h ON c.HOUSE_UUID = h.UUID where c.del = 0 and c.REGIST_NUM_2 in(%s)`, strings.Join(codeList, ","))).QueryString() if err != nil { err = errors.Wrap(err, "GetFactoryidByREGISTNUM2-db-error") log.Error(err) return dataList, err } return dataList, nil } func (d *Dao) GetNeckRingListingIDByFactoryId(factoryId []int64) ([]*model.NeckRingListing, error) { rows := d.db.NewSession() defer rows.Close() dataList := make([]*model.NeckRingListing, 0) rows.In("factoryid", factoryId) rows.Where("recall = 0") err := rows.Cols("REGIST_NUM_2").Find(&dataList) if err != nil { err = errors.Wrap(err, "GetNeckRingListingIDByFactoryId-db-error") log.Error(err) return dataList, err } return dataList, err }