PsMethodClass.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * 用途:花店配送方式配置业务逻辑类
  4. * 谁用:花店系统 hdApp 配送方式设置页
  5. * 解决什么问题:读写 xhPsMethod / xhPsExplain / xhPsReduceRule,与供货商 xhSh* 分离
  6. */
  7. namespace bizHd\order\classes;
  8. use bizHd\base\classes\BaseClass;
  9. use common\components\dict;
  10. use Yii;
  11. class PsMethodClass extends BaseClass
  12. {
  13. public static $baseFile = '\bizHd\order\models\PsMethod';
  14. /**
  15. * 获取所有配送方式的排序和别名信息
  16. * @param int $mainId 商户ID
  17. * @return array
  18. */
  19. public static function getSorts($mainId)
  20. {
  21. $list = [];
  22. $aliases = [
  23. 0 => '送货',
  24. 1 => '自取',
  25. 2 => '跑腿',
  26. 3 => '物流',
  27. 4 => '快递'
  28. ];
  29. for ($style = 0; $style <= 4; $style++) {
  30. $config = self::getByCondition(['mainId' => $mainId, 'style' => $style]);
  31. $sort = 0;
  32. $alias = $aliases[$style];
  33. $status = 1;
  34. if (!empty($config)) {
  35. $sort = isset($config['sort']) ? (int)$config['sort'] : 0;
  36. $alias = !empty($config['name']) ? $config['name'] : $aliases[$style];
  37. $status = isset($config['status']) ? (int)$config['status'] : 1;
  38. }
  39. $list[] = [
  40. 'style' => $style,
  41. 'name' => $alias,
  42. 'sort' => $sort,
  43. 'status' => $status
  44. ];
  45. }
  46. return $list;
  47. }
  48. /**
  49. * 获取指定配送方式的配置(不存在则按字典默认初始化)
  50. * @param int $shopId 门店ID
  51. * @param int $mainId 商户ID
  52. * @param int $style 配送类型 0-4
  53. * @return array
  54. */
  55. public static function getConfig($shopId, $mainId, $style)
  56. {
  57. // 复杂分支/关键逻辑:按 mainId + style 查 xhPsMethod,无记录则写入默认配置
  58. $config = self::getByCondition(['mainId' => $mainId, 'style' => $style]);
  59. if (empty($config)) {
  60. $defaultConfig = dict::getDict('shMethod', $style);
  61. $initData = array_merge([
  62. 'shopId' => $shopId,
  63. 'mainId' => $mainId,
  64. 'style' => $style,
  65. ], $defaultConfig);
  66. $config = self::add($initData, true);
  67. $config = $config->toArray();
  68. }
  69. // 关联说明项 xhPsExplain
  70. $config['explains'] = PsExplainClass::getAllByCondition(
  71. ['methodId' => $config['id']],
  72. 'sort ASC, id ASC'
  73. );
  74. // 跑腿 style=2 时加载满减规则 xhPsReduceRule
  75. if ($style == 2) {
  76. $config['reduceRules'] = PsReduceRuleClass::getAllByCondition(
  77. ['methodId' => $config['id']],
  78. 'sort ASC, id ASC'
  79. );
  80. } else {
  81. $config['reduceRules'] = [];
  82. }
  83. return $config;
  84. }
  85. /**
  86. * 保存指定配送方式配置及关联说明项、满减规则
  87. * @param int $shopId 门店ID
  88. * @param int $mainId 商户ID
  89. * @param array $data 配置数据
  90. * @return bool
  91. * @throws \Exception
  92. */
  93. public static function saveConfig($shopId, $mainId, $data)
  94. {
  95. $id = $data['id'] ?? 0;
  96. $style = $data['style'] ?? 0;
  97. $config = null;
  98. if ($id > 0) {
  99. $config = self::getByCondition(['id' => $id, 'mainId' => $mainId], true);
  100. }
  101. if (empty($config)) {
  102. $config = self::getByCondition(['mainId' => $mainId, 'style' => $style], true);
  103. }
  104. $saveData = [
  105. 'name' => $data['name'] ?? '',
  106. 'status' => isset($data['status']) ? (int)$data['status'] : 1,
  107. 'sort' => isset($data['sort']) ? (int)$data['sort'] : 0,
  108. 'minAmount' => isset($data['minAmount']) ? (float)$data['minAmount'] : 0.00,
  109. 'minNum' => isset($data['minNum']) ? (int)$data['minNum'] : 0,
  110. 'unMeet' => isset($data['unMeet']) ? (int)$data['unMeet'] : 0,
  111. 'unMeetFee' => isset($data['unMeetFee']) ? (float)$data['unMeetFee'] : 0.00,
  112. 'startKm' => isset($data['startKm']) ? (float)$data['startKm'] : 0.00,
  113. 'startPrice' => isset($data['startPrice']) ? (float)$data['startPrice'] : 0.00,
  114. 'perKmPrice' => isset($data['perKmPrice']) ? (float)$data['perKmPrice'] : 0.00,
  115. 'changeRate' => isset($data['changeRate']) ? (float)$data['changeRate'] : 0.00,
  116. ];
  117. $transaction = Yii::$app->db->beginTransaction();
  118. try {
  119. if (empty($config)) {
  120. $saveData['shopId'] = $shopId;
  121. $saveData['mainId'] = $mainId;
  122. $saveData['style'] = $style;
  123. $defaultConfig = dict::getDict('shMethod', $style);
  124. if (!empty($defaultConfig['originName'])) {
  125. $saveData['originName'] = $defaultConfig['originName'];
  126. }
  127. $config = self::add($saveData, true);
  128. } else {
  129. self::updateByCondition(['id' => $config->id], $saveData);
  130. }
  131. $methodId = $config->id;
  132. // 1. 保存说明项 xhPsExplain
  133. PsExplainClass::deleteByCondition(['methodId' => $methodId]);
  134. if (!empty($data['explains']) && is_array($data['explains'])) {
  135. $explainRows = [];
  136. foreach ($data['explains'] as $index => $exp) {
  137. if (empty($exp['explain'])) {
  138. continue;
  139. }
  140. $explainRows[] = [
  141. 'methodId' => $methodId,
  142. 'explain' => $exp['explain'],
  143. 'color' => isset($exp['color']) ? (int)$exp['color'] : 1,
  144. 'fontWeight' => isset($exp['fontWeight']) ? (int)$exp['fontWeight'] : 1,
  145. 'sort' => isset($exp['sort']) ? (int)$exp['sort'] : $index,
  146. ];
  147. }
  148. if (!empty($explainRows)) {
  149. PsExplainClass::batchAdd($explainRows);
  150. }
  151. }
  152. // 2. 保存跑腿满减规则 xhPsReduceRule
  153. PsReduceRuleClass::deleteByCondition(['methodId' => $methodId]);
  154. if ($style == 2 && !empty($data['reduceRules']) && is_array($data['reduceRules'])) {
  155. $ruleRows = [];
  156. foreach ($data['reduceRules'] as $index => $rule) {
  157. $ruleRows[] = [
  158. 'methodId' => $methodId,
  159. 'meetNum' => isset($rule['meetNum']) ? (int)$rule['meetNum'] : 0,
  160. 'meetAmount' => isset($rule['meetAmount']) ? (float)$rule['meetAmount'] : 0.00,
  161. 'freeKm' => isset($rule['freeKm']) ? (float)$rule['freeKm'] : 0.00,
  162. 'sort' => isset($rule['sort']) ? (int)$rule['sort'] : $index,
  163. ];
  164. }
  165. if (!empty($ruleRows)) {
  166. PsReduceRuleClass::batchAdd($ruleRows);
  167. }
  168. }
  169. $transaction->commit();
  170. return true;
  171. } catch (\Exception $e) {
  172. $transaction->rollBack();
  173. throw $e;
  174. }
  175. }
  176. }