123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- package flashbuy
-
- import (
- "spaceofcheng/services/models"
- "spaceofcheng/services/models/model"
- "spaceofcheng/services/utils"
- "strconv"
- "strings"
- "time"
-
- "github.com/go-xorm/xorm"
- )
-
- // FlashbuyDAO 当前数据库操作对象
- type FlashbuyDAO struct {
- ctx *utils.Context
- db *xorm.Session
- }
-
- // NewFlashbuyDAO New Inst
- func NewFlashbuyDAO(ctx *utils.Context) *FlashbuyDAO {
- return &FlashbuyDAO{
- ctx: ctx,
- db: ctx.DB,
- }
- }
-
- type FlashBuy struct {
- model.TaFlashBuy `xorm:"extends"`
- CaseName string
- }
- type CustomerFlashBuy struct {
- model.TaCustomerFlashBuy `xorm:"extends"`
- CaseName string
- CustomerName string
- Phone string
- FlashBuyName string
- Name string
- }
- type CustomerFlashResult struct {
- model.TaCustomerFlashBuy `xorm:"extends"`
- FlashBuyName string
- }
- type CustomerFlashDetail struct {
- model.TaCustomerFlashBuy `xorm:"extends"`
- CustomerQrcode string
- CaseName string
- IsAttend int
- }
- type FlashBuyDetial struct {
- model.TaFlashBuy `xorm:"extends"`
- ActivityMainImg string
- ActivityTitle string
- ShareImg string
- ShareContent string
- }
-
- func (m *FlashbuyDAO) GetFlashBuyList(caseid, flashBuyName, flashBuyStatus string, page, pageSize int) ([]FlashBuy, error) {
- var flashBuy []FlashBuy
- sql := `SELECT
- a.*,
- b.case_name
- FROM
- ta_flash_buy a
- INNER JOIN sys_case b ON a.case_id = b.case_id
- Where a.status > ` + strconv.Itoa(models.STATUS_DEL) + `
- and a.case_id in('` + strings.Replace(caseid, ",", "','", -1) + `')`
- if flashBuyName != "" {
- sql += ` and a.flash_buy_name like '%` + flashBuyName + `%'`
- }
- if flashBuyStatus != "" {
- sql += ` and a.flash_buy_status ='` + flashBuyStatus + `'`
- }
- sql += ` order by a.create_date desc limit ` + strconv.Itoa((page-1)*pageSize) + `, ` + strconv.Itoa(pageSize)
- err := m.db.Sql(sql).Find(&flashBuy)
- return flashBuy, err
- }
-
- func (m *FlashbuyDAO) GetFlashBuyListCount(caseid, flashBuyName, flashBuyStatus string) (int, error) {
- var flashBuy []model.TaFlashBuy
- sql := `SELECT
- a.*
- FROM
- ta_flash_buy a
- Where a.status > ` + strconv.Itoa(models.STATUS_DEL) + `
- and a.case_id in('` + strings.Replace(caseid, ",", "','", -1) + `')`
- if flashBuyName != "" {
- sql += ` and a.flash_buy_name like '%` + flashBuyName + `%'`
- }
- if flashBuyStatus != "" {
- sql += ` and a.flash_buy_status ='` + flashBuyStatus + `'`
- }
- err := m.db.Sql(sql).Find(&flashBuy)
- return len(flashBuy), err
- }
-
- func (m *FlashbuyDAO) GetFlashBuyById(flashBuyId string) (*FlashBuyDetial, error) {
- var flashBuy []FlashBuyDetial
- sql := `SELECT
- a.*,
- b.activity_main_img,
- b.activity_title,
- b.share_img,
- b.share_content
- FROM
- ta_flash_buy a
- LEFT JOIN ta_activity_share_info b ON a.flash_buy_id = b.activity_id
- AND b.activity_type = '` + models.ACTIVITY_FLASH + `'
- WHERE a.flash_buy_id = '` + flashBuyId + `'`
- err := m.db.Sql(sql).Find(&flashBuy)
- if err != nil {
- return nil, err
- }
- if len(flashBuy) > 0 {
- return &flashBuy[0], nil
- }
- return nil, nil
- }
-
- func (m *FlashbuyDAO) AddNewFlashBuy(flashbuy model.TaFlashBuy) (*model.TaFlashBuy, error) {
- flashbuy.FlashBuyId = utils.GetGUID()
- flashbuy.Status = models.STATUS_NORMAL
- flashbuy.CreateDate = time.Now()
- _, err := m.db.Insert(flashbuy)
- return &flashbuy, err
- }
-
- func (m *FlashbuyDAO) DeleteFlashBuy(flashBuyId string) error {
- var flashBuy = model.TaFlashBuy{
- FlashBuyId: flashBuyId,
- Status: models.STATUS_DEL,
- }
- var cols = []string{
- "status",
- }
- _, err := m.db.Cols(cols...).Where("flash_buy_id = ?", flashBuy.FlashBuyId).Update(flashBuy)
- return err
- }
-
- func (m *FlashbuyDAO) UpdateFlashBuy(flashBuyId, flashBuyStatus string) error {
- var flashBuy = model.TaFlashBuy{
- FlashBuyId: flashBuyId,
- FlashBuyStatus: flashBuyStatus,
- }
- var cols = []string{
- "flash_buy_status",
- }
- _, err := m.db.Cols(cols...).Where("flash_buy_id =?", flashBuy.FlashBuyId).Update(flashBuy)
- return err
- }
-
- func (m *FlashbuyDAO) EditFlashBuy(flashBuy model.TaFlashBuy) error {
- var cols = []string{
- "flash_buy_name",
- "flash_buy_info",
- }
- _, err := m.db.Cols(cols...).Where("flash_buy_id = ?", flashBuy.FlashBuyId).Update(flashBuy)
- return err
- }
- func (m *FlashbuyDAO) VerifyFlashBuy(customerFlashBuyId string) error {
- var customerFlashBuy = model.TaCustomerFlashBuy{
- CustomerFlashBuyId: customerFlashBuyId,
- VerifyStatus: models.VERIFY_USED,
- VerifyDate: time.Now(),
- }
- var cols = []string{
- "verify_status",
- "verify_date",
- }
- _, err := m.db.Cols(cols...).Where("customer_flash_buy_id = ?", customerFlashBuy.CustomerFlashBuyId).Update(customerFlashBuy)
- return err
- }
-
- func (m *FlashbuyDAO) GetCustomerFlashBuyById(flashBuyId, phone string, page, pageSize int) ([]CustomerFlashBuy, error) {
- var customerFlashBuy []CustomerFlashBuy
- sql := `SELECT
- a.*,
- b.customer_name,
- b.phone,
- b.name,
- c.case_name,
- d.flash_buy_id
- FROM
- ta_customer_flash_buy a
- INNER JOIN ta_customer b ON a.customer_id = b.customer_id
- INNER JOIN sys_case c ON a.case_id = c.case_id
- INNER JOIN ta_flash_buy d ON a.flash_buy_id = d.flash_buy_id
- WHERE
- a.flash_buy_id = '` + flashBuyId + `'
- and a.status >` + strconv.Itoa(models.STATUS_DEL)
- if phone != "" {
- sql += ` and b.phone like '%` + phone + `%'`
- }
- sql += ` order by a.create_date desc limit ` + strconv.Itoa((page-1)*pageSize) + `, ` + strconv.Itoa(pageSize)
- err := m.db.Sql(sql).Find(&customerFlashBuy)
- return customerFlashBuy, err
-
- }
-
- func (m *FlashbuyDAO) GetCustomerFlashBuyExcel(flashBuyId, phone string) ([]CustomerFlashBuy, error) {
- var customerFlashBuy []CustomerFlashBuy
- sql := `SELECT
- a.*,
- b.customer_name,
- b.phone,
- b.name,
- c.case_name,
- d.flash_buy_id
- FROM
- ta_customer_flash_buy a
- INNER JOIN ta_customer b ON a.customer_id = b.customer_id
- INNER JOIN sys_case c ON a.case_id = c.case_id
- INNER JOIN ta_flash_buy d ON a.flash_buy_id = d.flash_buy_id
- WHERE
- a.flash_buy_id = '` + flashBuyId + `'
- and a.status >` + strconv.Itoa(models.STATUS_DEL)
- if phone != "" {
- sql += ` and b.phone like '%` + phone + `%'`
- }
- sql += ` order by a.create_date desc`
- err := m.db.Sql(sql).Find(&customerFlashBuy)
- return customerFlashBuy, err
-
- }
-
- func (m *FlashbuyDAO) GetCustomerFlashBuyByIdCount(flashBuyId, phone string) (int, error) {
- var customerFlashBuy []CustomerFlashBuy
- sql := `SELECT
- a.*,
- b.customer_name,
- b.phone,
- c.case_name
- FROM
- ta_customer_flash_buy a
- INNER JOIN ta_customer b ON a.customer_id = b.customer_id
- INNER JOIN sys_case c ON a.case_id = c.case_id
- WHERE
- a.flash_buy_id = '` + flashBuyId + `'
- and a.status >` + strconv.Itoa(models.STATUS_DEL)
- if phone != "" {
- sql += ` and b.phone like '%` + phone + `%'`
- }
- err := m.db.Sql(sql).Find(&customerFlashBuy)
- return len(customerFlashBuy), err
- }
-
- func (m *FlashbuyDAO) GetCustomerFlashBuyByQr(customerFlashBuyId, caseId string) (*CustomerFlashBuy, error) {
- var customerFlashBuy []CustomerFlashBuy
- sql := `SELECT
- a.*,
- b.customer_name,
- b.phone,
- c.case_name,
- d.flash_buy_name
- FROM
- ta_customer_flash_buy a
- INNER JOIN ta_customer b ON a.customer_id = b.customer_id
- INNER JOIN sys_case c ON a.case_id = c.case_id
- INNER JOIN ta_flash_buy d ON a.flash_buy_id = d.flash_buy_id
- WHERE
- a.customer_flash_buy_id ='` + customerFlashBuyId + `' and a.case_id = '` + caseId + `'`
- sql += ` and a.validate_start <= now() and a.validate_end >= now()`
- err := m.db.Sql(sql).Find(&customerFlashBuy)
- if len(customerFlashBuy) > 0 {
- return &customerFlashBuy[0], err
- }
- return nil, nil
- }
-
- func (m *FlashbuyDAO) GetCustomerFlashBuyByCustomerId(customerId string, page, pageSize int) ([]CustomerFlashResult, error) {
- var customerResult []CustomerFlashResult
- sql := `select a.*,b.flash_buy_name
- from ta_customer_flash_buy a
- inner join ta_flash_buy b
- on a.flash_buy_id = b.flash_buy_id
- where a.customer_id = '` + customerId + `'`
- sql += ` order by a.validate_start desc limit ` + strconv.Itoa((page-1)*pageSize) + `, ` + strconv.Itoa(pageSize)
- err := m.db.Sql(sql).Find(&customerResult)
- return customerResult, err
-
- }
-
- func (m *FlashbuyDAO) GetCustomerFlashBuyByCustomerIdCount(customerId string) (int, error) {
- var customerResult []CustomerFlashResult
- sql := `select a.*,b.flash_buy_name
- from ta_customer_flash_buy a
- inner join ta_flash_buy b
- on a.flash_buy_id = b.flash_buy_id
- where a.customer_id = '` + customerId + `'`
- err := m.db.Sql(sql).Find(&customerResult)
- return len(customerResult), err
-
- }
-
- func (m *FlashbuyDAO) GetCustomerFlashBuyId(customerFlashBuyId string) (*CustomerFlashDetail, error) {
- var customerDetail []CustomerFlashDetail
- sql := `SELECT
- a.*,
- b.customer_qrcode,
- c.case_name
- FROM
- ta_customer_flash_buy a
- INNER JOIN ta_customer_course_qrcode b ON a.customer_flash_buy_id = b.customer_course_id
- INNER JOIN sys_case c ON a.case_id = c.case_id
- WHERE a.customer_flash_buy_id = '` + customerFlashBuyId + `'`
- err := m.db.Sql(sql).Find(&customerDetail)
- if len(customerDetail) > 0 {
- return &customerDetail[0], err
- }
- return nil, err
- }
-
- // GetCustomerFlashBuy 获取用户抢购信息
- func (m *FlashbuyDAO) GetCustomerFlashBuy(id, customerid string) ([]model.TaCustomerFlashBuy, error) {
- var buys []model.TaCustomerFlashBuy
- err := m.db.Where("flash_buy_id=?", id).And("customer_id=?", customerid).And("status>?", models.STATUS_DEL).Find(&buys)
- return buys, err
- }
-
- // UpdateFlashBuyJoin 更新抢购参与人数
- func (m *FlashbuyDAO) UpdateFlashBuyJoin(id string) (int64, error) {
- sql := `update ta_flash_buy set join_num = join_num + 1 where flash_buy_id=? and join_num < flash_buy_max_attendant`
- result, err := m.db.Exec(sql, id)
- if err != nil {
- return 0, err
- }
-
- rowCount, err := result.RowsAffected()
- return rowCount, err
- }
-
- // SaveCustomerFlashBuy 新增用户抢购信息
- func (m *FlashbuyDAO) SaveCustomerFlashBuy(cstFlashBuy model.TaCustomerFlashBuy) (*model.TaCustomerFlashBuy, error) {
- cstFlashBuy.Status = models.STATUS_NORMAL
- cstFlashBuy.CustomerFlashBuyId = utils.GetGUID()
- cstFlashBuy.CreateDate = time.Now()
- cstFlashBuy.VerifyStatus = models.VERIFY_USEABLE
- _, err := m.db.Insert(cstFlashBuy)
- return &cstFlashBuy, err
- }
-
- func (m *FlashbuyDAO) GetFlashModelList() ([]model.TdFlashbuyModel, error) {
- var model []model.TdFlashbuyModel
- sql := `select * from td_flashbuy_model`
- err := m.db.Sql(sql).Find(&model)
- return model, err
- }
-
- func (m *FlashbuyDAO) AddNewFlashBuyCustomer(customer model.TaFlashBuyCustomer) error {
- customer.CreateDate = time.Now()
- customer.FlashBuyCustomerId = utils.GetGUID()
- customer.IsAttend = 1
- _, err := m.db.Insert(customer)
- return err
- }
-
- func (m *FlashbuyDAO) UpdateFlashBuyCustomer(customerId, flashBuyId string) error {
- var customer = model.TaFlashBuyCustomer{
- CustomerId: customerId,
- IsNew: 0,
- IsAttend: 0,
- }
- var cols = []string{
- "is_new",
- "is_attend",
- }
- _, err := m.db.Cols(cols...).Where("customer_id = ?", customer.CustomerId).And("flash_buy_id =?", flashBuyId).Update(customer)
- return err
- }
-
- func (m *FlashbuyDAO) IsFlashBuyCustomer(customerId, flashBuyId string) (*model.TaFlashBuyCustomer, error) {
- var customer []model.TaFlashBuyCustomer
- sql := `SELECT
- *
- FROM
- ta_flash_buy_customer
- WHERE
- customer_id = '` + customerId + `'
- and flash_buy_id = '` + flashBuyId + `'`
- err := m.db.Sql(sql).Find(&customer)
- if len(customer) <= 0 {
- return nil, err
- }
- return &customer[0], err
- }
-
- func (m *FlashbuyDAO) IsRecord(customerId, flashBuyId string) (int, error) {
- var customer []model.TaFlashBuyCustomer
- sql := `SELECT
- *
- FROM
- ta_flash_buy_customer
- WHERE
- customer_id = '` + customerId + `'
- and flash_buy_id = '` + flashBuyId + `'`
- err := m.db.Sql(sql).Find(&customer)
- return len(customer), err
- }
|