DeliveryRule.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zhengmingwei
  5. * Date: 2019-07-14
  6. * Time: 22:45
  7. */
  8. namespace addons\unishop\model;
  9. use addons\unishop\model\Delivery as DeliveryModel;
  10. use think\Model;
  11. class DeliveryRule extends Model
  12. {
  13. // 表名
  14. protected $name = 'unishop_delivery_rule';
  15. // 自动写入时间戳字段
  16. protected $autoWriteTimestamp = 'int';
  17. // 定义时间戳字段名
  18. protected $createTime = 'createtime';
  19. protected $updateTime = 'updatetime';
  20. /**
  21. * 获取地区的运费配送列表
  22. * @param $cityId
  23. * @return array [list:运费模板, status:1=有,0=没]
  24. * @throws \think\db\exception\DataNotFoundException
  25. * @throws \think\db\exception\ModelNotFoundException
  26. * @throws \think\exception\DbException
  27. */
  28. public function getDelivetyByArea($cityId)
  29. {
  30. $prefix = \think\Config::get('database.prefix');
  31. $data = $this->alias('DR')
  32. ->join($prefix.DeliveryModel::TABLE_NAME . ' D', 'DR.delivery_id = D.id')
  33. //->where("D.switch = " . DeliveryModel::SWITCH_YES . " AND find_in_set($cityId,DR.area)")
  34. ->where("D.switch = " . DeliveryModel::SWITCH_YES)
  35. ->field('D.id,D.name,D.type,D.min,DR.first,DR.first_fee,DR.additional,DR.additional_fee,DR.area')
  36. ->order(['min' => SORT_ASC])
  37. ->cache(10)
  38. ->select();
  39. if ($data) {
  40. $data = collection($data)->toArray();
  41. foreach ($data as &$delivery) {
  42. if (!in_array($cityId, explode(',', $delivery['area']))) {
  43. $delivery['name'] = $delivery['name']. '(收货地址不在配送范围)';
  44. }
  45. unset($delivery['area']);
  46. }
  47. }
  48. $status = $data ? 1 : 0;
  49. $data = $data ? $data : [['name'=>'收货地址不在配送范围']];
  50. return [
  51. 'list' => $data,
  52. 'status' => $status
  53. ];
  54. }
  55. /**
  56. * 是否在配送范围内
  57. * @param int $cityId 城市id
  58. * @param int $deliveryId 配送方式id
  59. * @return int|string
  60. * @throws \think\Exception
  61. */
  62. public function cityInScopeOfDelivery($cityId, $deliveryId) {
  63. $prefix = \think\Config::get('database.prefix');
  64. return $this->alias('DR')
  65. ->join($prefix.DeliveryModel::TABLE_NAME . ' D', 'DR.delivery_id = D.id')
  66. ->where("D.id = $deliveryId AND find_in_set($cityId,DR.area) AND D.switch = " . DeliveryModel::SWITCH_YES)
  67. ->field('D.id,D.name,D.type,D.min,DR.first,DR.first_fee,DR.additional,DR.additional_fee')
  68. ->find();
  69. }
  70. }