/**
     * 新增、更新商品关联
     * @param array $ProductIds //商品数组
     * @param array $AgentS //代理数组
     * @param $is_sale //是否可以销售
     * @return \think\Collection|string
     * @throws \Exception
     */
    public function SetAgentProduct(array $ProductIds, array $AgentS, $is_sale)
    {
        $values = [];
        $LiuRunList = ['LiuRun', 'LiuRun_qj', 'LiuRun_jy'];
        $UserServices = app()->make(SystemAdminServices::class);
        foreach ($AgentS as $agent) {
            $AgentInfo = $UserServices->get($agent);
            if ($AgentInfo) {
                $AgentInfo = $AgentInfo->toArray();
            } else {
                continue;
            }
            $LiuRun = 0;
            if ((int)$AgentInfo['agentLevel'] < 3) {
                $LiuRun = (float)$LiuRunList[(int)$AgentInfo];
            }
            foreach ($ProductIds as $item) {
                $values[] = "('{$item['product_id']}', '{$item['agent_id']}', '{$item['is_sale']}', '{$item['Liurun']}', '{$item['code']}')";
            }
        }
        try {
            // 生成批量插入的SQL
            $sql = "INSERT INTO eb_agent_product (product_id, agent_id, is_sale, Liurun, code) VALUES " . implode(', ', $values) .
                " ON DUPLICATE KEY UPDATE is_sale = VALUES(is_sale), Liurun = VALUES(Liurun), code = VALUES(code)";
            Log::info('myhsql:' . $sql);
            // 执行SQL
            $res = Db::execute($sql);
            return $res;
        } catch (\Exception $e) {
            return $e->getMessage();
        }
    }
Last modification:July 15, 2024
反正也没人会打赏