Transport.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. namespace app\common\model;
  3. use think\facade\Db;
  4. /**
  5. * ============================================================================
  6. *
  7. * ============================================================================
  8. * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司,并保留所有权利。
  9. * 网站地址: https://www.valimart.net/
  10. * ----------------------------------------------------------------------------
  11. *
  12. * ============================================================================
  13. * 数据层模型
  14. */
  15. class Transport extends BaseModel {
  16. public $page_info;
  17. /**
  18. * 增加售卖区域
  19. * @access public
  20. * @author csdeshang
  21. * @param type $data 参数内容
  22. * @return type
  23. */
  24. public function addTransport($data) {
  25. return Db::name('transport')->insertGetId($data);
  26. }
  27. /**
  28. * 增加各地区详细运费设置
  29. * @access public
  30. * @author csdeshang
  31. * @param array $data
  32. * @return bool
  33. */
  34. public function addExtend($data) {
  35. return Db::name('transportextend')->insertAll($data);
  36. }
  37. /**
  38. * 取得一条售卖区域信息
  39. * @access public
  40. * @author csdeshang
  41. * @param array $condition 条件
  42. * @return array
  43. */
  44. public function getTransportInfo($condition) {
  45. return Db::name('transport')->where($condition)->find();
  46. }
  47. /**
  48. * 取得一条售卖区域扩展信息
  49. * @access public
  50. * @author csdeshang
  51. * @param array $condition 条件
  52. * @return array
  53. */
  54. public function getExtendInfo($condition) {
  55. return Db::name('transportextend')->where($condition)->order('transportext_is_default desc')->select()->toArray();
  56. }
  57. /**
  58. * 删除售卖区域
  59. * @access public
  60. * @author csdeshang
  61. * @param int $transport_id 条件
  62. * @return boolean
  63. */
  64. public function delTansport($transport_id) {
  65. try {
  66. Db::startTrans();
  67. $delete = Db::name('transport')->where('transport_id',$transport_id)->delete();
  68. if ($delete) {
  69. $delete = Db::name('transportextend')->where('transport_id',$transport_id)->delete();
  70. }
  71. Db::commit();
  72. } catch (Exception $e) {
  73. Db::rollback();
  74. return false;
  75. }
  76. return true;
  77. }
  78. /**
  79. * 删除售卖区域扩展信息
  80. * @access public
  81. * @author csdeshang
  82. * @param int $transport_id 售卖区域ID
  83. * @return bool
  84. */
  85. public function delTransportextend($transport_id) {
  86. return Db::name('transportextend')->where(array('transport_id' => $transport_id))->delete();
  87. }
  88. /**
  89. * 取得售卖区域列表
  90. * @access public
  91. * @author csdeshang
  92. * @param array $condition 查询条件
  93. * @param int $pagesize 分页信息
  94. * @param string $order 排序
  95. * @return array
  96. */
  97. public function getTransportList($condition = array(), $pagesize = '', $order = 'transport_id desc') {
  98. if($pagesize){
  99. $res = Db::name('transport')->where($condition)->order($order)->paginate(['list_rows'=>$pagesize,'query' => request()->param()],false);
  100. $this->page_info=$res;
  101. return $res->items();
  102. }else{
  103. return Db::name('transport')->where($condition)->order($order)->select()->toArray();
  104. }
  105. }
  106. /**
  107. * 取得扩展信息列表
  108. * @access public
  109. * @author csdeshang
  110. * @param array $condition 条件
  111. * @param string $order 排序
  112. * @return array
  113. */
  114. public function getTransportextendList($condition = array(), $order = 'transportext_id asc') {
  115. return Db::name('transportextend')->where($condition)->order($order)->select()->toArray();
  116. }
  117. /**
  118. * 编辑更新售卖区域
  119. * @access public
  120. * @author csdeshang
  121. * @param type $data 数据
  122. * @param type $condition 条件
  123. * @return type
  124. */
  125. public function editTransport($data, $condition = array()) {
  126. return Db::name('transport')->where($condition)->update($data);
  127. }
  128. /**
  129. * 检测售卖区域是否正在被使用
  130. * @access public
  131. * @author csdeshang
  132. * @param type $id ID编号
  133. * @return boolean
  134. */
  135. public function isTransportUsing($id) {
  136. if (!is_numeric($id)) {
  137. return false;
  138. }
  139. $goods_info = Db::name('goods')->where(array('transport_id' => $id))->field('goods_id')->find();
  140. return $goods_info ? true : false;
  141. }
  142. /**
  143. * 计算某地区某售卖区域ID下的商品总运费,如果售卖区域不存在或,按免运费处理
  144. * @access public
  145. * @author csdeshang
  146. * @param int $transport_id 售卖区域ID
  147. * @param int $area_id 区域ID
  148. * @param int $count 计数
  149. * @return number/boolean
  150. */
  151. public function calcTransport($transport_id, $area_id,$count=1,$weight=0) {
  152. if (empty($transport_id)) {
  153. return 0;
  154. }
  155. $transport = $this->getTransportInfo(array('transport_id' => $transport_id));
  156. $extend_list = $this->getTransportextendList(array('transport_id' => $transport_id));
  157. if (empty($extend_list) || !$transport) {
  158. return false;
  159. } else {
  160. return $this->_calc_unit($area_id,$transport, $extend_list,$count,$weight);
  161. }
  162. }
  163. /**
  164. * 计算某个具单元的运费
  165. * @access public
  166. * @author csdeshang
  167. * @param type $area_id 配送地区ID
  168. * @param type $extend 售卖区域内容
  169. * @param type $count 计数
  170. * @return type
  171. */
  172. private function _calc_unit($area_id, $transport,$extend, $count,$weight) {
  173. if($transport['transport_type']){
  174. $amount=$weight;
  175. }else{
  176. $amount=$count;
  177. }
  178. if (!empty($extend) && is_array($extend)) {
  179. foreach ($extend as $v) {
  180. if ($v['transportext_area_id'] == '' && !$transport['transport_is_limited']) {
  181. $calc_total = $v['transportext_sprice'];
  182. if ($v['transportext_xprice'] > 0 && $amount > $v['transportext_snum']) {
  183. if ($v['transportext_xnum']) {
  184. $calc_total += ceil(($amount - $v['transportext_snum']) / $v['transportext_xnum']) * $v['transportext_xprice'];
  185. } else {
  186. $calc_total += $v['transportext_xprice'];
  187. }
  188. }
  189. }
  190. if ($area_id) {
  191. if (strpos($v['transportext_area_id'], "," . $area_id . ",") !== false) {
  192. $calc_total = $v['transportext_sprice'];
  193. if ($v['transportext_xprice'] > 0 && $amount > $v['transportext_snum']) {
  194. if ($v['transportext_xnum']) {
  195. $calc_total += ceil(($amount - $v['transportext_snum']) / $v['transportext_xnum']) * $v['transportext_xprice'];
  196. } else {
  197. $calc_total += $v['transportext_xprice'];
  198. }
  199. }
  200. }
  201. }
  202. }
  203. }
  204. return isset($calc_total) ? ds_price_format($calc_total) : false;
  205. }
  206. }