Product.php 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. <?php
  2. namespace app\index\controller;
  3. use app\index\model\BuyCarModel;
  4. use app\index\model\ProductCategoryModel;
  5. use app\index\model\ProductModel;
  6. use app\index\model\ProductOrderDetailModel;
  7. use app\index\model\ProductOrderModel;
  8. use app\index\model\UserAddressModel;
  9. use app\index\service\HelperService;
  10. use app\index\service\ProductService;
  11. class Product extends CmsController
  12. {
  13. /**
  14. * 添加商品接口
  15. */
  16. public function addProduct(){
  17. $param_list = [
  18. 'category_id'=>'number',
  19. 'product_number'=>'string',
  20. 'product_name'=>'string',
  21. 'market_price'=>'number',
  22. 'sales_price'=>'number',
  23. 'remain_num'=>'number',
  24. 'product_value_id'=>'array',
  25. ];
  26. HelperService::diffParam($param_list,$this->post);
  27. $ProductService = new ProductService();
  28. $id = $ProductService->addProduct($this->post);
  29. $this->returnJson(['id'=>$id]);
  30. }
  31. /**
  32. * 添加商品分类的接口
  33. */
  34. public function addProductCategory(){
  35. $param_list = [
  36. 'category_name'=>'string',
  37. 'parent_id'=>'int',
  38. ];
  39. HelperService::diffParam($param_list,$this->post);
  40. if(isset($this->post['category_img']) && is_array($this->post['category_img'])){
  41. $category_img = json_encode($this->post['category_img']);
  42. }
  43. $data = [
  44. 'category_name'=>$this->post['category_name'],
  45. 'parent_id'=>$this->post['parent_id'],
  46. 'category_img'=>$category_img,
  47. 'status'=>0
  48. ];
  49. $ProductService = new ProductService();
  50. $product = $ProductService->getProductCategoryByCondition(['category_name'=>$this->post['category_name']]);
  51. if($product){
  52. $this->returnJson('category_name is duplicate');
  53. }
  54. $id=$ProductService->addProductCategory($data);
  55. $this->returnJson(['id'=>$id]);
  56. }
  57. /**
  58. * 更新商品分类接口
  59. */
  60. public function upProductCategory(){
  61. $param_list = [
  62. 'category_id'=>'number',
  63. ];
  64. HelperService::diffParam($param_list,$this->post);
  65. $update_list = ['category_id','category_name','parent_id','status','category_img'];
  66. $data = [];
  67. foreach($this->post as $key=>$item){
  68. if(!in_array($key,$update_list)){
  69. continue;
  70. }
  71. $data[$key] = $item;
  72. }
  73. if(isset($this->post['category_img']) && is_array($this->post['category_img'])){
  74. $data['category_img'] = json_encode($this->post['category_img']);
  75. }
  76. $ProductService = new ProductService();
  77. $row=$ProductService->upProductCategory($data);
  78. $this->returnJson(['row'=>$row]);
  79. }
  80. public function addProductAttr(){
  81. $param_list = [
  82. 'attr_name'=>'string',
  83. ];
  84. HelperService::diffParam($param_list,$this->post);
  85. $ProductService = new ProductService();
  86. $id=$ProductService->addProductAttr($this->post);
  87. $this->returnJson(['id'=>$id]);
  88. }
  89. public function upProductAttr(){
  90. $param_list = [
  91. 'attr_id'=>'number',
  92. ];
  93. HelperService::diffParam($param_list,$this->post);
  94. $ProductService = new ProductService();
  95. $row=$ProductService->upProductAttr($this->post);
  96. $this->returnJson(['row'=>$row]);
  97. }
  98. public function addProductAttrValue(){
  99. $param_list = [
  100. 'attr_id'=>'number',
  101. 'value_name'=>'string',
  102. ];
  103. HelperService::diffParam($param_list,$this->post);
  104. $ProductService = new ProductService();
  105. $id=$ProductService->addProductAttrValue($this->post);
  106. $this->returnJson(['id'=>$id]);
  107. }
  108. public function upProductAttrValue(){
  109. $param_list = [
  110. 'product_value_id'=>'number',
  111. ];
  112. HelperService::diffParam($param_list,$this->post);
  113. $ProductService = new ProductService();
  114. $row=$ProductService->upProductAttrValue($this->post);
  115. $this->returnJson(['row'=>$row]);
  116. }
  117. public function addProductImg(){
  118. $param_list = [
  119. 'product_id'=>'number',
  120. 'img'=>'string',
  121. ];
  122. HelperService::diffParam($param_list,$this->post);
  123. $ProductService = new ProductService();
  124. $id=$ProductService->addProductImg($this->post);
  125. $this->returnJson(['id'=>$id]);
  126. }
  127. public function upProductImg(){
  128. $param_list = [
  129. 'id'=>'number',
  130. ];
  131. HelperService::diffParam($param_list,$this->post);
  132. $ProductService = new ProductService();
  133. $row=$ProductService->upProductImg($this->post);
  134. $this->returnJson(['row'=>$row]);
  135. }
  136. /**
  137. * 获取分类列表
  138. */
  139. public function getCategoryList(){
  140. $productCategoryModel = new ProductCategoryModel();
  141. $productCategory = $productCategoryModel->getList(['status'=>0]);
  142. $this->returnJson($productCategory);
  143. }
  144. /**
  145. * 根据分类id获取分类信息
  146. */
  147. public function getCategoryInfoById(){
  148. $param_list = [
  149. 'category_id'=>'number',
  150. ];
  151. HelperService::diffParam($param_list,$this->post);
  152. $productCategoryModel = new ProductCategoryModel();
  153. $productCategory = $productCategoryModel->where([
  154. 'category_id'=>$this->post['category_id'],
  155. ])->find();
  156. $this->returnJson($productCategory);
  157. }
  158. /**
  159. * 获取当前分类下的产品列表
  160. */
  161. public function getProductListByCategoryId(){
  162. $param_list = [
  163. 'category_id'=>'number',
  164. ];
  165. HelperService::diffParam($param_list,$this->post);
  166. $productModel = new ProductModel();
  167. if(!isset($this->post['product_id'])){
  168. $productList = $productModel->getList(['category_id'=>$this->post['category_id'],'remain_num'=>['gt',0],'status'=>0],'add_time desc',300);
  169. }else{
  170. $product_id = $this->post['product_id'];
  171. $productList = $productModel->getList(['category_id'=>$this->post['category_id'],'remain_num'=>['gt',0],'status'=>0],"product_id <> {$product_id},add_time desc");
  172. }
  173. $this->returnJson($productList);
  174. }
  175. /**
  176. * 将产品加入购物
  177. */
  178. public function addCar(){
  179. $param_list = [
  180. 'product_id'=>'number',
  181. 'user_id'=>'number',
  182. 'num'=>'number',
  183. ];
  184. HelperService::diffParam($param_list,$this->post);
  185. $buyCarModel = new BuyCarModel();
  186. $buyCar = $buyCarModel->getOne(['user_id'=>$this->post['user_id'],'product_id'=>$this->post['product_id']]);
  187. if(empty($buyCar)){
  188. $data = [
  189. 'user_id'=>$this->post['user_id'],
  190. 'product_id'=>$this->post['product_id'],
  191. 'num'=>$this->post['num'],
  192. 'add_time'=>time()
  193. ];
  194. $id = $buyCarModel->addInfo($data);
  195. }else{
  196. $id = $buyCarModel->upInfo(['id'=>$buyCar['id'],'user_id'=>$this->post['user_id'],'num'=>$buyCar['num']],['num'=>$this->post['num']+$buyCar['num']]);
  197. }
  198. $this->returnJson(['id'=>$id]);
  199. }
  200. /**
  201. * 将产品自增
  202. */
  203. public function incCar(){
  204. $param_list = [
  205. 'product_id'=>'number',
  206. 'user_id'=>'number',
  207. ];
  208. HelperService::diffParam($param_list,$this->post);
  209. $buyCarModel = new BuyCarModel();
  210. $buyCar = $buyCarModel->getOne(['user_id'=>$this->post['user_id'],'product_id'=>$this->post['product_id']]);
  211. if(empty($buyCar)){
  212. $data = [
  213. 'user_id'=>$this->post['user_id'],
  214. 'product_id'=>$this->post['product_id'],
  215. 'num'=>1,
  216. 'add_time'=>time()
  217. ];
  218. $id = $buyCarModel->addInfo($data);
  219. }else{
  220. $id = $buyCarModel->where(['id'=>$buyCar['id']])->setInc('num',1);
  221. }
  222. $this->returnJson(['id'=>$id]);
  223. }
  224. /**
  225. * 获取购物车列表
  226. */
  227. public function getCarInfo(){
  228. $param_list = [
  229. 'user_id'=>'number',
  230. ];
  231. HelperService::diffParam($param_list,$this->post);
  232. $buyCarModel = new BuyCarModel();
  233. $list = $buyCarModel->getCarAndProductInfo(['user_id'=>$this->post['user_id'],'buy_car.status'=>0]);
  234. $priceList = [];
  235. $total_num = 0;
  236. foreach($list as $item){
  237. $total_num += $item['num'];
  238. $priceList[] = $item['sales_price'] * $item['num'];
  239. }
  240. $data = [
  241. 'count'=>$total_num,
  242. 'total_price'=>array_sum($priceList)
  243. ];
  244. $this->returnJson($data);
  245. }
  246. /**
  247. * 获取购物车列表
  248. */
  249. public function getCarList(){
  250. $param_list = [
  251. 'user_id'=>'number',
  252. ];
  253. HelperService::diffParam($param_list,$this->post);
  254. $buyCarModel = new BuyCarModel();
  255. $list = $buyCarModel->getCarAndProductInfo(['user_id'=>$this->post['user_id'],'buy_car.status'=>0]);
  256. $priceList = [];
  257. foreach($list as $item){
  258. $priceList[] = $item['sales_price'] * $item['num'];
  259. }
  260. $data = [
  261. 'total_price'=>array_sum($priceList),
  262. 'list'=>$list
  263. ];
  264. $this->returnJson($data);
  265. }
  266. /**
  267. *批量查询勾选的商品
  268. */
  269. public function getCarListByProductIds(){
  270. $param_list = [
  271. 'user_id'=>'number',
  272. 'productIds'=>'array',
  273. ];
  274. HelperService::diffParam($param_list,$this->post);
  275. $buyCarModel = new BuyCarModel();
  276. $list = $buyCarModel->getCarAndProductInfo([
  277. 'buy_car.user_id'=>$this->post['user_id'],
  278. 'buy_car.status'=>0,
  279. 'buy_car.product_id'=>['in',$this->post['productIds']]
  280. ]);
  281. $priceList = [];
  282. foreach($list as $item){
  283. $priceList[] = $item['sales_price'] * $item['num'];
  284. }
  285. $data = [
  286. 'total_price'=>array_sum($priceList),
  287. 'list'=>$list,
  288. ];
  289. $this->returnJson($data);
  290. }
  291. /**
  292. *批量更新勾选的商品
  293. */
  294. public function updateCarByProductIds(){
  295. $param_list = [
  296. 'user_id'=>'number',
  297. 'productIdAndNum'=>'array',
  298. ];
  299. HelperService::diffParam($param_list,$this->post);
  300. $buyCarModel = new BuyCarModel();
  301. $row = 0;
  302. foreach($this->post['productIdAndNum'] as $product_id=>$num){
  303. $row += $buyCarModel->upInfo(['product_id'=>$product_id,'user_id'=>$this->post['user_id']],['num'=>$num]);
  304. }
  305. $this->returnJson(['row'=>$row]);
  306. }
  307. /**
  308. * 批量删除被勾选的商品
  309. */
  310. public function delCarBatch(){
  311. $param_list = [
  312. 'user_id'=>'number',
  313. 'product_ids'=>'array',
  314. ];
  315. HelperService::diffParam($param_list,$this->post);
  316. $buyCarModel = new BuyCarModel();
  317. $row = $buyCarModel->where([
  318. 'user_id'=>$this->post['user_id'],
  319. 'product_id'=>['in',$this->post['product_ids']]
  320. ])->delete();
  321. $this->returnJson(['row'=>$row]);
  322. }
  323. /**
  324. * 保存 用户收货地址
  325. */
  326. public function saveAddress(){
  327. $param_list = [
  328. 'user_id'=>'number',
  329. 'name'=>'string',
  330. 'mobile'=>'mobile',
  331. 'city'=>'string',
  332. 'address'=>'string',
  333. 'address_name'=>'string',
  334. ];
  335. HelperService::diffParam($param_list,$this->post);
  336. $data = [
  337. 'user_id' => $this->post['user_id'],
  338. 'name' => $this->post['name'],
  339. 'mobile' => $this->post['mobile'],
  340. 'city' => $this->post['city'],
  341. 'address' => $this->post['address'],
  342. 'address_name' =>$this->post['address_name'],
  343. 'status' => 0
  344. ];
  345. $userAddressModel = new UserAddressModel();
  346. if(isset($this->post['address_id']) && !empty($this->post['address_id']) && is_numeric($this->post['address_id'])) {
  347. $where['address_id'] = $this->post['address_id'];
  348. $row = $userAddressModel->upInfo($where,$data);
  349. $this->returnJson(['row'=>$row]);
  350. }else{
  351. $data['add_datetime'] = date('Y-m-d H:i:s');
  352. $id = $userAddressModel->addInfo($data);
  353. $this->returnJson(['id'=>$id]);
  354. }
  355. }
  356. /**
  357. * 删除 用户的收货地址
  358. */
  359. public function delAddress(){
  360. $param_list = [
  361. 'address_id'=>'number',
  362. 'user_id'=>'number'
  363. ];
  364. HelperService::diffParam($param_list,$this->post);
  365. $userAddressModel = new UserAddressModel();
  366. $row = $userAddressModel
  367. ->where([
  368. 'address_id'=>$this->post['address_id'],
  369. 'user_id'=>$this->post['user_id']
  370. ])->delete();
  371. $this->returnJson(['row'=>$row]);
  372. }
  373. /**
  374. * 将收货地址设置为默认地址
  375. */
  376. public function setDefaultAddress(){
  377. $param_list = [
  378. 'user_id'=>'number',
  379. 'address_id'=>'number',
  380. ];
  381. HelperService::diffParam($param_list,$this->post);
  382. $userAddressModel = new UserAddressModel();
  383. $where = [
  384. 'user_id' => $this->post['user_id'],
  385. 'address_id'=> $this->post['address_id'],
  386. ];
  387. $row = 0;
  388. $res = $userAddressModel->upInfo(['user_id'=>$this->post['user_id']],['default_address'=>0]);
  389. if($res){
  390. $row += 1;
  391. }
  392. $res = $userAddressModel->upInfo($where,['default_address'=>1]);
  393. if($res){
  394. $row += 1;
  395. }
  396. $this->returnJson(['row'=>$row]);
  397. }
  398. /**
  399. * 获取默认地址的信息
  400. */
  401. public function getDefaultAddress(){
  402. $param_list = [
  403. 'user_id'=>'number',
  404. ];
  405. HelperService::diffParam($param_list,$this->post);
  406. $userAddressModel = new UserAddressModel();
  407. $userAddress = $userAddressModel->getOne([
  408. 'user_id'=>$this->post['user_id'],
  409. 'default_address'=>1,
  410. ]);
  411. $this->returnJson($userAddress);
  412. }
  413. /**
  414. *通过地址的id获取地址信息
  415. */
  416. public function getUserAddressById(){
  417. $param_list = [
  418. 'address_id'=>'int',
  419. ];
  420. HelperService::diffParam($param_list,$this->post);
  421. $userAddressModel = new UserAddressModel();
  422. $userAddress = $userAddressModel->getOne([
  423. 'address_id'=>$this->post['address_id']
  424. ]);
  425. $this->returnJson($userAddress);
  426. }
  427. /**
  428. * 根据用户id获取收货地址的列表
  429. */
  430. public function getUserAddressListByUserId(){
  431. $param_list = [
  432. 'user_id'=>'number',
  433. ];
  434. HelperService::diffParam($param_list,$this->post);
  435. $userAddressModel = new UserAddressModel();
  436. $userAddress = $userAddressModel->getList([
  437. 'user_id'=>$this->post['user_id'],
  438. ],"default_address desc,address_id desc");
  439. $this->returnJson($userAddress);
  440. }
  441. /**
  442. * 获取产品的分页列表
  443. */
  444. public function getProductPage(){
  445. $param_list = [
  446. 'pageSize'=>'number',
  447. 'page'=>'number',
  448. 'condition'=>'array',
  449. ];
  450. HelperService::diffParam($param_list,$this->post);
  451. $search_key = ['product_id','category_id','product_name','origin'];
  452. $condition = $this->post['condition'];
  453. $where = [];
  454. foreach($condition as $key=>$item){
  455. if(!in_array($key,$search_key)){
  456. continue;
  457. }
  458. switch($key){
  459. case "product_id":
  460. case "category_id":
  461. $where[$key] = $item;
  462. break;
  463. case 'product_name':
  464. case 'origin':
  465. $where[$key] = ['like',"%{$item}%"];
  466. break;
  467. }
  468. }
  469. $page = $this->post['page'];
  470. $pageSize = $this->post['pageSize'];
  471. $page = $page>=1?intval($page):1;
  472. $pageSize = $pageSize>2?intval($pageSize):2;
  473. $productModel = new ProductModel();
  474. if($where) {
  475. $productCount = $productModel->where($where)->count();
  476. $productList = $productModel->where($where)->page($page, $pageSize)->select();
  477. }else{
  478. $productCount = $productModel->count();
  479. $productList = $productModel->page($page, $pageSize)->select();
  480. }
  481. $data = [
  482. 'count' => $productCount,
  483. 'list'=>$productList
  484. ];
  485. $this->returnJson($data);
  486. }
  487. /**
  488. * 获取产品订单列表
  489. */
  490. public function getProductOrderPageByType(){
  491. $param_list = [
  492. 'page'=>'number',
  493. 'pageSize'=>'number',
  494. 'status'=>'int',
  495. ];
  496. HelperService::diffParam($param_list,$this->post);
  497. if(!in_array($this->post['status'],[0,1,2,3,10])){
  498. $this->returnJson('',400,'status is error');
  499. }
  500. //判断要查询的订单
  501. $where = ['status'=>$this->post['status']];
  502. if($this->post['status'] == 10){
  503. //全部订单
  504. $where = [];
  505. }
  506. //可选条件user_id
  507. $user_id = isset($this->post['user_id'])?$this->post['user_id']:0;
  508. if(!empty($user_id) && is_numeric($user_id)){
  509. $where['user_id'] = $this->post['user_id'];
  510. }
  511. //可选条件
  512. $condition = isset($this->post['condition'])?$this->post['condition']:[];
  513. if(!empty($condition) && is_array($condition)){
  514. foreach($condition as $key=>$value){
  515. switch($key){
  516. case 'order_no':
  517. case 'pay_style':
  518. case 'logistic_company':
  519. case 'logistic_number':
  520. $where[$key] = $value;
  521. break;
  522. case 'add_time':
  523. case 'pay_time':
  524. //确保是时间戳
  525. $time = intval($value);
  526. //日期当天的数据
  527. $where[$key] = ['between',[$time,$time+86400]];
  528. break;
  529. }
  530. }
  531. }
  532. $page = $this->post['page'] >1?$this->post['page']:1;
  533. $pageSize = $this->post['pageSize'] >1?$this->post['pageSize']:2;
  534. $productOrderModel = new ProductOrderModel();
  535. $res = $productOrderModel->getPage($where,"order_id desc",$page,$pageSize);
  536. $this->returnJson($res);
  537. }
  538. /**
  539. * 修改
  540. */
  541. /**
  542. * 无额外属性的产品添加接口
  543. */
  544. public function saveProductNotAttr(){
  545. $param_list = [
  546. 'category_id'=>'number',
  547. 'product_name'=>'string',
  548. 'product_number'=>'',
  549. 'market_price'=>'int',
  550. 'sales_price'=>'int',
  551. 'remain_num'=>'int',
  552. ];
  553. HelperService::diffParam($param_list,$this->post);
  554. $productModel = new ProductModel();
  555. $data = [
  556. 'category_id'=>$this->post['category_id'],
  557. 'product_number'=>$this->post['product_number'],
  558. 'product_name'=>$this->post['product_name'],
  559. 'market_price'=>$this->post['market_price'],
  560. 'sales_price'=>$this->post['sales_price'],
  561. 'remain_num'=>$this->post['remain_num'],
  562. 'add_time'=>time(),
  563. 'add_datetime'=>date('Y-m-d H:i:s'),
  564. 'content'=>isset($this->post['content'])?$this->post['content']:'',
  565. 'weight'=>isset($this->post['weight'])?$this->post['weight']:'',
  566. 'origin'=>isset($this->post['origin'])?$this->post['origin']:'',
  567. 'Unit'=>isset($this->post['Unit'])?$this->post['Unit']:'',
  568. 'img'=>isset($this->post['img'])?$this->post['img']:'',
  569. ];
  570. if(isset($this->post['product_id'])) {
  571. $product_id = intval($this->post['product_id'])>0?$this->post['product_id']:0;
  572. $row = $productModel->upInfo(['product_id'=>$product_id],$data);
  573. $this->returnJson(['row' => $row]);
  574. }else{
  575. $id = $productModel->addInfo($data);
  576. $this->returnJson(['id' => $id]);
  577. }
  578. }
  579. /**
  580. * 根据productId获取ProductInfo
  581. */
  582. public function getProductDetailById(){
  583. $param_list = [
  584. 'product_id'=>'number'
  585. ];
  586. HelperService::diffParam($param_list,$this->post);
  587. $productModel = new ProductModel();
  588. $product = $productModel->where(['product_id'=>$this->post['product_id']])->find();
  589. $this->returnJson($product);
  590. }
  591. /**
  592. * 根据productId修改产品信息
  593. */
  594. public function upProductInfoByProductId(){
  595. $param_list = [
  596. 'product_id'=>'number',
  597. ];
  598. HelperService::diffParam($param_list,$this->post);
  599. $data = [
  600. 'status'=>isset($this->post['status'])?$this->post['status']:0,
  601. ];
  602. $productModel = new ProductModel();
  603. $row = $productModel->upInfo(['product_id'=>$this->post['product_id']],$data);
  604. $this->returnJson(['row'=>$row]);
  605. }
  606. /**
  607. * 通过条件获取订单信息
  608. */
  609. public function getUserOrderByType(){
  610. $param_list = [
  611. 'user_id'=>'number',
  612. 'status'=>'int'
  613. ];
  614. HelperService::diffParam($param_list,$this->post);
  615. if(!in_array($this->post['status'],[0,1,2,3,4,10])){
  616. $this->returnJson([],400,'status is error');
  617. }
  618. $params = [];
  619. $params['user_id'] = $this->post['user_id'];
  620. if($this->post['status']!=10){
  621. $params['status'] = $this->post['status'];
  622. }
  623. $productOrderModel = new ProductOrderModel();
  624. $productOrder = $productOrderModel->getProductOrder($params);
  625. $this->returnJson($productOrder);
  626. }
  627. /**
  628. * 用户每种类型订单的数量
  629. */
  630. public function getUserOrderCountByType(){
  631. $param_list = [
  632. 'user_id'=>'number',
  633. 'status'=>'int'
  634. ];
  635. HelperService::diffParam($param_list,$this->post);
  636. if(!in_array($this->post['status'],[0,1,2,3,4,10])){
  637. $this->returnJson([],400,'status is error');
  638. }
  639. $params = [];
  640. $params['user_id'] = $this->post['user_id'];
  641. if($this->post['status']!=10){
  642. $params['status'] = $this->post['status'];
  643. }
  644. $productOrderModel = new ProductOrderModel();
  645. $productOrderCount = $productOrderModel->getProductOrderCount($params);
  646. $this->returnJson($productOrderCount);
  647. }
  648. /**
  649. * 根据订单编号或许订单详情
  650. */
  651. public function getOrderByOrderNo(){
  652. $param_list = [
  653. 'order_no'=>'string'
  654. ];
  655. HelperService::diffParam($param_list,$this->post);
  656. $params = [];
  657. $params['order_no'] = $this->post['order_no'];
  658. $productOrderModel = new ProductOrderModel();
  659. $productOrder = $productOrderModel->getOne($params);
  660. $order_no = isset($productOrder['order_no'])?$productOrder['order_no']:0;
  661. $productOrderDetailModel = new ProductOrderDetailModel();
  662. $productOrderDetail = $productOrderDetailModel->getList(['order_no'=>$order_no],'',1000);
  663. if($productOrder){
  664. $productOrder['detail'] = $productOrderDetail;
  665. }
  666. $this->returnJson($productOrder);
  667. }
  668. /**
  669. * 前台用户取消订单(暂时不用这个接口,因为和功能有点重复)
  670. */
  671. private function userCancelOrder(){
  672. $param_list = [
  673. 'order_no'=>'string',
  674. 'user_id'=>'number'
  675. ];
  676. HelperService::diffParam($param_list,$this->post);
  677. $orderModel = new ProductOrderModel();
  678. $orderModel->upInfo([
  679. 'order_no'=>$this->post['order_no'],
  680. 'user_id'=>$this->post['user_id']
  681. ],['status'=>3]);
  682. $this->returnJson('success');
  683. }
  684. /**
  685. * 后台需要订单
  686. */
  687. public function saveOrderStatus(){
  688. $param_list = [
  689. 'order_no'=>'string',
  690. 'status'=>'number',
  691. ];
  692. HelperService::diffParam($param_list,$this->post);
  693. if(!in_array($this->post['status'],[0,1,2,3,4])){
  694. $this->returnJson([],400,'status is error');
  695. }
  696. $orderModel = new ProductOrderModel();
  697. $orderModel->upInfo(
  698. ['order_no'=>$this->post['order_no']],
  699. ['status'=>$this->post['status']]
  700. );
  701. $this->returnJson('success');
  702. }
  703. /**
  704. * 保存订单信息
  705. */
  706. public function saveOrderInfo(){
  707. $param_list = [
  708. 'order_no'=>'string',
  709. 'status'=>'int'
  710. ];
  711. HelperService::diffParam($param_list,$this->post);
  712. $data = [];
  713. if(isset($this->post['logistic_company_name'])){
  714. $data['logistic_company_name'] = $this->post['logistic_company_name'];
  715. }
  716. if(isset($this->post['back_note'])){
  717. $data['back_note'] = $this->post['back_note'];
  718. }
  719. if(isset($this->post['logistic_number'])){
  720. $data['logistic_number'] = $this->post['logistic_number'];
  721. }
  722. if(!in_array($this->post['status'],[0,1,2,3,4])){
  723. $this->returnJson([],400,'status is error');
  724. }
  725. $data['status'] = $this->post['status'];
  726. $orderModel = new ProductOrderModel();
  727. $orderModel->upInfo(['order_no'=>$this->post['order_no']],$data);
  728. $this->returnJson('success');
  729. }
  730. /**
  731. * 提交订单信息
  732. */
  733. public function PayOrder(){
  734. $param_list = [
  735. 'user_id'=>'number',
  736. 'productIds'=>'array',
  737. 'send_style'=>'int',
  738. 'pay_style'=>'int',
  739. ];
  740. HelperService::diffParam($param_list,$this->post);
  741. $productService = new ProductService();
  742. $orderModel = new ProductOrderModel();
  743. $order_no = $this->createOrderNum();
  744. $total_price = $productService->getOrderPriceByProductIds($order_no,$this->post['productIds'],$this->post['user_id']);
  745. if(empty($total_price)){
  746. HelperService::returnJson(['code'=>400,'msg'=>'Payorder is error']);
  747. }
  748. switch($this->post['send_style']){
  749. //快递方式
  750. case 0:
  751. $order =[
  752. 'order_no'=>$order_no,
  753. 'address_id'=>$this->post['address_id']?:0,
  754. 'add_time'=>time(),
  755. 'user_id'=>$this->post['user_id'],
  756. 'total_price'=> $total_price,
  757. 'pay_style'=>$this->post['pay_style'],
  758. 'send_style'=>$this->post['send_style'],
  759. 'note'=>$this->post['note']?:'',
  760. 'status'=>0
  761. ];
  762. $order_id = $orderModel->addInfo($order);
  763. if($order_id === false){
  764. HelperService::returnJson(['code'=>400,'msg'=>'insert order fail']);
  765. }
  766. break;
  767. //自提方式
  768. case 1:
  769. $order =[
  770. 'order_no'=>$order_no,
  771. 'address_id'=>$this->post['address_id']?:0,
  772. 'add_time'=>time(),
  773. 'add_date'=>date('Y-m-d'),
  774. 'user_id'=>$this->post['user_id'],
  775. 'total_price'=> $total_price,
  776. 'pay_style'=>$this->post['pay_style'],
  777. 'send_style'=>$this->post['send_style'],
  778. 'note'=>$this->post['note']?:'',
  779. 'status'=>0,
  780. 'get_datetime'=>$this->post['get_datetime'],
  781. ];
  782. $order_id = $orderModel->addInfo($order);
  783. if($order_id === false){
  784. HelperService::returnJson(['code'=>400,'msg'=>'insert order fail']);
  785. }
  786. break;
  787. default:
  788. HelperService::returnJson(['code'=>400,'msg'=>'send_style is error']);
  789. break;
  790. }
  791. $this->returnJson(['order_no'=>$order_no]);
  792. }
  793. /**
  794. *商品每日订单数量
  795. */
  796. public function ProductOrderDayNum(){
  797. $param = $this->request->param();
  798. $end_date = isset($param['end_date'])?strtotime($param['end_date']):strtotime("-1 seconds",strtotime(date('Y-m-d')));
  799. $start_date = isset($param['start_date'])?strtotime($param['start_date']):0;
  800. //控制时间,以免增加服务器压力
  801. if(empty($start_date) || $end_date-$start_date>86400*20){
  802. $start_date = $end_date-86400*20;
  803. }
  804. $productOrderModel = new ProductOrderModel();
  805. $productOrder = $productOrderModel
  806. ->field('count(order_id) as total_count,add_date')
  807. ->where(['add_date'=>['between',[date('Y-m-d',$start_date),date('Y-m-d',$end_date)]]])->group('add_date')->cache(3600*2)->select();
  808. $this->returnJson($productOrder);
  809. }
  810. /**
  811. *订单总数量
  812. */
  813. public function totalOrderNum(){
  814. $productOrderModel = new ProductOrderModel();
  815. $productOrder = $productOrderModel->cache(10)->count();
  816. $this->returnJson(['count'=>$productOrder]);
  817. }
  818. }