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.StoreOpenAccountReadMapper;
import com.slodon.b2b2c.dao.write.seller.StoreOpenAccountWriteMapper;
import com.slodon.b2b2c.seller.example.StoreOpenAccountExample;
import com.slodon.b2b2c.seller.pojo.StoreOpenAccount;
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 StoreOpenAccountModel {
    @Resource
    private StoreOpenAccountReadMapper storeOpenAccountReadMapper;

    @Resource
    private StoreOpenAccountWriteMapper storeOpenAccountWriteMapper;

    /**
     * 新增商户中金开户表
     *
     * @param storeOpenAccount 商户中金开户信息
     * @return
     */
    public Integer saveStoreOpenAccount(StoreOpenAccount storeOpenAccount) {
        int count = storeOpenAccountWriteMapper.insert(storeOpenAccount);
        if (count == 0) {
            throw new MallException("添加商户中金开户表失败，请重试");
        }
        return count;
    }

    /**
     * 根据openAccountId删除商户中金开户表
     *
     * @param openAccountId openAccountId
     * @return
     */
    public Integer deleteStoreOpenAccount(Integer openAccountId) {
        if (StringUtils.isEmpty(openAccountId)) {
            throw new MallException("请选择要删除的数据");
        }
        int count = storeOpenAccountWriteMapper.deleteByPrimaryKey(openAccountId);
        if (count == 0) {
            log.error("根据openAccountId：" + openAccountId + "删除商户中金开户表失败");
            throw new MallException("删除商户中金开户表失败,请重试");
        }
        return count;
    }

    /**
     * 根据openAccountId更新商户中金开户表
     *
     * @param storeOpenAccount 商户中金开户信息
     * @return
     */
    public Integer updateStoreOpenAccount(StoreOpenAccount storeOpenAccount) {
        if (StringUtils.isEmpty(storeOpenAccount.getOpenAccountId())) {
            throw new MallException("请选择要修改的数据");
        }
        int count = storeOpenAccountWriteMapper.updateByPrimaryKeySelective(storeOpenAccount);
        if (count == 0) {
            log.error("根据openAccountId：" + storeOpenAccount.getOpenAccountId() + "更新商户中金开户表失败");
            throw new MallException("更新商户中金开户表失败,请重试");
        }
        return count;
    }

    /**
     * 根据openAccountId获取商户中金开户表详情
     *
     * @param openAccountId openAccountId
     * @return
     */
    public StoreOpenAccount getStoreOpenAccountByOpenAccountId(Integer openAccountId) {
        return storeOpenAccountReadMapper.getByPrimaryKey(openAccountId);
    }

    /**
     * 根据条件获取商户中金开户表列表
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<StoreOpenAccount> getStoreOpenAccountList(StoreOpenAccountExample example, PagerInfo pager) {
        List<StoreOpenAccount> storeOpenAccountList;
        if (pager != null) {
            pager.setRowsCount(storeOpenAccountReadMapper.countByExample(example));
            storeOpenAccountList = storeOpenAccountReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            storeOpenAccountList = storeOpenAccountReadMapper.listByExample(example);
        }
        return storeOpenAccountList;
    }

    /**
     * 根据店铺id获取开户信息
     *
     * @param storeId 店铺id
     * @return 开户进度信息
     */
    public StoreOpenAccount getStoreOpenAccountByStoreId(Long storeId){
        AssertUtil.notNull(storeId,"店铺id不能为空");
        StoreOpenAccountExample storeOpenAccountExample = new StoreOpenAccountExample();
        storeOpenAccountExample.setStoreId(storeId);
        List<StoreOpenAccount> storeOpenAccountList = storeOpenAccountReadMapper.listByExample(storeOpenAccountExample);
        return CollectionUtils.isEmpty(storeOpenAccountList) ? null : storeOpenAccountList.get(0);
    }
}