Bez popisu

Order.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2017/12/5
  6. * Time: 15:07
  7. */
  8. namespace App;
  9. use Illuminate\Database\Eloquent\Model;
  10. use PHPExcel;
  11. use PHPExcel_Writer_Excel2007;
  12. class Order extends Model
  13. {
  14. public $timestamps = false;
  15. protected $table = "order";
  16. public static function olist(){
  17. $result = Order::orderBy('id', 'desc')->get();
  18. return json_decode(json_encode($result),true);
  19. }
  20. /**
  21. * @param old_skus 只用于更新订单,计算sku变化
  22. * @param new_skus
  23. * @param flag 1;录入,加占库存 -1:到卖家或删除,减占库存
  24. */
  25. public static function diffSkuToRedis($old_skus, $new_skus, $flag=1){
  26. $new_arr = array();
  27. foreach($new_skus as $k=>$v){
  28. if($flag == 1){
  29. $new_arr[$v['sku_id']] = $v['num'];
  30. }elseif($flag == -1){
  31. $new_arr[$v['sku_id']] = 0-$v['num'];
  32. }
  33. }
  34. if(!empty($old_skus)){
  35. $old_arr = array();
  36. foreach($old_skus as $k=>$v){
  37. $old_arr[$v['sku_id']] = $v['num'];
  38. }
  39. $res_arr = array();
  40. foreach($new_arr as $sku_id=>$num){
  41. if(isset($old_arr[$sku_id])){
  42. if($old_arr[$sku_id] != $num){
  43. $diff_num = $num - $old_arr[$sku_id];
  44. unset($old_arr[$sku_id]);
  45. }else{
  46. unset($old_arr[$sku_id]);
  47. continue;
  48. }
  49. }else{
  50. $diff_num = $num;
  51. }
  52. $res_arr[$sku_id] = $diff_num;
  53. }
  54. if(!empty($old_arr)){
  55. foreach($old_arr as $k=>&$old_num){
  56. $old_num = 0 - $old_num;
  57. }
  58. $res_arr = $res_arr + $old_arr;
  59. }
  60. }else{
  61. $res_arr = $new_arr;
  62. }
  63. $redis_table = config('constants.SKU_QUANTITY_TABLE');
  64. foreach($res_arr as $key => $val){
  65. //redis 存货号
  66. $code = GoodsSkus::where('id', $key)->pluck('code');
  67. $sku_redis_val = RedisModel::hGet($redis_table, $code);
  68. $sku_up_val = $sku_redis_val>0 ? ($sku_redis_val + $val) : $val;
  69. RedisModel::hSet($redis_table, $code, $sku_up_val);
  70. }
  71. }
  72. /**
  73. * 添加订单
  74. */
  75. public static function mjOrderAdd($orderList){
  76. $orderList["method"] = "maijiayun.order.add";
  77. //status格式
  78. $staus_arr = array(
  79. 0 => '待付款',
  80. 1 => '已付款待审核',
  81. 2 => '已审核待发货',
  82. 3 => '已发货',
  83. );
  84. $orderList['status'] = $staus_arr[$orderList['status']];
  85. $result = self::mjApi($orderList);
  86. return $result;
  87. }
  88. /**
  89. * 更新订单
  90. */
  91. public static function mjOrderUpdate($orderList){
  92. $orderList["method"] = "maijiayun.order.update";
  93. $staus_arr = array(
  94. 0 => '待付款',
  95. 1 => '已付款待审核',
  96. 2 => '已审核待发货',
  97. 3 => '已发货',
  98. 4 => '已取消',
  99. );
  100. $orderList['status'] = $staus_arr[$orderList['status']];
  101. $result = self::mjApi($orderList);
  102. return $result;
  103. }
  104. /**
  105. * 取消订单
  106. */
  107. public static function mjOrderDel($orderList){
  108. $orderList["method"] = "maijiayun.order.update";
  109. $orderList['status'] = '已取消';
  110. $result = self::mjApi($orderList);
  111. return $result;
  112. }
  113. /**
  114. * 获取订单
  115. */
  116. public static function mjOrderGet($order){
  117. $order["method"] = "maijiayun.order.get";
  118. $result = self::mjApi($order);
  119. return $result;
  120. }
  121. /**
  122. * 添加Erp商品
  123. */
  124. public static function mjErpGoodsAdd($goods){
  125. $goods["method"] = "maijiayun.goods.sku.add";
  126. $result = self::mjApi($goods);
  127. return $result;
  128. }
  129. /**
  130. * 添加商品
  131. */
  132. public static function mjGoodsAdd($goods){
  133. $goods["method"] = "maijiayun.eshop.goods.add";
  134. $result = self::mjApi($goods);
  135. return $result;
  136. }
  137. /**
  138. * 更新商品
  139. */
  140. public static function mjGoodsUpdate($goods){
  141. $goods["method"] = "maijiayun.eshop.goods.update";
  142. $result = self::mjApi($goods);
  143. return $result;
  144. }
  145. /**
  146. * 更新erp商品
  147. */
  148. public static function mjErpGoodsUpdate($goods){
  149. $goods["method"] = "maijiayun.goods.update";
  150. $result = self::mjApi($goods);
  151. return $result;
  152. }
  153. /**
  154. * 批量添加sku
  155. */
  156. public static function mjBatchSkuAdd($sku){
  157. $sku["method"] = "maijiayun.eshop.sku.batchAdd"; //批量添加
  158. //$sku["method"] = "maijiayun.eshop.sku.add";
  159. $result = self::mjApi($sku);
  160. return $result;
  161. }
  162. /**
  163. * 添加单个sku
  164. */
  165. public static function mjSkuAdd($sku){
  166. $sku["method"] = "maijiayun.eshop.sku.add";
  167. $result = self::mjApi($sku);
  168. return $result;
  169. }
  170. /**
  171. * 修改Erp sku
  172. */
  173. public static function mjErpSkuUpdate($sku){
  174. $sku["method"] = "maijiayun.goods.sku.update";
  175. $result = self::mjApi($sku);
  176. return $result;
  177. }
  178. /**
  179. * 修改shop sku
  180. */
  181. public static function mjSkuUpdate($sku){
  182. $sku["method"] = "maijiayun.eshop.sku.update";
  183. $result = self::mjApi($sku);
  184. return $result;
  185. }
  186. /**
  187. * 获取 sku 库存
  188. */
  189. public static function mjWarehouseSkuGet($sku){
  190. $sku["method"] = "maijiayun.warehouse.sku.list";
  191. $result = self::mjApi($sku);
  192. return $result;
  193. }
  194. public static function mjApi($params = array()){
  195. $accessKey = env('ERP_ACCESS_KEY');
  196. $accessSecret = env('ERP_ACCESS_SECRET');
  197. $params["timestamp"] = time()."000";
  198. $params["version"] = "v1";
  199. $reqparams = $params;
  200. ksort($reqparams);
  201. $sign = "";
  202. foreach($reqparams as $k=>$val){
  203. if(is_array($val)){
  204. $val = json_encode($val,320);
  205. }
  206. $sign.=$k.$val;
  207. }
  208. $params["accessKey"] = $accessKey;
  209. $params["token"] = strtoupper(sha1($accessKey.$sign.$accessSecret));
  210. $param = json_encode($params);
  211. $ch = curl_init();
  212. $headers = array("Content-type:application/json;charset='utf-8'","Accept:application/json", "Cache-Control: no-cache", "Pragma: no-cache");
  213. curl_setopt($ch, CURLOPT_URL, "http://w8t1clwezn5y.cn-north-1.jdcloud-api.net/open/openapi"); //api 地址
  214. curl_setopt($ch, CURLOPT_POST, 1);
  215. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  216. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  217. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  218. curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
  219. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  220. $response = curl_exec($ch);
  221. curl_close($ch);
  222. $response = json_decode($response, true);
  223. Log::errorLog($params,['mjApi_response'=>$response],"order/mjApi",1,'');
  224. if (!$response || !is_array($response)) {
  225. return false;
  226. }
  227. if (!array_key_exists("isOk", $response)) {
  228. return false;
  229. } elseif( $response['isOk'] == false){
  230. // dd($response);
  231. Log::errorLog($params,['mjApi_response'=>$response],"order/mjApi",0,'');
  232. return false;
  233. } else {
  234. return $response;
  235. }
  236. }
  237. # 生成外部订单号
  238. public static function createOuterCode(){
  239. $prefix = config('constants.ORDER_PREFIX');
  240. $order_sn = $prefix.rand(100, 9999). substr(time(), 5, 5). rand(10, 9999);
  241. return $order_sn;
  242. }
  243. /**
  244. * 导出excel
  245. * @param $data
  246. * @param string
  247. */
  248. public static function export_excel($data, $filename = '未命名.xlsx', $indexKey, $title) {
  249. if( !is_array($indexKey)) return false;
  250. $header_arr = array('A','B','C','D','E','F','G','H','I','J','K','L','M', 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
  251. //初始化PHPExcel()
  252. $objPHPExcel = new PHPExcel();
  253. $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
  254. //接下来就是写数据到表格里面去
  255. $objActSheet = $objPHPExcel->getActiveSheet();
  256. foreach($title as $k=>$item){
  257. $objActSheet->setCellValue($header_arr[$k].'1',$item);
  258. }
  259. $startRow = 2;
  260. foreach ($data as $row) {
  261. foreach ($indexKey as $key => $value){
  262. //这里是设置单元格的内容
  263. $objActSheet->setCellValue($header_arr[$key].$startRow,$row[$value]);
  264. }
  265. $startRow++;
  266. }
  267. header("Pragma: public");
  268. header("Expires: 0");
  269. header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
  270. header("Content-Type:application/force-download");
  271. header("Content-Type:application/vnd.ms-execl");
  272. header("Content-Type:application/octet-stream");
  273. header("Content-Type:application/download");;
  274. header('Content-Disposition:attachment;filename='.$filename.'');
  275. header("Content-Transfer-Encoding:binary");
  276. $objWriter->save('php://output');
  277. exit();
  278. }
  279. /*更新城市天气信息*/
  280. public static function updateWeather($order_id) {
  281. $redisKey = 'seafood_order_city_weather';
  282. //查询订单收件人所在的城市(过滤关键字 市)
  283. $city = Order::where('id', $order_id)->pluck('receiverCity');
  284. $city = str_replace('市','',$city);
  285. $specialCity = array('朝阳区','嘉定区','海淀区','昌平区','密云区','大兴区','房山区','通州区','普陀区',);
  286. if(in_array($city, $specialCity)) {
  287. $city = str_replace('区','',$city);
  288. }
  289. //查询redis中是否有该键,若没有则查询
  290. $weather = RedisModel::get($redisKey.'_'.$city);
  291. if($weather) {
  292. $sellerMemo = Order::where('id', $order_id)->pluck('sellerMemo');
  293. if(strstr($sellerMemo, $city.'天气状况:'.$weather)) {
  294. return true;
  295. }
  296. $sellerMemo = $sellerMemo.' '.$city.'天气状况:'.$weather;
  297. Log::errorLog(['order_id'=>$order_id, 'city'=> $city , 'app_key'=>''],['weather'=>$weather],"order/weather",1,'');
  298. Order::where('id', $order_id)->update(['sellerMemo'=>$sellerMemo]);
  299. } else {
  300. $appKey = self::getAppKey();
  301. $url = 'http://apis.juhe.cn/simpleWeather/query?city='.urlencode($city).'&key='.$appKey;
  302. $result = file_get_contents($url);
  303. $result = json_decode($result, true);
  304. if($result['error_code'] == '0') {
  305. //将数据缓存到redis里
  306. $today = date('Y-m-d',time());
  307. $tomorrow = date('Y-m-d',strtotime('+1 days'));
  308. $weather = '';
  309. foreach ($result['result']['future'] as $value) {
  310. if($value['date'] == $today) {
  311. $weather .= $today.'日气温:'.$value['temperature'].'; ';
  312. }
  313. if($value['date'] == $tomorrow) {
  314. $weather .= $tomorrow.'日气温:'.$value['temperature'];
  315. }
  316. }
  317. RedisModel::set($redisKey.'_'.$city, $weather);
  318. $endTime = strtotime(date('Y-m-d 00:00:00',strtotime('+1 days'))) -1;
  319. $expire = $endTime - time();
  320. RedisModel::expire($redisKey.'_'.$city, $expire);
  321. $sellerMemo = Order::where('id', $order_id)->pluck('sellerMemo');
  322. if(strstr($sellerMemo, $city.'天气状况:'.$weather)) {
  323. return true;
  324. }
  325. $sellerMemo = $sellerMemo.' '.$city.'天气状况:'.$weather;
  326. Log::errorLog(['order_id'=>$order_id, 'city'=> $city , 'app_key'=>$appKey],['weather'=>$weather],"order/weather",1,'');
  327. Order::where('id', $order_id)->update(['sellerMemo'=>$sellerMemo]);
  328. } else if($result['error_code'] == '10012'){
  329. //判断是否为第二个APPkey也用完了
  330. $key = (string) RedisModel::get('seafood_order_city_weather_key');
  331. if($key == '0') {
  332. //超过100次/天上限 更换APPKey
  333. RedisModel::set('seafood_order_city_weather_key', '1');
  334. $endTime = strtotime(date('Y-m-d 00:00:00',strtotime('+1 days'))) -1;
  335. $expire = $endTime - time();
  336. RedisModel::expire('seafood_order_city_weather_key', $expire);
  337. self::updateWeather($order_id);
  338. }
  339. }
  340. }
  341. return true;
  342. }
  343. public static function getAppKey() {
  344. $redisKey = 'seafood_order_city_weather_key';
  345. $appKeyArr = ['32c8f844f8055f70f702fc28ec930e52', '85bfc19adb25998b2559d7ceba950e13'];
  346. $appKey = (string)RedisModel::get($redisKey);
  347. if($appKey != '') {
  348. return $appKeyArr[$appKey];
  349. } else {
  350. RedisModel::set($redisKey, '0');
  351. $endTime = strtotime(date('Y-m-d 00:00:00',strtotime('+1 days'))) -1;
  352. $expire = $endTime - time();
  353. RedisModel::expire($redisKey, $expire);
  354. return $appKeyArr['0'];
  355. }
  356. }
  357. }