You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

78 lines
2.5 KiB

  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. $data = $this->alias('DR')
  31. ->join(DeliveryModel::TABLE_NAME . ' D', 'DR.delivery_id = D.id')
  32. //->where("D.switch = " . DeliveryModel::SWITCH_YES . " AND find_in_set($cityId,DR.area)")
  33. ->where("D.switch = " . DeliveryModel::SWITCH_YES)
  34. ->field('D.id,D.name,D.type,D.min,DR.first,DR.first_fee,DR.additional,DR.additional_fee,DR.area')
  35. ->order(['min' => SORT_ASC])
  36. ->cache(10)
  37. ->select();
  38. if ($data) {
  39. $data = collection($data)->toArray();
  40. foreach ($data as &$delivery) {
  41. if (!in_array($cityId, explode(',', $delivery['area']))) {
  42. $delivery['name'] = $delivery['name']. '(收货地址不在配送范围)';
  43. }
  44. unset($delivery['area']);
  45. }
  46. }
  47. $status = $data ? 1 : 0;
  48. $data = $data ? $data : [['name'=>'收货地址不在配送范围']];
  49. return [
  50. 'list' => $data,
  51. 'status' => $status
  52. ];
  53. }
  54. /**
  55. * 是否在配送范围内
  56. * @param int $cityId 城市id
  57. * @param int $deliveryId 配送方式id
  58. * @return int|string
  59. * @throws \think\Exception
  60. */
  61. public function cityInScopeOfDelivery($cityId, $deliveryId) {
  62. return $this->alias('DR')
  63. ->join(DeliveryModel::TABLE_NAME . ' D', 'DR.delivery_id = D.id')
  64. ->where("D.id = $deliveryId AND find_in_set($cityId,DR.area) AND D.switch = " . DeliveryModel::SWITCH_YES)
  65. ->field('D.id,D.name,D.type,D.min,DR.first,DR.first_fee,DR.additional,DR.additional_fee')
  66. ->find();
  67. }
  68. }