package com.playlet.web.service.app.impl;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.playlet.system.domain.PlayletItem;
import com.playlet.system.domain.PlayletItemType;
import com.playlet.system.service.IPlayletItemService;
import com.playlet.system.service.IPlayletItemTypeService;
import com.playlet.web.service.app.PlayletItemAppService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PlayletItemAppServiceImpl implements PlayletItemAppService {

    private final IPlayletItemService iPlayletItemService;

    private final IPlayletItemTypeService iPlayletItemTypeService;

    @Override
    public PageInfo<PlayletItem> getItemPage(PlayletItem playletItem, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<PlayletItem> playletItems = iPlayletItemService.selectPlayletItemList(playletItem);
        playletItems.forEach(model->{
            if(ObjectUtil.isNotNull(model.getItemType())){
                PlayletItemType itemType = iPlayletItemTypeService.selectPlayletItemTypeById(Long.valueOf(model.getItemType()));
                model.setItemTypeName(itemType.getName());
            }
        });
        return PageInfo.of(playletItems);
    }

}
