Skip to content

Instantly share code, notes, and snippets.

@Xenofex
Created April 11, 2015 08:54
Show Gist options
  • Save Xenofex/90df7954bfb48791ed6c to your computer and use it in GitHub Desktop.
Save Xenofex/90df7954bfb48791ed6c to your computer and use it in GitHub Desktop.
ScanController用scala写了一遍
package com.jikabao.scala.merchant
import java.util
import java.util.{Collections, Date}
import com.google.common.collect.{Lists, Iterables}
import com.jikabao.common.ServiceResult
import com.jikabao.common.domain._
import com.jikabao.common.enums.QrCodeActionEnum._
import com.jikabao.common.service.UserHolder
import com.jikabao.common.utils.QrCodeUtils
import com.jikabao.merchant.model.api.{RedeemableVO, ActivityVO, ScanResultVO, AccountContext}
import com.jikabao.merchant.service.ActivityService
import com.jikabao.merchant.transformer.{RedeemableVOTransformer, ActivityVOTransformer}
import com.jikabao.module.redeem.RedeemModule
import com.jikabao.service.account.domain.{AccountPriviledgeEnum, AccountPriviledge}
import com.jikabao.service.card.RewardService
import com.jikabao.service.member.MemberService
import com.jikabao.user.dao.ActivityRuleDAO
import com.jikabao.user.enums.MemberTokenType
import com.jikabao.user.interceptors.LoginRequired
import com.jikabao.utils.guava.{Maps2, Lists2}
import lombok.extern.slf4j.Slf4j
import org.apache.commons.lang3.time.DateUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.expression.ParseException
import org.springframework.web.bind.annotation.{RequestParam, RequestMapping, ResponseBody, RequestMethod}
import org.springframework.web.servlet.mvc.Controller
import scala.collection.JavaConversions._
import scala.collection.mutable
/**
* Created by eli on 4/11/15.
*/
@Slf4j
@Controller("/api/merchant/scan")
@LoginRequired
class ScanController {
@Autowired var activityRuleDAO: ActivityRuleDAO = null
@Autowired var userHolder: UserHolder[AccountContext] = null
@Autowired var redeemModule: RedeemModule = null
@Autowired var activityService: ActivityService = null
@Autowired var memberService: MemberService = null
@Autowired var rewardService: RewardService = null
@RequestMapping(value = Array("/api/merchant/scan/detail"), method = Array(RequestMethod.POST))
@ResponseBody
def scan(@RequestParam("qrcode") qrcode: String): ServiceResult = {
try {
val priviledge: AccountPriviledge = userHolder.getUser.getPriviledge
val shopIds = priviledge.getPriviledgedShops(AccountPriviledgeEnum.VERIFICATION)
if (shopIds.size > 1) {
return ServiceResult.fail(400, "您的账号管理了多个门店,只能管理一个门店才能帮助用户集点和兑换喔~")
}
if (shopIds.isEmpty) {
return ServiceResult.fail(400, "您的账号还没有开通权限喔~")
}
val shopId: Int = Iterables.getOnlyElement(shopIds)
val activities = activityService.findActivityByShopId(shopId)
val qrCode: QrCode = QrCodeUtils.parse(qrcode)
if (COLLECT.is(qrCode.getAction)) {
scanCollect(activities.toList, qrCode)
}
else if (REDEEM.is(qrCode.getAction)) {
scanRedeem(activities.toList, qrCode)
} else {
ServiceResult.fail(400, "系统故障")
}
}
catch {
case e: ParseException => {
return ServiceResult.fail(400, "只有集卡包的二维码才能集点和兑换喔~")
}
}
}
private def scanCollect(activities: List[Activity], qrCode: QrCode): ServiceResult = {
val result: ScanResultVO = new ScanResultVO
val member: Member = memberService.getMemberByToken(qrCode.getToken, MemberTokenType.COLLECT_SHORT)
if (member == null) {
return ServiceResult.fail(400, "二维码已失效,请用户联网刷新后再试~")
}
result.setType(COLLECT.name)
result.setMemberName(member.getNickName)
result.setActivities(Lists.transform(activities, ActivityVOTransformer.INSTANCE))
return ServiceResult.success(result, "成功")
}
private def scanRedeem(activities: List[Activity], qrCode: QrCode): ServiceResult = {
val member: Member = memberService.getMemberByToken(qrCode.getToken, MemberTokenType.REDEEM)
if (member == null) {
return ServiceResult.fail(400, "二维码已失效,请用户联网刷新后再试~")
}
try {
val redeemableRecordId = qrCode.getParam.toInt
val activityIds = activities.map(_.getIdentifier)
val redeemableRecords = rewardService.findRedeemableRecordByMemberIdAndActivityIds(member.getId, activityIds).filter(_.getRemainCopies > 0)
val ruleIds = redeemableRecords.map(_.getActivityRuleId)
val rules = activityRuleDAO.findByIds(new util.ArrayList(ruleIds))
import ScanController.toVO
val activitityVOs: List[ActivityVO] = activities.map(toVO)
val redeemableVOs = redeemableRecords.map { r =>
val ret : RedeemableVO = r
ret.setSelected(r.getRedeemableRecordId == redeemableRecordId)
ret.setActivity(activitityVOs.find(a -> a.getId != null && a.getId == r.getActivityId).orNull)
ret
}
val result = ScanResultVO.builder.`type`(REDEEM.name).redeemables(redeemableVOs).memberName(member.getNickName).build
ServiceResult.success(result, "成功")
}
catch {
case e: NumberFormatException => {
ServiceResult.fail(403, "兑换码格式错误")
}
}
}
@RequestMapping(value = Array("/api/merchant/scan/redeem"), method = Array(RequestMethod.POST))
@ResponseBody
def redeem(@RequestParam("qrcode") qrCode: String,
@RequestParam("redeemableRecordId") redeemableRecordId: Int,
@RequestParam(value = "remark", required = false) remark: String): ServiceResult = {
val privilege: AccountPriviledge = userHolder.getUser.getPriviledge
val accountId: Int = userHolder.getUser.getAccount.getId
val shopIds = privilege.getPriviledgedShops(AccountPriviledgeEnum.VERIFICATION)
if (shopIds.size > 1) {
return ServiceResult.fail(400, "您的账号管理了多个门店,只能管理一个门店才能帮助用户兑换喔~")
}
if (shopIds.isEmpty) {
return ServiceResult.fail(400, "您的账号还没有开通权限喔~")
}
val shopId: Int = Iterables.getOnlyElement(shopIds)
val activities = activityService.findActivityByShopId(shopId)
val activityIds = activities.map(_.getIdentifier)
if (!activityIds.contains(accountId)) {
return ServiceResult.fail(400, "您无权为该活动集点喔~")
}
try {
val qrCode = QrCodeUtils.parse(qrCode)
val member: Member = memberService.getMemberByToken(qrCode.getToken, MemberTokenType.REDEEM)
if (member == null) {
return ServiceResult.fail(400, "二维码已失效,请用户联网刷新后再试~")
}
val redeemableRecord: RedeemableRecord = rewardService.getRedeemableRecordById(member.getId, redeemableRecordId)
if (!activityIds.contains(redeemableRecord.getActivityId)) {
return ServiceResult.fail(400, "选择的礼品只能在隔壁老王家兑换喔~")
}
if (redeemableRecord.getRemainCopies == 0) {
val now: Date = new Date
val minuteBefore: Date = DateUtils.addMinutes(now, -1)
if (redeemableRecord.getAccountId == accountId && redeemableRecord.getRedeemedTime.after(minuteBefore)) {
var secondDiff: Long = (now.getTime - redeemableRecord.getRedeemedTime.getTime) / 1000
secondDiff = Math.max(secondDiff, 1)
return ServiceResult.fail(400, "您" + secondDiff + "秒钟之前刚兑换过这个礼品,是~不是恍了个神儿~")
}
else {
return ServiceResult.fail(400, "一个礼品~只能~兑换~一次喔")
}
}
redeemModule.redeem(redeemableRecord, accountId, shopId, remark)
ServiceResult.success(true, "成功")
}
catch {
case e: ParseException =>
ServiceResult.fail(400, "只有集卡包的二维码才能集点和兑换喔~")
}
}
}
object ScanController {
implicit def toVO(redeemableRecord: RedeemableRecord) : RedeemableVO = {
val ret = new RedeemableVO
ret.setGiftName(redeemableRecord.getGiftName)
ret.setRedeemableRecordId(redeemableRecord.getId)
ret.setActivityId(redeemableRecord.getActivityId)
ret
}
implicit def toVO(activity: Activity) : ActivityVO = ActivityVOTransformer.INSTANCE(activity)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment