package dao import ( "fonchain-artshow/cmd/model" "fonchain-artshow/pb/artShow" "fonchain-artshow/pkg/db" "fonchain-artshow/pkg/m" uuid "github.com/satori/go.uuid" "go.uber.org/zap" "gorm.io/gorm" "strconv" "strings" "time" ) func SaveArtShow(tx *gorm.DB, artShow *model.ArtShow) (err error) { uid, err := uuid.NewV4() if err != nil { return nil } artShow.ShowUID = uid.String() artShow.ShowSeq = strings.Replace(artShow.ShowSeq, "T", m.ARTSHOW_PREFIX, -1) num := countArtistNumInShowCurrentYear(artShow.ArtistUID) if num < 10 { artShow.ShowSeq = strings.Join([]string{artShow.ShowSeq, time.Now().Format("06"), "00" + strconv.FormatInt(num, 10)}, "") } else if num >= 10 && num < 100 { artShow.ShowSeq = strings.Join([]string{artShow.ShowSeq, time.Now().Format("06"), "0" + strconv.FormatInt(num, 10)}, "") } else if num >= 100 { artShow.ShowSeq = strings.Join([]string{artShow.ShowSeq, time.Now().Format("06"), strconv.FormatInt(num, 10)}, "") } err = tx.Model(&model.ArtShow{}).Create(&artShow).Error if err != nil { zap.L().Error("ArtShow err", zap.Error(err)) return } return } func UpdateArtShow(tx *gorm.DB, artShow *model.ArtShow) (err error) { err = tx.Model(&model.ArtShow{}).Omit("show_uid,operator").Where("show_uid = ?", artShow.ShowUID).Updates(artShow).Error if err != nil { zap.L().Error("ArtShow err", zap.Error(err)) return } return } func ArtShowListWithRel(in *artShow.ShowListReq) (err error, total int64, out []*model.ArtShowRes) { queryDB := db.DbArtShow.Table("art_show as a ").Omit("a.created_at").Distinct("a.id"). Select("a.show_uid, a.show_seq, a.show_name, a.artist_name, a.artist_uid, a.artwork_num, a.ruler, a.price, a.reward ,a.create_time, a.operator, a.is_show, case when c.deleted_at is null then c.address else '' end as address, case when c.deleted_at is null then c.show_time else '' end as show_time, a.created_at"). //Joins("left join artwork_price as b on a.show_uid = b.show_uid"). Joins("left join ( select * from show_rel where deleted_at is null ) as c on a.show_uid = c.show_uid ") countDB := db.DbArtShow.Table("art_show as a ").Distinct("a.id"). Select("a.show_uid, a.show_seq, a.show_name, a.artist_name, a.artist_uid, a.artwork_num, a.ruler, a.price, a.reward , a.create_time, a.operator, a.is_show, case when c.deleted_at is null then c.address else '' end as address, case when c.deleted_at is null then c.show_time else '' end as show_time, a.created_at"). //Joins("left join artwork_price as b on a.show_uid = b.show_uid"). Joins("left join ( select * from show_rel where deleted_at is null ) as c on a.show_uid = c.show_uid ") if in.ArtistName != "" { queryDB.Where(" a.artist_name like ? ", "%"+in.ArtistName+"%") countDB.Where(" a.artist_name like ? ", "%"+in.ArtistName+"%") } if in.ShowName != "" { queryDB.Where(" a.show_name like ? ", "%"+in.ShowName+"%") countDB.Where(" a.show_name like ? ", "%"+in.ShowName+"%") } if in.ShowSeq != "" { queryDB.Where(" a.show_seq like ? ", "%"+in.ShowSeq+"%") countDB.Where(" a.show_seq like ? ", "%"+in.ShowSeq+"%") } if in.StartShowTime != "" && in.EndShowTime != "" { queryDB.Where("c.show_time between ? and ?", in.StartShowTime, in.EndShowTime) countDB.Where("c.show_time between ? and ?", in.StartShowTime, in.EndShowTime) } if in.EndPrice != 0 { queryDB.Where(" a.price between ? and ?", in.StartPrice, in.EndPrice) countDB.Where(" a.price between ? and ?", in.StartPrice, in.EndPrice) } if in.EndReward != 0 { queryDB.Where(" a.reward between ? and ?", in.StartReward, in.EndReward) countDB.Where(" a.reward between ? and ?", in.StartReward, in.EndReward) } if in.Address != "" { queryDB.Where(" c.address like ?", "%"+in.Address+"%") countDB.Where(" c.address like ?", "%"+in.Address+"%") } if len(in.IsShow) > 0 { queryDB.Where(" a.is_show in ?", in.IsShow) countDB.Where(" a.is_show in ?", in.IsShow) } //selectDb := queryDB //countDb := queryDB out = make([]*model.ArtShowRes, 0) err = queryDB.Where("a.deleted_at is null").Offset(int((in.Page - 1) * in.PageSize)). Limit(int(in.PageSize)).Order("a.created_at desc ").Find(&out).Error if err != nil { zap.L().Error("ArtShowList Find err", zap.Error(err)) return } err = countDB.Where("a.deleted_at is null").Count(&total).Error if err != nil { zap.L().Error("ArtShowList Count err", zap.Error(err)) return } return } func _ArtShowList(in *artShow.ShowListReq) (err error, total int64, out []*model.ArtShowRes) { queryDB := db.DbArtShow.Table("art_show as a ").Omit("a.created_at").Distinct("a.id"). Select("a.show_uid, a.show_seq, a.show_name, a.artist_name, a.artist_uid, a.artwork_num, a.ruler, a.price, a.create_time, a.operator, a.is_show, c.address ,c.show_time, a.created_at"). Joins("left join show_rel as c on a.show_uid = c.show_uid ") countDB := db.DbArtShow.Table("art_show as a ").Distinct("a.id"). Select("a.show_uid, a.show_seq, a.show_name, a.artist_name, a.artist_uid, a.artwork_num, a.ruler, a.price, a.create_time, a.operator, a.is_show, c.address ,c.show_time, a.created_at"). Joins("left join show_rel as c on a.show_uid = c.show_uid ") if len(in.IsShow) > 0 { queryDB.Where(" is_show in ?", in.IsShow) countDB.Where(" is_show in ?", in.IsShow) } err = queryDB.Where("a.deleted_at is null and c.deleted_at is null").Offset(int((in.Page - 1) * in.PageSize)). Limit(int(in.PageSize)).Order("a.created_at desc ").Find(&out).Error if err != nil { zap.L().Error("ArtShowList Find err", zap.Error(err)) return } err = countDB.Where("a.deleted_at is null").Group("a.id").Count(&total).Error if err != nil { zap.L().Error("ArtShowList Count err", zap.Error(err)) return } return } func ArtShowList(in *artShow.ShowListReq) (err error, total int64, out []*model.ArtShowRes) { queryDB := db.DbArtShow.Table("art_show as a"). Select("a.show_uid, a.show_seq, a.show_name, a.artist_name, a.artist_uid, a.artwork_num, a.ruler, a.price, a.create_time, a.operator, a.is_show, a.created_at") countDB := db.DbArtShow.Table("art_show as a "). Select("a.show_uid, a.show_seq, a.show_name, a.artist_name, a.artist_uid, a.artwork_num, a.ruler, a.price, a.create_time, a.operator, a.is_show, a.created_at") if len(in.IsShow) > 0 { queryDB.Where(" a.is_show in ?", in.IsShow) countDB.Where(" a.is_show in ?", in.IsShow) } err = queryDB.Offset(int((in.Page - 1) * in.PageSize)). Limit(int(in.PageSize)).Order("a.created_at desc ").Find(&out).Error if err != nil { zap.L().Error("ArtShowList Find err", zap.Error(err)) return } err = countDB.Count(&total).Error if err != nil { zap.L().Error("ArtShowList Count err", zap.Error(err)) return } return } func ArtShowList_apply(applyUID string) (err error, out []*model.ArtShowRes) { out = make([]*model.ArtShowRes, 0) err = db.DbArtShow.Table("art_show as a ").Distinct("a.show_uid").Select("a.show_uid, a.show_seq, a.show_name, a.artist_name, a.artist_uid, a.artwork_num, a.ruler, a.price, a.reward, a.create_time, a.is_show, b.address ,b.show_time").Joins(" right join show_rel as b on a.show_uid = b.show_uid").Where("b.apply_uid = ? ", applyUID).Find(&out).Error if err != nil { zap.L().Error("ArtShowList_apply Find err", zap.Error(err)) return } return } func ArtShowListByApplyStatus(in *artShow.ShowListReq) (err error, total int64, out []*model.ArtShowRes) { out = make([]*model.ArtShowRes, 0) queryDB := db.DbArtShow.Table("art_show as a").Select("a.show_uid, a.show_seq, a.show_name, a.artist_name, a.artist_uid, a.artwork_num, a.ruler, a.price, a.create_time, a.is_show, b.address ,b.show_time").Joins(" left join show_rel as b on b.show_uid = a.show_uid").Where("a.is_show = ?", in.IsShow[len(in.IsShow)-1]) err = queryDB.Count(&total).Error if err != nil { zap.L().Error("ArtShowListByApplyStatus Count err", zap.Error(err)) return } err = queryDB.Offset(int((in.Page - 1) * in.PageSize)). Limit(int(in.PageSize)).Find(&out).Error if err != nil { zap.L().Error("ArtShowListByApplyStatus Find err", zap.Error(err)) return } return } func DelArtShow(tx *gorm.DB, showUid string) (err error) { err = tx.Where("show_uid = ?", showUid).Delete(&model.ArtShow{}).Error if err != nil { zap.L().Error("ArtShow delete err", zap.Error(err)) return } return nil } func UniqueShowName(showName string) (out *model.ArtShowRes, err error) { out = new(model.ArtShowRes) //err = db.DbArtShow.Table("art_show as a ").Select("a.show_uid").Joins("left join show_rel as b on a.show_uid = b.show_uid").Where("a.show_name = ?", showName).Find(&out).Error err = db.DbArtShow.Model(&model.ArtShow{}).Select("show_uid").Where("show_name = ?", showName).Find(&out).Error if err != nil { zap.L().Error("ArtShow Find err", zap.Error(err)) return } return } func QueryArtShow(showUids []string) (err error, out []*model.ArtShowRes) { out = make([]*model.ArtShowRes, 0) err = db.DbArtShow.Table("art_show as a ").Select("a.show_uid, a.show_seq, a.show_name, a.artist_name, a.artist_uid, a.artwork_num, a.ruler, a.price, a.create_time, a.operator,a.is_show, b.address ,b.show_time").Joins("left join show_rel as b on a.show_uid = b.show_uid").Where("a.show_uid in ? and b.deleted_at is null ", showUids).Find(&out).Error if err != nil { zap.L().Error("ArtShow Find err", zap.Error(err)) return } return } func ShowRelDetail(in *artShow.ShowDetailReq) (err error, out []*model.ArtShowRes) { out = make([]*model.ArtShowRes, 0) query := db.DbArtShow.Table("art_show as a ").Select("a.show_uid, a.show_seq, a.show_name, a.artist_name, a.artist_uid, a.artwork_num, a.ruler, a.price, a.create_time, a.operator,a.is_show, b.address ,b.show_time").Joins("left join show_rel as b on a.show_uid = b.show_uid").Where("a.show_uid in ? ", in.ShowUID) if in.ApplyID != "" { query.Where(" b.apply_uid = ? ", in.ApplyID) } err = query.Find(&out).Error if err != nil { zap.L().Error("ShowRelDetail Find err", zap.Error(err)) return } return } func ShowStatistical(isShow int32) (err error, artistNum, packageNum, totalNum, NotShowNum, ShowHisNum int64) { artistNum = 0 err = db.DbArtShow.Model(&model.ArtShow{}).Distinct("artist_name").Where("is_show = ?", isShow).Count(&artistNum).Error if err != nil { zap.L().Error("ShowStatistical artistNum count err", zap.Error(err)) return } packageNum = 0 err = db.DbArtShow.Model(&model.ArtShow{}).Where("is_show = ?", isShow).Count(&packageNum).Error if err != nil { zap.L().Error("ShowStatistical packageNum count err", zap.Error(err)) return } totalNum = 0 err = db.DbArtShow.Model(&model.ArtShow{}).Count(&totalNum).Error if err != nil { zap.L().Error("ShowStatistical totalNum count err", zap.Error(err)) return } NotShowNum = 0 err = db.DbArtShow.Table("art_show as a").Distinct("a.artist_uid").Joins(" join artwork_price as b on b.show_uid = a.show_uid").Where("a.is_show in (1,2) and a.deleted_at is null").Count(&NotShowNum).Error if err != nil { zap.L().Error("ShowStatistical totalNum count err", zap.Error(err)) return } ShowHisNum = 0 err = db.DbArtShow.Table("art_show as a").Distinct("a.artist_uid").Joins(" join artwork_price as b on b.show_uid = a.show_uid").Where("a.is_show = 3 and a.deleted_at is null").Count(&ShowHisNum).Error if err != nil { zap.L().Error("ShowStatistical totalNum count err", zap.Error(err)) return } return } func QueryArtShowForArtwork(in *artShow.ShowListForArtworkReq) (err error, total int64, out []*model.ArtShowRes) { // 查询 画家已出展 画展包 queryDB := db.DbArtShow.Table("art_show as a ").Select("a.show_uid, a.show_seq, a.show_name, a.artist_name, a.artist_uid, a.artwork_num, a.ruler, a.price, a.create_time, a.is_show, b.address ,b.show_time").Joins("left join show_rel as b on a.show_uid = b.show_uid").Where("a.artist_uid = ? and a.is_show = ?", in.ArtistUID, 3) err = queryDB.Count(&total).Error if err != nil { zap.L().Error("QueryArtShowForArtwork count err", zap.Error(err)) return } out = make([]*model.ArtShowRes, 0) err = queryDB.Offset(int((in.Page - 1) * in.PageSize)). Limit(int(in.PageSize)).Find(&out).Error if err != nil { zap.L().Error("QueryArtShowForArtwork err", zap.Error(err)) return } return } func countArtistNumInShowCurrentYear(artistUid string) (num int64) { err := db.DbArtShow.Table("art_show").Where("artist_uid = ? and date_format(created_at,\"%Y\") = ? ", artistUid, time.Now().Format("2006")).Count(&num).Error if err != nil { return -1 } num = num + 1 return }