package com.ruoyi.web.modules.goodsservice.facade.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.modules.goodsservice.domain.BizGoodsService;
import com.ruoyi.web.modules.goodsservice.facade.IGoodsServiceFacade;
import com.ruoyi.web.modules.goodsservice.facade.IGoodsSnapshotFacade;
import com.ruoyi.web.modules.goodsservice.mapper.BizGoodsServiceMapper;
import com.ruoyi.web.modules.goodsservice.vo.GoodsServiceOptionVO;

/**
 * 商品服务 Facade 实现
 */
@Service
public class GoodsServiceFacadeImpl implements IGoodsServiceFacade
{
    @Autowired
    private BizGoodsServiceMapper goodsServiceMapper;

    @Autowired
    private IGoodsSnapshotFacade goodsSnapshotFacade;

    @Override
    public List<GoodsServiceOptionVO> listAllEnabled()
    {
        return toOptions(goodsServiceMapper.selectAllEnabled());
    }

    @Override
    public List<GoodsServiceOptionVO> listDefaultShow()
    {
        return toOptions(goodsServiceMapper.selectDefaultShowEnabled());
    }

    @Override
    public boolean isServiceEnabled(Long serviceId)
    {
        if (serviceId == null)
        {
            return false;
        }
        BizGoodsService row = goodsServiceMapper.selectById(serviceId);
        return row != null;
    }

    @Override
    public boolean existsGoodsRef(Long serviceId)
    {
        return goodsSnapshotFacade.existsGoodsRef(serviceId);
    }

    private List<GoodsServiceOptionVO> toOptions(List<BizGoodsService> list)
    {
        List<GoodsServiceOptionVO> options = new ArrayList<>();
        if (list == null)
        {
            return options;
        }
        for (BizGoodsService row : list)
        {
            options.add(new GoodsServiceOptionVO(row.getServiceId(), row.getServiceName(), row.getServiceIntro(),
                    row.getServiceIcon(), row.getDefaultShowFlag(), row.getSortNo()));
        }
        return options;
    }
}
