flashbuy.go 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. package flashbuy
  2. import (
  3. "spaceofcheng/services/models"
  4. "spaceofcheng/services/models/model"
  5. "spaceofcheng/services/utils"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "github.com/go-xorm/xorm"
  10. )
  11. // FlashbuyDAO 当前数据库操作对象
  12. type FlashbuyDAO struct {
  13. ctx *utils.Context
  14. db *xorm.Session
  15. }
  16. // NewFlashbuyDAO New Inst
  17. func NewFlashbuyDAO(ctx *utils.Context) *FlashbuyDAO {
  18. return &FlashbuyDAO{
  19. ctx: ctx,
  20. db: ctx.DB,
  21. }
  22. }
  23. type FlashBuy struct {
  24. model.TaFlashBuy `xorm:"extends"`
  25. CaseName string
  26. }
  27. type CustomerFlashBuy struct {
  28. model.TaCustomerFlashBuy `xorm:"extends"`
  29. CaseName string
  30. CustomerName string
  31. Phone string
  32. FlashBuyName string
  33. Name string
  34. }
  35. type CustomerFlashResult struct {
  36. model.TaCustomerFlashBuy `xorm:"extends"`
  37. FlashBuyName string
  38. }
  39. type CustomerFlashDetail struct {
  40. model.TaCustomerFlashBuy `xorm:"extends"`
  41. CustomerQrcode string
  42. CaseName string
  43. IsAttend int
  44. }
  45. type FlashBuyDetial struct {
  46. model.TaFlashBuy `xorm:"extends"`
  47. ActivityMainImg string
  48. ActivityTitle string
  49. ShareImg string
  50. ShareContent string
  51. }
  52. func (m *FlashbuyDAO) GetFlashBuyList(caseid, flashBuyName, flashBuyStatus string, page, pageSize int) ([]FlashBuy, error) {
  53. var flashBuy []FlashBuy
  54. sql := `SELECT
  55. a.*,
  56. b.case_name
  57. FROM
  58. ta_flash_buy a
  59. INNER JOIN sys_case b ON a.case_id = b.case_id
  60. Where a.status > ` + strconv.Itoa(models.STATUS_DEL) + `
  61. and a.case_id in('` + strings.Replace(caseid, ",", "','", -1) + `')`
  62. if flashBuyName != "" {
  63. sql += ` and a.flash_buy_name like '%` + flashBuyName + `%'`
  64. }
  65. if flashBuyStatus != "" {
  66. sql += ` and a.flash_buy_status ='` + flashBuyStatus + `'`
  67. }
  68. sql += ` order by a.create_date desc limit ` + strconv.Itoa((page-1)*pageSize) + `, ` + strconv.Itoa(pageSize)
  69. err := m.db.Sql(sql).Find(&flashBuy)
  70. return flashBuy, err
  71. }
  72. func (m *FlashbuyDAO) GetFlashBuyListCount(caseid, flashBuyName, flashBuyStatus string) (int, error) {
  73. var flashBuy []model.TaFlashBuy
  74. sql := `SELECT
  75. a.*
  76. FROM
  77. ta_flash_buy a
  78. Where a.status > ` + strconv.Itoa(models.STATUS_DEL) + `
  79. and a.case_id in('` + strings.Replace(caseid, ",", "','", -1) + `')`
  80. if flashBuyName != "" {
  81. sql += ` and a.flash_buy_name like '%` + flashBuyName + `%'`
  82. }
  83. if flashBuyStatus != "" {
  84. sql += ` and a.flash_buy_status ='` + flashBuyStatus + `'`
  85. }
  86. err := m.db.Sql(sql).Find(&flashBuy)
  87. return len(flashBuy), err
  88. }
  89. func (m *FlashbuyDAO) GetFlashBuyById(flashBuyId string) (*FlashBuyDetial, error) {
  90. var flashBuy []FlashBuyDetial
  91. sql := `SELECT
  92. a.*,
  93. b.activity_main_img,
  94. b.activity_title,
  95. b.share_img,
  96. b.share_content
  97. FROM
  98. ta_flash_buy a
  99. LEFT JOIN ta_activity_share_info b ON a.flash_buy_id = b.activity_id
  100. AND b.activity_type = '` + models.ACTIVITY_FLASH + `'
  101. WHERE a.flash_buy_id = '` + flashBuyId + `'`
  102. err := m.db.Sql(sql).Find(&flashBuy)
  103. if err != nil {
  104. return nil, err
  105. }
  106. if len(flashBuy) > 0 {
  107. return &flashBuy[0], nil
  108. }
  109. return nil, nil
  110. }
  111. func (m *FlashbuyDAO) AddNewFlashBuy(flashbuy model.TaFlashBuy) (*model.TaFlashBuy, error) {
  112. flashbuy.FlashBuyId = utils.GetGUID()
  113. flashbuy.Status = models.STATUS_NORMAL
  114. flashbuy.CreateDate = time.Now()
  115. _, err := m.db.Insert(flashbuy)
  116. return &flashbuy, err
  117. }
  118. func (m *FlashbuyDAO) DeleteFlashBuy(flashBuyId string) error {
  119. var flashBuy = model.TaFlashBuy{
  120. FlashBuyId: flashBuyId,
  121. Status: models.STATUS_DEL,
  122. }
  123. var cols = []string{
  124. "status",
  125. }
  126. _, err := m.db.Cols(cols...).Where("flash_buy_id = ?", flashBuy.FlashBuyId).Update(flashBuy)
  127. return err
  128. }
  129. func (m *FlashbuyDAO) UpdateFlashBuy(flashBuyId, flashBuyStatus string) error {
  130. var flashBuy = model.TaFlashBuy{
  131. FlashBuyId: flashBuyId,
  132. FlashBuyStatus: flashBuyStatus,
  133. }
  134. var cols = []string{
  135. "flash_buy_status",
  136. }
  137. _, err := m.db.Cols(cols...).Where("flash_buy_id =?", flashBuy.FlashBuyId).Update(flashBuy)
  138. return err
  139. }
  140. func (m *FlashbuyDAO) EditFlashBuy(flashBuy model.TaFlashBuy) error {
  141. var cols = []string{
  142. "flash_buy_name",
  143. "flash_buy_info",
  144. }
  145. _, err := m.db.Cols(cols...).Where("flash_buy_id = ?", flashBuy.FlashBuyId).Update(flashBuy)
  146. return err
  147. }
  148. func (m *FlashbuyDAO) VerifyFlashBuy(customerFlashBuyId string) error {
  149. var customerFlashBuy = model.TaCustomerFlashBuy{
  150. CustomerFlashBuyId: customerFlashBuyId,
  151. VerifyStatus: models.VERIFY_USED,
  152. VerifyDate: time.Now(),
  153. }
  154. var cols = []string{
  155. "verify_status",
  156. "verify_date",
  157. }
  158. _, err := m.db.Cols(cols...).Where("customer_flash_buy_id = ?", customerFlashBuy.CustomerFlashBuyId).Update(customerFlashBuy)
  159. return err
  160. }
  161. func (m *FlashbuyDAO) GetCustomerFlashBuyById(flashBuyId, phone string, page, pageSize int) ([]CustomerFlashBuy, error) {
  162. var customerFlashBuy []CustomerFlashBuy
  163. sql := `SELECT
  164. a.*,
  165. b.customer_name,
  166. b.phone,
  167. b.name,
  168. c.case_name,
  169. d.flash_buy_id
  170. FROM
  171. ta_customer_flash_buy a
  172. INNER JOIN ta_customer b ON a.customer_id = b.customer_id
  173. INNER JOIN sys_case c ON a.case_id = c.case_id
  174. INNER JOIN ta_flash_buy d ON a.flash_buy_id = d.flash_buy_id
  175. WHERE
  176. a.flash_buy_id = '` + flashBuyId + `'
  177. and a.status >` + strconv.Itoa(models.STATUS_DEL)
  178. if phone != "" {
  179. sql += ` and b.phone like '%` + phone + `%'`
  180. }
  181. sql += ` order by a.create_date desc limit ` + strconv.Itoa((page-1)*pageSize) + `, ` + strconv.Itoa(pageSize)
  182. err := m.db.Sql(sql).Find(&customerFlashBuy)
  183. return customerFlashBuy, err
  184. }
  185. func (m *FlashbuyDAO) GetCustomerFlashBuyExcel(flashBuyId, phone string) ([]CustomerFlashBuy, error) {
  186. var customerFlashBuy []CustomerFlashBuy
  187. sql := `SELECT
  188. a.*,
  189. b.customer_name,
  190. b.phone,
  191. b.name,
  192. c.case_name,
  193. d.flash_buy_id
  194. FROM
  195. ta_customer_flash_buy a
  196. INNER JOIN ta_customer b ON a.customer_id = b.customer_id
  197. INNER JOIN sys_case c ON a.case_id = c.case_id
  198. INNER JOIN ta_flash_buy d ON a.flash_buy_id = d.flash_buy_id
  199. WHERE
  200. a.flash_buy_id = '` + flashBuyId + `'
  201. and a.status >` + strconv.Itoa(models.STATUS_DEL)
  202. if phone != "" {
  203. sql += ` and b.phone like '%` + phone + `%'`
  204. }
  205. sql += ` order by a.create_date desc`
  206. err := m.db.Sql(sql).Find(&customerFlashBuy)
  207. return customerFlashBuy, err
  208. }
  209. func (m *FlashbuyDAO) GetCustomerFlashBuyByIdCount(flashBuyId, phone string) (int, error) {
  210. var customerFlashBuy []CustomerFlashBuy
  211. sql := `SELECT
  212. a.*,
  213. b.customer_name,
  214. b.phone,
  215. c.case_name
  216. FROM
  217. ta_customer_flash_buy a
  218. INNER JOIN ta_customer b ON a.customer_id = b.customer_id
  219. INNER JOIN sys_case c ON a.case_id = c.case_id
  220. WHERE
  221. a.flash_buy_id = '` + flashBuyId + `'
  222. and a.status >` + strconv.Itoa(models.STATUS_DEL)
  223. if phone != "" {
  224. sql += ` and b.phone like '%` + phone + `%'`
  225. }
  226. err := m.db.Sql(sql).Find(&customerFlashBuy)
  227. return len(customerFlashBuy), err
  228. }
  229. func (m *FlashbuyDAO) GetCustomerFlashBuyByQr(customerFlashBuyId, caseId string) (*CustomerFlashBuy, error) {
  230. var customerFlashBuy []CustomerFlashBuy
  231. sql := `SELECT
  232. a.*,
  233. b.customer_name,
  234. b.phone,
  235. c.case_name,
  236. d.flash_buy_name
  237. FROM
  238. ta_customer_flash_buy a
  239. INNER JOIN ta_customer b ON a.customer_id = b.customer_id
  240. INNER JOIN sys_case c ON a.case_id = c.case_id
  241. INNER JOIN ta_flash_buy d ON a.flash_buy_id = d.flash_buy_id
  242. WHERE
  243. a.customer_flash_buy_id ='` + customerFlashBuyId + `' and a.case_id = '` + caseId + `'`
  244. sql += ` and a.validate_start <= now() and a.validate_end >= now()`
  245. err := m.db.Sql(sql).Find(&customerFlashBuy)
  246. if len(customerFlashBuy) > 0 {
  247. return &customerFlashBuy[0], err
  248. }
  249. return nil, nil
  250. }
  251. func (m *FlashbuyDAO) GetCustomerFlashBuyByCustomerId(customerId string, page, pageSize int) ([]CustomerFlashResult, error) {
  252. var customerResult []CustomerFlashResult
  253. sql := `select a.*,b.flash_buy_name
  254. from ta_customer_flash_buy a
  255. inner join ta_flash_buy b
  256. on a.flash_buy_id = b.flash_buy_id
  257. where a.customer_id = '` + customerId + `'`
  258. sql += ` order by a.validate_start desc limit ` + strconv.Itoa((page-1)*pageSize) + `, ` + strconv.Itoa(pageSize)
  259. err := m.db.Sql(sql).Find(&customerResult)
  260. return customerResult, err
  261. }
  262. func (m *FlashbuyDAO) GetCustomerFlashBuyByCustomerIdCount(customerId string) (int, error) {
  263. var customerResult []CustomerFlashResult
  264. sql := `select a.*,b.flash_buy_name
  265. from ta_customer_flash_buy a
  266. inner join ta_flash_buy b
  267. on a.flash_buy_id = b.flash_buy_id
  268. where a.customer_id = '` + customerId + `'`
  269. err := m.db.Sql(sql).Find(&customerResult)
  270. return len(customerResult), err
  271. }
  272. func (m *FlashbuyDAO) GetCustomerFlashBuyId(customerFlashBuyId string) (*CustomerFlashDetail, error) {
  273. var customerDetail []CustomerFlashDetail
  274. sql := `SELECT
  275. a.*,
  276. b.customer_qrcode,
  277. c.case_name
  278. FROM
  279. ta_customer_flash_buy a
  280. INNER JOIN ta_customer_course_qrcode b ON a.customer_flash_buy_id = b.customer_course_id
  281. INNER JOIN sys_case c ON a.case_id = c.case_id
  282. WHERE a.customer_flash_buy_id = '` + customerFlashBuyId + `'`
  283. err := m.db.Sql(sql).Find(&customerDetail)
  284. if len(customerDetail) > 0 {
  285. return &customerDetail[0], err
  286. }
  287. return nil, err
  288. }
  289. // GetCustomerFlashBuy 获取用户抢购信息
  290. func (m *FlashbuyDAO) GetCustomerFlashBuy(id, customerid string) ([]model.TaCustomerFlashBuy, error) {
  291. var buys []model.TaCustomerFlashBuy
  292. err := m.db.Where("flash_buy_id=?", id).And("customer_id=?", customerid).And("status>?", models.STATUS_DEL).Find(&buys)
  293. return buys, err
  294. }
  295. // UpdateFlashBuyJoin 更新抢购参与人数
  296. func (m *FlashbuyDAO) UpdateFlashBuyJoin(id string) (int64, error) {
  297. sql := `update ta_flash_buy set join_num = join_num + 1 where flash_buy_id=? and join_num < flash_buy_max_attendant`
  298. result, err := m.db.Exec(sql, id)
  299. if err != nil {
  300. return 0, err
  301. }
  302. rowCount, err := result.RowsAffected()
  303. return rowCount, err
  304. }
  305. // SaveCustomerFlashBuy 新增用户抢购信息
  306. func (m *FlashbuyDAO) SaveCustomerFlashBuy(cstFlashBuy model.TaCustomerFlashBuy) (*model.TaCustomerFlashBuy, error) {
  307. cstFlashBuy.Status = models.STATUS_NORMAL
  308. cstFlashBuy.CustomerFlashBuyId = utils.GetGUID()
  309. cstFlashBuy.CreateDate = time.Now()
  310. cstFlashBuy.VerifyStatus = models.VERIFY_USEABLE
  311. _, err := m.db.Insert(cstFlashBuy)
  312. return &cstFlashBuy, err
  313. }
  314. func (m *FlashbuyDAO) GetFlashModelList() ([]model.TdFlashbuyModel, error) {
  315. var model []model.TdFlashbuyModel
  316. sql := `select * from td_flashbuy_model`
  317. err := m.db.Sql(sql).Find(&model)
  318. return model, err
  319. }
  320. func (m *FlashbuyDAO) AddNewFlashBuyCustomer(customer model.TaFlashBuyCustomer) error {
  321. customer.CreateDate = time.Now()
  322. customer.FlashBuyCustomerId = utils.GetGUID()
  323. customer.IsAttend = 1
  324. _, err := m.db.Insert(customer)
  325. return err
  326. }
  327. func (m *FlashbuyDAO) UpdateFlashBuyCustomer(customerId, flashBuyId string) error {
  328. var customer = model.TaFlashBuyCustomer{
  329. CustomerId: customerId,
  330. IsNew: 0,
  331. IsAttend: 0,
  332. }
  333. var cols = []string{
  334. "is_new",
  335. "is_attend",
  336. }
  337. _, err := m.db.Cols(cols...).Where("customer_id = ?", customer.CustomerId).And("flash_buy_id =?", flashBuyId).Update(customer)
  338. return err
  339. }
  340. func (m *FlashbuyDAO) IsFlashBuyCustomer(customerId, flashBuyId string) (*model.TaFlashBuyCustomer, error) {
  341. var customer []model.TaFlashBuyCustomer
  342. sql := `SELECT
  343. *
  344. FROM
  345. ta_flash_buy_customer
  346. WHERE
  347. customer_id = '` + customerId + `'
  348. and flash_buy_id = '` + flashBuyId + `'`
  349. err := m.db.Sql(sql).Find(&customer)
  350. if len(customer) <= 0 {
  351. return nil, err
  352. }
  353. return &customer[0], err
  354. }
  355. func (m *FlashbuyDAO) IsRecord(customerId, flashBuyId string) (int, error) {
  356. var customer []model.TaFlashBuyCustomer
  357. sql := `SELECT
  358. *
  359. FROM
  360. ta_flash_buy_customer
  361. WHERE
  362. customer_id = '` + customerId + `'
  363. and flash_buy_id = '` + flashBuyId + `'`
  364. err := m.db.Sql(sql).Find(&customer)
  365. return len(customer), err
  366. }