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

import com.alibaba.fastjson.JSONObject;
import com.cpop.common.utils.bean.BeanUtils;
import com.cpop.core.base.entity.PageDomain;
import com.cpop.core.utils.SecurityUtils;
import com.cpop.core.utils.sql.SqlUtils;
import com.cpop.mall.business.bo.CarouselBo;
import com.cpop.mall.business.vo.CarouselPageVo;
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.Carousel;
import com.cpop.mall.business.mapper.CarouselMapper;
import com.cpop.mall.business.service.CarouselService;
import org.springframework.stereotype.Service;

import static com.cpop.mall.business.entity.table.CarouselTableDef.CAROUSEL;
import static com.cpop.mall.business.entity.table.ProductTableDef.PRODUCT;

/**
 * 商城轮播图 服务层实现。
 *
 * @author DB
 * @since 2024-01-15
 */
@Service("carouselService")
public class CarouselServiceImpl extends ServiceImpl<CarouselMapper, Carousel> implements CarouselService {

    /**
     * 分页查询商城轮播图
     * @author DB
     * @since 2024/1/15
     * @param upStatus 上架状态
     * @return Page<CarouselPageVo>
     */
    @Override
    public Page<CarouselPageVo> getCarouselPage(Boolean upStatus) {
        JSONObject loginUserInfo = SecurityUtils.getInstance().getLoginUserInfo();
        PageDomain pageDomain = SqlUtils.getInstance().getPageDomain();
        return this.pageAs(Page.of(pageDomain.getPageNum(), pageDomain.getPageSize()),
                QueryWrapper.create()
                        .select(CAROUSEL.ID,CAROUSEL.PRODUCT_ID,CAROUSEL.PIC_URL,CAROUSEL.ORDER_NO,CAROUSEL.UP_STATUS)
                        .select(PRODUCT.PRODUCT_NAME)
                        .leftJoin(PRODUCT).on(PRODUCT.ID.eq(CAROUSEL.PRODUCT_ID))
                        .where(CAROUSEL.BRAND_ID.eq(loginUserInfo.getString("brandId")))
                        .and(CAROUSEL.UP_STATUS.eq(upStatus))
                        .orderBy(CAROUSEL.ORDER_NO.asc()),
                CarouselPageVo.class);
    }

    /**
     * 新增轮播图
     * @author DB
     * @since 2024/1/15
     * @param bo 请求参数
     */
    @Override
    public void insertCarousel(CarouselBo bo) {
        JSONObject loginUserInfo = SecurityUtils.getInstance().getLoginUserInfo();
        Carousel carousel = BeanUtils.mapToClass(bo, Carousel.class);
        carousel.setBrandId(loginUserInfo.getString("brandId"));
        this.save(carousel);
    }

    /**
     * 修改轮播图
     * @author DB
     * @since 2024/1/15
     * @param bo 请求参数
     */
    @Override
    public void updateCarousel(CarouselBo bo) {
        JSONObject loginUserInfo = SecurityUtils.getInstance().getLoginUserInfo();
        Carousel carousel = BeanUtils.mapToClass(bo, Carousel.class);
        carousel.setBrandId(loginUserInfo.getString("brandId"));
        this.updateById(carousel);
    }
}
