Commit ed0ae6b3 authored by zhangfeng's avatar zhangfeng

积分规则接口校验

parent 0612f246
......@@ -48,7 +48,8 @@ public interface ErrorCodeConstants {
ErrorCode SCORE_RULE_NOT_EXISTS = new ErrorCode(1004008004, "score.rule.not.exists");
ErrorCode SCORE_RULE_DELETE_ERROR = new ErrorCode(1004008005, "score.rule.delete.error");
ErrorCode SCORE_RULE_UPDATE_ERROR = new ErrorCode(1004008005, "score.rule.update.error");
ErrorCode SCORE_RULE_UPDATE_ERROR = new ErrorCode(1004008006, "score.rule.update.error");
ErrorCode SCORE_RULE_FIELD_ERROR = new ErrorCode(1004008007, "score.rule.field.error");
}
......@@ -90,22 +90,21 @@ public class ScoreRuleController {
@ApiOperation("启用关闭")
@PreAuthorize("@ss.hasPermission('member:score-rule:update')")
public CommonResult<Boolean> updateStatus(@Valid @RequestBody ScoreRuleStatusReqVO scoreRuleStatusReqVO) {
Boolean res = scoreRuleService.updateStatus(scoreRuleStatusReqVO);
return success(res);
scoreRuleService.updateStatus(scoreRuleStatusReqVO);
return success(true);
}
@PostMapping("/copy")
@ApiOperation("复制规则")
@PreAuthorize("@ss.hasPermission('member:score-rule:create')")
public CommonResult<Long> copyScoreRule(@NotNull @RequestBody Long id) {
Long newId = scoreRuleService.copyScoreRule(id);
return success(id);
return success(scoreRuleService.copyScoreRule(id));
}
@PostMapping("/delay")
@ApiOperation("延期规则")
@PreAuthorize("@ss.hasPermission('member:score-rule:update')")
public CommonResult<Boolean> delayScoreRule(@Valid @RequestBody ScoreDelayReqVO scoreDelayReqVO) {
Boolean res = scoreRuleService.delayScoreRule(scoreDelayReqVO);
return success(res);
scoreRuleService.delayScoreRule(scoreDelayReqVO);
return success(true);
}
@GetMapping("/export-excel")
......
......@@ -72,7 +72,7 @@ public interface ScoreRuleService extends IService<ScoreRuleDO> {
* @param scoreRuleStatusReqVO 积分规则状态
* @return
*/
Boolean updateStatus(ScoreRuleStatusReqVO scoreRuleStatusReqVO);
void updateStatus(ScoreRuleStatusReqVO scoreRuleStatusReqVO);
/**
* 积分规则复制
......@@ -86,5 +86,5 @@ public interface ScoreRuleService extends IService<ScoreRuleDO> {
* @param scoreDelayReqVO 积分规则延期
* @return
*/
Boolean delayScoreRule(ScoreDelayReqVO scoreDelayReqVO);
void delayScoreRule(ScoreDelayReqVO scoreDelayReqVO);
}
......@@ -6,17 +6,20 @@ import javax.annotation.Resource;
import cn.hutool.json.JSONUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQuery;
import cn.iocoder.yudao.framework.mybatis.core.service.AbstractService;
import cn.iocoder.yudao.module.member.convert.scoreRule.ScoreRuleConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.scoreRule.ScoreRuleDO;
import cn.iocoder.yudao.module.member.dal.mysql.scoreRule.ScoreRuleMapper;
import cn.iocoder.yudao.module.member.enums.ScoreRuleStatusEnum;
import cn.iocoder.yudao.module.member.enums.ScoreRuleTypeEnum;
import cn.iocoder.yudao.module.member.enums.YesOrNoTypeEnum;
import cn.iocoder.yudao.module.member.vo.scoreRule.*;
import cn.iocoder.yudao.module.member.vo.scoreRule.extra.ScoreRuleOrderVExtraVO;
import cn.iocoder.yudao.module.member.vo.scoreRule.extra.ScoreRuleRegisterExtraVO;
import cn.iocoder.yudao.module.member.vo.scoreRule.extra.ScoreRuleShareExtraVO;
import cn.iocoder.yudao.module.member.vo.scoreRule.extra.ScoreRulerRecommendExtraVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
......@@ -40,23 +43,56 @@ public class ScoreRuleServiceImpl extends AbstractService<ScoreRuleMapper, Score
@Override
public Long createScoreRule(ScoreRuleCreateReqVO createReqVO) {
ScoreRuleDO scoreRule = ScoreRuleConvert.INSTANCE.convert(createReqVO);
verifyCommon(scoreRule);
Integer scoreRuleType = scoreRule.getType();
setExtraDO(createReqVO, scoreRuleType, scoreRule);
verifyAndSetExtraDO(createReqVO, scoreRuleType, scoreRule);
scoreRuleMapper.insert(scoreRule);
// 返回
return scoreRule.getId();
}
private void verifyCommon(ScoreRuleDO scoreRule) {
//校验公共入参
if (scoreRule.getGetScoreOnce() <= 0) {
throw exception(SCORE_RULE_FIELD_ERROR);
}
if (scoreRule.getMaxScoreTotal() <= 0) {
throw exception(SCORE_RULE_FIELD_ERROR);
}
if (scoreRule.getStartTime().after((scoreRule.getEndTime())) || scoreRule.getEndTime().before(Date.from(Instant.now()))) {
throw exception(SCORE_RULE_FIELD_ERROR);
}
if (scoreRule.getSocrePeriod() <= 0) {
throw exception(SCORE_RULE_FIELD_ERROR);
}
//如果是启用,校验同一个规则下,有效期内有没有重复的规则设置,分享不校验
if (scoreRule.getStatus() == ScoreRuleStatusEnum.ENABLED.getValue() && scoreRule.getType() != ScoreRuleTypeEnum.SHARE.getValue()) {
LambdaQuery<ScoreRuleDO> scoreRuleDOLambdaQuery = new LambdaQuery<>();
scoreRuleDOLambdaQuery.eq(ScoreRuleDO::getStatus, ScoreRuleStatusEnum.ENABLED.getValue())
.eq(ScoreRuleDO::getType, scoreRule.getType())
.eq(ScoreRuleDO::getStatus, ScoreRuleStatusEnum.ENABLED.getValue());
Long count = scoreRuleMapper.selectCount(scoreRuleDOLambdaQuery);
if (count > 1) {
throw exception(SCORE_RULE_FIELD_ERROR);
}
}
}
@Override
public void updateScoreRule(ScoreRuleUpdateReqVO updateReqVO) {
// 校验存在
this.validateScoreRuleExists(updateReqVO.getId());
ScoreRuleDO scoreRuleDO = scoreRuleMapper.selectById(updateReqVO.getId());
if (scoreRuleDO == null) {
throw exception(SCORE_RULE_NOT_EXISTS);
}
if (scoreRuleDO.getStatus() == ScoreRuleStatusEnum.ENABLED.getValue()) {
throw exception(SCORE_RULE_UPDATE_ERROR);
}
ScoreRuleDO scoreRule = ScoreRuleConvert.INSTANCE.convert(updateReqVO);
setExtraDO(updateReqVO, updateReqVO.getType(), scoreRule);
verifyCommon(scoreRule);
verifyAndSetExtraDO(updateReqVO, updateReqVO.getType(), scoreRule);
// 更新
ScoreRuleDO updateObj = ScoreRuleConvert.INSTANCE.convert(updateReqVO);
scoreRuleMapper.updateById(updateObj);
scoreRuleMapper.updateById(scoreRule);
}
/**
......@@ -66,20 +102,70 @@ public class ScoreRuleServiceImpl extends AbstractService<ScoreRuleMapper, Score
* @param scoreRuleType 指标类型
* @param scoreRule 需要填充额外字段的DO
*/
private void setExtraDO(ScoreRuleBaseVO reqVO, Integer scoreRuleType, ScoreRuleDO scoreRule) {
private void verifyAndSetExtraDO(ScoreRuleBaseVO reqVO, Integer scoreRuleType, ScoreRuleDO scoreRule) {
if (scoreRuleType == ScoreRuleTypeEnum.ORDER_V.getValue()) {
scoreRule.setExtra(JSONUtil.toJsonStr(reqVO.getExtraOrderV()));
ScoreRuleOrderVExtraVO extraOrderV = reqVO.getExtraOrderV();
if (extraOrderV == null) {
throw exception(SCORE_RULE_FIELD_ERROR);
}
if (extraOrderV.getFirstOrder() != YesOrNoTypeEnum.YES.ordinal() && extraOrderV.getFirstOrder() != YesOrNoTypeEnum.NO.ordinal()) {
throw exception(SCORE_RULE_FIELD_ERROR);
}
if (StringUtils.isAnyBlank(extraOrderV.getTargetCountry(), extraOrderV.getTargetCity(), extraOrderV.getReceiveAddr(), extraOrderV.getOrderEntry())) {
throw exception(SCORE_RULE_FIELD_ERROR);
}
verifyOrderVRule(extraOrderV.getOrderVRule());
scoreRule.setExtra(JSONUtil.toJsonStr(extraOrderV));
} else if (scoreRuleType == ScoreRuleTypeEnum.REGISTER.getValue()) {
if (reqVO.getExtraRegister() == null) {
throw exception(SCORE_RULE_FIELD_ERROR);
}
if (StringUtils.isBlank(reqVO.getExtraRegister().getRegisterPlatform())) {
throw exception(SCORE_RULE_FIELD_ERROR);
}
scoreRule.setExtra(JSONUtil.toJsonStr(reqVO.getExtraRegister()));
} else if (scoreRuleType == ScoreRuleTypeEnum.RECOMMEND.getValue()) {
scoreRule.setExtra(JSONUtil.toJsonStr(reqVO.getExtraRecommend()));
ScoreRulerRecommendExtraVO extraRecommend = reqVO.getExtraRecommend();
if (extraRecommend == null) {
throw exception(SCORE_RULE_FIELD_ERROR);
}
if (extraRecommend.getShareStatus() != YesOrNoTypeEnum.YES.ordinal() && extraRecommend.getShareStatus() != YesOrNoTypeEnum.NO.ordinal()) {
throw exception(SCORE_RULE_FIELD_ERROR);
}
if (StringUtils.isAnyBlank(extraRecommend.getShareContentEn(), extraRecommend.getShareContentZh())) {
throw exception(SCORE_RULE_FIELD_ERROR);
}
scoreRule.setExtra(JSONUtil.toJsonStr(extraRecommend));
} else if (scoreRuleType == ScoreRuleTypeEnum.SHARE.getValue()) {
scoreRule.setExtra(JSONUtil.toJsonStr(reqVO.getExtraShare()));
ScoreRuleShareExtraVO extraShare = reqVO.getExtraShare();
if (extraShare == null) {
throw exception(SCORE_RULE_FIELD_ERROR);
}
if (StringUtils.isAnyBlank(extraShare.getActivityDescZh(), extraShare.getActivityDescEn(), extraShare.getActivityUrl())) {
throw exception(SCORE_RULE_FIELD_ERROR);
}
if (extraShare.getShareStatus() != YesOrNoTypeEnum.YES.ordinal() && extraShare.getShareStatus() != YesOrNoTypeEnum.NO.ordinal()) {
throw exception(SCORE_RULE_FIELD_ERROR);
}
scoreRule.setExtra(JSONUtil.toJsonStr(extraShare));
} else {
throw exception(SCORE_RULE_NOT_EXISTS);
}
}
private void verifyOrderVRule(List<ScoreRuleOrderVExtraVO.OrderVRule> extraOrderVRule) {
if (extraOrderVRule == null || extraOrderVRule.isEmpty()) {
throw exception(SCORE_RULE_FIELD_ERROR);
}
int low = 0;
for (ScoreRuleOrderVExtraVO.OrderVRule orderVRule : extraOrderVRule) {
if (orderVRule.getLow() < low || orderVRule.getHigh() <= orderVRule.getLow() || orderVRule.getScore() <= 0) {
throw exception(SCORE_RULE_FIELD_ERROR);
}
low = orderVRule.getHigh();
}
}
@Override
public void deleteScoreRule(Long id) {
ScoreRuleDO scoreRuleDO = scoreRuleMapper.selectById(id);
......@@ -151,16 +237,31 @@ public class ScoreRuleServiceImpl extends AbstractService<ScoreRuleMapper, Score
}
@Override
public Boolean updateStatus(ScoreRuleStatusReqVO scoreRuleStatusReqVO) {
public void updateStatus(ScoreRuleStatusReqVO scoreRuleStatusReqVO) {
ScoreRuleDO scoreRuleDO = scoreRuleMapper.selectById(scoreRuleStatusReqVO.getId());
if (scoreRuleDO == null) {
throw exception(SCORE_RULE_NOT_EXISTS);
}
Integer oldStatus = scoreRuleDO.getStatus();
ScoreRuleDO upScoreRuleDO = new ScoreRuleDO();
upScoreRuleDO.setId(scoreRuleStatusReqVO.getId());
upScoreRuleDO.setStatus(scoreRuleStatusReqVO.getStatus());
int updated = scoreRuleMapper.updateById(upScoreRuleDO);
return updated > 0;
if (oldStatus == ScoreRuleStatusEnum.ENABLED.getValue() && scoreRuleStatusReqVO.getStatus() == ScoreRuleStatusEnum.CLOSED.getValue()) {
upScoreRuleDO.setStatus(scoreRuleStatusReqVO.getStatus());
} else if (oldStatus == ScoreRuleStatusEnum.DISABLED.getValue() && scoreRuleStatusReqVO.getStatus() == ScoreRuleStatusEnum.ENABLED.getValue()) {
//如果是启用,校验同一个规则下,有效期内有没有重复的规则设置,分享不校验
if (scoreRuleDO.getType() != ScoreRuleTypeEnum.SHARE.getValue()) {
LambdaQuery<ScoreRuleDO> scoreRuleDOLambdaQuery = new LambdaQuery<>();
scoreRuleDOLambdaQuery.eq(ScoreRuleDO::getStatus, ScoreRuleStatusEnum.ENABLED.getValue())
.eq(ScoreRuleDO::getType, scoreRuleDO.getType())
.eq(ScoreRuleDO::getStatus, ScoreRuleStatusEnum.ENABLED.getValue());
Long count = scoreRuleMapper.selectCount(scoreRuleDOLambdaQuery);
if (count > 1) {
throw exception(SCORE_RULE_FIELD_ERROR);
}
}
upScoreRuleDO.setStatus(scoreRuleStatusReqVO.getStatus());
}
scoreRuleMapper.updateById(upScoreRuleDO);
}
@Override
......@@ -173,12 +274,13 @@ public class ScoreRuleServiceImpl extends AbstractService<ScoreRuleMapper, Score
scoreRuleDO.setId(null);
scoreRuleDO.setCreateTime(null);
scoreRuleDO.setUpdateTime(null);
scoreRuleDO.setUpdater(null);
scoreRuleMapper.insert(scoreRuleDO);
return scoreRuleDO.getId();
}
@Override
public Boolean delayScoreRule(ScoreDelayReqVO delayReqVO) {
public void delayScoreRule(ScoreDelayReqVO delayReqVO) {
ScoreRuleDO scoreRuleDO = scoreRuleMapper.selectById(delayReqVO.getId());
if (scoreRuleDO == null) {
throw exception(SCORE_RULE_NOT_EXISTS);
......@@ -194,7 +296,6 @@ public class ScoreRuleServiceImpl extends AbstractService<ScoreRuleMapper, Score
ScoreRuleDO upScoreRuleDO = new ScoreRuleDO();
upScoreRuleDO.setId(delayReqVO.getId());
upScoreRuleDO.setEndTime(delayReqVO.getEndTime());
int updated = scoreRuleMapper.updateById(upScoreRuleDO);
return updated > 0;
scoreRuleMapper.updateById(upScoreRuleDO);
}
}
......@@ -24,7 +24,7 @@ public class ScoreRuleBaseVO {
private Integer type;
@ApiModelProperty(value = "规则标题中文", required = true)
@NotNull(message = "规则标题中文不能为空")
@NotBlank(message = "规则标题中文不能为空")
private String titleZh;
@ApiModelProperty(value = "规则标题英文", required = true)
......
......@@ -14,5 +14,5 @@ public class ScoreRuleShareExtraVO {
@ApiModelProperty(value = "活动链接", required = true)
private String activityUrl ;
@ApiModelProperty(value = "是否分享(0是,1否)默认是", required = true)
private String shareStatus ;
private Integer shareStatus ;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment