| @@ -0,0 +1,150 @@ | |||
| package com.iformall.service.invest.impl; | |||
| import com.alibaba.fastjson.JSONArray; | |||
| import com.baomidou.mybatisplus.core.mapper.BaseMapper; | |||
| import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | |||
| import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | |||
| import com.iformall.common.ErrorCode; | |||
| import com.iformall.domain.po.*; | |||
| import com.iformall.domain.vo.invest.InvestPageQuery; | |||
| import com.iformall.enums.EnumInvestType; | |||
| import com.iformall.enums.EnumShopStatus; | |||
| import com.iformall.exception.MallinkException; | |||
| import com.iformall.service.MallUserInfoService; | |||
| import com.iformall.service.WxShopService; | |||
| import com.iformall.service.invest.InvestCustomerService; | |||
| import org.apache.commons.collections.CollectionUtils; | |||
| import org.apache.commons.lang3.ObjectUtils; | |||
| import java.io.Serializable; | |||
| import java.util.List; | |||
| import java.util.Objects; | |||
| import java.util.stream.Collectors; | |||
| public class InvestBaseService<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> { | |||
| @Override | |||
| public T getById(Serializable id) { | |||
| T t = super.getById(id); | |||
| checkResult(t, id); | |||
| return t; | |||
| } | |||
| /** | |||
| * 校验商铺 | |||
| * | |||
| * @param shopService | |||
| * @param inputEntity | |||
| */ | |||
| void checkShop(WxShopService shopService, T inputEntity) { | |||
| Long targetId = null; | |||
| EnumInvestType investType = null; | |||
| if (inputEntity instanceof InvestDemandEntity) { | |||
| InvestDemandEntity entity = (InvestDemandEntity) inputEntity; | |||
| targetId = entity.getTargetId(); | |||
| investType = entity.getTargetType(); | |||
| } else if (inputEntity instanceof InvestTaskEntity) { | |||
| InvestTaskEntity entity = (InvestTaskEntity) inputEntity; | |||
| targetId = entity.getTargetId(); | |||
| investType = entity.getTargetType(); | |||
| } | |||
| if (EnumInvestType.SHOP.equals(investType)) { | |||
| WxShop shop = shopService.getById(targetId); | |||
| checkShopNotNull(shop); | |||
| } | |||
| } | |||
| /** | |||
| * 校验参数是否为空 | |||
| * | |||
| * @param shop | |||
| */ | |||
| private void checkShopNotNull(WxShop shop) { | |||
| if (Objects.isNull(shop)) { | |||
| throw new MallinkException(ErrorCode.SHOP_IS_NOT_FOUND); | |||
| } | |||
| if (Objects.equals(shop.getStatus(), EnumShopStatus.RENT.getCode())) { | |||
| throw new MallinkException(ErrorCode.SHOP_IS_RENT); | |||
| } | |||
| } | |||
| /** | |||
| * 校验用户是否存在 | |||
| * | |||
| * @param userInfoService | |||
| * @param userIdJson | |||
| */ | |||
| void checkUserExit(MallUserInfoService userInfoService, String userIdJson) { | |||
| if (Objects.nonNull(userIdJson)) { | |||
| List<Long> ids = JSONArray.parseArray(userIdJson, Long.class); | |||
| if (CollectionUtils.isNotEmpty(ids)) { | |||
| List<Long> usersFromDb = userInfoService.getByIds(ids).stream().map(MallUserInfo::getId).collect(Collectors.toList()); | |||
| List<Long> notExsitIds = ids.stream().filter(uid -> !usersFromDb.contains(uid)).collect(Collectors.toList()); | |||
| if (CollectionUtils.isNotEmpty(notExsitIds)) { | |||
| throwException(ErrorCode.USER_IS_EMPTY, notExsitIds); | |||
| } | |||
| } | |||
| } | |||
| } | |||
| void checkUserExit(MallUserInfoService userInfoService, Long id) { | |||
| if (Objects.nonNull(id)) { | |||
| MallUserInfo userInfo = userInfoService.getById(id); | |||
| if (Objects.isNull(userInfo)) | |||
| throwException(ErrorCode.USER_IS_EMPTY, id); | |||
| } | |||
| } | |||
| void checkCustomerExit(InvestCustomerService customerService, Long id) { | |||
| InvestCustomerEntity customerEntity = customerService.getById(id); | |||
| if (Objects.isNull(customerEntity)) { | |||
| throwException(ErrorCode.INVEST_CUSTOMER_NOT_FOUND, id); | |||
| } | |||
| } | |||
| /** | |||
| * 校验参数是否为空 | |||
| * | |||
| * @param values | |||
| */ | |||
| protected void checkAllNotNull(final Object... values) { | |||
| if (!ObjectUtils.allNotNull(values)) { | |||
| throw new MallinkException(ErrorCode.SYS_PARAMETER_NOT_NULL); | |||
| } | |||
| } | |||
| /** | |||
| * 校验结果是否为空 | |||
| * | |||
| * @param value | |||
| */ | |||
| protected void checkResult(final Object value, Object info) { | |||
| if (Objects.isNull(value)) { | |||
| throwException(ErrorCode.INVEST_DATA_NOT_FOUND, info); | |||
| } | |||
| } | |||
| protected Page buildQueryPage(InvestPageQuery pageQuery) { | |||
| Page page = new Page(); | |||
| page.setCurrent(pageQuery.getPageNum()); | |||
| page.setSize(pageQuery.getPageSize()); | |||
| page.setAscs(pageQuery.getAscs()); | |||
| page.setDescs(pageQuery.getDescs()); | |||
| page.setOptimizeCountSql(true); | |||
| page.setSearchCount(true); | |||
| return page; | |||
| } | |||
| void notSupport() { | |||
| throw new MallinkException(ErrorCode.SYS_METHOD_NOT_SUPPORT); | |||
| } | |||
| void throwException(ErrorCode errorCode, Object info) { | |||
| throw new MallinkException(errorCode.getCode(), errorCode.getMessage(info)); | |||
| } | |||
| } | |||