Transport.php 7.7 KB

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