123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2017/12/5
- * Time: 15:07
- */
- namespace App;
- use Illuminate\Database\Eloquent\Model;
- use PHPExcel;
- use PHPExcel_Writer_Excel2007;
- class Order extends Model
- {
- public $timestamps = false;
- protected $table = "order";
-
- public static function olist(){
- $result = Order::orderBy('id', 'desc')->get();
- return json_decode(json_encode($result),true);
- }
- /**
- * @param old_skus 只用于更新订单,计算sku变化
- * @param new_skus
- * @param flag 1;录入,加占库存 -1:到卖家或删除,减占库存
- */
- public static function diffSkuToRedis($old_skus, $new_skus, $flag=1){
- $new_arr = array();
- foreach($new_skus as $k=>$v){
- if($flag == 1){
- $new_arr[$v['sku_id']] = $v['num'];
- }elseif($flag == -1){
- $new_arr[$v['sku_id']] = 0-$v['num'];
- }
- }
- if(!empty($old_skus)){
- $old_arr = array();
- foreach($old_skus as $k=>$v){
- $old_arr[$v['sku_id']] = $v['num'];
- }
-
- $res_arr = array();
- foreach($new_arr as $sku_id=>$num){
- if(isset($old_arr[$sku_id])){
- if($old_arr[$sku_id] != $num){
- $diff_num = $num - $old_arr[$sku_id];
- unset($old_arr[$sku_id]);
- }else{
- unset($old_arr[$sku_id]);
- continue;
- }
- }else{
- $diff_num = $num;
- }
- $res_arr[$sku_id] = $diff_num;
- }
- if(!empty($old_arr)){
- foreach($old_arr as $k=>&$old_num){
- $old_num = 0 - $old_num;
- }
- $res_arr = $res_arr + $old_arr;
- }
- }else{
- $res_arr = $new_arr;
- }
- $redis_table = config('constants.SKU_QUANTITY_TABLE');
- foreach($res_arr as $key => $val){
- //redis 存货号
- $code = GoodsSkus::where('id', $key)->pluck('code');
- $sku_redis_val = RedisModel::hGet($redis_table, $code);
- $sku_up_val = $sku_redis_val>0 ? ($sku_redis_val + $val) : $val;
- RedisModel::hSet($redis_table, $code, $sku_up_val);
- }
- }
- /**
- * 添加订单
- */
- public static function mjOrderAdd($orderList){
- $orderList["method"] = "maijiayun.order.add";
- //status格式
- $staus_arr = array(
- 0 => '待付款',
- 1 => '已付款待审核',
- 2 => '已审核待发货',
- 3 => '已发货',
- );
- $orderList['status'] = $staus_arr[$orderList['status']];
- $result = self::mjApi($orderList);
- return $result;
- }
- /**
- * 更新订单
- */
- public static function mjOrderUpdate($orderList){
- $orderList["method"] = "maijiayun.order.update";
- $staus_arr = array(
- 0 => '待付款',
- 1 => '已付款待审核',
- 2 => '已审核待发货',
- 3 => '已发货',
- 4 => '已取消',
- );
- $orderList['status'] = $staus_arr[$orderList['status']];
- $result = self::mjApi($orderList);
- return $result;
- }
- /**
- * 取消订单
- */
- public static function mjOrderDel($orderList){
- $orderList["method"] = "maijiayun.order.update";
- $orderList['status'] = '已取消';
- $result = self::mjApi($orderList);
- return $result;
- }
-
- /**
- * 获取订单
- */
- public static function mjOrderGet($order){
- $order["method"] = "maijiayun.order.get";
- $result = self::mjApi($order);
- return $result;
- }
- /**
- * 添加Erp商品
- */
- public static function mjErpGoodsAdd($goods){
- $goods["method"] = "maijiayun.goods.sku.add";
- $result = self::mjApi($goods);
- return $result;
- }
- /**
- * 添加商品
- */
- public static function mjGoodsAdd($goods){
- $goods["method"] = "maijiayun.eshop.goods.add";
- $result = self::mjApi($goods);
- return $result;
- }
-
- /**
- * 更新商品
- */
- public static function mjGoodsUpdate($goods){
- $goods["method"] = "maijiayun.eshop.goods.update";
- $result = self::mjApi($goods);
- return $result;
- }
-
- /**
- * 更新erp商品
- */
- public static function mjErpGoodsUpdate($goods){
- $goods["method"] = "maijiayun.goods.update";
- $result = self::mjApi($goods);
- return $result;
- }
- /**
- * 批量添加sku
- */
- public static function mjBatchSkuAdd($sku){
- $sku["method"] = "maijiayun.eshop.sku.batchAdd"; //批量添加
- //$sku["method"] = "maijiayun.eshop.sku.add";
- $result = self::mjApi($sku);
- return $result;
- }
- /**
- * 添加单个sku
- */
- public static function mjSkuAdd($sku){
- $sku["method"] = "maijiayun.eshop.sku.add";
- $result = self::mjApi($sku);
- return $result;
- }
- /**
- * 修改Erp sku
- */
- public static function mjErpSkuUpdate($sku){
- $sku["method"] = "maijiayun.goods.sku.update";
- $result = self::mjApi($sku);
- return $result;
- }
- /**
- * 修改shop sku
- */
- public static function mjSkuUpdate($sku){
- $sku["method"] = "maijiayun.eshop.sku.update";
- $result = self::mjApi($sku);
- return $result;
- }
- /**
- * 获取 sku 库存
- */
- public static function mjWarehouseSkuGet($sku){
- $sku["method"] = "maijiayun.warehouse.sku.list";
- $result = self::mjApi($sku);
- return $result;
- }
- public static function mjApi($params = array()){
- $accessKey = env('ERP_ACCESS_KEY');
- $accessSecret = env('ERP_ACCESS_SECRET');
- $params["timestamp"] = time()."000";
- $params["version"] = "v1";
- $reqparams = $params;
- ksort($reqparams);
- $sign = "";
- foreach($reqparams as $k=>$val){
- if(is_array($val)){
- $val = json_encode($val,320);
- }
- $sign.=$k.$val;
- }
- $params["accessKey"] = $accessKey;
- $params["token"] = strtoupper(sha1($accessKey.$sign.$accessSecret));
- $param = json_encode($params);
- $ch = curl_init();
- $headers = array("Content-type:application/json;charset='utf-8'","Accept:application/json", "Cache-Control: no-cache", "Pragma: no-cache");
- curl_setopt($ch, CURLOPT_URL, "http://w8t1clwezn5y.cn-north-1.jdcloud-api.net/open/openapi"); //api 地址
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_TIMEOUT, 5);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $response = curl_exec($ch);
- curl_close($ch);
- $response = json_decode($response, true);
- Log::errorLog($params,['mjApi_response'=>$response],"order/mjApi",1,'');
- if (!$response || !is_array($response)) {
- return false;
- }
- if (!array_key_exists("isOk", $response)) {
- return false;
- } elseif( $response['isOk'] == false){
- // dd($response);
- Log::errorLog($params,['mjApi_response'=>$response],"order/mjApi",0,'');
- return false;
- } else {
- return $response;
- }
- }
- # 生成外部订单号
- public static function createOuterCode(){
- $prefix = config('constants.ORDER_PREFIX');
- $order_sn = $prefix.rand(100, 9999). substr(time(), 5, 5). rand(10, 9999);
- return $order_sn;
- }
- /**
- * 导出excel
- * @param $data
- * @param string
- */
- public static function export_excel($data, $filename = '未命名.xlsx', $indexKey, $title) {
- if( !is_array($indexKey)) return false;
- $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');
- //初始化PHPExcel()
- $objPHPExcel = new PHPExcel();
- $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
-
- //接下来就是写数据到表格里面去
- $objActSheet = $objPHPExcel->getActiveSheet();
- foreach($title as $k=>$item){
- $objActSheet->setCellValue($header_arr[$k].'1',$item);
- }
- $startRow = 2;
- foreach ($data as $row) {
- foreach ($indexKey as $key => $value){
- //这里是设置单元格的内容
- $objActSheet->setCellValue($header_arr[$key].$startRow,$row[$value]);
- }
- $startRow++;
- }
- header("Pragma: public");
- header("Expires: 0");
- header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
- header("Content-Type:application/force-download");
- header("Content-Type:application/vnd.ms-execl");
- header("Content-Type:application/octet-stream");
- header("Content-Type:application/download");;
- header('Content-Disposition:attachment;filename='.$filename.'');
- header("Content-Transfer-Encoding:binary");
- $objWriter->save('php://output');
- exit();
- }
- /*更新城市天气信息*/
- public static function updateWeather($order_id) {
- $redisKey = 'seafood_order_city_weather';
- //查询订单收件人所在的城市(过滤关键字 市)
- $city = Order::where('id', $order_id)->pluck('receiverCity');
- $city = str_replace('市','',$city);
- $specialCity = array('朝阳区','嘉定区','海淀区','昌平区','密云区','大兴区','房山区','通州区','普陀区',);
- if(in_array($city, $specialCity)) {
- $city = str_replace('区','',$city);
- }
- //查询redis中是否有该键,若没有则查询
- $weather = RedisModel::get($redisKey.'_'.$city);
- if($weather) {
- $sellerMemo = Order::where('id', $order_id)->pluck('sellerMemo');
- if(strstr($sellerMemo, $city.'天气状况:'.$weather)) {
- return true;
- }
- $sellerMemo = $sellerMemo.' '.$city.'天气状况:'.$weather;
- Log::errorLog(['order_id'=>$order_id, 'city'=> $city , 'app_key'=>''],['weather'=>$weather],"order/weather",1,'');
- Order::where('id', $order_id)->update(['sellerMemo'=>$sellerMemo]);
- } else {
- $appKey = self::getAppKey();
- $url = 'http://apis.juhe.cn/simpleWeather/query?city='.urlencode($city).'&key='.$appKey;
- $result = file_get_contents($url);
- $result = json_decode($result, true);
- if($result['error_code'] == '0') {
- //将数据缓存到redis里
- $today = date('Y-m-d',time());
- $tomorrow = date('Y-m-d',strtotime('+1 days'));
- $weather = '';
- foreach ($result['result']['future'] as $value) {
- if($value['date'] == $today) {
- $weather .= $today.'日气温:'.$value['temperature'].'; ';
- }
- if($value['date'] == $tomorrow) {
- $weather .= $tomorrow.'日气温:'.$value['temperature'];
- }
- }
- RedisModel::set($redisKey.'_'.$city, $weather);
- $endTime = strtotime(date('Y-m-d 00:00:00',strtotime('+1 days'))) -1;
- $expire = $endTime - time();
- RedisModel::expire($redisKey.'_'.$city, $expire);
- $sellerMemo = Order::where('id', $order_id)->pluck('sellerMemo');
- if(strstr($sellerMemo, $city.'天气状况:'.$weather)) {
- return true;
- }
- $sellerMemo = $sellerMemo.' '.$city.'天气状况:'.$weather;
- Log::errorLog(['order_id'=>$order_id, 'city'=> $city , 'app_key'=>$appKey],['weather'=>$weather],"order/weather",1,'');
- Order::where('id', $order_id)->update(['sellerMemo'=>$sellerMemo]);
- } else if($result['error_code'] == '10012'){
- //判断是否为第二个APPkey也用完了
- $key = (string) RedisModel::get('seafood_order_city_weather_key');
- if($key == '0') {
- //超过100次/天上限 更换APPKey
- RedisModel::set('seafood_order_city_weather_key', '1');
- $endTime = strtotime(date('Y-m-d 00:00:00',strtotime('+1 days'))) -1;
- $expire = $endTime - time();
- RedisModel::expire('seafood_order_city_weather_key', $expire);
- self::updateWeather($order_id);
- }
- }
- }
- return true;
- }
- public static function getAppKey() {
- $redisKey = 'seafood_order_city_weather_key';
- $appKeyArr = ['32c8f844f8055f70f702fc28ec930e52', '85bfc19adb25998b2559d7ceba950e13'];
- $appKey = (string)RedisModel::get($redisKey);
- if($appKey != '') {
- return $appKeyArr[$appKey];
- } else {
- RedisModel::set($redisKey, '0');
- $endTime = strtotime(date('Y-m-d 00:00:00',strtotime('+1 days'))) -1;
- $expire = $endTime - time();
- RedisModel::expire($redisKey, $expire);
- return $appKeyArr['0'];
- }
- }
- }
|