package com.kmall.api.service;

import com.google.common.collect.Maps;
import com.kmall.api.annotation.LoginUser;
import com.kmall.api.dao.*;
import com.kmall.api.dto.CampMinusDto;
import com.kmall.api.dto.CheckOutDto;
import com.kmall.api.dto.UserCouponDto;
import com.kmall.api.entity.*;
import com.kmall.api.entity.mk.MkStoreCampMinusVo;
import com.kmall.api.service.mk.ApiMkStoreCampMinusService;
import com.kmall.common.constant.Dict;
import com.kmall.common.utils.Constant;
import com.kmall.common.utils.MapUtils;
import com.kmall.common.utils.RRException;
import com.kmall.common.utils.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class ApiCartService {
    private Log logger = LogFactory.getLog(ApiCartService.class);
    @Autowired
    private ApiCartMapper cartDao;
    @Autowired
    private ApiGoodsCrashMapper apiGoodsCrashMapper;
    @Autowired
    private ApiProductMapper apiProductMapper;
    @Autowired
    private ApiGoodsMapper apiGoodsMapper;
    @Autowired
    private ApiGoodsSpecificationService apiGoodsSpecificationService;
    @Autowired
    private ApiStoreMapper apiStoreMapper;
    @Autowired
    private ApiThirdMerchantBizMapper apiThirdMerchantBizMapper;
    @Autowired
    private ApiFreightMapper apiFreightMapper;
    @Autowired
    private ApiUserCouponService apiUserCouponService;
    @Autowired
    private ApiMkStoreCampMinusService apiMkStoreCampMinusService;

    public CartVo queryObject(Integer id) {
        return cartDao.queryObject(id);
    }

    public CartVo queryObjectByGoodsIdAndUserId(Long goods_id,Long user_id,Long storeId) {
        return cartDao.queryObjectByGoodsIdAndUserId(goods_id,user_id,storeId);
    }


    public List<CartVo> queryList(Map<String, Object> map) {
        return cartDao.queryList(map);
    }

    public List<CartVo> queryValidCartList(Map<String, Object> map) {
        return cartDao.queryValidCartList(map);
    }

    public int queryTotal(Map<String, Object> map) {
        return cartDao.queryTotal(map);
    }


    public void save(CartVo cart) {
        // 更新购物车搭配减价
        cartDao.save(cart);
        // 判断购物车中是否存在此规格商品
        Map cartParam = Maps.newHashMap();
        cartParam.put("user_id", cart.getUser_id());
        List<CartVo> cartInfoList = cartDao.queryList(cartParam);
        // 是否有搭配减价
        Map crashParam = Maps.newHashMap();
        List<Long> goods_ids = new ArrayList();
        List<CartVo> cartUpdateList = new ArrayList();
        if(cartInfoList != null && cartInfoList.size() > 0) {
            for (CartVo cartItem : cartInfoList) {
                if (null != cartItem.getChecked() && 1 == cartItem.getChecked()) {
                    goods_ids.add(cartItem.getGoods_id());
                }
                if (!cartItem.getRetail_price().equals(cartItem.getRetail_product_price())) {
                    cartItem.setRetail_price(cartItem.getRetail_product_price());
                    cartUpdateList.add(cartItem);
                }
            }
        }else{
            goods_ids.add(cart.getGoods_id());
        }
        crashParam.put("store_id", cart.getStore_id());
        crashParam.put("goods_ids", goods_ids);
        List<GoodsCrashVo> goodsCrashList = apiGoodsCrashMapper.queryList(crashParam);
        if (null != goodsCrashList) {
            for (GoodsCrashVo crashVo : goodsCrashList) {
                for (CartVo cartItem : cartInfoList) {
                    // 存在原始的
                    if (null != cartItem.getChecked() && 1 == cartItem.getChecked()
                            && cartItem.getGoods_id().equals(crashVo.getGoods_id())) {
                        for (CartVo cartCrash : cartInfoList) { // 搭配上了商品
                            if (!cartCrash.getId().equals(cartItem.getId())
                                    && cartCrash.getGoods_id().equals(crashVo.getGoods_crash_id())
                                    && cartCrash.getProduct_id().equals(crashVo.getProduct_crash_id())) {
                                cartItem.setRetail_price(crashVo.getRetail_crash_price());
                                cartUpdateList.add(cartItem);
                            }
                        }
                    }
                }
            }
        }
        if (null != cartUpdateList && cartUpdateList.size() > 0) {
            for (CartVo cartItem : cartUpdateList) {
                cartDao.update(cartItem);
            }
        }
    }

    public void update(CartVo cart) {
        cartDao.update(cart);
    }


    public void delete(Long id) {
        cartDao.delete(id);
    }

    public void deleteBatch(Integer[] ids) {
        cartDao.deleteBatch(ids);
    }

    public void updateCheck(String[] goodsIds, Integer isChecked, Long userId, Long storeId) {
        cartDao.updateCheck(goodsIds, isChecked, userId, storeId);
    }

    public void deleteByProductIds(String[] productIds, Long storeId) {
        cartDao.deleteByProductIds(productIds, storeId);
    }

    /**
     * 只根据商品Id添加到购物车
     *
     * @param user_id
     * @param goods_id
     */
    public Map addByGoodsId(Long user_id, Long goods_id, Long store_id) {
        Map<String, Object> resultObj = Maps.newHashMap();
        resultObj.put("errno", 0);
        resultObj.put("errmsg", "添加成功");
        // 判断购物车中是否存在此规格商品
        Map cartParam = Maps.newHashMap();
        cartParam.put("user_id", user_id);
        cartParam.put("goods_id", goods_id);
        List<CartVo> cartInfoList = cartDao.queryList(cartParam);
        if (null != cartInfoList && cartInfoList.size() > 0) {
            CartVo cartVo = cartInfoList.get(0);
            cartVo.setNumber(cartVo.getNumber() + 1);
            update(cartVo);
            return resultObj;
        }
        ProductVo productVo = apiProductMapper.queryOneByGoodsId(goods_id, store_id);
        if (null == productVo || productVo.getStock_num() < 1) {
//            resultObj.put("errno", 1);
//            resultObj.put("errmsg", "库存不足,仅剩余" + productVo.getStock_num());
//            return resultObj;
            logger.error("库存不足,仅剩余" + productVo.getStock_num());
            throw new RRException("库存不足,仅剩余" + productVo.getStock_num());
        }
        GoodsVo goodsVo = apiGoodsMapper.queryObjectByStoreId(goods_id, store_id);
        CartVo cartInfo = new CartVo();
        cartInfo.setGoods_id(productVo.getGoods_id());
        cartInfo.setProduct_id(productVo.getId());
        cartInfo.setGoods_sn(productVo.getGoods_sn());
        cartInfo.setGoods_name(goodsVo.getName());
        cartInfo.setList_pic_url(goodsVo.getList_pic_url());
        cartInfo.setNumber(1);
        cartInfo.setStore_id(store_id);
        cartInfo.setUser_id(user_id);
        cartInfo.setRetail_price(productVo.getRetail_price());
        cartInfo.setMarket_price(productVo.getMarket_price());

        //添加规格名和值
        cartInfo.setGoods_specification_name_value(productVo.getGoods_specification_name_value());
        cartInfo.setGoods_specification_ids(productVo.getGoods_specification_ids());
        cartInfo.setChecked(1);
        cartInfo.setGoodsBizType(goodsVo.getGoodsBizType());
        cartInfo.setMerchSn(goodsVo.getMerchSn());
        save(cartInfo);
        return resultObj;
    }

    public List<CartVo> queryCartByGoodsBizType(String goodsBizType) {
        return cartDao.queryCartByGoodsBizType(goodsBizType);
    }

    public Map<String, Object> getCartMoney(UserVo loginUser, CheckOutDto checkOutDto, Long storeId){
        Map<String, Object> resultObj = Maps.newHashMap();
        Long goodsId = checkOutDto.getGoodsId();
        Integer number = checkOutDto.getNumber();
        String checkCart = checkOutDto.getCheckCart();
        Integer goodsCount = 0;
        BigDecimal goodsAmount = new BigDecimal(0.00);
        Integer checkedGoodsCount = 0;
        BigDecimal checkedGoodsAmount = new BigDecimal(0.00);
        BigDecimal checkedGoodsAmount00 = new BigDecimal(0.00);
        BigDecimal checkedGoodsAmount02 = new BigDecimal(0.00);
        BigDecimal checkedGoodsAmount10 = new BigDecimal(0.00);
        BigDecimal checkedGoodsAmount11 = new BigDecimal(0.00);
        List<CartVo> cartList = new ArrayList<>();
        //商品id不为空，则提交订单是从商品详情页立即购买过来的
        if(goodsId != null){
            GoodsVo goodsVo = apiGoodsMapper.queryObjectByStoreId(goodsId, storeId);
            if(goodsVo != null){
                if(goodsVo.getGoods_number() == 0){
                    logger.error("商品库存不足" );
                    throw new RRException("商品库存不足");
                }
                CartVo vo = new CartVo();
                vo.setGoodsBizType(goodsVo.getGoodsBizType());
                vo.setGoods_id(goodsVo.getId());
                vo.setNumber(number);
                vo.setUser_id(checkOutDto.getLoginUser().getId());
                vo.setSku(goodsVo.getSku());
                vo.setGoods_sn(goodsVo.getGoods_sn());
                vo.setGoods_name(goodsVo.getName());
                vo.setRetail_price(goodsVo.getRetail_price());
                vo.setStore_id(Long.valueOf(goodsVo.getStoreId()));
                vo.setChecked(1);
                vo.setList_pic_url(goodsVo.getList_pic_url());
                vo.setProduct_id(Long.valueOf(goodsVo.getProduct_id()));
                vo.setMarket_price(goodsVo.getMarket_price());
                cartList.add(vo);
            }else {
                logger.error("商品信息不存在" );
                throw new RRException("商品信息不存在");
            }
        }else{
            //查询列表数据
            Map param = Maps.newHashMap();
            param.put("user_id", loginUser.getId());
            param.put("store_id", storeId);
            param.put("checkCart",checkCart);
            param.put("checked", "1");
            param.putAll(setIsStockShare(storeId));
            cartList = cartDao.queryList(param);
        }
        //获取购物车统计信息
        for (CartVo cartItem : cartList) {
            goodsCount += cartItem.getNumber();
            goodsAmount = goodsAmount.add(cartItem.getRetail_price().multiply(new BigDecimal(cartItem.getNumber())));
            if (null != cartItem.getChecked() && 1 == cartItem.getChecked()) {
                if(cartItem.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_00.getItem())){
                    checkedGoodsAmount00 = checkedGoodsAmount00.add(cartItem.getRetail_price().multiply(new BigDecimal(cartItem.getNumber())));
                }
                if(cartItem.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_02.getItem())){
                    checkedGoodsAmount02 = checkedGoodsAmount02.add(cartItem.getRetail_price().multiply(new BigDecimal(cartItem.getNumber())));
                }
                if(cartItem.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_10.getItem())){
                    checkedGoodsAmount10 = checkedGoodsAmount10.add(cartItem.getRetail_price().multiply(new BigDecimal(cartItem.getNumber())));
                }
                if (cartItem.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_11.getItem())) {
                    checkedGoodsAmount11 = checkedGoodsAmount11.add(cartItem.getRetail_price().multiply(new BigDecimal(cartItem.getNumber())));
                }
                checkedGoodsCount += cartItem.getNumber();
                checkedGoodsAmount = checkedGoodsAmount.add(cartItem.getRetail_price().multiply(new BigDecimal(cartItem.getNumber())));
            }
        }

        resultObj.put("cartList", cartList);
        Map<String, Object> cartTotal = Maps.newHashMap();
        cartTotal.put("goodsCount", goodsCount);
        cartTotal.put("goodsAmount", goodsAmount);
        cartTotal.put("checkedGoodsCount", checkedGoodsCount);
        cartTotal.put("checkedGoodsAmount", checkedGoodsAmount);
        cartTotal.put("checkedGoodsAmount00", checkedGoodsAmount00);
        cartTotal.put("checkedGoodsAmount02", checkedGoodsAmount02);
        cartTotal.put("checkedGoodsAmount10", checkedGoodsAmount10);
        cartTotal.put("checkedGoodsAmount11", checkedGoodsAmount11);
        resultObj.put("cartTotal", cartTotal);

        return resultObj;
    }

    private Map setIsStockShare(Long storeId){
        Map param = Maps.newHashMap();
        StoreVo storeVo = apiStoreMapper.queryObject(storeId);
        if(storeVo != null) {
            ThirdMerchantBizVo thirdMerchantBiz = apiThirdMerchantBizMapper.getThirdMerchangByCode(storeVo.getThirdPartyMerchCode());
            if (null == thirdMerchantBiz) {
                logger.error("第三方商户为空" );
                throw new RRException("第三方商户为空");
            }
            param.put("isStockShare", thirdMerchantBiz.getIsStockShare());
        }
        return param;
    }

    public Integer queryCartNumByStoreId(Map<String, Object> map){
        return cartDao.queryCartNumByStoreId(map);
    }

    /**
     * 优惠券活动计算实付抵扣
     * @param checkedCoupon
     * @param actualPrice
     * @return
     */
    public BigDecimal mathActualPrice(UserCouponVo checkedCoupon, BigDecimal actualPrice,List<CartVo> checkedGoodsList){
        BigDecimal actualPrice2 = Constant.ZERO;
        BigDecimal actualPrice3 = Constant.ZERO;
        if(checkedCoupon != null){
            if (checkedCoupon.getTickDiscType().equalsIgnoreCase(Dict.tickDiscType.item_00.getItem())) {
                actualPrice = actualPrice.subtract(checkedCoupon.getTypeMoney());
            }
            if (checkedCoupon.getTickDiscType().equalsIgnoreCase(Dict.tickDiscType.item_01.getItem())) {
                for(int i = 0; i < checkedGoodsList.size(); i++){
                    CartVo cartVo = checkedGoodsList.get(i);
                    List<Integer> goodsIds = checkedCoupon.getGoodsList();
                    for(int j=0;j<goodsIds.size();j++){
                        if(cartVo.getGoods_id().compareTo(Long.valueOf(goodsIds.get(j)))==0){
                            actualPrice2 = actualPrice2.add(cartVo.getRetail_price().multiply(BigDecimal.valueOf(cartVo.getNumber())));
                        }else{
                            actualPrice3 = actualPrice3.add(cartVo.getRetail_price().multiply(BigDecimal.valueOf(cartVo.getNumber())));
                        }
                    }
                }
                //实付金额*(优惠券折扣*0.1) = 折扣后的实付金额
                actualPrice = actualPrice3.add(actualPrice2.multiply(checkedCoupon.getTypeMoney().multiply(new BigDecimal(0.1))).setScale(2, BigDecimal.ROUND_HALF_UP));//满多少打几折
            }
        }
        return actualPrice;
    }

    /**
     * 满减满折活动计算实付满减
     * @param checkedCampMinus
     * @param actualPrice
     * @return
     */
    public BigDecimal mathActualPriceByCamp(CampMinusDto checkedCampMinus, BigDecimal actualPrice,List<CartVo> checkedGoodsList){
        BigDecimal actualPrice2 = Constant.ZERO;
        BigDecimal actualPrice3 = Constant.ZERO;
        if(checkedCampMinus != null){
            if (checkedCampMinus.getCampMinusType().equalsIgnoreCase(Dict.campMinusType.item_00.getItem()) || checkedCampMinus.getCampMinusType().equalsIgnoreCase(Dict.campMinusType.item_01.getItem())) {
                actualPrice = actualPrice.subtract(checkedCampMinus.getMoney());
            }
            if (checkedCampMinus.getCampMinusType().equalsIgnoreCase(Dict.campMinusType.item_10.getItem()) || checkedCampMinus.getCampMinusType().equalsIgnoreCase(Dict.campMinusType.item_11.getItem())) {
                for(int i = 0; i < checkedGoodsList.size(); i++){
                    CartVo cartVo = checkedGoodsList.get(i);
                    List<Integer> goodsIds = checkedCampMinus.getGoodsIds();
                    for(int j=0;j<goodsIds.size();j++){
                        if(cartVo.getGoods_id().compareTo(Long.valueOf(goodsIds.get(j)))==0){
                            actualPrice2 = actualPrice2.add(cartVo.getRetail_price().multiply(BigDecimal.valueOf(cartVo.getNumber())));
                        }else{
                            actualPrice3 = actualPrice3.add(cartVo.getRetail_price().multiply(BigDecimal.valueOf(cartVo.getNumber())));
                        }
                    }
                }
                //实付金额*(满减满折折扣*0.1) = 折扣后的实付金额
                actualPrice = actualPrice3.add(actualPrice2.multiply(checkedCampMinus.getMoney().multiply(new BigDecimal(0.1))).setScale(2, BigDecimal.ROUND_HALF_UP));//满多少打几折
            }
        }
        return actualPrice;
    }

    /**
     * 确认订单页
     * @param checkOutDto
     * @return
     */
    public Map<String, Object> getCheckOut(CheckOutDto checkOutDto){
        Map<String, Object> resultObj = Maps.newHashMap();
        UserVo loginUser = checkOutDto.getLoginUser();
        String checkCart = checkOutDto.getCheckCart();
        Long storeId = checkOutDto.getStoreId();
        Long tickDiscId00 = checkOutDto.getTickDiscId00();
        Long tickDiscId02 = checkOutDto.getTickDiscId02();
        Long tickDiscId10 = checkOutDto.getTickDiscId10();
        Long tickDiscId11 = checkOutDto.getTickDiscId11();
        Integer campId00 = checkOutDto.getCampId00();
        Integer campId02 = checkOutDto.getCampId02();
        Integer campId10 = checkOutDto.getCampId10();
        Integer campId11 = checkOutDto.getCampId11();
        Map<String, Object> cartData = checkOutDto.getCartData();
        List<CartVo> checkedGoodsList = new ArrayList();
        List<CartVo> cartGoodsList = (List<CartVo>) cartData.get("cartList");
        for (CartVo cartEntity : cartGoodsList) {
            if (cartEntity.getChecked() == 1) {
                checkedGoodsList.add(cartEntity);
            }
        }

        //计算订单的费用 商品总价
        BigDecimal goodsTotalPrice = (BigDecimal) ((HashMap) cartData.get("cartTotal")).get("checkedGoodsAmount");
        //商品销售价*数量
        BigDecimal goodsTotalPrice00 = (BigDecimal) ((HashMap) cartData.get("cartTotal")).get("checkedGoodsAmount00");
        BigDecimal goodsTotalPrice02 = (BigDecimal) ((HashMap) cartData.get("cartTotal")).get("checkedGoodsAmount02");
        BigDecimal goodsTotalPrice10 = (BigDecimal) ((HashMap) cartData.get("cartTotal")).get("checkedGoodsAmount10");
        BigDecimal goodsTotalPrice11 = (BigDecimal) ((HashMap) cartData.get("cartTotal")).get("checkedGoodsAmount11");

        // 根据收货地址计算运费
//        BigDecimal freightPrice = apiCouponService.matchShipping(loginUser.getId(), goodsTotalPrice);
        BigDecimal freightPrice00 = new BigDecimal("00");
        BigDecimal freightPrice02 = new BigDecimal("00");
        BigDecimal freightPrice10 = new BigDecimal("00");
        BigDecimal freightPrice11 = new BigDecimal("00");
        Boolean isBizType00 = false;
        Boolean isBizType02 = false;
        Boolean isBizType10 = false;
        Boolean isBizType11 = false;
        //查询运费模板
        for (CartVo vo: checkedGoodsList) {
            if(org.apache.commons.lang.StringUtils.isEmpty(checkCart)){
                if(vo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_00.getItem())){
                    FreightEntity freightEntity = apiFreightMapper.queryObjectByGoodsId(vo.getGoods_id(), storeId);
                    freightPrice00 = freightEntity!=null ? freightEntity.getDefaultFreight().add(freightPrice00) : freightPrice00;
                    isBizType00 =true;
                }
                if(vo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_02.getItem())){
                    FreightEntity freightEntity = apiFreightMapper.queryObjectByGoodsId(vo.getGoods_id(), storeId);
                    freightPrice02 = freightEntity!=null ? freightEntity.getDefaultFreight().add(freightPrice02) : freightPrice02;
                    isBizType02 =true;
                }
                if(vo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_10.getItem())){
                    FreightEntity freightEntity = apiFreightMapper.queryObjectByGoodsId(vo.getGoods_id(), storeId);
                    freightPrice10 = freightEntity!=null ? freightEntity.getDefaultFreight().add(freightPrice10) : freightPrice10;
                    isBizType10 =true;
                }
                if (vo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_11.getItem())) {
                    FreightEntity freightEntity = apiFreightMapper.queryObjectByGoodsId(vo.getGoods_id(), storeId);
                    freightPrice11 = freightEntity != null ? freightEntity.getDefaultFreight().add(freightPrice11) : freightPrice11;
                    isBizType11 =true;
                }
            }else{
                if(Dict.orderBizType.item_11.getItem().equalsIgnoreCase(checkCart)) {
                    if (vo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_11.getItem())) {
                        FreightEntity freightEntity = apiFreightMapper.queryObjectByGoodsId(vo.getGoods_id(), storeId);
                        freightPrice11 = freightEntity != null ? freightEntity.getDefaultFreight().add(freightPrice11) : freightPrice11;
                        isBizType11 =true;
                    }
                }else{
                    if(vo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_00.getItem())){
                        FreightEntity freightEntity = apiFreightMapper.queryObjectByGoodsId(vo.getGoods_id(), storeId);
                        freightPrice00 = freightEntity!=null ? freightEntity.getDefaultFreight().add(freightPrice00) : freightPrice00;
                        isBizType00 =true;
                    }
                    if(vo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_02.getItem())){
                        FreightEntity freightEntity = apiFreightMapper.queryObjectByGoodsId(vo.getGoods_id(), storeId);
                        freightPrice02 = freightEntity!=null ? freightEntity.getDefaultFreight().add(freightPrice02) : freightPrice02;
                        isBizType02 =true;
                    }
                    if(vo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_10.getItem())){
                        FreightEntity freightEntity = apiFreightMapper.queryObjectByGoodsId(vo.getGoods_id(), storeId);
                        freightPrice10 = freightEntity!=null ? freightEntity.getDefaultFreight().add(freightPrice10) : freightPrice10;
                        isBizType10 =true;
                    }
                }
            }
        }
        //整个订单包含运费的总价,商品总价+运费
        BigDecimal orderTotalPrice = goodsTotalPrice.add(freightPrice00).add(freightPrice02).add(freightPrice10).add(freightPrice11);
        // 计算满减 todo 暂时不计算全场金额满减
//        BigDecimal fullCutCouponDec = Constant.ZERO;
//        Long fullCutCouponId = 0L;
        /*CouponVo fullSubCoupon = apiCouponService.matchFullSub(loginUser.getId(), goodsTotalPrice);
        if (null != fullSubCoupon && null != fullSubCoupon.getId()) {
            // 满减
            fullCutCouponDec = fullSubCoupon.getType_money();
            fullCutCouponId = fullSubCoupon.getId();
        }*/
//        resultObj.put("fullCutCouponDec", fullCutCouponDec);
//        resultObj.put("fullCutCouponId", fullCutCouponId);
        List<UserCouponVo> couponList00 = null;
        List<UserCouponVo> couponList02 = null;
        List<UserCouponVo> couponList10 = null;
        List<UserCouponVo> couponList11 = null;
        List<CampMinusDto> campList00 = null;
        List<CampMinusDto> campList02 = null;
        List<CampMinusDto> campList10 = null;
        List<CampMinusDto> campList11 = null;
        //获取每个订单的可用的优惠券信息 抵用券
        if(goodsTotalPrice00.compareTo(new BigDecimal(0)) > 0){
            UserCouponDto userCouponDto00 = new UserCouponDto();
            userCouponDto00.setUserId(loginUser.getId());
            userCouponDto00.setStoreId(storeId);
            userCouponDto00.setGoodsTotalPrice(goodsTotalPrice00);
            userCouponDto00.setCartGoodsList(cartGoodsList);
            userCouponDto00.setBizType(Dict.orderBizType.item_00.getItem());
            if(campId00 != null && campId00 != 0){
                userCouponDto00.setCampId(campId00);
            }
            if(tickDiscId00 != null && tickDiscId00 != 0){
                userCouponDto00.setTickDiscId(Integer.parseInt(String.valueOf(tickDiscId00)));
            }
            couponList00 = apiUserCouponService.matchUserCouponList(userCouponDto00);
            campList00 = apiMkStoreCampMinusService.matchUserCampMinusList(userCouponDto00);
        }
        if(goodsTotalPrice02.compareTo(new BigDecimal(0)) > 0) {
            UserCouponDto userCouponDto02 = new UserCouponDto();
            userCouponDto02.setUserId(loginUser.getId());
            userCouponDto02.setStoreId(storeId);
            userCouponDto02.setGoodsTotalPrice(goodsTotalPrice02);
            userCouponDto02.setCartGoodsList(cartGoodsList);
            userCouponDto02.setBizType(Dict.orderBizType.item_02.getItem());
            if(campId02 != null && campId02 != 0){
                userCouponDto02.setCampId(campId02);
            }
            if(tickDiscId02 != null && tickDiscId02 != 0){
                userCouponDto02.setTickDiscId(Integer.parseInt(String.valueOf(tickDiscId02)));
            }
            couponList02 = apiUserCouponService.matchUserCouponList(userCouponDto02);
            campList02 = apiMkStoreCampMinusService.matchUserCampMinusList(userCouponDto02);
        }
        if(goodsTotalPrice10.compareTo(new BigDecimal(0)) > 0) {
            UserCouponDto userCouponDto10 = new UserCouponDto();
            userCouponDto10.setUserId(loginUser.getId());
            userCouponDto10.setStoreId(storeId);
            userCouponDto10.setGoodsTotalPrice(goodsTotalPrice10);
            userCouponDto10.setCartGoodsList(cartGoodsList);
            userCouponDto10.setBizType(Dict.orderBizType.item_10.getItem());
            if(campId10 != null && campId10 != 0){
                userCouponDto10.setCampId(campId10);
            }
            if(tickDiscId10 != null && tickDiscId10 != 0){
                userCouponDto10.setTickDiscId(Integer.parseInt(String.valueOf(tickDiscId10)));
            }
            couponList10 = apiUserCouponService.matchUserCouponList(userCouponDto10);
            campList10 = apiMkStoreCampMinusService.matchUserCampMinusList(userCouponDto10);
        }
        if(goodsTotalPrice11.compareTo(new BigDecimal(0)) > 0) {
            UserCouponDto userCouponDto11 = new UserCouponDto();
            userCouponDto11.setUserId(loginUser.getId());
            userCouponDto11.setStoreId(storeId);
            userCouponDto11.setGoodsTotalPrice(goodsTotalPrice11);
            userCouponDto11.setCartGoodsList(cartGoodsList);
            userCouponDto11.setBizType(Dict.orderBizType.item_11.getItem());
            if(campId11 != null && campId11 != 0){
                userCouponDto11.setCampId(campId11);
            }
            if(tickDiscId11 != null && tickDiscId11 != 0){
                userCouponDto11.setTickDiscId(Integer.parseInt(String.valueOf(tickDiscId11)));
            }
            couponList11 = apiUserCouponService.matchUserCouponList(userCouponDto11);
            campList11 = apiMkStoreCampMinusService.matchUserCampMinusList(userCouponDto11);
        }
        CampMinusDto campMinusDto00 = null;
        CampMinusDto campMinusDto02 = null;
        CampMinusDto campMinusDto10 = null;
        CampMinusDto campMinusDto11 = null;

        UserCouponVo checkedCoupon00 = null;
        UserCouponVo checkedCoupon02 = null;
        UserCouponVo checkedCoupon10 = null;
        UserCouponVo checkedCoupon11 = null;
        Integer isLoadStatus = checkOutDto.getIsLoadStatus();
        //商品销售价*数量
        BigDecimal actualPrice00 = goodsTotalPrice00;
        BigDecimal actualPrice02 = goodsTotalPrice02;
        BigDecimal actualPrice10 = goodsTotalPrice10;
        BigDecimal actualPrice11 = goodsTotalPrice11;

        //根据购物车的商品list、实付金额、满减满折id、购物车商品满足可用的满减满折券list、加载状态，获取当前页面该选中的满减满折券信息
        Map campMinusMap00 = checkedCampMinus(campList00, checkedGoodsList, actualPrice00, campId00, isLoadStatus);
        Map campMinusMap02 = checkedCampMinus(campList02, checkedGoodsList, actualPrice02, campId02, isLoadStatus);
        Map campMinusMap10 = checkedCampMinus(campList10, checkedGoodsList, actualPrice10, campId10, isLoadStatus);
        Map campMinusMap11 = checkedCampMinus(campList11, checkedGoodsList, actualPrice11, campId11, isLoadStatus);

        //根据购物车的商品list、实付金额、优惠券id、购物车商品满足可用的优惠券list、加载状态，获取当前页面该选中的优惠券信息
        Map couponMap00 = checkedCoupon(couponList00, checkedGoodsList, tickDiscId00, campMinusMap00, isLoadStatus);
        Map couponMap02 = checkedCoupon(couponList02, checkedGoodsList, tickDiscId02, campMinusMap02, isLoadStatus);
        Map couponMap10 = checkedCoupon(couponList10, checkedGoodsList, tickDiscId10, campMinusMap10, isLoadStatus);
        Map couponMap11 = checkedCoupon(couponList11, checkedGoodsList, tickDiscId11, campMinusMap11, isLoadStatus);

        campMinusDto00 = getCampMinusByMap(campMinusMap00);
        campMinusDto02 = getCampMinusByMap(campMinusMap02);
        campMinusDto10 = getCampMinusByMap(campMinusMap10);
        campMinusDto11 = getCampMinusByMap(campMinusMap11);

        checkedCoupon00 = getUserCouponByMap(couponMap00);
        checkedCoupon02 = getUserCouponByMap(couponMap02);
        checkedCoupon10 = getUserCouponByMap(couponMap10);
        checkedCoupon11 = getUserCouponByMap(couponMap11);

        actualPrice00 = getActualPriceByMap(couponMap00);
        actualPrice02 = getActualPriceByMap(couponMap02);
        actualPrice10 = getActualPriceByMap(couponMap10);
        actualPrice11 = getActualPriceByMap(couponMap11);

        //商品销售价*数量-优惠金额+运费
        actualPrice00 = actualPrice00.add(freightPrice00);
        actualPrice02 = actualPrice02.add(freightPrice02);
        actualPrice10 = actualPrice10.add(freightPrice10);
        actualPrice11 = actualPrice11.add(freightPrice11);

        BigDecimal actualPrice = actualPrice00.add(actualPrice02).add(actualPrice10).add(actualPrice11);  //实付金额:商品销售价*数量-优惠金额+运费的总和

        for(CartVo cartVo: checkedGoodsList){
            List<Integer> goodsList = null;
            List<Integer> goodsDiscountList = null;
            if(cartVo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_00.getItem())){
                if(campMinusDto00 != null) {
                    goodsList = campMinusDto00.getGoodsIds();
                }
                if(checkedCoupon00 != null) {
                    goodsDiscountList = checkedCoupon00.getGoodsList();
                }
            }
            if(cartVo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_02.getItem())){
                if(campMinusDto02 != null) {
                    goodsList = campMinusDto02.getGoodsIds();
                }
                if(checkedCoupon02 != null) {
                    goodsDiscountList = checkedCoupon02.getGoodsList();
                }
            }
            if(cartVo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_10.getItem())){
                if(campMinusDto10 != null) {
                    goodsList = campMinusDto10.getGoodsIds();
                }
                if(checkedCoupon10 != null) {
                    goodsDiscountList = checkedCoupon10.getGoodsList();
                }
            }
            if(cartVo.getGoodsBizType().equalsIgnoreCase(Dict.orderBizType.item_11.getItem())){
                if(campMinusDto11 != null) {
                    goodsList = campMinusDto11.getGoodsIds();
                }
                if(checkedCoupon11 != null) {
                    goodsDiscountList = checkedCoupon11.getGoodsList();
                }
            }
            if(goodsList != null) {
                for (Integer goodsId : goodsList) {
                    if (cartVo.getGoods_id().compareTo(Long.valueOf(goodsId)) == 0) {
                        cartVo.setIsCamp("true");
                    }
                }
            }
            if(goodsDiscountList != null) {
                for (Integer goodsId : goodsDiscountList) {
                    if (cartVo.getGoods_id().compareTo(Long.valueOf(goodsId)) == 0) {
                        cartVo.setIsDiscount("true");
                    }
                }
            }
        }

        resultObj.put("freightPrice00", freightPrice00);
        resultObj.put("freightPrice02", freightPrice02);
        resultObj.put("freightPrice10", freightPrice10);
        resultObj.put("freightPrice11", freightPrice11);
        resultObj.put("checkedCoupon00", checkedCoupon00);
        resultObj.put("checkedCoupon02", checkedCoupon02);
        resultObj.put("checkedCoupon10", checkedCoupon10);
        resultObj.put("checkedCoupon11", checkedCoupon11);
        resultObj.put("campMinusDto00", campMinusDto00);
        resultObj.put("campMinusDto02", campMinusDto02);
        resultObj.put("campMinusDto10", campMinusDto10);
        resultObj.put("campMinusDto11", campMinusDto11);
        resultObj.put("couponList00", couponList00);
        resultObj.put("couponList02", couponList02);
        resultObj.put("couponList10", couponList10);
        resultObj.put("couponList11", couponList11);
        resultObj.put("campList00", campList00);
        resultObj.put("campList02", campList02);
        resultObj.put("campList10", campList10);
        resultObj.put("campList11", campList11);
        resultObj.put("campId00", campMinusDto00 != null ? campMinusDto00.getCampMinusId():0);
        resultObj.put("campId02", campMinusDto02 != null ? campMinusDto02.getCampMinusId():0);
        resultObj.put("campId10", campMinusDto10 != null ? campMinusDto10.getCampMinusId():0);
        resultObj.put("campId11", campMinusDto11 != null ? campMinusDto11.getCampMinusId():0);
        resultObj.put("tickDiscId00", checkedCoupon00 != null ? checkedCoupon00.getId():0);
        resultObj.put("tickDiscId02", checkedCoupon02 != null ? checkedCoupon02.getId():0);
        resultObj.put("tickDiscId10", checkedCoupon10 != null ? checkedCoupon10.getId():0);
        resultObj.put("tickDiscId11", checkedCoupon11 != null ? checkedCoupon11.getId():0);

        BigDecimal couponPrice = new BigDecimal(0.00);  //使用优惠券减免的金额
        resultObj.put("couponPrice", couponPrice);
        resultObj.put("checkedGoodsList", checkedGoodsList);
        resultObj.put("goodsTotalPrice", goodsTotalPrice);//整个订单商品总价，不包含运费和优惠金额
        resultObj.put("orderTotalPrice", orderTotalPrice);//整个订单包含运费的总价
        resultObj.put("actualPrice", actualPrice);
        resultObj.put("actualPrice00", actualPrice00);
        resultObj.put("actualPrice02", actualPrice02);
        resultObj.put("actualPrice10", actualPrice10);
        resultObj.put("actualPrice11", actualPrice11);
        resultObj.put("goodsTotalPrice00", goodsTotalPrice00);
        resultObj.put("goodsTotalPrice02", goodsTotalPrice02);
        resultObj.put("goodsTotalPrice10", goodsTotalPrice10);
        resultObj.put("goodsTotalPrice11", goodsTotalPrice11);
        resultObj.put("isBizType00", isBizType00);
        resultObj.put("isBizType02", isBizType02);
        resultObj.put("isBizType10", isBizType10);
        resultObj.put("isBizType11", isBizType11);
        return resultObj;
    }

    /**
     * 选择满减满折
     * @param campList
     * @param checkedGoodsList
     * @param actualPrice
     * @param campId
     * @param isLoadStatus 0则为进入页面加载优惠信息，默认选择最高层级促销活动，1为选择促销活动后加载优惠信息，2为选择优惠券时触发，优惠券不能与促销活动同时使用
     */
    private Map checkedCampMinus(List<CampMinusDto> campList, List<CartVo> checkedGoodsList, BigDecimal actualPrice, Integer campId, Integer isLoadStatus){
        Map campMap = new HashMap();
        CampMinusDto campMinusDto = null;
        if(campList != null && campList.size() > 0 && isLoadStatus != null){
            if(isLoadStatus == 0){ //0则为进入页面加载优惠信息，默认选择最高层级促销活动
                campMinusDto = campList.get(0);
                if(campMinusDto.getCampMinusId().compareTo(0) > 0){
                    //商品销售价*数量-优惠金额
                    actualPrice = mathActualPriceByCamp(campMinusDto, actualPrice, checkedGoodsList);
                    campMap.put("actualPrice", actualPrice);
                    campMap.put("isCamp", true);//满减活动是否满足该次订单
                }else{
                    campMap.put("actualPrice", actualPrice);
                    campMap.put("isCamp", false);
                }
                campMap.put("campMinusDto", campMinusDto);
                return campMap;
            }else if(isLoadStatus == 1) { //1为选择促销活动后加载优惠信息
                if (null != campId) {
                    if (campId == 0) {
                        //不参与满减活动
                        setNotCampMinus(campList, actualPrice, campMap);
                    } else {
                        for (CampMinusDto dto : campList) {
                            if (campId.compareTo(dto.getCampMinusId()) == 0) {
                                campMinusDto = dto;
                                //商品销售价*数量-优惠金额
                                actualPrice = mathActualPriceByCamp(campMinusDto, actualPrice, checkedGoodsList);
                                campMap.put("actualPrice", actualPrice);
                                campMap.put("isCamp", true);
                                campMap.put("campMinusDto", campMinusDto);
                            }
                        }
                    }
                }else{
                    campMap.put("actualPrice", actualPrice);
                    campMap.put("isCamp", false);
                }
            }else{//2为选择优惠券时触发，优惠券不能与促销活动同时使用
                setNotCampMinus(campList, actualPrice, campMap);
            }
            return campMap;
        }else{
            campMap.put("actualPrice", actualPrice);
            campMap.put("isCamp", false);
            return campMap;
        }
    }

    /**
     * 选择优惠券
     * @param couponList
     * @param checkedGoodsList
     * @param tickDiscId
     * @param campMinusMap
     * @param isLoadStatus 0则为进入页面加载优惠信息，默认选择最高层级促销活动，1为选择促销活动后加载优惠信息，2为选择优惠券时触发，优惠券不能与促销活动同时使用
     */
    private Map checkedCoupon(List<UserCouponVo> couponList, List<CartVo> checkedGoodsList, Long tickDiscId, Map campMinusMap, Integer isLoadStatus){
        BigDecimal actualPrice = MapUtils.getBigDecimal("actualPrice", campMinusMap);
        Boolean isCamp = (Boolean)campMinusMap.get("isCamp");
        Map couponMap = new HashMap();
        UserCouponVo checkedCoupon = null;
        //满减活动不与优惠券同时使用，当isCamp为false时可使用优惠券
        if(isCamp == false) {
            if (null != couponList && couponList.size() > 0) {
                if (isLoadStatus == 0) {
                    checkedCoupon = couponList.get(0);
                    actualPrice = mathActualPrice(checkedCoupon, actualPrice, checkedGoodsList);
                    couponMap.put("actualPrice", actualPrice);
                    couponMap.put("checkedCoupon", checkedCoupon);
                }else if(isLoadStatus == 1) {
                    //不参与优惠活动
                    setNotCoupon(couponList,actualPrice,couponMap);
                }else{
                    if (null != tickDiscId && tickDiscId == 0) {//不参与优惠活动
                        setNotCoupon(couponList,actualPrice,couponMap);
                    } else {
                        for (UserCouponVo userCouponVo : couponList) {
                            if (null != tickDiscId && tickDiscId.equals(userCouponVo.getId())) {
                                checkedCoupon = userCouponVo;
                                //商品销售价*数量-优惠金额
                                actualPrice = mathActualPrice(checkedCoupon, actualPrice, checkedGoodsList);
                                couponMap.put("actualPrice", actualPrice);
                                couponMap.put("checkedCoupon", checkedCoupon);
                            }
                        }
                    }
                }
            }else {
                couponMap.put("actualPrice", actualPrice);
            }
            return couponMap;
        }else{
            setNotCoupon(couponList,actualPrice,couponMap);
            return couponMap;
        }
    }

    /**
     * 不参与满减活动
     * @return
     */
    private void setNotCampMinus(List<CampMinusDto> campList,BigDecimal actualPrice, Map campMap){
        for (CampMinusDto dto : campList) {
            if (dto.getCampMinusId().compareTo(0)==0) {
                CampMinusDto campMinusDto = dto;
                campMap.put("actualPrice", actualPrice);
                campMap.put("isCamp", false);
                campMap.put("campMinusDto", campMinusDto);
                return;
            }
        }
    }

    /**
     * 不参与优惠券活动
     * @return
     */
    private void setNotCoupon(List<UserCouponVo> couponList,BigDecimal actualPrice, Map couponMap){
        if(couponList != null && couponList.size() > 0){
            for (UserCouponVo userCouponVo : couponList) {
                if (userCouponVo.getId().compareTo(0L)==0) {
                    UserCouponVo checkedCoupon = userCouponVo;
                    couponMap.put("actualPrice", actualPrice);
                    couponMap.put("checkedCoupon", checkedCoupon);
                    return;
                }
            }
        }else{
            couponMap.put("actualPrice", actualPrice);
            return;
        }
    }

    private CampMinusDto getCampMinusByMap(Map campMinusMap){
        if(campMinusMap.get("campMinusDto") != null){
            return (CampMinusDto)campMinusMap.get("campMinusDto");
        }
        return null;
    }

    private UserCouponVo getUserCouponByMap(Map couponMap){
        if(couponMap.get("checkedCoupon") != null){
            return (UserCouponVo) couponMap.get("checkedCoupon");
        }
        return null;
    }

    private BigDecimal getActualPriceByMap(Map couponMap){
        if(couponMap.get("actualPrice") != null){
            return MapUtils.getBigDecimal("actualPrice", couponMap);
        }
        return null;
    }
}
