package com.slodon.b2b2c.model.seller;

import cn.hutool.core.bean.BeanUtil;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.dao.read.seller.StoreSiteInfoReadMapper;
import com.slodon.b2b2c.dao.write.seller.StoreSiteInfoWriteMapper;
import com.slodon.b2b2c.seller.example.StoreSiteInfoExample;
import com.slodon.b2b2c.seller.pojo.Store;
import com.slodon.b2b2c.seller.pojo.StoreSiteInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
@Slf4j
public class StoreSiteInfoModel {

    @Resource
    private StoreSiteInfoReadMapper storeSiteInfoReadMapper;
    @Resource
    private StoreSiteInfoWriteMapper storeSiteInfoWriteMapper;

    public Store getStoreByStoreIdAndWebSite(Long storeId, String webSite) {
       StoreSiteInfo storeSiteInfo = storeSiteInfoReadMapper.getByStoreIdAndWebSite(storeId, webSite);
       return BeanUtil.copyProperties(storeSiteInfo, Store.class);
    }

    public void saveStoreSiteInfo(StoreSiteInfo storeSiteInfo) {
        storeSiteInfoWriteMapper.insert(storeSiteInfo);
    }

    public void updateStoreSiteInfo(StoreSiteInfo storeSiteInfo) {
        StoreSiteInfoExample example =new StoreSiteInfoExample();
        example.setWebSite(storeSiteInfo.getWebSite());
        example.setStoreId(storeSiteInfo.getStoreId());
        storeSiteInfoWriteMapper.updateByExampleSelective(storeSiteInfo, example);
    }

    public StoreSiteInfo getStoreSiteInfo(StoreSiteInfoExample example) {
        List<StoreSiteInfo> storeSiteInfoList = storeSiteInfoReadMapper.listByExample(example);
        return CollectionUtils.isEmpty(storeSiteInfoList) ? null : storeSiteInfoList.get(0);
    }

    public List<StoreSiteInfo> getStoreSiteInfoList(StoreSiteInfoExample example, PagerInfo pager) {
        List<StoreSiteInfo> storeSiteInfoList;
        if (pager != null) {
            pager.setRowsCount(storeSiteInfoReadMapper.countByExample(example));
            storeSiteInfoList = storeSiteInfoReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            storeSiteInfoList = storeSiteInfoReadMapper.listByExample(example);
        }
        return storeSiteInfoList;
    }
}