FreightLogic.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\adminapi\logic\settings\delivery;
  20. use app\common\logic\BaseLogic;
  21. use app\common\model\Freight;
  22. use app\common\model\FreightConfig;
  23. use app\common\model\Region;
  24. use think\facade\Db;
  25. class FreightLogic extends BaseLogic
  26. {
  27. /**
  28. * @notes 添加运费模版
  29. * @param $params
  30. * @return bool|string
  31. * @author ljj
  32. * @date 2021/7/30 2:46 下午
  33. */
  34. public function add($params)
  35. {
  36. // 启动事务
  37. Db::startTrans();
  38. try {
  39. //模版数据入库
  40. $freight = new Freight;
  41. $freight->name = $params['name'];
  42. $freight->charge_way = $params['charge_way'];
  43. $freight->remark = $params['remark'];
  44. $freight->save();
  45. //模版配置数据入库
  46. $freight_config = new FreightConfig;
  47. $data = [];
  48. foreach ($params['region'] as $val) {
  49. $data[] = [
  50. 'freight_id' => $freight->id,
  51. 'region_id' => $val['region_id'],
  52. 'first_unit' => $val['first_unit'],
  53. 'first_money' => $val['first_money'],
  54. 'continue_unit' => $val['continue_unit'],
  55. 'continue_money' => $val['continue_money'],
  56. ];
  57. }
  58. $freight_config->saveAll($data);
  59. // 提交事务
  60. Db::commit();
  61. return true;
  62. } catch (\Exception $e) {
  63. // 回滚事务
  64. Db::rollback();
  65. return $e->getMessage();
  66. }
  67. }
  68. /**
  69. * @notes 编辑运费模版
  70. * @param $params
  71. * @return bool|string
  72. * @author ljj
  73. * @date 2021/7/30 6:06 下午
  74. */
  75. public function edit($params)
  76. {
  77. // 启动事务
  78. Db::startTrans();
  79. try {
  80. //更新运费模版
  81. $freight = Freight::find($params['id']);
  82. $freight->name = $params['name'];
  83. $freight->charge_way = $params['charge_way'];
  84. $freight->remark = $params['remark'];
  85. $freight->save();
  86. //删除旧的运费模版配置
  87. FreightConfig::destroy(function($query) use($params){
  88. $query->where('freight_id',$params['id']);
  89. });
  90. //添加新的运费模版配置
  91. $data = [];
  92. foreach ($params['region'] as $val) {
  93. $data[] = [
  94. 'freight_id' => $freight->id,
  95. 'region_id' => $val['region_id'],
  96. 'first_unit' => $val['first_unit'],
  97. 'first_money' => $val['first_money'],
  98. 'continue_unit' => $val['continue_unit'],
  99. 'continue_money' => $val['continue_money'],
  100. ];
  101. }
  102. $freight_config = new FreightConfig;
  103. $freight_config->saveAll($data);
  104. // 提交事务
  105. Db::commit();
  106. return true;
  107. } catch (\Exception $e) {
  108. // 回滚事务
  109. Db::rollback();
  110. return $e->getMessage();
  111. }
  112. }
  113. /**
  114. * @notes 查看运费模版详情
  115. * @param $params
  116. * @return array
  117. * @throws \think\db\exception\DataNotFoundException
  118. * @throws \think\db\exception\DbException
  119. * @throws \think\db\exception\ModelNotFoundException
  120. * @author ljj
  121. * @date 2021/7/30 6:52 下午
  122. */
  123. public function detail($params)
  124. {
  125. $freight = Freight::field('id,name,charge_way,remark')->find($params['id']);
  126. $freight->region->toArray();
  127. $freight = $freight->toArray();
  128. foreach ($freight['region'] as &$val) {
  129. if ($val['region_id'] == 100000) {
  130. $val['region_name'] = '全国统一运费';
  131. }else {
  132. $val['region_name'] = implode('、',Region::where('id','in', $val['region_id'])->column('name'));
  133. }
  134. }
  135. return $freight;
  136. }
  137. /**
  138. * @notes 删除运费模版
  139. * @param $params
  140. * @return bool
  141. * @author ljj
  142. * @date 2021/7/30 7:00 下午
  143. */
  144. public function del($params)
  145. {
  146. return Freight::destroy($params['id']);
  147. }
  148. }