package com.iformall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iformall.common.IdWorker;
import com.iformall.domain.po.WxMerchantShop;
import com.iformall.domain.po.WxShop;
import com.iformall.mapper.WxMerchantShopMapper;
import com.iformall.mapper.WxShopMapper;
import com.iformall.service.WxMerchantShopService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class WxMerchantShopServiceImpl implements WxMerchantShopService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
    WxMerchantShopMapper wxMerchantShopMapper;

    @Autowired
    WxShopMapper wxShopMapper;


    @Override
    public PageInfo<WxMerchantShop> listAsPage(WxMerchantShop record, Integer pageIndex, Integer pageSize) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> wxMerchantShopMapper.findList(record));
    }

    @Override
    public PageInfo<WxShop> queryShopList(WxMerchantShop record, Integer pageIndex, Integer pageSize) {
        List<WxShop> resultlist=new ArrayList<>();
        PageHelper.startPage(pageIndex, pageSize);

        List<WxMerchantShop> merchantShops = wxMerchantShopMapper.findList(record);
        for(WxMerchantShop merchantShop:merchantShops){
            WxShop wxShop = new WxShop();
            wxShop.setId(merchantShop.getShopId());
            List<WxShop> shopList = wxShopMapper.findList(wxShop);
            if(shopList.size()>0){
                resultlist.add(shopList.get(0));
            }
        }

        PageInfo<WxShop> pageInfo = new PageInfo<>(resultlist);
        return pageInfo;
    }

    @Override
    public WxMerchantShop getById(Long id) {
        return wxMerchantShopMapper.selectById(id);
    }

    @Override
    public void saveOrUpdate(WxMerchantShop record) {
        if (record.getId() == null) {
            //record.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            final IdWorker idWorker = IdWorker.get();
        	record.setId(idWorker.nextId());
            wxMerchantShopMapper.insert(record);
        } else {
            wxMerchantShopMapper.updateById(record);
        }
    }

    @Override
    public void deleteById(Long id) {
        wxMerchantShopMapper.deleteById(id);
    }
    
    
    
    
    
    
    
    
}
