IntegralOrderLogic.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop开源商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | gitee下载:https://gitee.com/likeshop_gitee
  7. // | github下载:https://github.com/likeshop-github
  8. // | 访问官网:https://www.likeshop.cn
  9. // | 访问社区:https://home.likeshop.cn
  10. // | 访问手册:http://doc.likeshop.cn
  11. // | 微信公众号:likeshop技术社区
  12. // | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
  13. // | likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
  14. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  15. // | likeshop团队版权所有并拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshop.cn.team
  18. // +----------------------------------------------------------------------
  19. namespace app\adminapi\logic\integral;
  20. use app\common\enum\IntegralDeliveryEnum;
  21. use app\common\enum\IntegralGoodsEnum;
  22. use app\common\enum\IntegralOrderEnum;
  23. use app\common\enum\IntegralOrderRefundEnum;
  24. use app\common\enum\NoticeEnum;
  25. use app\common\enum\PayEnum;
  26. use app\common\logic\BaseLogic;
  27. use app\common\logic\IntegralOrderRefundLogic;
  28. use app\common\model\Express;
  29. use app\common\model\IntegralDelivery;
  30. use app\common\model\IntegralOrder;
  31. use app\common\service\ConfigService;
  32. use app\common\service\FileService;
  33. use expressage\Kd100;
  34. use expressage\Kdniao;
  35. use think\facade\Db;
  36. class IntegralOrderLogic extends BaseLogic
  37. {
  38. /**
  39. * @notes 兑换订单详情
  40. * @param $id
  41. * @return array
  42. * @author ljj
  43. * @date 2022/3/31 11:23 上午
  44. */
  45. public function detail($id)
  46. {
  47. $result = IntegralOrder::with(['user'])
  48. ->where('id', $id)
  49. ->append(['delivery_address', 'pay_status_desc', 'order_status_desc','exchange_type_desc','pay_way_desc','admin_btns'])
  50. ->findOrEmpty()
  51. ->toArray();
  52. $result['confirm_time'] = empty($result['confirm_time']) ? '-' : date('Y-m-d H:i:s', $result['confirm_time']);
  53. $result['goods_snap']['image'] = FileService::getFileUrl($result['goods_snap']['image']);
  54. $integral_delivery = IntegralDelivery::where('order_id',$id)->findOrEmpty()->toArray();
  55. $result['express_name'] = $integral_delivery['express_name'] ?? '-';
  56. $result['invoice_no'] = $integral_delivery['invoice_no'] ?? '-';
  57. return $result;
  58. }
  59. /**
  60. * @notes 发货
  61. * @param $params
  62. * @return bool|string
  63. * @author ljj
  64. * @date 2022/3/31 2:29 下午
  65. */
  66. public function delivery($params)
  67. {
  68. Db::startTrans();
  69. try {
  70. $order = IntegralOrder::where(['id'=>$params['id']])->findOrEmpty()->toArray();
  71. $express = Express::where('id',$params['express_id'])->findOrEmpty()->toArray();
  72. //添加发货单
  73. $delivery_data = [
  74. 'order_id' => $order['id'],
  75. 'order_sn' => $order['sn'],
  76. 'user_id' => $order['user_id'],
  77. 'admin_id' => $params['admin_id'],
  78. 'consignee' => $order['address']['contact'],
  79. 'mobile' => $order['address']['mobile'],
  80. 'province' => $order['address']['province'],
  81. 'city' => $order['address']['city'],
  82. 'district' => $order['address']['district'],
  83. 'address' => $order['address']['address'],
  84. 'invoice_no' => $params['invoice_no'],
  85. 'send_type' => 1,
  86. 'express_id' => $params['express_id'],
  87. 'express_name' => $express['name'],
  88. 'express_status' => 1,
  89. 'create_time' => time(),
  90. ];
  91. IntegralDelivery::create($delivery_data);
  92. //更新订单信息
  93. IntegralOrder::update([
  94. 'express_time' => time(),
  95. 'express_status' => IntegralOrderEnum::SHIPPING_FINISH,
  96. 'order_status' => IntegralOrderEnum::ORDER_STATUS_GOODS,
  97. ],['id'=>$order['id']]);
  98. // 消息通知
  99. event('Notice', [
  100. 'scene_id' => NoticeEnum::ORDER_SHIP_NOTICE,
  101. 'params' => [
  102. 'user_id' => $order['user_id'],
  103. 'order_id' => $order['id'],
  104. 'express_name' => $express['name'],
  105. 'invoice_no' => $params['invoice_no'],
  106. 'ship_time' => date('Y-m-d H:i:s'),
  107. 'order_type' => 'integral'
  108. ]
  109. ]);
  110. // 提交事务
  111. Db::commit();
  112. return true;
  113. } catch (\Exception $e) {
  114. // 回滚事务
  115. Db::rollback();
  116. return $e->getMessage();
  117. }
  118. }
  119. /**
  120. * @notes 发货信息
  121. * @param $params
  122. * @return array
  123. * @throws \think\db\exception\DataNotFoundException
  124. * @throws \think\db\exception\DbException
  125. * @throws \think\db\exception\ModelNotFoundException
  126. * @author ljj
  127. * @date 2022/3/31 2:37 下午
  128. */
  129. public function deliveryInfo($params)
  130. {
  131. $result = IntegralOrder::where('id', $params['id'])
  132. ->append(['delivery_address'])
  133. ->findOrEmpty()
  134. ->toArray();
  135. $exchangeNeed = $result['goods_snap']['need_integral'] . '积分';
  136. if ($result['goods_snap']['exchange_way'] == IntegralGoodsEnum::EXCHANGE_WAY_HYBRID) {
  137. $exchangeNeed .= $result['goods_snap']['need_money'] . '元';
  138. }
  139. $actualPay = $result['order_integral'] . '积分';
  140. if ($result['order_amount'] > 0) {
  141. $actualPay .= $result['order_amount'] . '元';
  142. }
  143. $result['order_goods'] = [
  144. 'name' => $result['goods_snap']['name'],
  145. 'image' => FileService::getFileUrl($result['goods_snap']['image']),
  146. 'market_price' => $result['goods_snap']['market_price'],
  147. 'exchange_need' => $exchangeNeed,
  148. 'goods_num' => $result['total_num'],
  149. 'need_integral' => $result['goods_snap']['need_integral'],
  150. 'need_money' => $result['goods_snap']['need_money'],
  151. 'express_price' => $result['express_price'],
  152. 'actual_payment' => $actualPay,
  153. 'order_amount' => $result['order_amount'],
  154. 'order_integral' => $result['order_integral'],
  155. ];
  156. unset($result['goods_snap']);
  157. //获取物流公司
  158. $result['express'] = Express::field('id,name')->select()->toArray();
  159. return $result;
  160. }
  161. /**
  162. * @notes 确认收货
  163. * @param $params
  164. * @return bool
  165. * @author ljj
  166. * @date 2022/3/31 4:39 下午
  167. */
  168. public function confirm($params)
  169. {
  170. IntegralOrder::update([
  171. 'order_status' => IntegralOrderEnum::ORDER_STATUS_COMPLETE,
  172. 'confirm_time' => time(),
  173. ],['id'=>$params['id']]);
  174. return true;
  175. }
  176. /**
  177. * @notes 物流信息
  178. * @param $params
  179. * @return mixed
  180. * @author 段誉
  181. * @date 2022/4/1 15:01
  182. */
  183. public function logistics($params)
  184. {
  185. $order = IntegralOrder::alias('o')->field([
  186. 'o.id', 'o.express_time', 'o.express_status', 'o.address', 'o.goods_snap',
  187. 'o.order_integral', 'o.order_amount', 'o.total_num','o.express_price',
  188. 'd.send_type', 'd.express_name', 'd.invoice_no', 'd.express_id'
  189. ])
  190. ->join('integral_delivery d', 'o.id = d.order_id')
  191. ->where('o.id', $params['id'])
  192. ->find()
  193. ->toArray();
  194. $exchangeNeed = $order['goods_snap']['need_integral'] . '积分';
  195. if ($order['goods_snap']['exchange_way'] == IntegralGoodsEnum::EXCHANGE_WAY_HYBRID) {
  196. $exchangeNeed .= $order['goods_snap']['need_money'] . '元';
  197. }
  198. $actualPay = $order['order_integral'] . '积分';
  199. if ($order['order_amount'] > 0) {
  200. $actualPay .= $order['order_amount'] . '元';
  201. }
  202. $order['order_goods'] = [
  203. 'name' => $order['goods_snap']['name'],
  204. 'image' => FileService::getFileUrl($order['goods_snap']['image']),
  205. 'market_price' => $order['goods_snap']['market_price'],
  206. 'exchange_need' => $exchangeNeed,
  207. 'goods_num' => $order['total_num'],
  208. 'need_integral' => $order['goods_snap']['need_integral'],
  209. 'need_money' => $order['goods_snap']['need_money'],
  210. 'express_price' => $order['express_price'],
  211. 'actual_payment' => $actualPay,
  212. 'order_amount' => $order['order_amount'],
  213. 'order_integral' => $order['order_integral'],
  214. ];
  215. unset($order['goods_snap']);
  216. //发货方式
  217. $order['send_type_desc'] = IntegralDeliveryEnum::getSendTypeDesc($order['send_type']);
  218. if ($order['send_type'] == IntegralDeliveryEnum::NO_EXPRESS) {
  219. $order['traces'] = ['无需物流'];
  220. return $order;
  221. }
  222. //查询物流信息
  223. $expressType = ConfigService::get('logistics_config', 'express_type', '');
  224. $expressBird = unserialize(ConfigService::get('logistics_config', 'express_bird', ''));
  225. $expressHundred = unserialize(ConfigService::get('logistics_config', 'express_hundred', ''));
  226. if (empty($expressType) || $order['express_status'] != IntegralOrderEnum::SHIPPING_NO) {
  227. $order['traces'] = ['暂无物流信息'];
  228. return $order;
  229. }
  230. //快递配置设置为快递鸟时
  231. if ($expressType === 'express_bird') {
  232. $expressage = (new Kdniao($expressBird['ebussiness_id'], $expressBird['app_key']));
  233. $expressField = 'codebird';
  234. } elseif ($expressType === 'express_hundred') {
  235. $expressage = (new Kd100($expressHundred['customer'], $expressHundred['app_key']));
  236. $expressField = 'code100';
  237. }
  238. //快递编码
  239. $express_code = Express::where('id', $order['express_id'])->value($expressField);
  240. //获取物流轨迹
  241. if ($express_code === 'SF' && $expressType === 'express_bird') {
  242. $expressage->logistics($express_code, $order['invoice_no'], substr($order['address']->mobile, -4));
  243. } else {
  244. $expressage->logistics($express_code, $order['invoice_no']);
  245. }
  246. $order['traces'] = $expressage->logisticsFormat();
  247. if ($order['traces'] == false) {
  248. $order['traces'] = ['暂无物流信息'];
  249. } else {
  250. foreach ($order['traces'] as &$item) {
  251. $item = array_values(array_unique($item));
  252. }
  253. }
  254. return $order;
  255. }
  256. /**
  257. * @notes 取消订单
  258. * @param $id
  259. * @return bool
  260. * @throws \think\db\exception\DataNotFoundException
  261. * @throws \think\db\exception\DbException
  262. * @throws \think\db\exception\ModelNotFoundException
  263. * @author 段誉
  264. * @date 2022/4/1 15:40
  265. */
  266. public function cancel($id)
  267. {
  268. Db::startTrans();
  269. try {
  270. $order = IntegralOrder::findOrEmpty($id);
  271. // 更新订单状态, 退回库存, 扣减销量
  272. IntegralOrderRefundLogic::cancelOrder($id);
  273. // 退回已支付积分
  274. IntegralOrderRefundLogic::refundOrderIntegral($id);
  275. // 退回订单已支付积分或已支付金额
  276. if ($order['pay_status'] == PayEnum::ISPAID) {
  277. IntegralOrderRefundLogic::refundOrderAmount($id);
  278. }
  279. Db::commit();
  280. return true;
  281. } catch (\Exception $e) {
  282. Db::rollback();
  283. self::$error = $e->getMessage();
  284. IntegralOrderRefundLogic::addRefundLog(
  285. $order, $order['order_amount'],
  286. IntegralOrderRefundEnum::STATUS_FAIL,
  287. $e->getMessage()
  288. );
  289. return false;
  290. }
  291. }
  292. }