package com.slodon.b2b2c.model.seller;

import com.slodon.b2b2c.core.constant.NumConstant;
import com.slodon.b2b2c.core.exception.MallException;
import com.slodon.b2b2c.dao.read.seller.StoreProfileTextListReadMapper;
import com.slodon.b2b2c.dao.write.seller.StoreProfileListWriteMapper;
import com.slodon.b2b2c.dao.write.seller.StoreProfileTextListWriteMapper;
import com.slodon.b2b2c.seller.example.StoreCompanyShowListExample;
import com.slodon.b2b2c.seller.example.StoreProfileTextListExample;
import com.slodon.b2b2c.seller.pojo.StoreProfileTextList;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

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

/**
 * 店铺简介内容表model
 */
@Component
@Slf4j
public class StoreProfileTextListModel {

    @Resource
    private StoreProfileTextListReadMapper storeProfileTextListReadMapper;

    @Resource
    private StoreProfileTextListWriteMapper storeProfileTextListWriteMapper;

    /**
     * 根据店铺id查询这笔记录是否存在
     * @param storeId 店铺id
     * @return
     */
    public StoreProfileTextList getList(Long storeId,String webSite){
        StoreProfileTextList storeProfileTextList = null;
        StoreProfileTextListExample storeProfileTextListExample = new StoreProfileTextListExample();
        storeProfileTextListExample.createCriteria().andStoreIdEqualTo(storeId).andWebSiteEqualTo(webSite);
        List<StoreProfileTextList> storeProfileTextInfoList = storeProfileTextListReadMapper.selectByExample(storeProfileTextListExample);
        if(!CollectionUtils.isEmpty(storeProfileTextInfoList)){
            storeProfileTextList = storeProfileTextInfoList.get(NumConstant.ZERO);
        }
        return storeProfileTextList;
    }

    /**
     * 更新店铺简介内容表
     * @param storeProfileTextList
     * @return
     */
    public Integer updateProfileText(StoreProfileTextList storeProfileTextList,Long storeId){
        StoreProfileTextListExample storeProfileTextListExample = new StoreProfileTextListExample();
        storeProfileTextListExample.createCriteria().andStoreIdEqualTo(storeId).andWebSiteEqualTo(storeProfileTextList.getWebSite());
        int count = storeProfileTextListWriteMapper.updateByExampleSelective(storeProfileTextList,storeProfileTextListExample);
        if(count==0){
            throw new MallException("更新店铺简介内容表失败,请重试");
        }
        return count;
    }

    /**
     * 新增店铺简介内容表
     * @param storeProfileTextList
     * @return
     */
    public Integer saveProfileText(StoreProfileTextList storeProfileTextList){
        int count = storeProfileTextListWriteMapper.insert(storeProfileTextList);
        if(count==0){
            throw new MallException("新增店铺简介内容表失败,请重试");
        }
        return count;
    }

}
