diff --git a/mallinkAdmin/src/main/java/com/iformall/controller/WxGameController.java b/mallinkAdmin/src/main/java/com/iformall/controller/WxGameController.java index e4fdcdd64..8811cf4ac 100644 --- a/mallinkAdmin/src/main/java/com/iformall/controller/WxGameController.java +++ b/mallinkAdmin/src/main/java/com/iformall/controller/WxGameController.java @@ -1,46 +1,50 @@ package com.iformall.controller; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.util.Assert; -import org.springframework.web.bind.annotation.*; - +import com.alibaba.fastjson.JSONArray; import com.github.pagehelper.PageInfo; import com.iformall.common.Result; import com.iformall.common.ResultData; - import com.iformall.domain.po.WxGame; +import com.iformall.enums.EnumGameStatus; import com.iformall.service.WxGameService; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("wxGame") public class WxGameController extends BaseController { private final Logger logger = LoggerFactory.getLogger(this.getClass()); - @Autowired + @Autowired private WxGameService wxGameService; - - @ApiOperation("分页列表接口") + + @ApiOperation("分页列表接口") @GetMapping("list") - @ApiImplicitParams({ - @ApiImplicitParam(name="pageNum",value="页数",dataType="int", paramType = "query",required=true), - @ApiImplicitParam(name="pageSize",value="每页条数",dataType="int", paramType = "query",required=true)}) - public ResultData list(@ModelAttribute WxGame wxGame,Integer pageNum, Integer pageSize) { - if (null == wxGame) wxGame = new WxGame(); - wxGame.setTenantId(getTenantId()); + @ApiImplicitParams({ + @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true), + @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)}) + public ResultData list(@ModelAttribute WxGame wxGame, Integer pageNum, Integer pageSize) { + if (null == wxGame) wxGame = new WxGame(); + wxGame.setTenantId(getTenantId()); final PageInfo page = wxGameService.listAsPage(wxGame, pageNum, pageSize); return new ResultData(page); } - @ApiOperation("新增接口") + @ApiOperation("新增接口") @PostMapping("add") public ResultData add(@RequestBody WxGame wxGame) { //Assert.notNull(wxGame.getName(), "角色名不能为空"); //Assert.isTrue(!checkUnique(sysRole.getName(), null), "重复的角色名"); + if (StringUtils.isBlank(wxGame.getCouponIds())) { + wxGame.setCouponIds(JSONArray.toJSONString(new String[0])); + } + wxGame.setStatus(EnumGameStatus.STATUS_THROW_IN.getCode()); wxGame.setTenantId(getTenantId()); wxGameService.saveOrUpdate(wxGame); return new ResultData(); @@ -49,26 +53,28 @@ public class WxGameController extends BaseController { @ApiOperation("根据id更新接口") @PostMapping("update") public ResultData update(@RequestBody WxGame wxGame) { - wxGame.setTenantId(getTenantId()); + if (StringUtils.isBlank(wxGame.getCouponIds())) { + wxGame.setCouponIds(JSONArray.toJSONString(new String[0])); + } + wxGame.setTenantId(getTenantId()); wxGameService.saveOrUpdate(wxGame); return new ResultData(); } @ApiOperation("根据id删除接口") @GetMapping("/del") - @ApiImplicitParam(name="id",value="id",dataType="Long", paramType = "query",required=true) + @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true) public ResultData delete(Long id) { wxGameService.deleteById(id); return new ResultData(Result.SUCCESS, "删除成功", null); } - - @ApiOperation("根据id查询接口") - @GetMapping("/findById") - @ApiImplicitParam(name="id",value="id",dataType="Long", paramType = "query",required=true) + + @ApiOperation("根据id查询接口") + @GetMapping("/findById") + @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true) public ResultData findById(Long id) { - return new ResultData(Result.SUCCESS,"查询成功",wxGameService.getById(id)); + return new ResultData(Result.SUCCESS, "查询成功", wxGameService.getById(id)); } - - - + + } diff --git a/mallinkService/src/main/java/com/iformall/domain/po/WxGame.java b/mallinkService/src/main/java/com/iformall/domain/po/WxGame.java index ab7addf57..7ec655c0d 100644 --- a/mallinkService/src/main/java/com/iformall/domain/po/WxGame.java +++ b/mallinkService/src/main/java/com/iformall/domain/po/WxGame.java @@ -62,11 +62,14 @@ public class WxGame implements Serializable { @io.swagger.annotations.ApiModelProperty(value = "已投放券信息") protected List couponIdsList; // couponChannel - @Transient - private List couponIdList; // couponId List, 前端选券使用 - public static enum Field { - Id_ASC("`id` ASC"), Id_DESC("`id` DESC"), TenantId_ASC("`tenant_id` ASC"), TenantId_DESC("`tenant_id` DESC"), GameId_ASC("`game_id` ASC"), GameId_DESC("`game_id` DESC"), Status_ASC("`status` ASC"), Status_DESC("`status` DESC"), ValidStartDate_ASC("`valid_start_date` ASC"), ValidStartDate_DESC("`valid_start_date` DESC"), ValidEndDate_ASC("`valid_end_date` ASC"), ValidEndDate_DESC("`valid_end_date` DESC"), TriggleAction_ASC("`triggle_action` ASC"), TriggleAction_DESC("`triggle_action` DESC"); + Id_ASC("`id` ASC"), Id_DESC("`id` DESC") + , TenantId_ASC("`tenant_id` ASC"), TenantId_DESC("`tenant_id` DESC") + , GameId_ASC("`game_id` ASC"), GameId_DESC("`game_id` DESC") + , Status_ASC("`status` ASC"), Status_DESC("`status` DESC") + , ValidStartDate_ASC("`valid_start_date` ASC"), ValidStartDate_DESC("`valid_start_date` DESC") + , ValidEndDate_ASC("`valid_end_date` ASC"), ValidEndDate_DESC("`valid_end_date` DESC") + , TriggleAction_ASC("`triggle_action` ASC"), TriggleAction_DESC("`triggle_action` DESC"); private String value; Field(String value) { diff --git a/mallinkService/src/main/java/com/iformall/service/impl/WxGameServiceImpl.java b/mallinkService/src/main/java/com/iformall/service/impl/WxGameServiceImpl.java index bfd4f708b..48f40e9c4 100644 --- a/mallinkService/src/main/java/com/iformall/service/impl/WxGameServiceImpl.java +++ b/mallinkService/src/main/java/com/iformall/service/impl/WxGameServiceImpl.java @@ -3,12 +3,18 @@ package com.iformall.service.impl; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import com.iformall.common.ErrorCode; +import com.iformall.domain.po.WxCoupon; import com.iformall.domain.po.WxCouponChannel; import com.iformall.domain.vo.WxCouponChannelVo; import com.iformall.enums.EnumCouponChannelStatus; import com.iformall.enums.EnumCouponChannelType; +import com.iformall.enums.EnumCouponStatus; +import com.iformall.enums.EnumGameStatus; +import com.iformall.exception.MallinkException; import com.iformall.mapper.WxCouponChannelMapper; import com.iformall.service.WxCouponChannelService; +import com.iformall.service.WxCouponService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.*; @@ -31,10 +37,12 @@ public class WxGameServiceImpl implements WxGameService { @Autowired WxCouponChannelMapper wxCouponChannelMapper; + @Autowired + WxCouponService wxCouponService; + @Autowired WxCouponChannelService wxCouponChannelService; - @Override public PageInfo listAsPage(WxGame record, Integer pageIndex, Integer pageSize) { return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> wxGameMapper.findList(record)); @@ -69,32 +77,31 @@ public class WxGameServiceImpl implements WxGameService { logger.error(e.getMessage()); return null; } + // 获取已上架的CouponChannel + WxCouponChannel couponChannelQ = new WxCouponChannel(); + couponChannelQ.setTenantId(record.getTenantId()); + couponChannelQ.setTargetAd(EnumCouponChannelType.COUPON_CHANNEL_ID_GAME.getCode()); + couponChannelQ.setSubTargetId(record.getId()); + couponChannelQ.setStatus(EnumCouponChannelStatus.STATUS_THROW_IN.getCode()); + List wxCouponChannelVos = wxCouponChannelService.listAPI(couponChannelQ); + Map wxCouponChannelsMap = new HashMap(); + for(WxCouponChannelVo wxCouponChannelVo: wxCouponChannelVos) { + wxCouponChannelsMap.put(wxCouponChannelVo.getCouponId(), wxCouponChannelVo); + } + // get game's coupon List couponIdsList = new ArrayList(); JSONArray couponChannelIds = JSON.parseArray(wxGame.getCouponIds()); for(int i=0; i couponChannelVoList = wxCouponChannelService.listAPI(couponChannelQ); - if (couponChannelVoList.size() > 0) { - WxCouponChannelVo couponChannelVo = couponChannelVoList.get(0); - if (couponChannelVo != null) { - couponChanObj.put("couponId", couponChannelVo.getCouponId()); - couponChanObj.put("couponName", couponChannelVo.getTitle()); - couponChanObj.put("coverImg", couponChannelVo.getCoverImg()); - couponIdsList.add(couponChanObj); - } - } - } catch (Exception e) { - logger.error(e.getMessage()); + Long couponId = couponChanObj.getLong("couponId"); + + WxCouponChannelVo couponChannelVo = wxCouponChannelsMap.get(couponId); + if (couponChannelVo != null) { + couponChanObj.put("couponId", couponChannelVo.getCouponId()); + couponChanObj.put("couponName", couponChannelVo.getTitle()); + couponChanObj.put("coverImg", couponChannelVo.getCoverImg()); + couponIdsList.add(couponChanObj); } } if (couponIdsList.size() > 0) { @@ -120,18 +127,107 @@ public class WxGameServiceImpl implements WxGameService { } else { wxGameMapper.updateByPrimaryKeySelective(record); } + + record = wxGameMapper.selectByPrimaryKey(record); + if (record.getStatus().equals(EnumGameStatus.STATUS_THROW_IN.getCode())) { + addOrUpdateCouponChannelBatch(JSONArray.parseArray(record.getCouponIds()), record); + } else { + delCouponChannelBatch(record); + } + } + + private void delCouponChannelBatch(WxGame record) { + // couponChannel 下架 + WxCouponChannel wxCouponChannelQ = new WxCouponChannel(); + wxCouponChannelQ.setTenantId(record.getTenantId()); + wxCouponChannelQ.setTargetAd(EnumCouponChannelType.COUPON_CHANNEL_ID_GAME.getCode()); + wxCouponChannelQ.setSubTargetId(record.getId()); + wxCouponChannelQ.setStatus(EnumCouponChannelStatus.STATUS_THROW_IN.getCode()); + List wxCouponChannels = wxCouponChannelMapper.findList(wxCouponChannelQ); + if (wxCouponChannels.size() > 0) { + for (WxCouponChannel ch : wxCouponChannels) { + ch.setStatus(EnumCouponChannelStatus.STATUS_TAKE_OFFF.getCode()); + ch.setUpdateDate(new Date()); + wxCouponChannelMapper.updateByPrimaryKeySelective(ch); + } + } + } + + private void addOrUpdateCouponChannelBatch(List couponIds, WxGame record) { + // 获取已上架的CouponChannel + WxCouponChannel wxCouponChannelQ = new WxCouponChannel(); + wxCouponChannelQ.setTenantId(record.getTenantId()); + wxCouponChannelQ.setTargetAd(EnumCouponChannelType.COUPON_CHANNEL_ID_GAME.getCode()); + wxCouponChannelQ.setSubTargetId(record.getId()); + wxCouponChannelQ.setStatus(EnumCouponChannelStatus.STATUS_THROW_IN.getCode()); + List wxCouponChannels = wxCouponChannelMapper.findList(wxCouponChannelQ); + Map wxCouponChannelsMap = new HashMap(); + for(WxCouponChannel wxCouponChannel: wxCouponChannels) { + wxCouponChannelsMap.put(wxCouponChannel.getCouponId(), wxCouponChannel); + } + + // 上架 couponChannel 更新 + for(int i=0; i wxCouponChannelsMap, WxGame record){ + + WxCoupon wxCoupon = wxCouponService.getById(couponId); + if (wxCoupon == null) { + throw new MallinkException(ErrorCode.COUPON_IS_EMPTY); + } + if (wxCoupon.getStatus() != EnumCouponStatus.COUPON_STATUS_THROW_IN.getCode()) { + throw new MallinkException(ErrorCode.COUPON_IS_TAKE_OFF); + } + if (wxCoupon.getValidEndDate() != null && wxCoupon.getValidEndDate().before(new Date())) { + throw new MallinkException(ErrorCode.COUPON_IS_TAKE_OFF); + } + + // 更新已有的couponChannel + WxCouponChannel wxCouponChannel = wxCouponChannelsMap.get(couponId); + if (wxCouponChannel != null) { + wxCouponChannel.setStatus(EnumCouponChannelStatus.STATUS_THROW_IN.getCode()); + wxCouponChannel.setBeginTime(record.getValidStartDate()); + wxCouponChannel.setEndTime(record.getValidEndDate()); + wxCouponChannel.setUpdateDate(new Date()); + wxCouponChannelMapper.updateByPrimaryKeySelective(wxCouponChannel); + wxCouponChannelsMap.remove(couponId); + } else { + // 不存在的新增 + wxCouponChannel = new WxCouponChannel(); + wxCouponChannel.setBeginTime(record.getValidStartDate()); + wxCouponChannel.setEndTime(record.getValidEndDate()); + wxCouponChannel.setStatus(EnumCouponChannelStatus.STATUS_THROW_IN.getCode()); + wxCouponChannel.setCouponId(couponId); + wxCouponChannel.setMerchantId(wxCoupon.getMerchantId()); + wxCouponChannel.setType(wxCoupon.getType()); + wxCouponChannel.setTargetAd(EnumCouponChannelType.COUPON_CHANNEL_ID_GAME.getCode()); + wxCouponChannel.setTenantId(wxCoupon.getTenantId()); + wxCouponChannel.setBusiness(wxCoupon.getBusiness()); + wxCouponChannel.setTitle(wxCoupon.getTitle()); + wxCouponChannel.setSubTargetId(record.getId()); + final IdWorker idWorker = IdWorker.get(); + wxCouponChannel.setId(idWorker.nextId()); + wxCouponChannelMapper.insertSelective(wxCouponChannel); + } } @Override public void deleteById(Long id) { wxGameMapper.deleteByPrimaryKey(id); } - - - - - - - - }