| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395 |
- <?php
- namespace app\console\Commands;
- use app\common\facades\EasyWeChat;
- use app\common\facades\Setting;
- use app\common\models\AccountWechats;
- use app\common\models\goods\GoodsFiltering;
- use app\common\models\Member;
- use app\common\models\notice\MessageTemp;
- use app\common\models\TemplateMessageRecord;
- use app\common\services\MessageService;
- use app\platform\modules\system\models\SystemSetting;
- use GuzzleHttp\Client;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Facades\DB;
- use Yunshop\Commission\models\Commission;
- use Yunshop\Integral\Common\Models\IntegralGoodsModel;
- use Yunshop\YzSupply\models\Goods;
- use Illuminate\Console\Command;
- class MsgGoodsPrice extends Command
- {
- const HOST = 'http://110.42.254.179/OpenPlatForm/openapi';
- const ACCOUNT = 'gbcszh_adm';
- const ACTION = 'sendbatch';
- const FORMAT = 'json';
- const AUTH_KEY = 'e10adc3949ba59abbe56e057f20f883e';
- const AUTH_NAME = '【世纪汇杰】';
- protected $signature = 'msg:goodsPrice {--phone=13453837482}';
- protected $pageSize = 2000;
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '商品价格异常,发送短信';
- /**
- * Execute the console command.
- *
- */
- /*public function handle()
- {
- $p = $this->option("phone");
- $templates = $this->makeMsgTemplate($this->getGoodsList());
- $query = [
- 'account' => self::ACCOUNT,
- 'p' => $p,
- 's' => time(),
- 'action' => self::ACTION,
- 'format' => self::FORMAT,
- 'authkey' => self::AUTH_KEY,
- ];
- $client = new Client();
- foreach ($templates as $template) {
- $query['c'] = self::AUTH_NAME . $template;
- $url = self::HOST . '?' . $this->build_query($query, "&");
- \Log::info($url);
- $client->get($url);
- }
- $this->output->success('发送短信成功');
- }*/
- public function handle(){
- $send_type=2;
- $p = $this->option("phone");
- $templates = $this->makeMsgTemplate($this->getGoodsList());
- if($send_type==1){
- if (!app('plugins')->isEnabled('elisoftsms')) {
- throw new \Exception('未开启验证码,请联系管理员!');
- }
- $setting = SystemSetting::settingLoad('sms', 'system_sms');
- $sms = ['elisoft_account'=>'hxn_adm',
- 'elisoft_password'=>'5404392e586ea34541c62db58694eb00',
- 'elisoft_signname'=>'【世纪汇杰】'];//Setting::get('shop.sms');
- $account = $sms['elisoft_account'];
- $passward = $sms['elisoft_password'];
- $signname=$sms['elisoft_signname'];
- if($setting['interior_sms_type']==1){
- $sms = new \Yunshop\Elisoftsms\services\ElisoftSmsService($account,$passward);
- foreach ($templates as $template) {
- $msg=$signname.$template;
- $param = [
- 'c' => urlencode($msg),
- 'p' => $p,
- ];
- $res = $sms->setAction('sendbatch')->send($param);
- if($res==null){
- throw new \Exception('请检查后台短信配置');
- }
- if ($res['result'] != 1) {
- throw new \Exception($res['errormsg']);
- }else{
- $this->output->success('发送短信成功');
- }
- }
- }else{
- $query = [
- 'uid'=>$setting['klws_account'],
- 'pw'=>md5($setting['klws_password'].date('YmdHis')),
- 'mb'=>$p,
- 'tm'=>date('YmdHis'),
- ];
- $url="http://3qdx.com:18002/send.do";
- $client = new Client();
- foreach ($templates as $template) {
- $query['ms'] = self::AUTH_NAME . $template;
- $url = $url . '?' . $this->build_query($query, "&");
- \Log::info($url);
- $client->get($url);
- }
- $this->output->success('发送短信成功');
- }
- }else{
- $params = [
- ['value' => '商品通知'],
- ['value'=>'2024-02-06 12:13:42'],
- ['value' => '紧急'],
- ];
- $temp=DB::table('yz_message_template')->where(['template_id'=>'B9273ptcxVk6hDSoj_Z9g7mb6t0Llr_QfR9JNjm9MTw'])->first();
- $templates = [];
- $temp_data=json_decode($temp['data'],true);
- foreach ($temp_data as $k=> $row) {
- $templates[$row['keywords']] = [
- 'value' => $params[$k]['value'],
- 'color' => $row['color']
- ];
- }
- dump($templates);exit;
- $app=$this->wxNoticeSend('q3s8oLEhl8agyTspBf8eJTGfFqW3OpKVxvyo_NuMQfI', $templates, [1270],'', 4);
- }
- }
- private static function replaceTemplate($str, $datas = array())
- {
- $str='';
- foreach ($datas as $row ) {
- $str = str_replace('[' . $row['name'] . ']', $row['value'], $str);
- }
- return $str;
- }
- /**
- * @param $data
- * @param string $separator
- * @return string
- */
- private function build_query($data, string $separator): string
- {
- $string = '';
- foreach ($data as $key => $item) {
- $string .= "{$key}={$item}" . $separator;
- }
- return rtrim($string, $separator) ;
- }
- private function getGoodsList(): array
- {
- $requestSearch = [];
- /*$list = Goods::Search($requestSearch)->with(['hasOneSmallCodeUrl', 'hasManyOptions', 'hasManyGoodsDiscount', 'hasOneYzGoods' => function ($query) {
- $query->with('hasOneMiddlegroundConfiguration');
- }])->pluginId()->orderBy('display_order', 'desc')->orderBy('yz_goods.id', 'desc');
- if (!empty($requestSearch['middleground_configuration_id'])) {
- $list = $list->whereHas('hasOneYzGoods', function ($query) use ($requestSearch) {
- $query->where('middleground_configuration_id', $requestSearch['middleground_configuration_id']);
- });
- }*/
- $list=DB::table('yz_goods')
- ->select('yz_goods.id','yz_goods.status','yz_goods.price','yz_goods.cost_price','yz_goods_option.id as goods_option_id','yz_goods_option.title as goods_option_title','yz_goods_option.product_price as goods_option_product_price','yz_goods_option.cost_price as goods_option_cost_price','yz_goods_option.market_price as goods_option_market_price',
- 'yz_community_marketing_goods.is_open as qtj_status','yz_community_marketing_goods.alone_rule','yz_community_marketing_goods.alone_rule_rate','yz_community_marketing_goods.cashier_rate',
- 'yz_goods_sale.max_point_deduct')
- ->leftJoin('yz_goods_option','yz_goods_option.goods_id','=','yz_goods.id')
- ->leftJoin('yz_community_marketing_goods','yz_community_marketing_goods.goods_id','=','yz_goods.id')
- ->leftJoin('yz_goods_sale','yz_goods_sale.goods_id','=','yz_goods.id')
- ->where(['yz_goods.status'=>1,'yz_goods.uniacid'=>4])
- ->whereNull('yz_goods.deleted_at');
- $result = [];
- while (($data = $list->paginate($this->pageSize)) && $data->currentPage() <= $data->lastPage()) {
- $result[] = $this->processData($data->items());
- $currentPage = $data->currentPage();
- request()->offsetSet('page', ++$currentPage);
- }
- return Arr::collapse($result);
- }
- /**
- * 处理数据
- * @param $data
- * @return array
- */
- private function processData($data): array
- {
- $alert = [];
- $deduction_set=0;
- $deduction_set_max='0';
- foreach ($data as $l) {
- $price=$l['goods_option_product_price']?$l['goods_option_product_price']:$l['price'];
- $cost_price=$l['goods_option_cost_price']?$l['goods_option_cost_price']:$l['cost_price'];
- $goods_discount=DB::table('yz_goods_discount')->select('discount_method','level_id','discount_value')->where(['goods_id'=>$l['id']])->get();
- $max_member_price='0';
- $integralData=IntegralGoodsModel::uniacid()->select('deduction_set','deduction_set_max')->where('goods_id',$l['id'])->first();
- if($integralData){
- $integralData=$integralData->toArray();
- if($integralData['deduction_set']==1){
- $deduction_set=$integralData['deduction_set'];
- $deduction_set_max=$integralData['deduction_set_max'];
- }else{
- $deduction_set=\Setting::get('integral.deduction_set');
- $deduction_set_max=\Setting::get('integral.deduction_set_max');
- }
- }
- foreach ($goods_discount as $v => $d){
- if($d['discount_value']){
- if($d['discount_method']==1){
- //$value=$d['discount_value'].'折';
- //$member_price=$price-($price*$d['discount_value']/10);
- $member_yh_price=$price-$price*$d['discount_value']/10;
- }elseif ($d['discount_method']==2){
- //$value=$d['discount_value'].'元';
- //$member_price=$price-$d['discount_value'];
- $member_yh_price=$d['discount_value'];
- }elseif ($d['discount_method']==3){
- //$value=$d['discount_value'].'%';
- //$member_price=$price-($cost_price*$d['discount_value']/100);
- $member_yh_price=$cost_price*$d['discount_value']/100;
- }
- if($max_member_price<$member_yh_price){
- $max_member_price=$member_yh_price;
- }
- /*$member_hyj_lr=$member_price-$cost_price;
- $member_hyj_lrl=round(($member_price-$cost_price)*100/$cost_price.'%',2);
- $arr[]=$level_name.':'.$value;
- $arr1[]=$level_name.":".$member_price.'元';
- $arr2[]=$level_name.":".$member_yh_price.'元';
- $arr3[]=$level_name.":".$member_hyj_lr.'元';
- $arr4[]=$level_name.":".$member_hyj_lrl.'%';*/
- }
- }
- $qyjyh=$price*$l['alone_rule_rate']/100;
- if (strpos($l['max_point_deduct'], '%') !== false) {
- $jfzgdkje=$price*$l['max_point_deduct']/100;
- }else{
- $jfzgdkje=$l['max_point_deduct'];
- }
- $commission_goods = Commission::getGoodsSet($l['id'])->toArray();
- $commission_goods_rule=unserialize($commission_goods['rule']);
- $commission_first_arr1=$commission_first_arr2=$commission_first_arr3=$commission_first_arr4=[];
- $commission_second_arr1=$commission_second_arr2=$commission_second_arr3=$commission_second_arr4=[];
- // $commission_third_arr1=$commission_third_arr2=$commission_third_arr3=$commission_third_arr4=[];
- $max_commission_first_price=$max_first_rate='0';
- $max_commission_second_price=$max_second_rate='0';
- foreach ($commission_goods_rule as $cc){
- // $levelName=$cc['levelName'];
- if($max_first_rate <$cc['first_level_rate']){
- $max_first_rate=$cc['first_level_rate'];
- }
- if($max_second_rate <$cc['second_level_rate']){
- $max_second_rate=$cc['first_level_rate'];
- }
- /*if(isset($cc['first_level_rate'])&&$cc['first_level_rate']>0){
- $first_level_rate=$cc['first_level_rate'];
- $commission_first_arr1[]=$levelName.':'.$first_level_rate.'%';
- $commission_first_arr2[]=$levelName.':'.$first_level_rate/100*$price.'元';
- $commission_first_arr3[]=$levelName.':'.($price-($first_level_rate/100*$price)-$cost_price).'元';
- $commission_first_arr4[]=$levelName.':'.round(($price-($first_level_rate/100*$cost_price)-$cost_price)/$cost_price,2).'%';
- }
- if(isset($cc['second_level_rate'])&&$cc['second_level_rate']>0){
- $second_level_rate=$cc['second_level_rate'];
- $commission_second_arr1[]=$levelName.':'.$second_level_rate.'%';
- $commission_second_arr2[]=$levelName.':'.$second_level_rate/100*$price.'元';
- $commission_second_arr3[]=$levelName.':'.($price-($second_level_rate/100*$price)-$cost_price).'元';
- $commission_second_arr4[]=$levelName.':'.round(($price-($second_level_rate/100*$cost_price)-$cost_price)/$cost_price,2).'%';
- }*/
- /* if(isset($cc['third_level_rate'])&&$cc['third_level_rate']>0){
- $third_level_rate=$cc['third_level_rate'];
- $commission_third_arr1[]=$levelName.':'.$third_level_rate.'%';
- $commission_third_arr2[]=$levelName.':'.$third_level_rate/100*$price.'元';
- $commission_third_arr3[]=$levelName.':'.($price-($third_level_rate/100*$price)-$cost_price).'元';
- $commission_third_arr4[]=$levelName.':'.round(($price-($third_level_rate/100*$cost_price)-$cost_price)/$cost_price,2).'%';
- }*/
- }
- $max_commission_first_price=$max_first_rate/100*$price;
- $max_commission_second_price=$max_second_rate/100*$price;
- $max_price_arr=[$max_member_price,$jfzgdkje];
- if($l['qtj_status']==1&&$l['alone_rule']==1){
- array_push($max_price_arr,$qyjyh);
- }
- if($deduction_set==1){
- array_push($max_price_arr,$price*$deduction_set_max/100);
- }
- if($commission_goods['is_commission']==1&& $commission_goods['has_commission']==1){
- array_push($max_price_arr,$max_commission_first_price,$max_commission_second_price);
- }
- $max_price=array_sum($max_price_arr);
- $record = [
- 'goods_id' => $l['id'],
- 'goods_option_id'=>$l['goods_option_id'],
- 'product_price' => $price,
- 'cost_price' => $cost_price,
- 'max_price'=>$max_price
- ];
- $sf_price=$price-$max_price;
- if ($record['product_price'] <= $record['cost_price']||$sf_price<=$record['cost_price']) {
- DB::table('yz_goods')->where('id',$record['goods_id'])->update(['status'=>0]);
- $alert[] = $record;
- }
- }
- return $alert;
- }
- /**
- * 生成短信模板
- * @param array $data
- * @return array
- */
- private function makeMsgTemplate(array $data): array
- {
- // $template = '商品id:1,现价:122,会员:100,VIP:82,成本:90,VIP会员利润报警';
- \Log::info($data);
- $templates = [];
- foreach (array_chunk($data, 4) as $chunk) {
- $template = '下架异常商品:';
- foreach ($chunk as $item) {
- if($item['goods_option_id']){
- $template .= sprintf('商品id:%d,商品规格id:%d,商品规格现价:%.2F,峰值金额:%.2f,商品规格成本:%.2f,利润报警|', $item['goods_id'],$item['goods_option_id'], $item['product_price'],$item['max_price'], $item['cost_price']);
- }else{
- $template .= sprintf('商品id:%d,商品现价:%.2F,峰值金额:%.2f,成本:%.2f,利润报警|', $item['goods_id'], $item['product_price'],$item['max_price'], $item['cost_price']);
- }
- }
- $templates[] = rtrim($template, "|");
- }
- return $templates;
- }
- private function wxNoticeSend($temp_id, array $params,array $member_ids, $url='', $uniacid='')
- {
- if ($uniacid) {
- \Setting::$uniqueAccountId = \YunShop::app()->uniacid = $uniacid;
- } else{
- \Setting::$uniqueAccountId = \YunShop::app()->uniacid;
- }
- foreach ($member_ids as $member_id){
- $member = Member::whereUid($member_id)->first();
- if (!isset($member)) {
- \Log::error("微信消息推送失败,未找到uid:{$member_id}的用户");
- return false;
- }
- if (!$member->isFollow()) {
- return false;
- }
- $accountWechat = AccountWechats::getAccountByUniacid(\YunShop::app()->uniacid);
- if (!isset($accountWechat)) {
- \Log::error("微信消息推送:未找到uniacid:{$uniacid}的配置信息");
- return false;
- }
- $config=['app_id' => $accountWechat->key, 'secret' => $accountWechat->secret];
- try {
- $app = EasyWeChat::officialAccount($config);
- $userService = $app->user;
- $user = $userService->get($member->hasOneFans->openid);
- if ($user['subscribe'] != 1 && $user->subscribe != 1) {
- \Log::debug('微信消息推送:用户未关注公众号',$user);
- return false;
- }
- $app->template_message->send([
- 'touser' => $member->hasOneFans->openid,
- 'template_id' => $temp_id,
- 'url' => $url,
- 'data' => $params,
- ]);
- } catch (Exception $error) {
- TemplateMessageRecord::create([
- 'uniacid' => \YunShop::app()->uniacid,
- 'member_id' => $member_id,
- 'template_id' => $temp_id,
- 'url' => $url?:'无',
- 'openid' => $member->hasOneFans->openid ?: 0,
- 'data' => json_encode($params),
- 'send_time' => time(),
- 'status' => -1,
- 'extend_data' => $error->getMessage(),
- ]);
- return true;
- }
- }
- }
- }
|