Transport.php 7.2 KB

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