package com.gree.mall.manager.logic;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.CarouselMap;
import com.gree.mall.manager.plus.entity.Goods;
import com.gree.mall.manager.plus.service.CarouselMapService;
import com.gree.mall.manager.plus.service.GoodsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class CarouselMapLogic {

    @Resource
    CarouselMapService carouselMapService;
    @Resource
    GoodsService goodsService;
    @Autowired
    CommonLogic commonLogic;

    /**
     * 轮播图列表分页
     */
    public IPage<CarouselMap> page(HttpServletRequest request, Boolean state, Integer pageNum, Integer pageSize) {
        AdminUserCom adminUser = commonLogic.getAdminUser(request);
        List<String> companyWechatIds = adminUser.getAdminCompanyIds();

        return carouselMapService.lambdaQuery()
                .eq(state != null, CarouselMap::getState, state)
                .orderByDesc(CarouselMap::getCreateTime)
                .page(new Page<>(pageNum, pageSize));
    }
    /**
     * 轮播图详情
     */
    public CarouselMap detail(String id){
        return carouselMapService.getById(id);
    }



    /**
     * 新增轮播图
     */
    @Transactional(rollbackFor = Exception.class)
    public void add(HttpServletRequest request, CarouselMap carouselMap) {
        AdminUserCom adminUser = commonLogic.getAdminUser(request);

        //新增轮播


        carouselMapService.save(carouselMap);
    }

    /**
     * 修改轮播图
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(CarouselMap carouselMap) {
        //修改轮播
        carouselMapService.updateById(carouselMap);
    }

    /**
     * 删除轮播图
     */
    @Transactional(rollbackFor = Exception.class)
    public void delete(String carouselMapId) {
        carouselMapService.removeById(carouselMapId);
    }

    /**
     * 批量删除轮播图
     */
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<String> carouselMapIds) {
        carouselMapService.removeByIds(carouselMapIds);
    }


    /**
     * 更改轮播图状态
     */
    public void updateState(String carouselMapId, Boolean state) {
        carouselMapService.lambdaUpdate().set(CarouselMap::getState, state)
                .eq(CarouselMap::getId, carouselMapId).update();
    }

    /**
     * 获取商品列表
     * @param goodsId
     * @param goodsName
     * @param pageNum
     * @param pageSize
     * @return
     */
    public IPage<Goods> getGoods(HttpServletRequest request, String goodsId, String goodsName, Integer pageNum, Integer pageSize) {
        AdminUserCom adminUser = commonLogic.getAdminUser(request);
        List<String> companyWechatIds = adminUser.getAdminCompanyIds();

        if (StringUtils.isNotBlank(goodsId)) {
            return goodsService.lambdaQuery()
                    .eq(Goods::getGoodsId, goodsId)
                    .page(new Page<>(pageNum, pageSize));
        }
        return goodsService.lambdaQuery()
                .eq(Goods::getStatus, true)
                .like(StringUtils.isNotBlank(goodsName), Goods::getGoodsName, goodsName)
                .page(new Page<>(pageNum, pageSize));
    }
}
