package com.sifang.modules.sunshine.SystemSetting.dao;

import com.jfinal.plugin.activerecord.Db;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmTraderWarehouseRel;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.common.persistence.Parameter;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Administrator
 */
@Repository
public class TraderWarehouseRelDao extends BaseDao<CrmTraderWarehouseRel> {

    /**
     * 创建授权二级代理商仓库
     */
    public void createOne(String traderId, String warehouseId) {
        CrmTraderWarehouseRel rel = new CrmTraderWarehouseRel();
        rel.setTraderId(traderId);
        rel.setWarehouseId(warehouseId);
        save(rel);
    }

    /**
     * 删除二级代理商授权仓库
     */
    public void deleteRelByTrader(String traderId) {
        update("delete from CrmTraderWarehouseRel where traderId=:p1", new Parameter(traderId));
    }

    /**
     * 获取授权二级代理商仓库
     */
    public List<String> findByTraderId(String traderId) {
        return Db.query("SELECT warehouse_id FROM crm_trader_warehouse_rel WHERE trader_id = ?", traderId);
    }

    /**
     * 校验出库仓库是否在二级代理商授权仓库内
     */
    public boolean checkByTraderIdAndWarehouseId(String traderId, String warehouseId) {
        return Db.queryLong("SELECT COUNT(1) FROM crm_trader_warehouse_rel WHERE trader_id = ? AND warehouse_id = ?", traderId, warehouseId) > 0;
    }
}
