Delivery.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace app\admin\model\unishop;
  3. use think\Model;
  4. use think\Db;
  5. use think\Exception;
  6. use think\Request;
  7. class Delivery extends Model
  8. {
  9. //数据库
  10. protected $connection = 'database';
  11. // 表名
  12. protected $name = 'unishop_delivery';
  13. // 自动写入时间戳字段
  14. protected $autoWriteTimestamp = 'int';
  15. // 定义时间戳字段名
  16. protected $createTime = 'createtime';
  17. protected $updateTime = 'updatetime';
  18. protected $deleteTime = false;
  19. // 是否上架
  20. const SWITCH_YES = 1;
  21. const SWITCH_NO = 0;
  22. // 追加属性
  23. protected $append = [
  24. 'type_text'
  25. ];
  26. public function getTypeList()
  27. {
  28. return ['quantity' => __('Quantity'), 'weight' => __('Weight')];
  29. }
  30. public function getTypeTextAttr($value, $data)
  31. {
  32. $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
  33. $list = $this->getTypeList();
  34. return isset($list[$value]) ? $list[$value] : '';
  35. }
  36. /**
  37. * 关联配送模板区域及运费
  38. * @return \think\model\relation\HasMany
  39. */
  40. public function rule()
  41. {
  42. return $this->hasMany('DeliveryRule','delivery_id','id');
  43. }
  44. /**
  45. * 计费方式
  46. * @param $value
  47. * @return array
  48. */
  49. public function getMethodAttr($value)
  50. {
  51. $method = ['quantity' => '按件数', 'weight' => '按重量'];
  52. return ['text' => $method[$value], 'value' => $value];
  53. }
  54. /**
  55. * 获取全部
  56. * @return unied
  57. */
  58. public static function getAll()
  59. {
  60. $model = new static;
  61. return $model->select();
  62. }
  63. /**
  64. * 获取列表
  65. * @return \think\Paginator
  66. * @throws \think\exception\DbException
  67. */
  68. public function getList()
  69. {
  70. return $this->with(['rule'])
  71. ->order(['sort' => 'asc'])
  72. ->paginate(15, false, [
  73. 'query' => Request::instance()->request()
  74. ]);
  75. }
  76. /**
  77. * 运费模板详情
  78. * @param $delivery_id
  79. * @return null|static
  80. * @throws \think\exception\DbException
  81. */
  82. public static function detail($delivery_id)
  83. {
  84. return self::get($delivery_id, ['rule']);
  85. }
  86. /**
  87. * 保存运费模板
  88. * @param array $params
  89. * @return false|int|void
  90. */
  91. public static function saveDelivery($params){
  92. Db::startTrans();
  93. try{
  94. $result = self::create($params,true);
  95. $data['delivery_id'] = $result['id'];
  96. foreach ($params['first'] as $k => $v){
  97. $data['area'] = $params['area'][$k];
  98. $data['first'] = $params['first'][$k];
  99. $data['first_fee'] = $params['first_fee'][$k];
  100. $data['additional'] = $params['additional'][$k];
  101. $data['additional_fee'] = $params['additional_fee'][$k];
  102. DeliveryRule::create($data,true);
  103. }
  104. }catch (Exception $e){
  105. Db::rollback();
  106. return false;
  107. }
  108. Db::commit();
  109. return true;
  110. }
  111. /**
  112. * 修改运费模板
  113. * @param $params
  114. * @param $ids
  115. * @return bool
  116. */
  117. public static function editDelivery($params,$ids){
  118. Db::startTrans();
  119. try{
  120. $model = new static;
  121. $model->update($params,['id'=>$ids],true);
  122. DeliveryRule::where('delivery_id',$ids)->delete();
  123. $data['delivery_id'] = $ids;
  124. foreach ($params['first'] as $k => $v){
  125. $data['area'] = $params['area'][$k];
  126. $data['first'] = $params['first'][$k];
  127. $data['first_fee'] = $params['first_fee'][$k];
  128. $data['additional'] = $params['additional'][$k];
  129. $data['additional_fee'] = $params['additional_fee'][$k];
  130. DeliveryRule::create($data,true);
  131. }
  132. }catch (Exception $e){
  133. Db::rollback();
  134. return false;
  135. }
  136. Db::commit();
  137. return true;
  138. }
  139. }