package com.cpop.mall.business.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.cpop.common.utils.StringUtils;
import com.cpop.common.utils.bean.BeanUtils;
import com.cpop.core.base.entity.PageDomain;
import com.cpop.core.base.exception.ServiceException;
import com.cpop.core.utils.SecurityUtils;
import com.cpop.core.utils.sql.SqlUtils;
import com.cpop.mall.business.bo.ShoppingCartBo;
import com.cpop.mall.business.bo.ShoppingCartPageBo;
import com.cpop.mall.business.vo.MiniShoppingCartPageVo;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.cpop.mall.business.entity.ShoppingCart;
import com.cpop.mall.business.mapper.ShoppingCartMapper;
import com.cpop.mall.business.service.ShoppingCartService;
import org.springframework.stereotype.Service;

import static com.cpop.mall.business.entity.table.ProductRecordTableDef.PRODUCT_RECORD;
import static com.cpop.mall.business.entity.table.ProductTableDef.PRODUCT;
import static com.cpop.mall.business.entity.table.ShoppingCartTableDef.SHOPPING_CART;
import static com.cpop.system.business.entity.table.StoreTableDef.STORE;

/**
 * 商城购物车表 服务层实现。
 *
 * @author DB
 * @since 2023-11-06
 */
@Service("shoppingCartService")
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    /**
     * @descriptions 分页查询小程序-商城-购物车分页
     * @author DB
     * @date 2023/11/06 14:02
     * @param bo 请求参数
     * @return: com.mybatisflex.core.paginate.Page<com.cpop.mall.business.vo.MiniShoppingCartPageVo>
     */
    @Override
    public Page<MiniShoppingCartPageVo> getMiniShoppingCartPage(ShoppingCartPageBo bo) {
        //获取当前用户信息
        JSONObject loginUserInfo = SecurityUtils.getInstance().getLoginUserInfo();
        PageDomain pageDomain = SqlUtils.getInstance().getPageDomain();
        QueryWrapper queryWrapper = QueryWrapper.create();
        if (StringUtils.isNotBlank(bo.getProductName())) {
            queryWrapper.and(PRODUCT.PRODUCT_NAME.like(bo.getProductName()));
        }
        return this.mapper.paginateAs(pageDomain.getPageNum(), pageDomain.getPageSize(),
                queryWrapper.select(SHOPPING_CART.ALL_COLUMNS)
                        //记录
                        .select(PRODUCT_RECORD.RECORD_NAMES)
                        //商品
                        .select(PRODUCT.PRODUCT_NAME,PRODUCT.ID.as(MiniShoppingCartPageVo::getProductId),PRODUCT.PIC_URL,PRODUCT.PIC_DETAIL_URL,PRODUCT.PAY_TYPE,PRODUCT.IS_UP)
                        //校区
                        .select(STORE.STORE_NAME)
                        .from(SHOPPING_CART)
                        .leftJoin(PRODUCT_RECORD).on(PRODUCT_RECORD.ID.eq(SHOPPING_CART.PRODUCT_RECORD_ID))
                        .leftJoin(PRODUCT).on(PRODUCT.ID.eq(PRODUCT_RECORD.PRODUCT_ID))
                        .leftJoin(STORE).on(STORE.ID.eq(SHOPPING_CART.STORE_ID))
                        .where(SHOPPING_CART.USER_ID.eq(loginUserInfo.getString("userId")))
                        .orderBy(SHOPPING_CART.CREATE_TIME.desc()),
                MiniShoppingCartPageVo.class);
    }

    /**
     * @descriptions 小程序-商城-购物车添加商品
     * @author DB
     * @date 2023/11/06 14:17
     * @param bo 请求参数
     * @return: void
     */
    @Override
    public void insertShoppingCart(ShoppingCartBo bo) {
        //获取当前用户信息
        JSONObject loginUserInfo = SecurityUtils.getInstance().getLoginUserInfo();
        //查询用户购物车是否有同种商品
        long count = this.count(QueryWrapper.create().where(SHOPPING_CART.PRODUCT_RECORD_ID.eq(bo.getProductRecordId()))
                .and(SHOPPING_CART.USER_ID.eq(loginUserInfo.getString("userId"))));
        if (count > 0) {
            throw new ServiceException("您当前购物车已有此商品,请勿重复添加");
        }
        ShoppingCart shoppingCart = BeanUtils.mapToClass(bo, ShoppingCart.class);
        shoppingCart.setUserId(loginUserInfo.getString("userId"));
        this.save(shoppingCart);
    }
}
