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.LambdaQueryWrapper;
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 com.foreverwin.modular.core.util.CommonMethods;
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;

/**
 * <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 List<Item> getItemDetailList() {
      String site=  CommonMethods.getSite();
      String local=LocaleContextHolder.getLocale().getLanguage();
        return itemMapper.getItemDetailList(site,local);
    }

    @Override
    public Item getItemDescription(String item) {
        String site=  CommonMethods.getSite();
        String local=LocaleContextHolder.getLocale().getLanguage();
        return itemMapper.getItemDescription(item,local,site);
    }


    @Override
    public List<Item> selectItemWithDesc(Item item) {
        Locale locale = LocaleContextHolder.getLocale();
        String site = CommonMethods.getSite();
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(Item.SITE, site);
        if( item.getItem() != null){
            queryWrapper.like(Item.ITEM, item.getItem());
        }
        if(item.getDescription()!= null){
            queryWrapper.like(Item.DESCRIPTION, item.getDescription());
        }
        return itemMapper.selectListByCriteria(locale.getLanguage(), queryWrapper);
    }

    @Override
    public List<Item> selectItemWithDesc(QueryWrapper<Item> queryWrapper) {
        return itemMapper.selectListByCriteria(LocaleContextHolder.getLocale().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) {
        Locale locale = LocaleContextHolder.getLocale();
        //----------------------------------------------------------------------------------
        LambdaQueryWrapper<Item> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Item::getSite, site);
        queryWrapper.eq(Item::getItem, item);
        queryWrapper.orderByDesc(Item::getCurrentRevision);
        //----------------------------------------------------------------------------------
        List<Item> resultLit = itemMapper.selectList(locale.getLanguage(), queryWrapper);
        //----------------------------------------------------------------------------------
        return resultLit != null && resultLit.size() > 0 ? resultLit.get(0) : null;
    }

    @Override
    public List<Item> selectItemLabelControl(QueryWrapper<Item> queryWrapper) {
        return itemMapper.selectItemControlList(LocaleContextHolder.getLocale().getLanguage(), queryWrapper);
    }



    @Override
    public IPage<Item> getItemPageByProdVer(IPage page,  String query) {
        String site=CommonMethods.getSite();
        String local=LocaleContextHolder.getLocale().getLanguage();

        return itemMapper.getItemPageByProdVer(page,site,local,query);
    }

    @Override
    public IPage<Item> getBklasItem(IPage page, String itemOrDesc) {
        String site=CommonMethods.getSite();
        String local=LocaleContextHolder.getLocale().getLanguage();
        return  itemMapper.getBklasItem(page,site,local,itemOrDesc);
    }


    @Override
    public IPage<Item> getRawMaterial(IPage page, String item) {
          String local=LocaleContextHolder.getLocale().getLanguage();
          String site=CommonMethods.getSite();
        return itemMapper.getRawMaterial(page,site,local,item);
    }

    @Override
    public Integer checkItemUnit(String unit, String site, String item) {
        return itemMapper.checkItemUnit(unit,site,item);
    }

    @Override
    public Item getItemDescription(String item, String local) {
        String site = CommonMethods.getSite();
        return itemMapper.getItemDescription(item, local, site);
    }

    @Override
    public List<Item> selectInnerOrder(String site, String handle) {
        return itemMapper.selectInnerOrder(site, handle);
    }


    @Override
    public List<Item> selectItemSnp(String site, String item) {
        return itemMapper.selectItemSnp(site, item);
    }

    @Override
    public Item selectBatchReview(String site, String item) {
        return itemMapper.selectBatchReview(site,item);
    }
}