Transport.php 7.4 KB

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