package com.gree.mall.miniapp.logic.goods;

import com.gree.mall.miniapp.logic.common.outside.WechatLogic;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.gree.mall.miniapp.logic.promotion.PromotionFullPieceLogic;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.miniapp.bean.goods.*;
import com.gree.mall.miniapp.bean.user.CurrentCompanyWechat;
import com.gree.mall.miniapp.commonmapper.GoodsSpecDetailMapper;
import com.gree.mall.miniapp.commonmapper.SecActivityStageMapper;
import com.gree.mall.miniapp.constant.Constant;
import com.gree.mall.miniapp.enums.GoodsTypeEnum;
import com.gree.mall.miniapp.enums.QrCodeEnum;
import com.gree.mall.miniapp.enums.UserTypeEnum;
import com.gree.mall.miniapp.exception.RemoteServiceException;
import com.gree.mall.miniapp.logic.common.CommonLogic;
import com.gree.mall.miniapp.logic.seckill.SecKillLogic;
import com.gree.mall.miniapp.plus.entity.*;
import com.gree.mall.miniapp.plus.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;


@Component
public class GoodsLogic {
    @Resource
    CommonLogic commonLogic;
    @Autowired
    WechatLogic wechatLogic;
    @Autowired
    SecKillLogic secKillLogic;
    @Autowired
    PromotionFullPieceLogic promotionFullPieceLogic;
    @Resource
    GoodsService goodsService;
    @Resource
    GoodsSpecService goodsSpecService;
    @Autowired
    GoodsPackagePopService goodsPackagePopService;

    @Autowired
    private GoodsFavoriteService goodsFavoriteService;
    @Autowired
    private GoodsSpecDetailMapper goodsSpecDetailMapper;
    @Autowired
    private PubTemplateService pubTemplateService;
    @Autowired
    PromotionGroupSpecUserService promotionGroupSpecUserService;
    @Autowired
    PromotionGroupSpecService promotionGroupSpecService;
    @Autowired
    UserService userService;
    @Autowired
    PromotionGroupService promotionGroupService;
    @Autowired
    SecActivityStageMapper secActivityStageMapper;

    @Autowired
    GoodsTagRelaService goodsTagRelaService;

    /**
     * 商品列表
     *
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    public IPage<GoodsNewBean> page(HttpServletRequest request,List<String> brandId,BigDecimal minPrice,BigDecimal maxPrice,List<String> tag,
                                    String keyword,String categoryId, Integer sort,Boolean yjhx,Integer pageNum, Integer pageSize) {
        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat(request);

        IPage<GoodsNewBean> page = goodsSpecDetailMapper.queryGoodsList(new Page(pageNum, pageSize),
                brandId,minPrice,maxPrice,tag,
                currentCompanyWechat.getUser().getUserId(), currentCompanyWechat.getUser().getType(), keyword, categoryId,sort,yjhx,currentCompanyWechat.getCompanyWechatId());
        this.supplyGoodsNewBean(page,currentCompanyWechat);
        return page;
    }



    public void supplyGoodsNewBean(IPage<GoodsNewBean> page, CurrentCompanyWechat currentCompanyWechat){
        //判定上坪是否在秒杀中 在就覆盖其价格
        for (GoodsNewBean goodsNewBean : page.getRecords()) {
            if(goodsNewBean == null){
                continue;
            }
            SecKillActivitySpec secKillActivitySpec = secActivityStageMapper.getSecKillActivitySpecByGoodId(currentCompanyWechat.getCurrentCompanyWechatId(),goodsNewBean.getGoodsId());
            if(secKillActivitySpec != null){
                goodsNewBean.setOrgGoodsPrice(secKillActivitySpec.getGoodsPrice());
                goodsNewBean.setGoodsPrice(secKillActivitySpec.getPrice());
            }
            if(goodsNewBean.getGoodsType().equals(GoodsTypeEnum.PACKAGE.toString())){
                goodsNewBean.setOrgGoodsPrice(goodsNewBean.getPackageMinAmount());
                goodsNewBean.setGoodsPrice(goodsNewBean.getPackageMinAmount());
            }

        }

        //补充标签
        for (GoodsNewBean goodsNewBean : page.getRecords()) {
            //查询上标签
            List<GoodsTagRela> list1 = goodsTagRelaService.lambdaQuery().eq(GoodsTagRela::getGoodsId, goodsNewBean.getGoodsId())
                    .eq(GoodsTagRela::getType,1).list();
            if (list1.size() > 0) {
                List<String> collect1 = list1.stream().map(GoodsTagRela::getGoodsTagName).collect(Collectors.toList());
                goodsNewBean.setTags1(collect1);
            }

            //查询活动专区标签 //查询下标签
            //活动商品
            List<GoodsTagRela> list = goodsTagRelaService.lambdaQuery().eq(GoodsTagRela::getGoodsId, goodsNewBean.getGoodsId())
                    .eq(GoodsTagRela::getType,2).list();
            if (list.size() > 0) {
                List<String> collect1 = list.stream().map(GoodsTagRela::getGoodsTagName).collect(Collectors.toList());
                goodsNewBean.setTags2(collect1);
            }
        }
    }

    /**
     * 商品详情
     */
    public GoodsBean detail(HttpServletRequest request, String goodsId,String userId,String wxSceneId){
        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat(request);

        List<PromotionGroupSpecUser> list = new ArrayList<>();

        User user = userService.getById(userId);
        //没有团购了
//        if(user.getPromotionGroupLeader()){
//            //团长在该活动商品的规格
//           list = promotionGroupSpecUserService.lambdaQuery()
//                    .eq(PromotionGroupSpecUser::getUserId,userId)
//                    .eq(PromotionGroupSpecUser::getGoodsId,goodsId)
//                    .list();
//        }else{
//            String promotionGroupUserId = user.getPromotionGroupUserId();
//            if(StringUtils.isNotBlank(promotionGroupUserId)) {
//                list = promotionGroupSpecUserService.lambdaQuery()
//                        .eq(PromotionGroupSpecUser::getUserId, promotionGroupUserId)
//                        .eq(PromotionGroupSpecUser::getGoodsId, goodsId)
//                        .list();
//            }
//        }

        GoodsBean goodsBean = this.commonDetail(goodsId, user,wxSceneId);
//        if(list.size() == 0){
//            goodsBean = this.commonDetail(goodsId, userId,wxSceneId);
//        }else{
//            goodsBean = this.groupGoodsdetail(list);
//        }

        //补充标签
        //查询上标签
        List<GoodsTagRela> list1 = goodsTagRelaService.lambdaQuery().eq(GoodsTagRela::getGoodsId, goodsBean.getGoodsId())
                .eq(GoodsTagRela::getType,1).list();
        if (list1.size() > 0) {
            List<String> collect1 = list1.stream().map(GoodsTagRela::getGoodsTagName).collect(Collectors.toList());
            goodsBean.setTags1(collect1);
        }

        //查询活动专区标签 //查询下标签
        //活动商品
        List<GoodsTagRela> list2 = goodsTagRelaService.lambdaQuery().eq(GoodsTagRela::getGoodsId, goodsBean.getGoodsId())
                .eq(GoodsTagRela::getType,2).list();
        if (list2.size() > 0) {
            List<String> collect1 = list2.stream().map(GoodsTagRela::getGoodsTagName).collect(Collectors.toList());
            goodsBean.setTags2(collect1);
        }


        //记录用户浏览记录
        if(user != null) {
            GoodsVisit goodsVisit = new GoodsVisit();
            goodsVisit.setGoodsId(goodsId);
            goodsVisit.setGoodsName(goodsBean.getGoodsName());
            goodsVisit.setUserId(userId);
            goodsVisit.setUserName(user.getNickName());
            goodsVisit.setUserPhone(user.getMobile());
            goodsVisit.setPromotionGroupId(goodsBean.getPromotionGroupId());
            goodsVisit.setGoodsPrice(goodsBean.getGoodsPrice());
            goodsVisit.setGoodsStatus(goodsBean.getStatus());
            goodsVisit.setShareUserId(goodsBean.getGroupUserId());
            goodsVisit.setGoodsImg(goodsBean.getImgUrl());
            goodsVisit.setCompanyWechatId(currentCompanyWechat.getCurrentCompanyWechatId());
            goodsVisit.setCompanyName(currentCompanyWechat.getCurrentCompanyName());
            goodsVisit.setCreateTime(new Date());
            goodsVisit.insert();
        }
        return goodsBean;
    }


    /**
     * 商品详情
     *
     * @param goodsId
     * @return
     */
    public GoodsBean commonDetail(String goodsId,User user,String wxSceneId) throws RemoteServiceException {
        User service = null;
        String serviceId = null;
        AdminCompanyWechat adminCompanyWechat = null;
        if(user != null) {
            serviceId = user.getServiceId();
            if (user.getType().equals(UserTypeEnum.SERVICE.toString())) {
                serviceId = user.getUserId();
            }
            service = userService.getById(serviceId);
            adminCompanyWechat = commonLogic.getAdminCompanyWechat(user.getCompanyWechatId());
        }

        Goods goods = goodsService.lambdaQuery()
                .eq(Goods::getGoodsId, goodsId)
                .eq(Goods::getDel, false)
                .eq(Goods::getStatus, true)
                .eq(Goods::getPromotionGroup,false)
                .one();
        if (goods == null) {
            throw new RemoteServiceException("商品不存在或已下架");
        }
        //商品规格（包含秒杀商品）
        List<GoodsSpecSecBean> goodsSpecs = goodsSpecDetailMapper.querySpecSec(goodsId);
        //是否有收藏
        Integer goodsFavoritesCount = 0;
        if (Objects.nonNull(user)) {
            // 有user对象再查找
            goodsFavoritesCount = goodsFavoriteService.lambdaQuery()
                    .eq(GoodsFavorite::getGoodsId, goodsId)
                    .eq(GoodsFavorite::getUserId, user.getUserId())
                    .count();
        }

        //商品库存
        Integer goodsStock = goodsSpecs.stream().collect(Collectors.summingInt(x -> x.getStockNum()));
        GoodsBean goodsBean = new GoodsBean();
        BeanUtils.copyProperties(goods, goodsBean);

        BigDecimal shareAmount1 = BigDecimal.ZERO;
        BigDecimal shareAmount2 = BigDecimal.ZERO;
        //内部人员、秒杀活动、让利分享 等对价格分佣金额产生的影响处理
        for (GoodsSpecSecBean goodsSpec : goodsSpecs) {
            if (goodsSpec.getOrgPrice().doubleValue() == 0) {
                goodsSpec.setOrgPrice(goodsSpec.getOrgGoodsPrice());
            }
            shareAmount1 = goodsSpec.getShareAmount();
            shareAmount2 = goodsSpec.getInnerShareAmount();
            //内部人员
//            if (service != null && service.getInnerr() && service.getType().equals(UserTypeEnum.SERVICE.toString())) {
//                goodsSpec.setShareAmount(goodsSpec.getInnerShareAmount());
//            }
            //秒杀
            if (goodsSpec.getSecType()) {
                goodsSpec.setPrice(goodsSpec.getSecPrice());
                goodsSpec.setShareAmount(goodsSpec.getSecShareAmount());
                goodsSpec.setInnerShareAmount(goodsSpec.getSecShareAmount());

                shareAmount1 = goodsSpec.getShareAmount();
                shareAmount2 = goodsSpec.getInnerShareAmount();
            }
            //满件打折
            if(goods.getPromotionFullPiece()){
                List<PromotionFullPieceItem> items = promotionFullPieceLogic.getItems();
                if(items.size() > 0) {
                    PromotionFullPieceItem promotionFullPieceItem = items.get(0);
                    if (promotionFullPieceItem != null) {
                        goodsSpec.setPromotionFullPieceNum(promotionFullPieceItem.getSatisfyNum());
                        goodsSpec.setPromotionFullPiecePrice(promotionFullPieceItem.getDiscountRate()
                                .multiply(goodsSpec.getPrice()).setScale(2, RoundingMode.HALF_UP));
                    }
                }
            }
        }

        if(!CollectionUtils.isEmpty(goodsSpecs)) {
            //秒杀商品，需要覆盖一些值
            if (goodsSpecs.get(0).getSecType()) {
                goodsBean.setSecType(goodsSpecs.get(0).getSecType());
                goodsBean.setLimitBuy(goodsSpecs.get(0).getLimitBuy());
                goodsBean.setSecStockNum(goodsSpecs.get(0).getSecStockNum());
                goodsBean.setSecKillId(goodsSpecs.get(0).getSecKillId());
                goodsBean.setSecKillSpecId(goodsSpecs.get(0).getSecKillSpecId());
                goodsBean.setEndHour(goodsSpecs.get(0).getEndHour());
                goodsBean.setGoodsPrice(goodsSpecs.get(0).getSecPrice());
            }
            goodsBean.setShareAmount(goodsSpecs.get(0).getShareAmount());
        }
        if(goodsBean.getGoodsType().equals(GoodsTypeEnum.PACKAGE.toString())){
            goodsBean.setShareAmount(goodsBean.getPackageMinShareAmount());
            goodsBean.setGoodsPrice(goodsBean.getPackageMinAmount());
            goodsBean.setOrgGoodsPrice(goodsBean.getPackageMinAmount());
        }
        goodsBean.setShareAmount1(shareAmount1);
        goodsBean.setShareAmount2(shareAmount2);
        if(adminCompanyWechat != null) {
            goodsBean.setIsMaster(StringUtils.equals(adminCompanyWechat.getLinkPhone(), user.getMobile()));
        }
        goodsBean.setGoodsSpecs(goodsSpecs);
        goodsBean.setImages(commonLogic.queryFileByObjId(goods.getGoodsId(), Constant.Img.GOODS_IMG));
        goodsBean.setFavorite(goodsFavoritesCount > 0);
        goodsBean.setStock(goodsStock);
        goodsBean.setCommonTemplate(goodsSpecDetailMapper.queryCommonTemplate(goodsId));
        goodsBean.setPubCommonTemplate(pubTemplateService.getById(goods.getCompanyWechatId()));
        //满件打折商品
        if(goodsBean.getPromotionFullPiece()){
            List<PromotionFullPieceItem> items = promotionFullPieceLogic.getItems();
            if(items.size() == 0) {
                return goodsBean;
            }
            PromotionFullPieceItem promotionFullPieceItem = items.get(0);
            if(promotionFullPieceItem == null){
                return goodsBean;
            }
            goodsBean.setPromotionFullPieceNum(promotionFullPieceItem.getSatisfyNum());
            goodsBean.setPromotionFullPiecePrice(promotionFullPieceItem.getDiscountRate().multiply(goodsBean.getGoodsPrice()).setScale(2, RoundingMode.HALF_UP));
        }
        return goodsBean;
    }


    /**
     * 返回 降价的金额幅度map key=商品规格id，value = 降价的金额
     * @param wxSceneId
     * @return
     */
    public Map<String,BigDecimal> cutAmountGoods(String wxSceneId){
        //通过分享的形式购买的
        Map<String,BigDecimal> cutMap = new HashMap<>();
        if(StringUtils.isNotBlank(wxSceneId)) {
            WxScene scene = commonLogic.getScene(wxSceneId);
            if(scene.getType().equals(QrCodeEnum.CUT.toString().toLowerCase())) {
                //如果是秒杀商品，不在秒杀活动中则无效
                if(StringUtils.isNotBlank(scene.getSecKillActivityId()) && !secKillLogic.isSecing(scene.getSecKillActivityId())){
                    throw new RemoteServiceException("分享链接活动已结束");
                }
                //如果不是秒杀商品，但在秒杀活动中则无效
                if(StringUtils.isBlank(scene.getSecKillActivityId()) && secKillLogic.isSecing(scene.getSecKillActivityId())){
                    throw new RemoteServiceException("分享链接已过期");
                }
                List<WxSceneDetail> sceneDetail = commonLogic.getSceneDetail(wxSceneId);
                for(WxSceneDetail wxSceneDetail : sceneDetail){
                    cutMap.put(wxSceneDetail.getGoodsSpecId(),wxSceneDetail.getCutAmount());
                }
            }
        }

        if(cutMap.keySet().size() == 0){
            return null;
        }
        return cutMap;
    }

    /**
     * 团购商品列表
     * @param userId
     * @return
     */
    public IPage<PromotionGoodsBean> myGroupGoods(String userId,String goodsCategoryId, String keyword, Integer pageNum, Integer pageSize,HttpServletRequest request){
        User user = userService.getById(userId);
        if(!user.getPromotionGroupLeader()){
            userId = user.getPromotionGroupUserId();
        }
        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat(request);
        String companyWechatId = currentCompanyWechat.getCompanyWechatId();
        IPage<PromotionGoodsBean> promotionGoodsBeanIPage = goodsSpecDetailMapper.queryPromotionGoods(new Page(pageNum, pageSize), userId,goodsCategoryId, keyword,companyWechatId);
        return promotionGoodsBeanIPage;
    }

    /**
     * 团购商品详情
     */
    public GoodsBean groupGoodsdetail(List<PromotionGroupSpecUser> list){

        PromotionGroupSpecUser promotionGroupSpecUser = list.get(0);

        //团长id
        String groupUserId = promotionGroupSpecUser.getUserId();
        User groupUser = userService.getById(groupUserId);
        if(!groupUser.getPromotionGroupLeader()){
            throw new RemoteServiceException("链接已失效");
        }

        Goods goods = goodsService.getById(promotionGroupSpecUser.getGoodsId());

        GoodsBean goodsBean = new GoodsBean();
        BeanUtils.copyProperties(goods,goodsBean);
        //多规格
        List<GoodsSpecSecBean> goodsSpecSecBeans = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            PromotionGroupSpecUser specUser = list.get(i);
            PromotionGroupSpec promotionGroupSpec = promotionGroupSpecService.getById(specUser.getPromotionGroupSpecId());

            GoodsSpecSecBean specSecBean = new GoodsSpecSecBean();
            specSecBean.setPrice(specUser.getGroupPrice());
            specSecBean.setOrgPrice(specUser.getOrgGoodsPrice());
            specSecBean.setImgUrl(specUser.getGoodsImgSrc());
            specSecBean.setShareAmount(specUser.getShareAmount());
            specSecBean.setStockNum(promotionGroupSpec.getStock());
            specSecBean.setSoldNum(promotionGroupSpec.getSalesNum());
            specSecBean.setName(promotionGroupSpec.getGoodsSpecName());
            specSecBean.setSpecValue(promotionGroupSpec.getGoodsSpecValue());
            specSecBean.setGoodsSpecId(promotionGroupSpec.getGoodsSpecId());
            specSecBean.setGoodsId(promotionGroupSpec.getGoodsId());

            if(i == 0) {
                goodsBean.setStock(promotionGroupSpec.getStock());
                goodsBean.setSoldNum(promotionGroupSpec.getSalesNum());
                goodsBean.setPromotionGroupId(promotionGroupSpec.getPromotionGroupId());
            }
            goodsSpecSecBeans.add(specSecBean);
        }
        goodsBean.setOrgGoodsPrice(promotionGroupSpecUser.getOrgGoodsPrice());
        goodsBean.setGoodsPrice(promotionGroupSpecUser.getGroupPrice());
        goodsBean.setImages(commonLogic.queryFileByObjId(goods.getGoodsId(), Constant.Img.GOODS_IMG));
        goodsBean.setGroupUserId(groupUserId);
        goodsBean.setGroupUserName(promotionGroupSpecUser.getUserName());
        goodsBean.setGroupPic(groupUser.getAvatar());
        goodsBean.setGoodsSpecs(goodsSpecSecBeans);
        return goodsBean;
    }
    /**
     * 团购活动二维码
     */
    public PromotionShareQrCode queryShareQrcode(String userId,HttpServletRequest request) throws IOException {
        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat(request);
        User user = userService.getById(currentCompanyWechat.getUserId());
        if(!user.getStatus() || !user.getPromotionGroupLeader()){
            throw new RemoteServiceException("无效的用户");
        }
        //查询活动背景图
        List<PromotionGroup> list = promotionGroupService.lambdaQuery()
                .le(PromotionGroup::getStartTime, new Date())
                .ge(PromotionGroup::getEndTime, new Date())
                .eq(PromotionGroup::getStatus, true)
                .eq(PromotionGroup::getCompanyWechatId,user.getCompanyWechatId())
                .orderByDesc(PromotionGroup::getCreateTime)
                .list();
        if(list.size() == 0){
            return null;
        }
        PromotionGroup promotionGroup = list.get(0);
        String qrcode = wechatLogic.getQrcode(QrCodeEnum.PROMOTION_GROUP.toString().toLowerCase(),promotionGroup.getPromotionGroupId(),userId,user.getCompanyWechatId());
        PromotionShareQrCode promotionShareQrCode = new PromotionShareQrCode();
        promotionShareQrCode.setPromotionImgUrl(qrcode);
        promotionShareQrCode.setQrcode(qrcode);
        promotionShareQrCode.setPromotionImgUrl(promotionGroup.getImgUrl());
        promotionShareQrCode.setPosterImgUrl(promotionGroup.getPosterImgUrl());
        return promotionShareQrCode;
    }


    /**
     * 让利二维码或链接
     */
//    public String getCutQrcode(List<CutGoodsBean> cutGoodsBeans, HttpServletRequest request){
//        if(CollectionUtils.isEmpty(cutGoodsBeans)){
//            throw new RemoteServiceException("降价二维码参数异常");
//        }
//        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat(request);
//        String userId = currentCompanyWechat.getUserId();
//        String companyWechatId = currentCompanyWechat.getCompanyWechatId();
//        String secKillActivityId = cutGoodsBeans.get(0).getSecKillActivityId();
//        String goodsId = cutGoodsBeans.get(0).getGoodsId();
//        Boolean qrcode = cutGoodsBeans.get(0).getQrcode();
//        User user = userService.getById(userId);
//        //检查让利是否合理
//        Goods goods = this.getGoodsById(goodsId);
//        if(goods.getPromotionGroup()){
//            throw new RemoteServiceException("团购商品不支持降价分享");
//        }
//        //检查合法性
//        for(CutGoodsBean cutGoodsBean : cutGoodsBeans){
//            this.checkCut(user, cutGoodsBean.getGoodsId(), cutGoodsBean.getGoodsSpecId(), cutGoodsBean.getCutAmount(), companyWechatId);
//        }
//        String cutQrcode = wechatLogic.getCutQrcode(goodsId,userId, companyWechatId, qrcode,secKillActivityId, cutGoodsBeans);
//        return cutQrcode;
//    }

    /**
     * 判断让利金额是否合
     */
    public Boolean checkCut(User user,String goodsId,String goodsSpecId,BigDecimal cutAmount,String companyWechatId){
        if(!user.getType().equals(UserTypeEnum.SERVICE.toString())){
            throw new RemoteServiceException("非分销员不可降价分享");
        }
        //覆盖其上秒杀价格
        SecKillActivitySpec secKillActivitySpec = secActivityStageMapper.getSecKillActivitySpecByGoodId(companyWechatId,goodsId);
        if(secKillActivitySpec != null){
            //秒杀的分佣金额
            BigDecimal amount = secKillActivitySpec.getAmount();
            if(amount.doubleValue() < cutAmount.doubleValue()){
                throw new RemoteServiceException("让利降价金额设置不合理");
            }
        }else {
            //检查普通商品
            GoodsSpec goodsSpec = this.getGoodsSpecById(goodsSpecId);
            //内部人员价格
            boolean innerrUserShare = user.getInnerr() && goodsSpec.getInnerShareAmount().doubleValue() < cutAmount.doubleValue();
            //外部人员价格
            boolean notInnerrUserShare = !user.getInnerr() && goodsSpec.getShareAmount().doubleValue() < cutAmount.doubleValue();
            if(innerrUserShare || notInnerrUserShare){
                throw new RemoteServiceException("让利降价金额设置不合理");
            }
        }

        return true;
    }

    /**
     * 获取商品
     * @param goodsId
     * @return
     */
    public Goods getGoodsById(String goodsId) {
        return goodsService.getById(goodsId);
    }

    public GoodsSpec getGoodsSpecById(String goodsSpecId) {
        return goodsSpecService.getById(goodsSpecId);
    }

    /**
     * 团购商品-选购列表
     */
    public List<List<GoodsPackageBean>> choicePackage(String goodsId,HttpServletRequest request){
        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat(request);
        String companyWechatId = currentCompanyWechat.getCompanyWechatId();
        List<GoodsPackageBean> goodsPackageBeans = goodsSpecDetailMapper.queryGoodsPackage(currentCompanyWechat.getUser().getUserId(),currentCompanyWechat.getUser().getType(),goodsId, companyWechatId);
        if(CollectionUtils.isEmpty(goodsPackageBeans)){
            return new ArrayList();
        }
        List<Integer> types = goodsPackageBeans.stream().map(GoodsPackageBean::getType).distinct().collect(Collectors.toList());
        List<List<GoodsPackageBean>> list = new ArrayList<>();
        for(Integer type : types){
            List<GoodsPackageBean> collect = goodsPackageBeans.stream().filter(v -> v.getType() == type).collect(Collectors.toList());
            list.add(collect);
        }
        return list;
    }

}
