package com.slodon.b2b2c.model.seller;

import com.slodon.b2b2c.core.exception.MallException;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.core.util.AssertUtil;
import com.slodon.b2b2c.dao.read.seller.StoreExpressSettingReadMapper;
import com.slodon.b2b2c.dao.write.seller.StoreExpressSettingWriteMapper;
import com.slodon.b2b2c.seller.example.StoreExpressSettingExample;
import com.slodon.b2b2c.seller.pojo.StoreExpressSetting;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

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

/**
 * 店铺快递配置表model
 */
@Component
@Slf4j
public class StoreExpressSettingModel {

    @Resource
    private StoreExpressSettingReadMapper storeExpressSettingReadMapper;
    @Resource
    private StoreExpressSettingWriteMapper storeExpressSettingWriteMapper;

    /**
     * 新增店铺快递配置表
     *
     * @param storeExpressSetting
     * @return
     */
    public Integer saveStoreExpressSetting(StoreExpressSetting storeExpressSetting) {
        int count = storeExpressSettingWriteMapper.insert(storeExpressSetting);
        if (count == 0) {
            throw new MallException("添加店铺快递配置表失败，请重试");
        }
        return storeExpressSetting.getSettingId();
    }

    /**
     * 根据settingId删除店铺快递配置表
     *
     * @param settingId settingId
     * @return
     */
    public Integer deleteStoreExpressSetting(Integer settingId) {
        if (StringUtils.isEmpty(settingId)) {
            throw new MallException("请选择要删除的数据");
        }
        int count = storeExpressSettingWriteMapper.deleteByPrimaryKey(settingId);
        if (count == 0) {
            log.error("根据settingId：" + settingId + "删除店铺快递配置表失败");
            throw new MallException("删除店铺快递配置表失败,请重试");
        }
        return count;
    }

    /**
     * 根据settingId更新店铺快递配置表
     *
     * @param storeExpressSetting
     * @return
     */
    public Integer updateStoreExpressSetting(StoreExpressSetting storeExpressSetting) {
        int count = 0;
        //查询是否有店铺配置
        StoreExpressSettingExample example = new StoreExpressSettingExample();
        example.setStoreId(storeExpressSetting.getStoreId());
        List<StoreExpressSetting> list = storeExpressSettingReadMapper.listByExample(example);
        if (!CollectionUtils.isEmpty(list)) {
            storeExpressSetting.setSettingId(list.get(0).getSettingId());
            count = storeExpressSettingWriteMapper.updateByPrimaryKeySelective(storeExpressSetting);
            AssertUtil.isTrue(count == 0, "更新店铺快递配置表失败,请重试");
        } else {
            count = this.saveStoreExpressSetting(storeExpressSetting);
        }
        return count;
    }

    /**
     * 根据settingId获取店铺快递配置表详情
     *
     * @param settingId settingId
     * @return
     */
    public StoreExpressSetting getStoreExpressSettingBySettingId(Integer settingId) {
        return storeExpressSettingReadMapper.getByPrimaryKey(settingId);
    }

    /**
     * 根据条件获取店铺快递配置表列表
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<StoreExpressSetting> getStoreExpressSettingList(StoreExpressSettingExample example, PagerInfo pager) {
        List<StoreExpressSetting> storeExpressSettingList;
        if (pager != null) {
            pager.setRowsCount(storeExpressSettingReadMapper.countByExample(example));
            storeExpressSettingList = storeExpressSettingReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            storeExpressSettingList = storeExpressSettingReadMapper.listByExample(example);
        }
        return storeExpressSettingList;
    }
}