flashbuy.go 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. package flashbuy
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "net/http"
  6. "spaceofcheng/services/controllers"
  7. flashmodel "spaceofcheng/services/models/flashbuy"
  8. "spaceofcheng/services/models/model"
  9. "spaceofcheng/services/service/flashbuy"
  10. "spaceofcheng/services/utils"
  11. "time"
  12. )
  13. // CaseController 信息
  14. type FlashBuyController struct {
  15. dao *flashbuy.FlashBuyServ
  16. controllers.BaseController
  17. }
  18. // Constructor 初始化 Controller
  19. // @Title Constructor
  20. // @Description 初始化 Controller, 系统自动调用
  21. func (c *FlashBuyController) Constructor() {
  22. c.dao = flashbuy.NewFlashBuyServ(c.Context)
  23. }
  24. func (c *FlashBuyController) GetFlashBuyList() {
  25. caseIDs := c.GetString("caseid")
  26. if caseIDs == "" {
  27. cases := c.Context.Get("cases").([]model.SysUserCase)
  28. caseIDs = c.GetCaseIDs(cases)
  29. }
  30. flashBuyName := c.GetString("flashbuyname")
  31. flashBuyStatus := c.GetString("flashbuystatus")
  32. page, _ := c.GetInt("page")
  33. pageSize, _ := c.GetInt("pagesize")
  34. list, err := c.dao.GetFlashBuyList(caseIDs, flashBuyName, flashBuyStatus, page, pageSize)
  35. if err != nil {
  36. c.ResponseError(err)
  37. }
  38. c.ResponseJSON(list)
  39. }
  40. func (c *FlashBuyController) GetCustomerFlashBuyByCustomerId() {
  41. userRaw := c.Context.Get("customer")
  42. if userRaw == nil {
  43. c.ResponseError(errors.New("系统内部错误"), http.StatusInternalServerError)
  44. }
  45. user := userRaw.(model.TaCustomer)
  46. customerId := user.CustomerId
  47. page, _ := c.GetInt("page")
  48. pageSize, _ := c.GetInt("pagesize")
  49. list, err := c.dao.GetCustomerFlashBuyByCustomerId(customerId, page, pageSize)
  50. if err != nil {
  51. c.ResponseError(err)
  52. }
  53. c.ResponseJSON(list)
  54. }
  55. func (c *FlashBuyController) GetCustomerFlashBuyList() {
  56. flashBuyId := c.GetString(":flashBuyId")
  57. phone := c.GetString("phone")
  58. page, _ := c.GetInt("page")
  59. pageSize, _ := c.GetInt("pagesize")
  60. list, err := c.dao.GetCustomerFlashBuyById(flashBuyId, phone, page, pageSize)
  61. if err != nil {
  62. c.ResponseError(err)
  63. }
  64. c.ResponseJSON(list)
  65. }
  66. func (c *FlashBuyController) GetCustomerFlashBuyExcel() {
  67. flashBuyId := c.GetString(":flashBuyId")
  68. phone := c.GetString("phone")
  69. list, err := c.dao.GetCustomerFlashBuyExcel(flashBuyId, phone)
  70. if err != nil {
  71. c.ResponseError(err)
  72. }
  73. excel, err := utils.NewTinyXLSXEngine()
  74. if err != nil {
  75. utils.LogError("初始化Excel服务失败: " + err.Error())
  76. c.ResponseError(errors.New("初始化Excel服务失败"))
  77. }
  78. excel.SetCell(excel.InsertRow(), []string{
  79. "案场",
  80. "姓名",
  81. "微信昵称",
  82. "手机号",
  83. "获取时间",
  84. "核销时间",
  85. "核销状态",
  86. })
  87. for _, item := range list {
  88. row := excel.InsertRow()
  89. verifyStatus := ""
  90. switch item.VerifyStatus {
  91. case "useable":
  92. verifyStatus = "未使用"
  93. break
  94. case "used":
  95. verifyStatus = "已核销"
  96. break
  97. case "late":
  98. verifyStatus = "逾期核销"
  99. break
  100. case "expire":
  101. verifyStatus = "已过期"
  102. break
  103. }
  104. var createTime string
  105. var verifyTime string
  106. voidTime := time.Time{}
  107. if item.CreateDate == voidTime {
  108. createTime = ""
  109. } else {
  110. createTime = item.CreateDate.Format("2006-01-02 15:04:05")
  111. }
  112. if item.VerifyDate == voidTime {
  113. verifyTime = ""
  114. } else {
  115. verifyTime = item.VerifyDate.Format("2006-01-02 15:04:05")
  116. }
  117. excel.SetCell(row, []string{
  118. item.CaseName,
  119. item.Name,
  120. item.CustomerName,
  121. item.Phone,
  122. createTime,
  123. verifyTime,
  124. verifyStatus,
  125. })
  126. }
  127. c.SaveToExcel("抢购记录列表.xlsx", excel)
  128. }
  129. // GetFlashBuyById 获取抢购详情
  130. func (c *FlashBuyController) GetFlashBuyById() {
  131. flashBuyId := c.GetString(":flashBuyId")
  132. flashBuy, err := c.dao.GetFlashBuyById(flashBuyId)
  133. if err != nil {
  134. c.ResponseError(err)
  135. }
  136. c.ResponseJSON(flashBuy)
  137. }
  138. // GetWechatFlashBuyById
  139. func (c *FlashBuyController) GetWechatFlashBuyById() {
  140. flashBuyId := c.GetString(":flashBuyId")
  141. cust := c.Context.Get("customer").(model.TaCustomer)
  142. flashBuy, err := c.dao.GetFlashBuyById(flashBuyId)
  143. if err != nil {
  144. c.ResponseError(err)
  145. }
  146. userinfo, err := c.dao.GetFlashByUser(flashBuyId)
  147. if err != nil {
  148. c.ResponseError(err)
  149. }
  150. flashCustomer, err := c.dao.IsFlashBuyCustomer(cust.CustomerId, flashBuyId)
  151. if err != nil {
  152. c.ResponseError(err)
  153. }
  154. c.ResponseJSON(map[string]interface{}{
  155. "flashBuy": flashBuy,
  156. "userInfo": userinfo,
  157. "flashCustomer": flashCustomer,
  158. })
  159. }
  160. func (c *FlashBuyController) GetCustomerFlashBuyId() {
  161. flashBuyId := c.GetString(":customerFlashBuyId")
  162. flashBuy, err := c.dao.GetCustomerFlashBuyId(flashBuyId)
  163. if err != nil {
  164. c.ResponseError(err)
  165. }
  166. c.ResponseJSON(flashBuy)
  167. }
  168. func (c *FlashBuyController) SaveFlashBuy() {
  169. jsnStr := c.GetString("info")
  170. if jsnStr == "" {
  171. c.ResponseError(errors.New("未接收到保存内容"))
  172. }
  173. info := flashmodel.FlashBuyDetial{}
  174. if err := json.Unmarshal([]byte(jsnStr), &info); err != nil {
  175. utils.LogError("抢购数据转换失败: " + err.Error())
  176. c.ResponseError(err)
  177. }
  178. newFlashBuy, err := c.dao.SaveFlashBuy(info)
  179. if err != nil {
  180. c.ResponseError(err)
  181. }
  182. c.ResponseJSON(newFlashBuy)
  183. }
  184. func (c *FlashBuyController) DeleteFlashBuy() {
  185. flashBuyId := c.GetString(":flashBuyId")
  186. err := c.dao.DeleteFlashBuy(flashBuyId)
  187. if err != nil {
  188. c.ResponseError(err)
  189. }
  190. c.ResponseJSON("删除成功")
  191. }
  192. func (c *FlashBuyController) UpdateFlashBuy() {
  193. flashBuyId := c.GetString(":flashBuyId")
  194. flashBuyStatus := c.GetString(":flashBuyStatus")
  195. err := c.dao.UpdateFlashBuy(flashBuyId, flashBuyStatus)
  196. if err != nil {
  197. c.ResponseError(err)
  198. }
  199. c.ResponseJSON("修改成功")
  200. }
  201. func (c *FlashBuyController) VerifyCustomerFlashBuyList() {
  202. qrcode := c.GetString(":qrcode")
  203. caseids := c.GetString(":caseId")
  204. customerFlashBuy, err := c.dao.VerifyCustomerFlashBuyList(qrcode, caseids)
  205. if err != nil {
  206. c.ResponseError(err)
  207. }
  208. c.ResponseJSON(customerFlashBuy)
  209. }
  210. func (c *FlashBuyController) Verify() {
  211. customerFlashBuyId := c.GetString(":customerFlashBuyId")
  212. err := c.dao.Verify(customerFlashBuyId)
  213. if err != nil {
  214. c.ResponseError(err)
  215. }
  216. c.ResponseJSON("核销成功")
  217. }
  218. // FlashBuy 抢购
  219. func (c *FlashBuyController) FlashBuy() {
  220. id := c.GetString(":id")
  221. c.Context.Set("clienturl", c.Configer[controllers.WeChatConf].String("flashBuyNoticeURL"))
  222. c.Context.Set("tplid", c.Configer[controllers.WeChatConf].String("luckdrawMessageTplID"))
  223. err := c.dao.FlashBuy(id)
  224. if err != nil {
  225. c.ResponseError(err)
  226. }
  227. c.ResponseJSON("恭喜您!抢购成功!")
  228. }
  229. func (c *FlashBuyController) GetFlashModelList() {
  230. model, err := c.dao.GetFlashModelList()
  231. if err != nil {
  232. c.ResponseError(err)
  233. }
  234. c.ResponseJSON(model)
  235. }
  236. func (c *FlashBuyController) AddNewFlashBuyCustomer() {
  237. cust := c.Context.Get("customer").(model.TaCustomer)
  238. flashBuyId := c.GetString(":flashBuyId")
  239. err := c.dao.AddNewFlashBuyCustomer(cust, flashBuyId)
  240. if err != nil {
  241. c.ResponseError(err)
  242. }
  243. c.ResponseJSON("添加成功")
  244. }
  245. func (c *FlashBuyController) UpdateFlashBuyCustomer() {
  246. cust := c.Context.Get("customer").(model.TaCustomer)
  247. flashBuyId := c.GetString(":flashBuyId")
  248. err := c.dao.UpdateFlashBuyCustomer(cust.CustomerId, flashBuyId)
  249. if err != nil {
  250. c.ResponseError(err)
  251. }
  252. c.ResponseJSON("修改成功")
  253. }
  254. func (c *FlashBuyController) IsNewCustomer() {
  255. cust := c.Context.Get("customer").(model.TaCustomer)
  256. if cust.Phone == "" {
  257. c.ResponseError(errors.New("不是新用户"))
  258. }
  259. c.ResponseJSON("是新用户")
  260. }