package com.wzj.soopin.goods.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzj.soopin.goods.domain.bo.ProductBo;
import com.wzj.soopin.goods.domain.entity.Product;
import com.wzj.soopin.goods.domain.vo.ProductVO;
import com.wzj.soopin.goods.mapper.ProductMapper;
import com.wzj.soopin.goods.mapper.SkuMapper;
import com.wzj.soopin.goods.service.ProductService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.enums.TenantType;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.tenant.helper.TenantHelper;
import org.dromara.system.domain.SysDept;
import org.dromara.system.domain.SysTenant;
import org.dromara.system.domain.vo.SysUserTenantVO;
import org.dromara.system.mapper.SysDeptMapper;
import org.dromara.system.mapper.SysTenantMapper;
import org.dromara.system.mapper.SysUserTenantMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品信息Service业务层处理
 *
 *
 * @author zcc
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {


    private final ProductMapper productMapper;
    private final SkuMapper skuMapper;
    private final SysTenantMapper sysTenantMapper;
    private final SysDeptMapper sysDeptMapper;
    private final SysUserTenantMapper sysUserTenantMapper;

    @Override
    public IPage<ProductVO> getList(ProductBo query, Page<Product> page) {
        String tenantId = LoginHelper.getTenantId();
        List<String> tenantIds = CollUtil.newArrayList(tenantId);
        if(LoginHelper.isTenantAdmin()){
            SysTenant sysTenant = sysTenantMapper.selectOne(Wrappers.lambdaQuery(SysTenant.class).eq(SysTenant::getTenantId, tenantId));
            if(sysTenant.getType() != TenantType.MERCHANT.getType()){
                // 非商铺管理员则查看其所属租户创建的全部商铺
                List<Long> deptIdList = sysDeptMapper.selectList().stream().map(SysDept::getDeptId).toList();
                tenantIds = sysTenantMapper.selectByTenantTypeAndCreateDept(TenantType.MERCHANT.getType(), deptIdList).stream().map(SysTenant::getTenantId).toList();
            }
        }
        // 获取当前登录人被分配的,可管理的商铺租户id
        List<String> list = sysUserTenantMapper.getUserTenantList(LoginHelper.getUserId(), TenantType.MERCHANT.getType())
            .stream().map(SysUserTenantVO::getTenantId).toList();
        if(CollUtil.isNotEmpty(list)){
            tenantIds = new ArrayList<>(tenantIds);
            tenantIds.addAll(list);
        }
        List<String> finalTenantIds = tenantIds;
        return TenantHelper.ignore(() -> productMapper.getlist(page, query, finalTenantIds));
    }


    @Override
    public IPage<ProductVO> getRecommendPage( Page<Product> page) {

        //推荐商品要根据算法获取
        List<String> tenantIds = new ArrayList<>();

        return productMapper.getlist(page,new ProductBo(), tenantIds);
    }


    /**
     * 查询商品信息
     *
     * @param id 商品信息主键
     * @return 商品信息
     */
    @Override
    public ProductVO selectById(Long id) {
        return TenantHelper.ignore(() -> productMapper.selectProductWithSkus(id));
    }



    /**
     * 不带租户的获取商品信息
     * @param page
     * @param query
     * @return
     */
    public IPage<ProductVO> getProduct(Page<Product> page, ProductBo query) {
        return TenantHelper.ignore(() -> productMapper.getProduct(page, query));
    }


    /**
     * 不带租户的获取商品信息
     *
     * @param query
     * @param page
     * @return
     */
    @Override
    public IPage<ProductVO> getProductPageList(ProductBo query, Page<Product> page) {
        return TenantHelper.ignore(() -> productMapper.getProduct(page, query));
    }
}
