ShippingTemplatesFree.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace app\admin\model\system;
  3. use crmeb\traits\ModelTrait;
  4. use crmeb\basic\BaseModel;
  5. use lib\help\Arr;
  6. /**
  7. * 菜单 model
  8. * Class SystemMenus
  9. * @package app\admin\model\system
  10. */
  11. class ShippingTemplatesFree extends BaseModel
  12. {
  13. /**
  14. * 数据表主键
  15. * @var string
  16. */
  17. protected $pk = 'id';
  18. /**
  19. * 模型名称
  20. * @var string
  21. */
  22. protected $name = 'shipping_templates_free';
  23. use ModelTrait;
  24. /**
  25. * 添加包邮信息
  26. * @param array $appointInfo
  27. * @param int $type
  28. * @param int $tempId
  29. * @return bool
  30. * @throws \Exception
  31. */
  32. public static function saveFree(array $appointInfo, int $type = 0, int $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. $placeList = [];
  41. foreach ($appointInfo as $item) {
  42. if (isset($item['place']) && is_array($item['place'])) {
  43. $uniqid = uniqid(true) . rand(1000, 9999);
  44. foreach ($item['place'] 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. $placeList [] = [
  51. 'temp_id' => $tempId,
  52. 'province_id' => $value['city_id'] ?? 0,
  53. 'city_id' => $vv['city_id'] ?? 0,
  54. 'number' => $item['a_num'] ?? 0,
  55. 'price' => $item['a_price'] ?? 0,
  56. 'type' => $type,
  57. 'uniqid' => $uniqid,
  58. ];
  59. }
  60. }
  61. }
  62. }
  63. }
  64. if (count($placeList)) {
  65. return $res && self::insertAll($placeList);
  66. } else {
  67. return $res;
  68. }
  69. }
  70. public static function getFreeList(int $tempId)
  71. {
  72. $freeIdList = self::where('temp_id', $tempId)->group('uniqid')->column('uniqid');
  73. $freeData = [];
  74. foreach ($freeIdList as $uniqid) {
  75. $info = self::where(['uniqid' => $uniqid, 'temp_id' => $tempId])->find();
  76. $freeData[] = [
  77. 'place' => self::getFreeTemp($uniqid, $info['province_id']),
  78. 'a_num' => $info['number'],
  79. 'a_price' => $info['price'],
  80. ];
  81. }
  82. foreach ($freeData as &$item) {
  83. $item['placeName'] = array_map(function ($val) {
  84. return $val['name'];
  85. }, $item['place']);
  86. }
  87. return $freeData;
  88. }
  89. public static function getFreeTemp(string $uniqid, int $provinceId)
  90. {
  91. $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();
  92. $infoList = count($infoList) ? $infoList->toArray() : [];
  93. $childrenData = [];
  94. foreach ($infoList as $item) {
  95. $childrenData[] = [
  96. 'city_id' => $item['province_id'],
  97. 'name' => $item['name'] ?? '全国',
  98. 'children' => self::getCityTemp($uniqid, $provinceId)
  99. ];
  100. }
  101. return $childrenData;
  102. }
  103. public static function getCityTemp(string $uniqid, int $provinceId)
  104. {
  105. $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();
  106. $childrenData = [];
  107. foreach ($infoList as $item) {
  108. $childrenData[] = [
  109. 'city_id' => $item['city_id'],
  110. 'name' => $item['name'] ?? '全国',
  111. ];
  112. }
  113. return $childrenData;
  114. }
  115. }