ShippingTemplatesRegion.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace app\models\system;
  3. use crmeb\traits\ModelTrait;
  4. use crmeb\basic\BaseModel;
  5. /**
  6. * Class ShippingTemplatesRegion
  7. * @package app\models\system
  8. */
  9. class ShippingTemplatesRegion extends BaseModel
  10. {
  11. /**
  12. * 数据表主键
  13. * @var string
  14. */
  15. protected $pk = 'id';
  16. /**
  17. * 模型名称
  18. * @var string
  19. */
  20. protected $name = 'shipping_templates_region';
  21. use ModelTrait;
  22. /**
  23. * 添加运费信息
  24. * @param array $regionInfo
  25. * @param int $type
  26. * @param int $tempId
  27. * @return bool
  28. * @throws \Exception
  29. */
  30. public static function saveRegion(array $regionInfo, int $type = 0, $tempId = 0)
  31. {
  32. $res = true;
  33. if ($tempId) {
  34. if (self::where('temp_id', $tempId)->count()) {
  35. $res = self::where('temp_id', $tempId)->delete();
  36. }
  37. }
  38. $regionList = [];
  39. foreach ($regionInfo as $item) {
  40. if (isset($item['region']) && is_array($item['region'])) {
  41. $uniqid = uniqid(true);
  42. foreach ($item['region'] as $value) {
  43. if (isset($value['children']) && is_array($value['children'])) {
  44. foreach ($value['children'] as $vv) {
  45. if (!isset($vv['city_id'])) {
  46. return self::setErrorInfo('缺少城市id无法保存');
  47. }
  48. $regionList[] = [
  49. 'temp_id' => $tempId,
  50. 'province_id' => $value['city_id'] ?? 0,
  51. 'city_id' => $vv['city_id'] ?? 0,
  52. 'first' => $item['first'] ?? 0,
  53. 'first_price' => $item['price'] ?? 0,
  54. 'continue' => $item['continue'] ?? 0,
  55. 'continue_price' => $item['continue_price'] ?? 0,
  56. 'type' => $type,
  57. 'uniqid' => $uniqid,
  58. ];
  59. }
  60. } else {
  61. $regionList[] = [
  62. 'temp_id' => $tempId,
  63. 'province_id' => 0,
  64. 'city_id' => 0,
  65. 'first' => $item['first'] ?? 0,
  66. 'first_price' => $item['price'] ?? 0,
  67. 'continue' => $item['continue'] ?? 0,
  68. 'continue_price' => $item['continue_price'] ?? 0,
  69. 'type' => $type,
  70. 'uniqid' => $uniqid,
  71. ];
  72. }
  73. }
  74. }
  75. }
  76. return $res && self::insertAll($regionList);
  77. }
  78. public static function getRegionList(int $tempId)
  79. {
  80. $regionList = self::where('temp_id', $tempId)->group('uniqid')->column('uniqid');
  81. $regionData = [];
  82. foreach ($regionList as $uniqid) {
  83. $info = self::where(['uniqid' => $uniqid, 'temp_id' => $tempId])->find();
  84. if ($info['province_id'] == 0) {
  85. $regionData[] = [
  86. 'region' => [
  87. 'city_id' => 0,
  88. 'name' => '默认全国',
  89. ],
  90. 'regionName' => '默认全国',
  91. 'first' => $info['first'],
  92. 'price' => $info['first_price'],
  93. 'continue' => $info['continue'],
  94. 'continue_price' => $info['continue_price'],
  95. 'uniqid' => $info['uniqid'],
  96. ];
  97. } else {
  98. $regionData[] = [
  99. 'region' => self::getRegionTemp($uniqid, $info['province_id']),
  100. 'regionName' => '',
  101. 'first' => $info['first'],
  102. 'price' => $info['first_price'],
  103. 'continue' => $info['continue'],
  104. 'continue_price' => $info['continue_price'],
  105. 'uniqid' => $info['uniqid'],
  106. ];
  107. }
  108. }
  109. foreach ($regionData as &$item) {
  110. if (!$item['regionName']) {
  111. $item['regionName'] = array_map(function ($val) {
  112. return $val['name'];
  113. }, $item['region']);
  114. }
  115. }
  116. return $regionData;
  117. }
  118. public static function getRegionTemp(string $uniqid, int $provinceId)
  119. {
  120. $infoList = self::where(['a.uniqid' => $uniqid])->group('a.province_id')->field('a.province_id,c.name')->alias('a')->join('system_city c', 'a.province_id = c.city_id', 'left')->select();
  121. $infoList = count($infoList) ? $infoList->toArray() : [];
  122. $childrenData = [];
  123. foreach ($infoList as $item) {
  124. $childrenData[] = [
  125. 'city_id' => $item['province_id'],
  126. 'name' => $item['name'] ?? '全国',
  127. 'children' => self::getCityTemp($uniqid, $item['province_id'])
  128. ];
  129. }
  130. return $childrenData;
  131. }
  132. public static function getCityTemp(string $uniqid, int $provinceId)
  133. {
  134. $infoList = self::where(['a.uniqid' => $uniqid, 'province_id' => $provinceId])->field('a.city_id,c.name')->alias('a')->join('system_city c', 'a.city_id = c.city_id', 'left')->select();
  135. $childrenData = [];
  136. foreach ($infoList as $item) {
  137. $childrenData[] = [
  138. 'city_id' => $item['city_id'],
  139. 'name' => $item['name'] ?? '全国',
  140. ];
  141. }
  142. return $childrenData;
  143. }
  144. }