ShippingTemplatesFree.php 4.0 KB

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