ShippingTemplatesRegion.php 5.6 KB

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