package com.foreverwin.mesnac.meapi.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.foreverwin.mesnac.meapi.mapper.ItemMapper;
import com.foreverwin.mesnac.meapi.model.Item;
import com.foreverwin.mesnac.meapi.service.ItemService;
import com.foreverwin.mesnac.meapi.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Ervin Chen
 * @since 2020-10-19
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item> implements ItemService {


    @Autowired
    private ItemMapper itemMapper;

    @Override
    public Item selectById(String handle) {
        Item item = null;
        Locale locale = LocaleContextHolder.getLocale();
        if(StrUtil.isNotBlank(handle) && handle.endsWith("#")) {
            List<Item> itemList = this.itemMapper.selectList(locale.getLanguage(), new QueryWrapper<Item>()
                    .eq(Item.SITE, StringUtils.trimSite(handle))
                    .eq(Item.ITEM, StringUtils.trimHandle(handle))
                    .eq(Item.CURRENT_REVISION, "true"));
            return itemList == null || itemList.size() == 0 ? null : itemList.get(0);
        }
        return this.itemMapper.selectById(locale.getLanguage(), handle);
    }

    @Override
    public IPage<Item> selectPage(Page<Item> page, QueryWrapper<Item> queryWrapper) {
        Locale locale = LocaleContextHolder.getLocale();
        page.setRecords(this.itemMapper.selectPage(locale.getLanguage(), page, queryWrapper));
        return page;
    }

    @Override
    public List<Item> selectList(Item item) {
        Locale locale = LocaleContextHolder.getLocale();
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(item);
        return this.itemMapper.selectList(locale.getLanguage(), queryWrapper);
    }

    @Override
    public List<Item> selectList(Wrapper<Item> queryWrapper) {
        Locale locale = LocaleContextHolder.getLocale();
        return this.itemMapper.selectList(locale.getLanguage(), queryWrapper);
    }


    @Override
    public String selectCurrent(String handle){
        if(handle.endsWith("#")) {
            String item = StringUtils.trimHandle(handle);
            String site = StringUtils.trimSite(handle);
            Item currentItem = selectCurrent(site, item);
            return currentItem.getHandle();
        }
        return handle;
    }
    @Override
    public Item selectCurrent(String site, String item) {
        return itemMapper.selectCurrentRevision(site, item);
    }

    @Override
    public Map<String, Object> findMapByItemBo(String itemBo) {
        return itemMapper.findMapByItem(itemBo);
    }


}