|
@@ -0,0 +1,291 @@
|
|
|
|
|
+<?php
|
|
|
|
|
+declare(strict_types=1);
|
|
|
|
|
+
|
|
|
|
|
+namespace SixShop\ShippingTemplate\Controller;
|
|
|
|
|
+
|
|
|
|
|
+use SixShop\ShippingTemplate\Enum\ShippingTemplateAreaTypeEnum;
|
|
|
|
|
+use SixShop\ShippingTemplate\Model\ShippingTemplateModel;
|
|
|
|
|
+use SixShop\ShippingTemplate\Model\ShippingTemplateRelationModel;
|
|
|
|
|
+use SixShop\ShippingTemplate\Model\ShippingTemplateRuleModel;
|
|
|
|
|
+use think\Request;
|
|
|
|
|
+use function SixShop\Core\success_response;
|
|
|
|
|
+use function SixShop\Core\error_response;
|
|
|
|
|
+
|
|
|
|
|
+class ShippingTemplateController
|
|
|
|
|
+{
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取运费模板列表
|
|
|
|
|
+ * 根据业务类型和业务ID返回可用的运费模板列表,供前端下拉框使用
|
|
|
|
|
+ * 并标识出当前选中的模板
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param Request $request
|
|
|
|
|
+ * @return \think\response\Json
|
|
|
|
|
+ */
|
|
|
|
|
+ public function index(Request $request)
|
|
|
|
|
+ {
|
|
|
|
|
+ $bizType = $request->get('biz_type/s', '');
|
|
|
|
|
+ $bizId = $request->get('biz_id/d', 0);
|
|
|
|
|
+
|
|
|
|
|
+ // 参数验证
|
|
|
|
|
+ if (empty($bizType) || $bizId <= 0) {
|
|
|
|
|
+ return error_response('参数错误:biz_type和biz_id不能为空且biz_id必须大于0');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 获取所有启用的运费模板
|
|
|
|
|
+ $templates = ShippingTemplateModel::where('status', 1)
|
|
|
|
|
+ ->order('sort', 'asc')
|
|
|
|
|
+ ->order('id', 'asc')
|
|
|
|
|
+ ->select();
|
|
|
|
|
+
|
|
|
|
|
+ // 获取当前业务实体关联的模板ID
|
|
|
|
|
+ $relatedTemplateId = ShippingTemplateRelationModel::getTemplateIdByBiz($bizType, $bizId);
|
|
|
|
|
+
|
|
|
|
|
+ // 构造返回数据
|
|
|
|
|
+ $result = [];
|
|
|
|
|
+ foreach ($templates as $template) {
|
|
|
|
|
+ $result[] = [
|
|
|
|
|
+ 'id' => $template->id,
|
|
|
|
|
+ 'name' => $template->name,
|
|
|
|
|
+ 'calc_method' => $template->calc_method->value,
|
|
|
|
|
+ 'calc_method_text' => $template->calc_method->toString(),
|
|
|
|
|
+ 'unit' => $template->unit,
|
|
|
|
|
+ 'selected' => $relatedTemplateId == $template->id // 标识是否为当前选中模板
|
|
|
|
|
+ ];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return success_response($result);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 新增运费模板
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param Request $request
|
|
|
|
|
+ * @return \think\response\Json
|
|
|
|
|
+ */
|
|
|
|
|
+ public function save(Request $request)
|
|
|
|
|
+ {
|
|
|
|
|
+ // 获取参数
|
|
|
|
|
+ $name = $request->param('name', '');
|
|
|
|
|
+ $calcMethod = $request->param('calc_method', '');
|
|
|
|
|
+ $unit = $request->param('unit', '');
|
|
|
|
|
+ $sort = $request->param('sort', 100);
|
|
|
|
|
+ $defaultRule = $request->param('default_rule', []);
|
|
|
|
|
+ $specialAreas = $request->param('special_areas', []);
|
|
|
|
|
+
|
|
|
|
|
+ // 参数验证
|
|
|
|
|
+ if (empty($name)) {
|
|
|
|
|
+ return error_response('模板名称不能为空');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (empty($calcMethod)) {
|
|
|
|
|
+ return error_response('计费方式不能为空');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 开始事务
|
|
|
|
|
+ $templateModel = new ShippingTemplateModel();
|
|
|
|
|
+ $templateRuleModel = new ShippingTemplateRuleModel();
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ // 保存模板信息
|
|
|
|
|
+ $template = $templateModel->save([
|
|
|
|
|
+ 'name' => $name,
|
|
|
|
|
+ 'calc_method' => $calcMethod,
|
|
|
|
|
+ 'unit' => $unit,
|
|
|
|
|
+ 'sort' => $sort,
|
|
|
|
|
+ 'status' => 1
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
|
|
+ if (!$template) {
|
|
|
|
|
+ return error_response('保存模板失败');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $templateId = $templateModel->id;
|
|
|
|
|
+
|
|
|
|
|
+ // 保存默认规则
|
|
|
|
|
+ if (!empty($defaultRule)) {
|
|
|
|
|
+ $defaultRuleData = [
|
|
|
|
|
+ 'template_id' => $templateId,
|
|
|
|
|
+ 'first' => $defaultRule['first'] ?? 1,
|
|
|
|
|
+ 'first_price' => $defaultRule['first_price'] ?? 0,
|
|
|
|
|
+ 'next_price' => $defaultRule['next_price'] ?? 0,
|
|
|
|
|
+ 'area_type' => 'default',
|
|
|
|
|
+ 'area_name' => '默认区域'
|
|
|
|
|
+ ];
|
|
|
|
|
+
|
|
|
|
|
+ $rule = new ShippingTemplateRuleModel();
|
|
|
|
|
+ if (!$rule->save($defaultRuleData)) {
|
|
|
|
|
+ return error_response('保存默认规则失败');
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 保存特殊区域规则
|
|
|
|
|
+ if (!empty($specialAreas)) {
|
|
|
|
|
+ foreach ($specialAreas as $area) {
|
|
|
|
|
+ $areaData = [
|
|
|
|
|
+ 'template_id' => $templateId,
|
|
|
|
|
+ 'first' => $area['first'] ?? 1,
|
|
|
|
|
+ 'first_price' => $area['first_price'] ?? 0,
|
|
|
|
|
+ 'next_price' => $area['next_price'] ?? 0,
|
|
|
|
|
+ 'area_type' => 'special',
|
|
|
|
|
+ 'area_name' => $area['name'] ?? '',
|
|
|
|
|
+ 'regions' => !empty($area['regions']) ? json_encode($area['regions']) : null
|
|
|
|
|
+ ];
|
|
|
|
|
+
|
|
|
|
|
+ $rule = new ShippingTemplateRuleModel();
|
|
|
|
|
+ if (!$rule->save($areaData)) {
|
|
|
|
|
+ return error_response('保存特殊区域规则失败');
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return success_response([
|
|
|
|
|
+ 'id' => $templateId
|
|
|
|
|
+ ], '模板创建成功');
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 更新运费模板
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param Request $request
|
|
|
|
|
+ * @return \think\response\Json
|
|
|
|
|
+ */
|
|
|
|
|
+ public function update(Request $request)
|
|
|
|
|
+ {
|
|
|
|
|
+ // 获取参数
|
|
|
|
|
+ $id = $request->param('id', 0);
|
|
|
|
|
+ $name = $request->param('name', '');
|
|
|
|
|
+ $calcMethod = $request->param('calc_method', '');
|
|
|
|
|
+ $unit = $request->param('unit', '');
|
|
|
|
|
+ $sort = $request->param('sort', 100);
|
|
|
|
|
+ $defaultRule = $request->param('default_rule', []);
|
|
|
|
|
+ $specialAreas = $request->param('special_areas', []);
|
|
|
|
|
+
|
|
|
|
|
+ // 参数验证
|
|
|
|
|
+ if ($id <= 0) {
|
|
|
|
|
+ return error_response('模板ID不能为空');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (empty($name)) {
|
|
|
|
|
+ return error_response('模板名称不能为空');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (empty($calcMethod)) {
|
|
|
|
|
+ return error_response('计费方式不能为空');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 查找模板
|
|
|
|
|
+ $templateModel = ShippingTemplateModel::find($id);
|
|
|
|
|
+ if (!$templateModel) {
|
|
|
|
|
+ return error_response('指定的模板不存在');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ // 更新模板信息
|
|
|
|
|
+ $templateModel->save([
|
|
|
|
|
+ 'name' => $name,
|
|
|
|
|
+ 'calc_method' => $calcMethod,
|
|
|
|
|
+ 'unit' => $unit,
|
|
|
|
|
+ 'sort' => $sort
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
|
|
+ // 删除原有的规则
|
|
|
|
|
+ ShippingTemplateRuleModel::where('template_id', $id)->delete();
|
|
|
|
|
+
|
|
|
|
|
+ // 保存默认规则
|
|
|
|
|
+ if (!empty($defaultRule)) {
|
|
|
|
|
+ $defaultRuleData = [
|
|
|
|
|
+ 'template_id' => $id,
|
|
|
|
|
+ 'first' => $defaultRule['first'] ?? 1,
|
|
|
|
|
+ 'first_price' => $defaultRule['first_price'] ?? 0,
|
|
|
|
|
+ 'next_price' => $defaultRule['next_price'] ?? 0,
|
|
|
|
|
+ 'area_type' => 'default',
|
|
|
|
|
+ 'area_name' => '默认区域'
|
|
|
|
|
+ ];
|
|
|
|
|
+
|
|
|
|
|
+ $rule = new ShippingTemplateRuleModel();
|
|
|
|
|
+ if (!$rule->save($defaultRuleData)) {
|
|
|
|
|
+ return error_response('保存默认规则失败');
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 保存特殊区域规则
|
|
|
|
|
+ if (!empty($specialAreas)) {
|
|
|
|
|
+ foreach ($specialAreas as $area) {
|
|
|
|
|
+ $areaData = [
|
|
|
|
|
+ 'template_id' => $id,
|
|
|
|
|
+ 'first' => $area['first'] ?? 1,
|
|
|
|
|
+ 'first_price' => $area['first_price'] ?? 0,
|
|
|
|
|
+ 'next_price' => $area['next_price'] ?? 0,
|
|
|
|
|
+ 'area_type' => 'special',
|
|
|
|
|
+ 'area_name' => $area['name'] ?? '',
|
|
|
|
|
+ 'regions' => !empty($area['regions']) ? json_encode($area['regions']) : null
|
|
|
|
|
+ ];
|
|
|
|
|
+
|
|
|
|
|
+ $rule = new ShippingTemplateRuleModel();
|
|
|
|
|
+ if (!$rule->save($areaData)) {
|
|
|
|
|
+ return error_response('保存特殊区域规则失败');
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return success_response([
|
|
|
|
|
+ 'id' => $id
|
|
|
|
|
+ ], '模板更新成功');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 读取运费模板详情
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param Request $request
|
|
|
|
|
+ * @return \think\response\Json
|
|
|
|
|
+ */
|
|
|
|
|
+ public function read(int $id)
|
|
|
|
|
+ {
|
|
|
|
|
+ // 查找模板
|
|
|
|
|
+ $templateModel = ShippingTemplateModel::with('template_rule_list')->find($id);
|
|
|
|
|
+ if (!$templateModel) {
|
|
|
|
|
+ return error_response('指定的模板不存在');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 查找规则
|
|
|
|
|
+ $rules = $templateModel->template_rule_list;
|
|
|
|
|
+
|
|
|
|
|
+ return success_response([
|
|
|
|
|
+ 'id' => $id,
|
|
|
|
|
+ 'name' => $templateModel->name,
|
|
|
|
|
+ 'calc_method' => $templateModel->calc_method,
|
|
|
|
|
+ 'unit' => $templateModel->unit,
|
|
|
|
|
+ 'sort' => $templateModel->sort,
|
|
|
|
|
+ 'default_rule' => $rules->filter(function($rule) {
|
|
|
|
|
+ return $rule->area_type === ShippingTemplateAreaTypeEnum::DEFAULT;
|
|
|
|
|
+ })->first(),
|
|
|
|
|
+ 'special_areas' => $rules->filter(function($rule) {
|
|
|
|
|
+ return $rule->area_type === ShippingTemplateAreaTypeEnum::SPECIAL;
|
|
|
|
|
+ })->values()
|
|
|
|
|
+ ], '模板详情读取成功');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 删除运费模板
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param Request $request
|
|
|
|
|
+ * @return \think\response\Json
|
|
|
|
|
+ */
|
|
|
|
|
+ public function delete(int $id)
|
|
|
|
|
+ {
|
|
|
|
|
+ // 删除模板
|
|
|
|
|
+ $templateModel = ShippingTemplateModel::with('template_rule_list')->find($id);
|
|
|
|
|
+ if (!$templateModel) {
|
|
|
|
|
+ return error_response('指定的模板不存在');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 删除规则
|
|
|
|
|
+ $templateModel->template_rule_list()->delete();
|
|
|
|
|
+
|
|
|
|
|
+ // 删除模板
|
|
|
|
|
+ $templateModel->delete();
|
|
|
|
|
+
|
|
|
|
|
+ return success_response([], '模板删除成功');
|
|
|
|
|
+ }
|
|
|
|
|
+}
|