Order.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author Qinii
  6. * @day 2020-06-15
  7. *
  8. *
  9. */
  10. namespace app\controller\admin\order;
  11. use crmeb\basic\BaseController;
  12. use app\common\repositories\store\ExcelRepository;
  13. use app\common\repositories\system\merchant\MerchantRepository;
  14. use app\common\repositories\store\order\StoreOrderRepository as repository;
  15. use crmeb\services\SpreadsheetExcelService;
  16. use think\App;
  17. use think\facade\Db;
  18. class Order extends BaseController
  19. {
  20. protected $repository;
  21. public function __construct(App $app, repository $repository)
  22. {
  23. parent::__construct($app);
  24. $this->repository = $repository;
  25. }
  26. public function lst($id)
  27. {
  28. [$page, $limit] = $this->getPage();
  29. $where = $this->request->params(['date','order_sn','order_type','keywords','username']);
  30. $where['reconciliation_type'] = $this->request->param('status', 1);
  31. $where['mer_id'] = $id;
  32. return app('json')->success($this->repository->adminGetList($where, $page, $limit));
  33. }
  34. public function markForm($id)
  35. {
  36. if (!$this->repository->getWhereCount([$this->repository->getPk() => $id]))
  37. return app('json')->fail('数据不存在');
  38. return app('json')->success(formToData($this->repository->adminMarkForm($id)));
  39. }
  40. public function mark($id)
  41. {
  42. if (!$this->repository->getWhereCount([$this->repository->getPk() => $id]))
  43. return app('json')->fail('数据不存在');
  44. $data = $this->request->params(['admin_mark']);
  45. $this->repository->update($id, $data);
  46. return app('json')->success('备注成功');
  47. }
  48. /**
  49. * TODO
  50. * @return mixed
  51. * @author Qinii
  52. * @day 2020-06-25
  53. */
  54. public function getAllList()
  55. {
  56. [$page, $limit] = $this->getPage();
  57. $where = $this->request->params(['type', 'date', 'mer_id','keywords','status','username','order_sn','order_ids','paid', 'zero_line_uid']);
  58. $pay_type=$this->request->param('pay_type');
  59. if($pay_type==1 || $pay_type==4){
  60. $where['pay_type']=$pay_type;
  61. }
  62. if($pay_type==6){
  63. //一壶茶红积分兑换的
  64. $where['mer_id']=496;
  65. $where['pay_type']=5;
  66. }
  67. // 导出
  68. $is_excel = request()->param('is_excel', 0);
  69. $result = $this->repository->merchantGetList($where, $page, $limit, $is_excel);
  70. if ($is_excel == 1) {
  71. $data = $result['list'];
  72. $arr = [];
  73. foreach ($data as $item) {
  74. $arr[] = [
  75. $item['order_sn'], // 订单编号
  76. $item['order_type'] == 0 ? "普通订单" : "核销订单", // 订单类型
  77. $item['user']['nickname'] ?? '', // 用户昵称
  78. $item['user']['phone'] ?? '', // 用户手机号
  79. $item['real_name'], // 收货人
  80. $item['merchant']['mer_name'] ?? '', // 商户名称
  81. // $item['orderProduct'][0]['cart_info']['product']['image'],
  82. ($item['orderProduct'][0]['cart_info']['product']['store_name'] ?? '') . ' | ' . ($item['orderProduct'][0]['cart_info']['productAttr']['sku'] ?? ''),
  83. $item['orderProduct'][0]['cart_info']['productAttr']['price'] ?? 0.00,
  84. $item['orderProduct'][0]['product_num'] ?? 0,
  85. $item['pay_price'], // 订单金额
  86. $item['paid'] == 0 ? "未支付" : "已支付", // 支付状态
  87. $item['fzone_pay_note'], // 支付方式
  88. $item['fzone_pay_jifen'], // 支付方式
  89. $item['fzone_pay_price'], // 支付方式
  90. $item['fzone_pay_jingdou'], // 支付方式
  91. $item['fzone_pay_vr'], // 支付方式
  92. $item['is_del'] == 0 ? ($item['status'] == 5 ? '待付款' : ($item['status'] == 0 ? '待发货' : ($item['status'] == 1 ? '待收货' : ($item['status'] == 2 ? '待评价' : ($item['status'] == 3 ? '已完成' : ($item['status'] == 4 ? '已申请退款' : ($item['status'] == -1 ? '已退款' : ($item['status'] == 6 ? '已取消' : '')))))))) : '已删除', // 订单状态
  93. $item['create_time'], // 下单时间
  94. ];
  95. }
  96. return app('json')->success($this->getStatisticsExcel([
  97. '订单编号',
  98. '订单类型',
  99. '用户昵称',
  100. '用户手机号',
  101. '收货人',
  102. '商户名称',
  103. '商品图片',
  104. // '商品名称',
  105. '商品单价',
  106. '商品数量',
  107. '订单金额',
  108. '支付状态',
  109. '支付方式',
  110. '支付积分',
  111. '支付现金',
  112. '支付京豆',
  113. '支付VR',
  114. '订单状态',
  115. '下单时间'
  116. ], $arr, '订单明细'));
  117. }
  118. return app('json')->success($result);
  119. }
  120. /**
  121. * 获取统计导出
  122. */
  123. public function getStatisticsExcel($header,$arr,$title)
  124. {
  125. $filename = $title.'_'.date('YmdHis').'_'.rand(10000,99999);
  126. $name = $filename;
  127. $info = '生成时间:' . date('Y-m-d H:i:s',time());
  128. $suffix='xlsx';
  129. $path='extract';
  130. unset($_instance);
  131. $_instance = SpreadsheetExcelService::instance_xin();
  132. $_path =$_instance
  133. ->createOrActive()
  134. ->setExcelHeader($header)
  135. ->setExcelTile($title, $name, $info)
  136. ->setExcelContent($arr)
  137. ->excelSave($filename, $suffix, $path);
  138. $domain = request()->domain();
  139. $data= [
  140. 'name' => $filename.'.'.$suffix,
  141. 'status' => 1,
  142. 'path' => $domain.'/'.$_path
  143. ];
  144. return $data;
  145. }
  146. /**
  147. * TODO 自提订单列表
  148. * @return mixed
  149. * @author Qinii
  150. * @day 2020-08-17
  151. */
  152. public function getTakeList()
  153. {
  154. [$page, $limit] = $this->getPage();
  155. $where = $this->request->params(['date','order_sn','keywords','username']);
  156. $where['take_order'] = 1;
  157. $where['status'] = -1;
  158. $where['verify_date'] = $where['date'];
  159. unset($where['date']);
  160. return app('json')->success($this->repository->merchantGetList($where, $page, $limit));
  161. }
  162. /**
  163. * TODO
  164. * @return mixed
  165. * @author Qinii
  166. * @day 2020-08-17
  167. */
  168. public function chart()
  169. {
  170. $where = $this->request->params(['type', 'date', 'mer_id','keywords','username','order_sn','order_ids','paid']);
  171. return app('json')->success($this->repository->adminOrderNumber(-1, $where));
  172. }
  173. /**
  174. * TODO 自提订单头部统计
  175. * @return mixed
  176. * @author Qinii
  177. * @day 2020-08-17
  178. */
  179. public function takeChart()
  180. {
  181. return app('json')->success($this->repository->adminOrderNumber(1));
  182. }
  183. /**
  184. * TODO 订单类型
  185. * @return mixed
  186. * @author Qinii
  187. * @day 2020-08-15
  188. */
  189. public function orderType()
  190. {
  191. return app('json')->success($this->repository->orderType([]));
  192. }
  193. public function detail($id)
  194. {
  195. $data = $this->repository->getOne($id, null);
  196. if (!$data)
  197. return app('json')->fail('数据不存在');
  198. return app('json')->success($data);
  199. }
  200. /**
  201. * TODO 快递查询
  202. * @param $id
  203. * @return mixed
  204. * @author Qinii
  205. * @day 2020-06-25
  206. */
  207. public function express($id)
  208. {
  209. if (!$this->repository->getWhereCount(['order_id' => $id, 'delivery_type' => 1]))
  210. return app('json')->fail('订单信息或状态错误');
  211. return app('json')->success($this->repository->express($id));
  212. }
  213. public function reList($id)
  214. {
  215. [$page, $limit] = $this->getPage();
  216. $where = ['reconciliation_id' => $id, 'type' => 0];
  217. return app('json')->success($this->repository->reconList($where, $page, $limit));
  218. }
  219. /**
  220. * TODO 导出文件
  221. * @author Qinii
  222. * @day 2020-07-30
  223. */
  224. public function excel()
  225. {
  226. $where = $this->request->params(['type', 'date', 'mer_id','keywords','status','username','order_sn']);
  227. app()->make(ExcelRepository::class)->create($where, $this->request->adminId(), 'order',0);
  228. return app('json')->success('开始导出数据');
  229. }
  230. /**
  231. * 养老金打款凭证 列表
  232. * @return mixed
  233. * @throws \think\db\exception\DataNotFoundException
  234. * @throws \think\db\exception\DbException
  235. * @throws \think\db\exception\ModelNotFoundException
  236. */
  237. public function order_red_list()
  238. {
  239. [$page, $limit] = $this->getPage();
  240. $where = $this->request->params(['status', 'date']);
  241. // $where['mer_id'] = $this->request->merId();
  242. $query = Db::name('enterprise_red_order_voucher')->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
  243. getModelTime($query, $where['date']);
  244. })->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
  245. $query->where('status',$where['status']);
  246. });
  247. $data['count'] = $query->count();
  248. $data['list'] = $query->page($page, $limit)->order('id desc')->select();
  249. return app('json')->success('ok', $data);
  250. }
  251. /**
  252. * 养老金提交打款凭证 审核
  253. * @return mixed
  254. * @throws \think\db\exception\DataNotFoundException
  255. * @throws \think\db\exception\DbException
  256. * @throws \think\db\exception\ModelNotFoundException
  257. */
  258. public function order_red_status()
  259. {
  260. $post= $this->request->params(['id', 'status', 'refund_message']);
  261. $voucher=Db::name('enterprise_red_order_voucher')->find($post['id']);
  262. if(!$voucher){
  263. return app('json')->fail('订单信息错误');
  264. }
  265. if($voucher['status']!=0){
  266. return app('json')->fail('请勿重复操作');
  267. }
  268. if($post['status']==2){
  269. $update['status']=2;
  270. $update['refund_message']=$post['refund_message'];
  271. if($post['refund_message']==''){
  272. return app('json')->fail('请输入不通过原因');
  273. }
  274. $is_red_ylj=0;
  275. }else{
  276. $update['status']=1;
  277. $is_red_ylj=2;
  278. }
  279. $update['status_time']=date('Y-m-d H:i:s');
  280. $re=Db::name('enterprise_red_order_voucher')->where('id',$post['id'])->update($update);
  281. Db::name('store_order')->whereIn('order_id',$voucher['order_ids'])->where('is_red_ylj',1)->update(['is_red_ylj'=>$is_red_ylj]);
  282. $where['o.paid'] = 1;
  283. // $where['o.pay_type'] = 5;
  284. $where['u.commission_type'] = 5;
  285. $where['u.gzc'] = 3;
  286. $where['u.status'] = 0;
  287. $bill_id = Db::name('store_order')
  288. ->alias('o')
  289. ->join('user_bill u', 'u.order_sn=o.order_sn')
  290. ->where($where)
  291. ->whereIN('o.pay_type',[0,5])
  292. ->whereIn('o.order_id', $voucher['order_ids'])
  293. ->column('u.bill_id');
  294. Db::name('user_bill')->whereIn('bill_id',$bill_id)->update(['status'=>1,'gzc'=>0]);
  295. if($re){
  296. return app('json')->success('操作成功');
  297. }
  298. return app('json')->fail('操作失败');
  299. }
  300. /**
  301. * 撤回订单,并撤回订单奖励
  302. */
  303. public function killOrderRewards(){
  304. $group_order_id = request()->param('group_order_id');
  305. $order_id = request()->param('order_id');
  306. $uid = request()->param('uid');
  307. if (empty($order_id) || empty($group_order_id) || empty($uid)) {
  308. return app('json')->fail('参数缺失');
  309. }
  310. try {
  311. Db::startTrans();
  312. // 删除订单相关表 对应备份还没有创建
  313. $this->backupAndDelete('store_order', ['group_order_id' => $group_order_id]);
  314. $this->backupAndDelete('store_group_order', ['group_order_id' => $group_order_id]);
  315. // 取出并删除 pv
  316. $pv = $this->getValue('user_pv_log', ['order_id' => $order_id, 'status' => 1,], 'pv');
  317. $this->backupAndDelete('user_pv_log', ['order_id' => $order_id]);
  318. // 取出 vr 消耗
  319. // $payVr = $this->getValue('user_sign_vr', ['order_id' => $order_id, 'type' => 2,], 'number');
  320. // 取出购买赠送积分 / 支出积分
  321. // $score = $this->getValue('user_sign_score', ['order_id' => $order_id, 'status' => 1,'source_type' => 3,'uid' => $uid,], 'reward_score');
  322. // $payScore = $this->getValue('user_sign_score', ['order_id' => $order_id, 'status' => 1,'pm' => 2,'uid' => $uid,], 'reward_score');
  323. // // 取出推广赠送的积分和推广人的 uid
  324. // $spreadScore = $this->getValue('user_sign_score', ['order_id' => $order_id, 'status' => 1,'pm' => 2,'source_type' => 3,['mark','like','%推广%'],], 'reward_score');
  325. // $spreadUid = $this->getValue('user_sign_score', [['order_id' => $order_id], ['status' => 1],['pm' => 2],['source_type' => 3],['mark','like','%推广%'],], 'uid');
  326. // // 删除所有相关积分记录
  327. // $this->backupAndDelete('user_sign_score', ['order_id' => $order_id]);
  328. // // 贡献值
  329. // $gongXian = $this->getValue('user_sign_gongxian', ['order_id' => $order_id, 'status' => 1,'type' => 1,'uid' => $uid,], 'number');
  330. // $spreadGongXian = 0;
  331. // if ($spreadUid) {
  332. // $spreadGongXian = $this->getValue('user_sign_gongxian', ['order_id' => $order_id, 'status' => 1,'type' => 1,'uid' => $spreadUid,], 'number');
  333. // }
  334. // $this->backupAndDelete('user_sign_gongxian', ['order_id' => $order_id]);
  335. // // 养老金 / 推广养老金 / 推广佣金
  336. // $yangLaoJin = $this->getValue('user_bill', ['link_id' => $order_id, 'status' => 1,'pm' => 1,'uid' => $uid,], 'number');
  337. // $spreadYlj = 0;
  338. // $spreadYongJin = 0;
  339. // if ($spreadUid) {
  340. // $spreadYlj = $this->getValue('user_bill', ['link_id' => $order_id, 'commission_type' => 5,'uid' => $spreadUid,], 'number'); //推荐获得养老金
  341. // $spreadYongJin = $this->getValue('user_bill', ['link_id' => $order_id, 'commission_type' => 3,'uid' => $spreadUid,], 'number'); //推荐获得佣金
  342. // }
  343. // $spreadYongJinTwo = $this->getValue('user_bill', ['link_id' => $order_id, 'commission_type' => 3, ['uid', '<>', $spreadUid],],'number');
  344. // $spreadYongJinTwoUid = $this->getValue('user_bill', ['link_id' => $order_id, 'commission_type' => 3, ['uid', '<>', $spreadUid],],'uid');
  345. // $this->backupAndDelete('user_bill', ['link_id' => $order_id]);
  346. // $this->backupAndDelete('gz_logs', ['link_id' => $order_id]);
  347. // //更新对应的user表
  348. // $user = Db::name('user')->where('uid', $uid)->find();
  349. // if ($user) {
  350. // Db::name('backup_user')->insert($user);
  351. // }
  352. // Db::name('user')->where('uid', $uid)->dec('pv', $pv)->update();
  353. // Db::name('user')->where('uid', $uid)->inc('vr', $payVr)->update();
  354. // Db::name('user')->where('uid', $uid)->inc('score', $payScore)->update();
  355. // Db::name('user')->where('uid', $uid)->dec('score', $score)->update();
  356. // Db::name('user')->where('uid', $uid)->dec('contribute', $gongXian)->update();
  357. // Db::name('user')->where('uid', $uid)->dec('annuity', $yangLaoJin)->update();
  358. // //推荐人user表
  359. // $spreadUser = Db::name('user')->where('uid', $spreadUid)->find();
  360. // if ($spreadUser) {
  361. // Db::name('backup_user')->insert($spreadUser);
  362. // }
  363. // Db::name('user')->where('uid', $spreadUid)->dec('score', $spreadScore)->update();
  364. // Db::name('user')->where('uid', $spreadUid)->dec('per_contribute', $spreadGongXian)->update();
  365. // Db::name('user')->where('uid', $spreadUid)->dec('annuity', $spreadYlj)->update();
  366. // Db::name('user')->where('uid', $spreadUid)->dec('brokerage_price', $spreadYongJin)->update();
  367. // //第二个推荐人
  368. // $spreadUserTwo = Db::name('user')->where('uid', $spreadYongJinTwoUid)->find();
  369. // if ($spreadUserTwo) {
  370. // Db::name('backup_user')->insert($spreadUserTwo);
  371. // }
  372. // Db::name('user')->where('uid', $spreadYongJinTwoUid)->dec('brokerage_price', $spreadYongJinTwo)->update();
  373. Db::commit();
  374. } catch (\Exception $e) {
  375. Db::rollback();
  376. return app('json')->fail('操作失败');
  377. }
  378. return app('json')->success('操作成功');
  379. }
  380. private function backupAndDelete($table, array $where)
  381. {
  382. $backupTable = 'backup_' . $table;
  383. $rows = Db::name($table)->where($where)->select()->toArray();
  384. if ($rows) {
  385. Db::name($backupTable)->insertAll($rows);
  386. Db::name($table)->where($where)->delete();
  387. }
  388. }
  389. private function getValue($table, array $where, string $field, $default = 0)
  390. {
  391. $value = Db::name($table)->where($where)->value($field);
  392. return $value ?? $default;
  393. }
  394. }