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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.miniapp.bean.user.CurrentCompanyWechat;
import com.gree.mall.miniapp.commonmapper.CommonMapper;
import com.gree.mall.miniapp.logic.common.CommonLogic;
import com.gree.mall.miniapp.plus.entity.GoodsCategory;
import com.gree.mall.miniapp.plus.entity.GoodsLease;
import com.gree.mall.miniapp.plus.service.GoodsLeaseService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

@Service
@Slf4j
@RequiredArgsConstructor
public class GoodsLeaseLogic {

    private final CommonLogic commonLogic;
    private final CommonMapper commonMapper;
    private final GoodsLeaseService goodsLeaseService;

    public IPage<GoodsLease> page(HttpServletRequest request, String categoryId, String goodsName, Integer pageNum, Integer pageSize) {
        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat(request);
        final Page<GoodsLease> page = goodsLeaseService.lambdaQuery()
                .eq(GoodsLease::getCompanyWechatId, currentCompanyWechat.getCurrentCompanyWechatId())
                .eq(StringUtils.isNotBlank(categoryId), GoodsLease::getCategoryId, categoryId)
                .like(StringUtils.isNotBlank(goodsName), GoodsLease::getName, goodsName)
                .eq(GoodsLease::getStatus, true)
                .page(new Page<>(pageNum, pageSize));
        return page;
    }

    public List<GoodsCategory> categoryList(HttpServletRequest request, String categoryName) {
        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat(request);
        List<GoodsCategory> categoryList = commonMapper.goodsLeaseCategoryList(currentCompanyWechat.getCurrentCompanyWechatId(), categoryName);
        return categoryList;
    }


    public GoodsLease detail(HttpServletRequest request, String id) {
        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat(request);
        return goodsLeaseService.getById(id);
    }
}
