123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551 |
- package services
- import (
- "errors"
- "fmt"
- "hongze/hongze_cygx/models"
- "hongze/hongze_cygx/models/company"
- "hongze/hongze_cygx/utils"
- "strconv"
- "strings"
- "time"
- )
- func SpecialTripPopupMsg(activityInfo *models.CygxActivitySpecialDetail, user *models.WxUserItem) (signupStatus int, popupMsg, popupMsg2 string, err error) {
-
- signupStatus = 1
- resultTime := utils.StrTimeToTime(activityInfo.ActivityTime)
- if time.Now().After(resultTime.Add(-time.Minute * 60)) {
- signupStatus = 4
- popupMsg = "活动开始前1小时内无法报名,请联系对口销售处理"
- return
- }
- errMsg, e := CheckTripRemainingtimesBycompany(user, activityInfo)
- if e != nil {
- err = errors.New("获取客户剩余报名次数失败 GetActivitySpecialUserType, Err: " + e.Error())
- return
- }
- if errMsg != "" {
- popupMsg = errMsg
- signupStatus = 3
- return
- }
- var condition string
- var pars []interface{}
- condition += ` AND activity_id = ? AND is_cancel = 0 `
- pars = append(pars, activityInfo.ActivityId)
- tripTota, e := models.GetActivitySpecialTripCountByActivityId(condition, pars)
- if e != nil {
- err = errors.New("GetCygxActivitySpecialDetailList, Err: " + e.Error())
- return
- }
- if activityInfo.LimitPeopleNum-tripTota < 1 {
- signupStatus = 2
- popupMsg = "此活动报名人数已满,请留意下期活动"
- return
- }
-
- popupMsg = "感谢参与,本次报名会扣除一次贵司在弘则的调研点数。"
- popupMsg2 = "由于每场活动人数有限,如果不能参加请提前48小时取消,未及时取消导致影响其他客户报名将会维持扣点。"
- return
- }
- func CheckTripRemainingtimesBycompany(user *models.WxUserItem, activityInfo *models.CygxActivitySpecialDetail) (errMsg string, err error) {
- msgTemplate := "您的专项调研次数已用完,如仍想参加,请与您的对口销售商议"
- companyId := user.CompanyId
- chartPermissionName := activityInfo.ChartPermissionName
- companyDetail, e := models.GetCompanyDetailByIdGroup(companyId)
- if e != nil {
- err = errors.New("GetCompanyDetailByIdGroup, Err:" + e.Error())
- return
- }
-
- if companyDetail.Status == utils.COMPANY_STATUS_FOREVER {
- return
- }
- var condition string
- var pars []interface{}
- condition = " AND company_id = ? AND points > 0 ORDER BY chart_permission_id DESC "
- pars = append(pars, companyId)
- activitySpecialPermissionPointsList, e := models.GetCygxActivitySpecialPermissionPointsList(condition, pars)
- if e != nil && e.Error() != utils.ErrNoRow() {
- err = errors.New("GetCygxActivitySpecialPermissionPointsList, Err:" + e.Error())
- return
- }
-
- if len(activitySpecialPermissionPointsList) == 0 {
- errMsg = msgTemplate
- return
- }
-
-
- if utils.InArrayByStr(utils.ACTIVITY_SPECIAL_TRIP_PERMISSION_NAME_OTHER, activityInfo.ChartPermissionName) {
- return
- } else {
- for _, v := range activitySpecialPermissionPointsList {
- if v.ChartPermissionName == chartPermissionName || v.ChartPermissionId == 0 {
- return
- }
- }
- }
- return
- }
- func GetSpecialBillMaxChartPermissionId(user *models.WxUserItem) (maxChartPermissionId int, maxChartPermissionName string, err error) {
- companyId := user.CompanyId
- var condition string
- var pars []interface{}
- condition = " AND company_id = ? AND points > 0 ORDER BY points DESC LIMIT 1 "
- pars = append(pars, companyId)
- activitySpecialPermissionPointsList, e := models.GetCygxActivitySpecialPermissionPointsList(condition, pars)
- if e != nil && e.Error() != utils.ErrNoRow() {
- err = errors.New("GetCygxActivitySpecialPermissionPointsList, Err:" + e.Error())
- return
- }
- for _, v := range activitySpecialPermissionPointsList {
- maxChartPermissionId = v.ChartPermissionId
- maxChartPermissionName = v.ChartPermissionName
- }
- return
- }
- func DeductTripRemainingtimesByUser(user *models.WxUserItem, activityInfo *models.CygxActivitySpecialDetail) (err error) {
- msgTemplate := "您的专项调研次数已用完,如仍想参加,请与您的对口销售商议"
- companyId := user.CompanyId
-
- companyProduct, e := company.GetCompanyProductByCompanyIdAndProductId(companyId, utils.COMPANY_PRODUCT_RAI_ID)
- if e != nil {
- err = errors.New("GetCompanyProductByCompanyIdAndProductId, Err:" + e.Error())
- return
- }
- var condition string
- var pars []interface{}
- var maxChartPermissionId int
- var points int
- var activitySpecialPermissionPointsList []*models.CygxActivitySpecialPermissionPoints
-
- if companyProduct.Status == utils.COMPANY_STATUS_FOREVER {
- condition = " AND company_id = ? ORDER BY points DESC LIMIT 1 "
- pars = append(pars, companyId)
- activitySpecialPermissionPointsList, e = models.GetCygxActivitySpecialPermissionPointsList(condition, pars)
- if e != nil && e.Error() != utils.ErrNoRow() {
- err = errors.New("GetCygxActivitySpecialPermissionPointsList, Err:" + e.Error())
- return
- }
-
- if len(activitySpecialPermissionPointsList) == 0 {
- item := new(models.CygxActivitySpecialPermissionPoints)
- item.CompanyId = companyProduct.CompanyId
- item.Points = -1
- item.CompanyName = companyProduct.CompanyName
-
-
- item.CreateTime = time.Now()
- item.ModifyTime = time.Now()
- e = models.AddCygxActivitySpecialPermissionPoints(item)
- if e != nil {
- err = errors.New("AddCygxActivitySpecialPermissionPoints, Err:" + e.Error())
- return
- }
- return
- }
- } else {
- condition = " AND company_id = ? AND points > 0 ORDER BY points DESC LIMIT 1 "
- pars = append(pars, companyId)
-
-
- if !utils.InArrayByStr(utils.ACTIVITY_SPECIAL_TRIP_PERMISSION_NAME_OTHER, activityInfo.ChartPermissionName) {
- pars = make([]interface{}, 0)
- condition = " AND company_id = ? AND ( chart_permission_id = ? OR chart_permission_id =0 ) AND points > 0 ORDER BY points DESC LIMIT 1 "
- pars = append(pars, companyId, activityInfo.ChartPermissionId)
- }
- activitySpecialPermissionPointsList, e = models.GetCygxActivitySpecialPermissionPointsList(condition, pars)
- if e != nil && e.Error() != utils.ErrNoRow() {
- err = errors.New("GetCygxActivitySpecialPermissionPointsList, Err:" + e.Error())
- return
- }
- }
-
- if len(activitySpecialPermissionPointsList) == 0 {
- err = errors.New(msgTemplate)
- return
- }
- for _, v := range activitySpecialPermissionPointsList {
- maxChartPermissionId = v.ChartPermissionId
- points = v.Points - 1
- }
-
- e = models.UpdateCygxActivitySpecialPermissionPoints(points, companyId, maxChartPermissionId)
- if e != nil {
- err = errors.New("UpdateCygxActivitySpecialPermissionPoints, Err:" + e.Error())
- return
- }
- return
- }
- func RebateTripRemainingtimesByUser(user *models.WxUserItem, activityInfo *models.CygxActivitySpecialDetail) (err error) {
- companyId := user.CompanyId
-
- var condition string
- var pars []interface{}
- condition = " AND company_id = ? ORDER BY points DESC LIMIT 1 "
- pars = append(pars, companyId)
-
-
- if !utils.InArrayByStr(utils.ACTIVITY_SPECIAL_TRIP_PERMISSION_NAME_OTHER, activityInfo.ChartPermissionName) {
- pars = make([]interface{}, 0)
- condition = " AND company_id = ? AND (chart_permission_id = ? OR chart_permission_id = 0 ) LIMIT 1 "
- pars = append(pars, companyId, activityInfo.ChartPermissionId)
- } else {
- lastTripBill, e := models.GetCygxActivitySpecialTripBillLastDetialByActivityId(activityInfo.ActivityId, user.UserId)
- if e != nil {
- err = errors.New("GetActivitySpecialTripCountByActivitySpecial, Err: " + e.Error())
- return
- }
- pars = make([]interface{}, 0)
- condition = " AND company_id = ? AND (chart_permission_id = ? OR chart_permission_id = 0 ) LIMIT 1 "
- pars = append(pars, companyId, lastTripBill.ChartPermissionId)
- }
- activitySpecialPermissionPointsList, e := models.GetCygxActivitySpecialPermissionPointsList(condition, pars)
- if e != nil && e.Error() != utils.ErrNoRow() {
- err = errors.New("GetCygxActivitySpecialPermissionPointsList, Err:" + e.Error())
- return
- }
- var maxChartPermissionId int
- var points int
- for _, v := range activitySpecialPermissionPointsList {
- maxChartPermissionId = v.ChartPermissionId
- points = v.Points + 1
- }
-
- e = models.UpdateCygxActivitySpecialPermissionPoints(points, companyId, maxChartPermissionId)
- if e != nil {
- err = errors.New("UpdateCygxActivitySpecialPermissionPoints, Err:" + e.Error())
- return
- }
- return
- }
- func HandleActivitySpecialTripBillTotalText(companyId int) (totalText string, err error) {
-
- companyDetail, e := models.GetCompanyDetailByIdGroup(companyId)
- if e != nil {
- err = errors.New("GetCompanyDetailByIdGroup, Err:" + e.Error())
- return
- }
- var condition string
- var pars []interface{}
- pars = make([]interface{}, 0)
- condition = " AND company_id = ? AND points != 0 ORDER BY chart_permission_id DESC "
- pars = append(pars, companyId)
- activitySpecialPermissionPointsList, e := models.GetCygxActivitySpecialPermissionPointsList(condition, pars)
- if e != nil && e.Error() != utils.ErrNoRow() {
- err = errors.New("GetCygxActivitySpecialPermissionPointsList, Err:" + e.Error())
- return
- }
-
- if companyDetail.Status == utils.COMPANY_STATUS_FOREVER {
- var points int
- for _, v := range activitySpecialPermissionPointsList {
- points += v.Points
- }
- totalText = fmt.Sprint(points, "次")
- return
- }
- var itemBillTotal []string
- itemBillTotalMap := make(map[string]string)
- for _, v := range activitySpecialPermissionPointsList {
- if v.ChartPermissionId > 0 {
- itemBillTotal = append(itemBillTotal, fmt.Sprint(v.ChartPermissionName, v.Points, "次"))
- itemBillTotalMap[v.ChartPermissionName] = fmt.Sprint(v.ChartPermissionName, v.Points, "次")
- }
- if v.ChartPermissionId == 0 {
- totalText = fmt.Sprint(v.Points, "次")
- }
- }
- if len(itemBillTotal) > 0 {
- permissionNameSlice := []string{"医药", "消费", "科技", "智造"}
- var itemBillTotalNew []string
- for _, v := range permissionNameSlice {
- if itemBillTotalMap[v] == "" {
- itemBillTotalNew = append(itemBillTotalNew, fmt.Sprint(v, "0次"))
- } else {
- itemBillTotalNew = append(itemBillTotalNew, itemBillTotalMap[v])
- }
- }
- totalText = strings.Join(itemBillTotalNew, "+")
- }
- return
- }
- func ActivitySpecialUserRmind(user *models.WxUserItem, activityId, TripStatus int) (err error) {
- defer func() {
- if err != nil {
- go utils.SendAlarmMsg("用户搜专项调研操作操作行为,模板消息推送失败"+err.Error(), 2)
- }
- }()
- countUser, err := models.GetUserRemind(user.UserId)
- if err != nil {
- return err
- }
- if countUser == 0 {
- return err
- }
- var first string
- var keyword1 string
- var keyword2 string
- var keyword3 string
- var keyword4 string
- var remark string
-
- sellerItemQy, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
- if err != nil && err.Error() != utils.ErrNoRow() {
- return err
- }
- if sellerItemQy != nil {
- sllerAndShareMobileArr, e := GetCompanySellerAndShareMobileByRai(user.CompanyId)
- if e != nil {
- err = errors.New("GetCompanySellerAndShareMobileByRai, Err: " + e.Error())
- return
- }
- sllerAndShareMobiles := strings.Join(sllerAndShareMobileArr, ",")
- openIdList, e := models.GetWxOpenIdByMobileList(sllerAndShareMobiles)
- if e != nil {
- err = errors.New("GetSellerByAdminId, Err: " + e.Error())
- return
- }
- activityInfo, e := models.GetCygxActivitySpecialDetailById(user.UserId, activityId)
- if e != nil {
- err = errors.New("GetCygxActivitySpecialDetailById, Err: " + e.Error())
- return
- }
- if activityInfo == nil {
- return
- }
-
- if TripStatus == 1 {
- keyword2 = fmt.Sprint("互动:专项调研预报名,", user.RealName, "--", user.CompanyName)
- } else {
- keyword2 = fmt.Sprint("互动:活动报名,", user.RealName, "--", user.CompanyName)
- }
- keyword1 = activityInfo.ResearchTheme
-
- remark = "点击查看活动详情"
- openIdArr := make([]string, 0)
- for _, v := range openIdList {
- openIdArr = append(openIdArr, v.OpenId)
- }
- redirectUrl := utils.WX_MSG_PATH_ACTIVITY_SPECIAL_DETAIL + strconv.Itoa(activityId)
- sendInfo := new(SendWxTemplate)
- sendInfo.First = first
- sendInfo.Keyword1 = keyword1
- sendInfo.Keyword2 = keyword2
- sendInfo.Keyword3 = keyword3
- sendInfo.Keyword4 = keyword4
- sendInfo.Remark = remark
- sendInfo.TemplateId = utils.WxMsgTemplateIdArticleUserRemind
- sendInfo.RedirectUrl = redirectUrl
- sendInfo.RedirectTarget = 3
- sendInfo.Resource = strconv.Itoa(activityId)
- sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD
- sendInfo.OpenIdArr = openIdArr
- err = PublicSendTemplateMsg(sendInfo)
- if err != nil {
- return
- }
- }
- return
- }
- func ActivitySpecialUserAddTrip(user *models.WxUserItem, activityId int) (err error) {
- defer func() {
- if err != nil {
- go utils.SendAlarmMsg("确定行程用户报名模版消息推送,模板消息推送失败"+err.Error(), 2)
- }
- }()
- var first string
- var keyword1 string
- var keyword2 string
- var keyword3 string
- var keyword4 string
- var remark string
-
- sellerItemQy, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
- if err != nil && err.Error() != utils.ErrNoRow() {
- return err
- }
- cnf, _ := models.GetConfigByCode("tpl_msg")
- if cnf != nil && sellerItemQy != nil {
- openIdList, e := models.GetWxOpenIdByMobileList(cnf.ConfigValue + "," + sellerItemQy.Mobile)
- if e != nil {
- err = errors.New("GetSellerByAdminId, Err: " + e.Error())
- return
- }
- activityInfo, e := models.GetCygxActivitySpecialDetailById(user.UserId, activityId)
- if e != nil {
- err = errors.New("GetCygxActivitySpecialDetailById, Err: " + e.Error())
- return
- }
- if activityInfo == nil {
- return
- }
- keyword1 = fmt.Sprint(user.RealName, "--", user.CompanyName, "(所属销售:", sellerItemQy.RealName, ")")
- keyword2 = user.Mobile
- keyword3 = time.Now().Format(utils.FormatDateTime)
- keyword4 = "报名专项调研【" + activityInfo.ResearchTheme + "】 "
- remark = ""
- openIdArr := make([]string, 0)
- for _, v := range openIdList {
- openIdArr = append(openIdArr, v.OpenId)
- }
- redirectUrl := utils.WX_MSG_PATH_ACTIVITY_SPECIAL_DETAIL + strconv.Itoa(activityId)
- sendInfo := new(SendWxTemplate)
- sendInfo.First = first
- sendInfo.Keyword1 = keyword1
- sendInfo.Keyword2 = keyword2
- sendInfo.Keyword3 = keyword3
- sendInfo.Keyword4 = keyword4
- sendInfo.Remark = remark
- sendInfo.TemplateId = utils.WxMsgTemplateIdApplyXzs
- sendInfo.RedirectUrl = redirectUrl
- sendInfo.RedirectTarget = 3
- sendInfo.Resource = strconv.Itoa(activityId)
- sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD
- sendInfo.OpenIdArr = openIdArr
- err = PublicSendTemplateMsg(sendInfo)
- if err != nil {
- return
- }
- }
- return
- }
|