Commit ed0ae6b3 authored by zhangfeng's avatar zhangfeng

积分规则接口校验

parent 0612f246
...@@ -48,7 +48,8 @@ public interface ErrorCodeConstants { ...@@ -48,7 +48,8 @@ public interface ErrorCodeConstants {
ErrorCode SCORE_RULE_NOT_EXISTS = new ErrorCode(1004008004, "score.rule.not.exists"); 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_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 { ...@@ -90,22 +90,21 @@ public class ScoreRuleController {
@ApiOperation("启用关闭") @ApiOperation("启用关闭")
@PreAuthorize("@ss.hasPermission('member:score-rule:update')") @PreAuthorize("@ss.hasPermission('member:score-rule:update')")
public CommonResult<Boolean> updateStatus(@Valid @RequestBody ScoreRuleStatusReqVO scoreRuleStatusReqVO) { public CommonResult<Boolean> updateStatus(@Valid @RequestBody ScoreRuleStatusReqVO scoreRuleStatusReqVO) {
Boolean res = scoreRuleService.updateStatus(scoreRuleStatusReqVO); scoreRuleService.updateStatus(scoreRuleStatusReqVO);
return success(res); return success(true);
} }
@PostMapping("/copy") @PostMapping("/copy")
@ApiOperation("复制规则") @ApiOperation("复制规则")
@PreAuthorize("@ss.hasPermission('member:score-rule:create')") @PreAuthorize("@ss.hasPermission('member:score-rule:create')")
public CommonResult<Long> copyScoreRule(@NotNull @RequestBody Long id) { public CommonResult<Long> copyScoreRule(@NotNull @RequestBody Long id) {
Long newId = scoreRuleService.copyScoreRule(id); return success(scoreRuleService.copyScoreRule(id));
return success(id);
} }
@PostMapping("/delay") @PostMapping("/delay")
@ApiOperation("延期规则") @ApiOperation("延期规则")
@PreAuthorize("@ss.hasPermission('member:score-rule:update')") @PreAuthorize("@ss.hasPermission('member:score-rule:update')")
public CommonResult<Boolean> delayScoreRule(@Valid @RequestBody ScoreDelayReqVO scoreDelayReqVO) { public CommonResult<Boolean> delayScoreRule(@Valid @RequestBody ScoreDelayReqVO scoreDelayReqVO) {
Boolean res = scoreRuleService.delayScoreRule(scoreDelayReqVO); scoreRuleService.delayScoreRule(scoreDelayReqVO);
return success(res); return success(true);
} }
@GetMapping("/export-excel") @GetMapping("/export-excel")
......
...@@ -72,7 +72,7 @@ public interface ScoreRuleService extends IService<ScoreRuleDO> { ...@@ -72,7 +72,7 @@ public interface ScoreRuleService extends IService<ScoreRuleDO> {
* @param scoreRuleStatusReqVO 积分规则状态 * @param scoreRuleStatusReqVO 积分规则状态
* @return * @return
*/ */
Boolean updateStatus(ScoreRuleStatusReqVO scoreRuleStatusReqVO); void updateStatus(ScoreRuleStatusReqVO scoreRuleStatusReqVO);
/** /**
* 积分规则复制 * 积分规则复制
...@@ -86,5 +86,5 @@ public interface ScoreRuleService extends IService<ScoreRuleDO> { ...@@ -86,5 +86,5 @@ public interface ScoreRuleService extends IService<ScoreRuleDO> {
* @param scoreDelayReqVO 积分规则延期 * @param scoreDelayReqVO 积分规则延期
* @return * @return
*/ */
Boolean delayScoreRule(ScoreDelayReqVO scoreDelayReqVO); void delayScoreRule(ScoreDelayReqVO scoreDelayReqVO);
} }
...@@ -6,17 +6,20 @@ import javax.annotation.Resource; ...@@ -6,17 +6,20 @@ import javax.annotation.Resource;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult; 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.framework.mybatis.core.service.AbstractService;
import cn.iocoder.yudao.module.member.convert.scoreRule.ScoreRuleConvert; 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.dataobject.scoreRule.ScoreRuleDO;
import cn.iocoder.yudao.module.member.dal.mysql.scoreRule.ScoreRuleMapper; 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.ScoreRuleStatusEnum;
import cn.iocoder.yudao.module.member.enums.ScoreRuleTypeEnum; 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.*;
import cn.iocoder.yudao.module.member.vo.scoreRule.extra.ScoreRuleOrderVExtraVO; 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.ScoreRuleRegisterExtraVO;
import cn.iocoder.yudao.module.member.vo.scoreRule.extra.ScoreRuleShareExtraVO; import cn.iocoder.yudao.module.member.vo.scoreRule.extra.ScoreRuleShareExtraVO;
import cn.iocoder.yudao.module.member.vo.scoreRule.extra.ScoreRulerRecommendExtraVO; import cn.iocoder.yudao.module.member.vo.scoreRule.extra.ScoreRulerRecommendExtraVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
...@@ -40,23 +43,56 @@ public class ScoreRuleServiceImpl extends AbstractService<ScoreRuleMapper, Score ...@@ -40,23 +43,56 @@ public class ScoreRuleServiceImpl extends AbstractService<ScoreRuleMapper, Score
@Override @Override
public Long createScoreRule(ScoreRuleCreateReqVO createReqVO) { public Long createScoreRule(ScoreRuleCreateReqVO createReqVO) {
ScoreRuleDO scoreRule = ScoreRuleConvert.INSTANCE.convert(createReqVO); ScoreRuleDO scoreRule = ScoreRuleConvert.INSTANCE.convert(createReqVO);
verifyCommon(scoreRule);
Integer scoreRuleType = scoreRule.getType(); Integer scoreRuleType = scoreRule.getType();
setExtraDO(createReqVO, scoreRuleType, scoreRule); verifyAndSetExtraDO(createReqVO, scoreRuleType, scoreRule);
scoreRuleMapper.insert(scoreRule); scoreRuleMapper.insert(scoreRule);
// 返回 // 返回
return scoreRule.getId(); 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 @Override
public void updateScoreRule(ScoreRuleUpdateReqVO updateReqVO) { public void updateScoreRule(ScoreRuleUpdateReqVO updateReqVO) {
// 校验存在 ScoreRuleDO scoreRuleDO = scoreRuleMapper.selectById(updateReqVO.getId());
this.validateScoreRuleExists(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); 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(scoreRule);
scoreRuleMapper.updateById(updateObj);
} }
/** /**
...@@ -66,20 +102,70 @@ public class ScoreRuleServiceImpl extends AbstractService<ScoreRuleMapper, Score ...@@ -66,20 +102,70 @@ public class ScoreRuleServiceImpl extends AbstractService<ScoreRuleMapper, Score
* @param scoreRuleType 指标类型 * @param scoreRuleType 指标类型
* @param scoreRule 需要填充额外字段的DO * @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()) { 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()) { } 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())); scoreRule.setExtra(JSONUtil.toJsonStr(reqVO.getExtraRegister()));
} else if (scoreRuleType == ScoreRuleTypeEnum.RECOMMEND.getValue()) { } 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()) { } 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 { } else {
throw exception(SCORE_RULE_NOT_EXISTS); 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 @Override
public void deleteScoreRule(Long id) { public void deleteScoreRule(Long id) {
ScoreRuleDO scoreRuleDO = scoreRuleMapper.selectById(id); ScoreRuleDO scoreRuleDO = scoreRuleMapper.selectById(id);
...@@ -151,16 +237,31 @@ public class ScoreRuleServiceImpl extends AbstractService<ScoreRuleMapper, Score ...@@ -151,16 +237,31 @@ public class ScoreRuleServiceImpl extends AbstractService<ScoreRuleMapper, Score
} }
@Override @Override
public Boolean updateStatus(ScoreRuleStatusReqVO scoreRuleStatusReqVO) { public void updateStatus(ScoreRuleStatusReqVO scoreRuleStatusReqVO) {
ScoreRuleDO scoreRuleDO = scoreRuleMapper.selectById(scoreRuleStatusReqVO.getId()); ScoreRuleDO scoreRuleDO = scoreRuleMapper.selectById(scoreRuleStatusReqVO.getId());
if (scoreRuleDO == null) { if (scoreRuleDO == null) {
throw exception(SCORE_RULE_NOT_EXISTS); throw exception(SCORE_RULE_NOT_EXISTS);
} }
Integer oldStatus = scoreRuleDO.getStatus();
ScoreRuleDO upScoreRuleDO = new ScoreRuleDO(); ScoreRuleDO upScoreRuleDO = new ScoreRuleDO();
upScoreRuleDO.setId(scoreRuleStatusReqVO.getId()); upScoreRuleDO.setId(scoreRuleStatusReqVO.getId());
upScoreRuleDO.setStatus(scoreRuleStatusReqVO.getStatus()); if (oldStatus == ScoreRuleStatusEnum.ENABLED.getValue() && scoreRuleStatusReqVO.getStatus() == ScoreRuleStatusEnum.CLOSED.getValue()) {
int updated = scoreRuleMapper.updateById(upScoreRuleDO); upScoreRuleDO.setStatus(scoreRuleStatusReqVO.getStatus());
return updated > 0; } 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 @Override
...@@ -173,12 +274,13 @@ public class ScoreRuleServiceImpl extends AbstractService<ScoreRuleMapper, Score ...@@ -173,12 +274,13 @@ public class ScoreRuleServiceImpl extends AbstractService<ScoreRuleMapper, Score
scoreRuleDO.setId(null); scoreRuleDO.setId(null);
scoreRuleDO.setCreateTime(null); scoreRuleDO.setCreateTime(null);
scoreRuleDO.setUpdateTime(null); scoreRuleDO.setUpdateTime(null);
scoreRuleDO.setUpdater(null);
scoreRuleMapper.insert(scoreRuleDO); scoreRuleMapper.insert(scoreRuleDO);
return scoreRuleDO.getId(); return scoreRuleDO.getId();
} }
@Override @Override
public Boolean delayScoreRule(ScoreDelayReqVO delayReqVO) { public void delayScoreRule(ScoreDelayReqVO delayReqVO) {
ScoreRuleDO scoreRuleDO = scoreRuleMapper.selectById(delayReqVO.getId()); ScoreRuleDO scoreRuleDO = scoreRuleMapper.selectById(delayReqVO.getId());
if (scoreRuleDO == null) { if (scoreRuleDO == null) {
throw exception(SCORE_RULE_NOT_EXISTS); throw exception(SCORE_RULE_NOT_EXISTS);
...@@ -194,7 +296,6 @@ public class ScoreRuleServiceImpl extends AbstractService<ScoreRuleMapper, Score ...@@ -194,7 +296,6 @@ public class ScoreRuleServiceImpl extends AbstractService<ScoreRuleMapper, Score
ScoreRuleDO upScoreRuleDO = new ScoreRuleDO(); ScoreRuleDO upScoreRuleDO = new ScoreRuleDO();
upScoreRuleDO.setId(delayReqVO.getId()); upScoreRuleDO.setId(delayReqVO.getId());
upScoreRuleDO.setEndTime(delayReqVO.getEndTime()); upScoreRuleDO.setEndTime(delayReqVO.getEndTime());
int updated = scoreRuleMapper.updateById(upScoreRuleDO); scoreRuleMapper.updateById(upScoreRuleDO);
return updated > 0;
} }
} }
...@@ -24,7 +24,7 @@ public class ScoreRuleBaseVO { ...@@ -24,7 +24,7 @@ public class ScoreRuleBaseVO {
private Integer type; private Integer type;
@ApiModelProperty(value = "规则标题中文", required = true) @ApiModelProperty(value = "规则标题中文", required = true)
@NotNull(message = "规则标题中文不能为空") @NotBlank(message = "规则标题中文不能为空")
private String titleZh; private String titleZh;
@ApiModelProperty(value = "规则标题英文", required = true) @ApiModelProperty(value = "规则标题英文", required = true)
......
...@@ -14,5 +14,5 @@ public class ScoreRuleShareExtraVO { ...@@ -14,5 +14,5 @@ public class ScoreRuleShareExtraVO {
@ApiModelProperty(value = "活动链接", required = true) @ApiModelProperty(value = "活动链接", required = true)
private String activityUrl ; private String activityUrl ;
@ApiModelProperty(value = "是否分享(0是,1否)默认是", required = true) @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