ShippingTemplatesFree.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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);
  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. //self::insert($v);
  60. }
  61. }
  62. }
  63. }
  64. }
  65. if (count($placeList)) {
  66. return $res && self::insertAll($placeList);
  67. } else {
  68. return $res;
  69. }
  70. }
  71. public static function getFreeList(int $tempId)
  72. {
  73. $freeIdList = self::where('temp_id', $tempId)->group('uniqid')->column('uniqid');
  74. $freeData = [];
  75. foreach ($freeIdList as $uniqid) {
  76. $info = self::where(['uniqid' => $uniqid, 'temp_id' => $tempId])->find();
  77. $freeData[] = [
  78. 'place' => self::getFreeTemp($uniqid, $info['province_id']),
  79. 'a_num' => $info['number'],
  80. 'a_price' => $info['price'],
  81. ];
  82. }
  83. foreach ($freeData as &$item) {
  84. $item['placeName'] = array_map(function ($val) {
  85. return $val['name'];
  86. }, $item['place']);
  87. }
  88. return $freeData;
  89. }
  90. public static function getFreeTemp(string $uniqid, int $provinceId)
  91. {
  92. $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();
  93. $infoList = count($infoList) ? $infoList->toArray() : [];
  94. $childrenData = [];
  95. foreach ($infoList as $item) {
  96. $childrenData[] = [
  97. 'city_id' => $item['province_id'],
  98. 'name' => $item['name'] ?? '全国',
  99. 'children' => self::getCityTemp($uniqid, $item['province_id'])
  100. ];
  101. }
  102. return $childrenData;
  103. }
  104. public static function getCityTemp(string $uniqid, int $provinceId)
  105. {
  106. $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();
  107. $childrenData = [];
  108. foreach ($infoList as $item) {
  109. $childrenData[] = [
  110. 'city_id' => $item['city_id'],
  111. 'name' => $item['name'] ?? '全国',
  112. ];
  113. }
  114. return $childrenData;
  115. }
  116. }