123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- package flashbuy
-
- import (
- "encoding/json"
- "errors"
- "net/http"
- "spaceofcheng/services/controllers"
- flashmodel "spaceofcheng/services/models/flashbuy"
- "spaceofcheng/services/models/model"
- "spaceofcheng/services/service/flashbuy"
- "spaceofcheng/services/utils"
- "time"
- )
-
- // CaseController 信息
- type FlashBuyController struct {
- dao *flashbuy.FlashBuyServ
- controllers.BaseController
- }
-
- // Constructor 初始化 Controller
- // @Title Constructor
- // @Description 初始化 Controller, 系统自动调用
- func (c *FlashBuyController) Constructor() {
- c.dao = flashbuy.NewFlashBuyServ(c.Context)
- }
- func (c *FlashBuyController) GetFlashBuyList() {
- caseIDs := c.GetString("caseid")
- if caseIDs == "" {
- cases := c.Context.Get("cases").([]model.SysUserCase)
- caseIDs = c.GetCaseIDs(cases)
- }
- flashBuyName := c.GetString("flashbuyname")
- flashBuyStatus := c.GetString("flashbuystatus")
- page, _ := c.GetInt("page")
- pageSize, _ := c.GetInt("pagesize")
- list, err := c.dao.GetFlashBuyList(caseIDs, flashBuyName, flashBuyStatus, page, pageSize)
- if err != nil {
- c.ResponseError(err)
- }
- c.ResponseJSON(list)
- }
-
- func (c *FlashBuyController) GetCustomerFlashBuyByCustomerId() {
- userRaw := c.Context.Get("customer")
- if userRaw == nil {
- c.ResponseError(errors.New("系统内部错误"), http.StatusInternalServerError)
- }
-
- user := userRaw.(model.TaCustomer)
- customerId := user.CustomerId
- page, _ := c.GetInt("page")
- pageSize, _ := c.GetInt("pagesize")
- list, err := c.dao.GetCustomerFlashBuyByCustomerId(customerId, page, pageSize)
- if err != nil {
- c.ResponseError(err)
- }
- c.ResponseJSON(list)
- }
-
- func (c *FlashBuyController) GetCustomerFlashBuyList() {
- flashBuyId := c.GetString(":flashBuyId")
- phone := c.GetString("phone")
- page, _ := c.GetInt("page")
- pageSize, _ := c.GetInt("pagesize")
- list, err := c.dao.GetCustomerFlashBuyById(flashBuyId, phone, page, pageSize)
- if err != nil {
- c.ResponseError(err)
- }
- c.ResponseJSON(list)
-
- }
- func (c *FlashBuyController) GetCustomerFlashBuyExcel() {
- flashBuyId := c.GetString(":flashBuyId")
- phone := c.GetString("phone")
- list, err := c.dao.GetCustomerFlashBuyExcel(flashBuyId, phone)
- if err != nil {
- c.ResponseError(err)
- }
- excel, err := utils.NewTinyXLSXEngine()
- if err != nil {
- utils.LogError("初始化Excel服务失败: " + err.Error())
- c.ResponseError(errors.New("初始化Excel服务失败"))
- }
- excel.SetCell(excel.InsertRow(), []string{
- "案场",
- "姓名",
- "微信昵称",
- "手机号",
- "获取时间",
- "核销时间",
- "核销状态",
- })
- for _, item := range list {
- row := excel.InsertRow()
- verifyStatus := ""
- switch item.VerifyStatus {
- case "useable":
- verifyStatus = "未使用"
- break
- case "used":
- verifyStatus = "已核销"
- break
- case "late":
- verifyStatus = "逾期核销"
- break
- case "expire":
- verifyStatus = "已过期"
- break
- }
- var createTime string
- var verifyTime string
- voidTime := time.Time{}
- if item.CreateDate == voidTime {
- createTime = ""
- } else {
- createTime = item.CreateDate.Format("2006-01-02 15:04:05")
- }
- if item.VerifyDate == voidTime {
- verifyTime = ""
- } else {
- verifyTime = item.VerifyDate.Format("2006-01-02 15:04:05")
- }
- excel.SetCell(row, []string{
- item.CaseName,
- item.Name,
- item.CustomerName,
- item.Phone,
- createTime,
- verifyTime,
- verifyStatus,
- })
- }
- c.SaveToExcel("抢购记录列表.xlsx", excel)
- }
-
- // GetFlashBuyById 获取抢购详情
- func (c *FlashBuyController) GetFlashBuyById() {
- flashBuyId := c.GetString(":flashBuyId")
- flashBuy, err := c.dao.GetFlashBuyById(flashBuyId)
- if err != nil {
- c.ResponseError(err)
- }
-
- c.ResponseJSON(flashBuy)
- }
-
- // GetWechatFlashBuyById
- func (c *FlashBuyController) GetWechatFlashBuyById() {
- flashBuyId := c.GetString(":flashBuyId")
- cust := c.Context.Get("customer").(model.TaCustomer)
- flashBuy, err := c.dao.GetFlashBuyById(flashBuyId)
- if err != nil {
- c.ResponseError(err)
- }
- userinfo, err := c.dao.GetFlashByUser(flashBuyId)
- if err != nil {
- c.ResponseError(err)
- }
- flashCustomer, err := c.dao.IsFlashBuyCustomer(cust.CustomerId, flashBuyId)
- if err != nil {
- c.ResponseError(err)
- }
- c.ResponseJSON(map[string]interface{}{
- "flashBuy": flashBuy,
- "userInfo": userinfo,
- "flashCustomer": flashCustomer,
- })
- }
-
- func (c *FlashBuyController) GetCustomerFlashBuyId() {
- flashBuyId := c.GetString(":customerFlashBuyId")
- flashBuy, err := c.dao.GetCustomerFlashBuyId(flashBuyId)
- if err != nil {
- c.ResponseError(err)
- }
- c.ResponseJSON(flashBuy)
- }
-
- func (c *FlashBuyController) SaveFlashBuy() {
- jsnStr := c.GetString("info")
- if jsnStr == "" {
- c.ResponseError(errors.New("未接收到保存内容"))
- }
- info := flashmodel.FlashBuyDetial{}
- if err := json.Unmarshal([]byte(jsnStr), &info); err != nil {
- utils.LogError("抢购数据转换失败: " + err.Error())
- c.ResponseError(err)
- }
- newFlashBuy, err := c.dao.SaveFlashBuy(info)
- if err != nil {
- c.ResponseError(err)
- }
- c.ResponseJSON(newFlashBuy)
- }
-
- func (c *FlashBuyController) DeleteFlashBuy() {
- flashBuyId := c.GetString(":flashBuyId")
- err := c.dao.DeleteFlashBuy(flashBuyId)
- if err != nil {
- c.ResponseError(err)
- }
- c.ResponseJSON("删除成功")
- }
-
- func (c *FlashBuyController) UpdateFlashBuy() {
- flashBuyId := c.GetString(":flashBuyId")
- flashBuyStatus := c.GetString(":flashBuyStatus")
- err := c.dao.UpdateFlashBuy(flashBuyId, flashBuyStatus)
- if err != nil {
- c.ResponseError(err)
- }
- c.ResponseJSON("修改成功")
- }
-
- func (c *FlashBuyController) VerifyCustomerFlashBuyList() {
- qrcode := c.GetString(":qrcode")
- caseids := c.GetString(":caseId")
- customerFlashBuy, err := c.dao.VerifyCustomerFlashBuyList(qrcode, caseids)
- if err != nil {
- c.ResponseError(err)
- }
- c.ResponseJSON(customerFlashBuy)
- }
- func (c *FlashBuyController) Verify() {
- customerFlashBuyId := c.GetString(":customerFlashBuyId")
- err := c.dao.Verify(customerFlashBuyId)
- if err != nil {
- c.ResponseError(err)
- }
- c.ResponseJSON("核销成功")
- }
-
- // FlashBuy 抢购
- func (c *FlashBuyController) FlashBuy() {
- id := c.GetString(":id")
-
- c.Context.Set("clienturl", c.Configer[controllers.WeChatConf].String("flashBuyNoticeURL"))
- c.Context.Set("tplid", c.Configer[controllers.WeChatConf].String("luckdrawMessageTplID"))
-
- err := c.dao.FlashBuy(id)
- if err != nil {
- c.ResponseError(err)
- }
- c.ResponseJSON("恭喜您!抢购成功!")
- }
-
- func (c *FlashBuyController) GetFlashModelList() {
- model, err := c.dao.GetFlashModelList()
- if err != nil {
- c.ResponseError(err)
- }
- c.ResponseJSON(model)
- }
-
- func (c *FlashBuyController) AddNewFlashBuyCustomer() {
- cust := c.Context.Get("customer").(model.TaCustomer)
- flashBuyId := c.GetString(":flashBuyId")
- err := c.dao.AddNewFlashBuyCustomer(cust, flashBuyId)
- if err != nil {
- c.ResponseError(err)
- }
- c.ResponseJSON("添加成功")
- }
-
- func (c *FlashBuyController) UpdateFlashBuyCustomer() {
- cust := c.Context.Get("customer").(model.TaCustomer)
- flashBuyId := c.GetString(":flashBuyId")
- err := c.dao.UpdateFlashBuyCustomer(cust.CustomerId, flashBuyId)
- if err != nil {
- c.ResponseError(err)
- }
- c.ResponseJSON("修改成功")
- }
-
- func (c *FlashBuyController) IsNewCustomer() {
- cust := c.Context.Get("customer").(model.TaCustomer)
- if cust.Phone == "" {
- c.ResponseError(errors.New("不是新用户"))
- }
- c.ResponseJSON("是新用户")
- }
|