$params['effective_num'], 'effective_unit' => $params['effective_unit'], 'remark' => $params['remark'], ]); if(!empty($params['goods_category_ids'])){ $effectiveCategoryArr = []; $ruleId = $effectiveRules->id; foreach($params['goods_category_ids'] as $v){ $effectiveCategoryArr[] = end($v); } $categoryIds = EffectiveCategory::where([['goods_category_id','in',$effectiveCategoryArr],['effective_id','<>',$ruleId]])->column('goods_category_id'); if(!empty($categoryIds)){ throw new \Exception('存在已分配的分类:'.implode(',',$categoryIds)); } $effectiveRules->effectiveWithCategory()->saveAll($effectiveCategoryArr); } Db::commit(); return true; } catch (\Exception $e) { Db::rollback(); self::setError($e->getMessage()); return false; } } /** * @notes 编辑 * @param array $params * @return bool * @author likeadmin * @date 2024/07/17 11:49 */ public static function edit(array $params): bool { Db::startTrans(); try { EffectiveRules::where('id', $params['id'])->update([ 'effective_num' => $params['effective_num'], 'effective_unit' => $params['effective_unit'], 'remark' => $params['remark'], ]); EffectiveCategory::where('effective_id',$params['id'])->delete(); $effectiveCategoryArr = []; if(!empty($params['goods_category_ids'])){ foreach($params['goods_category_ids'] as $v){ $effectiveCategoryArr[] =end($v); } $categoryIds = EffectiveCategory::where([['goods_category_id','in',$effectiveCategoryArr],['effective_id','<>', $params['id']]])->column('goods_category_id'); if(!empty($categoryIds)){ throw new \Exception('存在已分配的分类:'.implode(',',$categoryIds)); } $effectiveRules = EffectiveRules::find($params['id']); $effectiveRules->effectiveWithCategory()->saveAll($effectiveCategoryArr); } Db::commit(); return true; } catch (\Exception $e) { Db::rollback(); self::setError($e->getMessage()); return false; } } /** * @notes 删除 * @param array $params * @return bool * @author likeadmin * @date 2024/07/17 11:49 */ public static function delete(array $params): bool { EffectiveCategory::where('effective_id',$params['id'])->delete(); return EffectiveRules::destroy($params['id']); } /** * @notes 获取详情 * @param $params * @return array * @author likeadmin * @date 2024/07/17 11:49 */ public static function detail($params): array { $rules = EffectiveRules::findOrEmpty($params['id'])->toArray(); if(!empty($rules)){ $rules['goods_category_ids'] = EffectiveCategory::where('effective_id',$params['id'])->column('goods_category_id'); } return $rules; } }