MerchantRepository.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author xaboy
  6. * @day 2020-04-16
  7. *
  8. *
  9. */
  10. namespace app\common\repositories\system\merchant;
  11. use app\common\dao\system\merchant\MerchantDao;
  12. use app\common\model\store\product\ProductReply;
  13. use app\common\repositories\BaseRepository;
  14. use app\common\repositories\store\product\ProductCopyRepository;
  15. use app\common\repositories\store\product\ProductRepository;
  16. use app\common\repositories\store\shipping\ShippingTemplateRepository;
  17. use app\common\repositories\store\StoreCategoryRepository;
  18. use app\common\repositories\system\attachment\AttachmentRepository;
  19. use app\common\repositories\user\UserRelationRepository;
  20. use app\common\repositories\user\UserVisitRepository;
  21. use app\common\repositories\wechat\RoutineQrcodeRepository;
  22. use app\controller\admin\system\merchant\MerchantIntention;
  23. use app\controller\merchant\gong\Goods;
  24. use app\traits\HaikeApiRequest;
  25. use crmeb\services\QrcodeService;
  26. use crmeb\services\UploadService;
  27. use FormBuilder\Exception\FormBuilderException;
  28. use FormBuilder\Factory\Elm;
  29. use FormBuilder\Form;
  30. use think\db\exception\DataNotFoundException;
  31. use think\db\exception\DbException;
  32. use think\db\exception\ModelNotFoundException;
  33. use think\Exception;
  34. use think\exception\ValidateException;
  35. use think\facade\Db;
  36. use think\facade\Log;
  37. use think\facade\Route;
  38. /**
  39. * Class MerchantRepository
  40. * @package app\common\repositories\system\merchant
  41. * @mixin MerchantDao
  42. * @author xaboy
  43. * @day 2020-04-16
  44. */
  45. class MerchantRepository extends BaseRepository
  46. {
  47. use HaikeApiRequest;
  48. /**
  49. * MerchantRepository constructor.
  50. * @param MerchantDao $dao
  51. */
  52. public function __construct(MerchantDao $dao)
  53. {
  54. $this->dao = $dao;
  55. }
  56. /**
  57. * @param array $where
  58. * @param $page
  59. * @param $limit
  60. * @return array
  61. * @throws DataNotFoundException
  62. * @throws DbException
  63. * @throws ModelNotFoundException
  64. * @author xaboy
  65. * @day 2020-04-16
  66. */
  67. public function lst(array $where, $page, $limit)
  68. {
  69. if (!empty($where["city"])){
  70. $city_id=Db::name("user_city")->where("city_name",$where["city"])->value("city_id");
  71. $where['city_id'] = $city_id;
  72. unset($where["city"]);
  73. }
  74. $query = $this->dao->search($where);
  75. $query = $query->with(['merchantCategory','spread']);
  76. $count = $query->count($this->dao->getPk());
  77. $list = $query->page($page, $limit)
  78. ->setOption('field', [])
  79. ->field('mer_id,merch_no, mer_name, real_name,name, mer_phone, mer_address, mark, status, create_time,is_best,recommender,category_id,type,withdrawal_setting,dc_id,merchant_level')
  80. ->select();
  81. foreach ($list as &$item) {
  82. if (!$item->withdrawal_setting && $item->merch_no != '') {
  83. try {
  84. $response = $this->merchantInfo(['merch_no' => $item->merch_no]);
  85. $item->withdrawal_setting = $response['pay_product_info']['settlement_cycle'];
  86. $item->save();
  87. } catch (\Throwable $e) {
  88. }
  89. }
  90. if($item['dc_id'] == 0){
  91. $item['dc'] = '无';
  92. }else{
  93. $item['dc'] = Db::name('big_warehouse') -> where('id',$item['dc_id']) -> value('warehouse_name');
  94. }
  95. $item['category_id']=(int)$item['category_id'];
  96. $item['status']=(int)$item['status'];
  97. $item['is_best']=(int)$item['is_best'];
  98. }
  99. unset($item);
  100. $merdata= [
  101. ['className'=> 'el-icon-s-order', 'count'=>$count, 'field'=> '个', 'name'=> '商户总数']
  102. ];
  103. return compact('count', 'list','merdata');
  104. }
  105. /**
  106. * @param array $where
  107. * @param $page
  108. * @param $limit
  109. * @return array
  110. * @throws DataNotFoundException
  111. * @throws DbException
  112. * @throws ModelNotFoundException
  113. */
  114. public function lstLeft(array $where, $page, $limit)
  115. {
  116. if (!empty($where["city"])){
  117. $city_id=Db::name("user_city")->where("city_name",$where["city"])->value("city_id");
  118. $where['city_id'] = $city_id;
  119. unset($where["city"]);
  120. }
  121. $query = $this->dao->searchLeft($where);
  122. $query = $query->with(['merchantCategory','spread']);
  123. $query = $query->withJoin(['MerchantIntention']);
  124. // $count = $query->count($this->dao->getPk());
  125. $count = $query->count('merchant.mer_id');
  126. $list = $query->page($page, $limit)
  127. ->setOption('field', [])
  128. ->field('merchant.mer_id,merchant.merch_no, merchant.mer_name, merchant.real_name,merchant.name, merchant.mer_phone, merchant.mer_address, merchant.mark, merchant.status, merchant.create_time,merchant.is_best,merchant.recommender,merchant.category_id,merchant.type,merchant.withdrawal_setting,merchant.dc_id,merchant.merchant_level')
  129. ->select();
  130. foreach ($list as &$item) {
  131. if (!$item->withdrawal_setting && $item->merch_no != '') {
  132. try {
  133. $response = $this->merchantInfo(['merch_no' => $item->merch_no]);
  134. $item->withdrawal_setting = $response['pay_product_info']['settlement_cycle'];
  135. $item->save();
  136. } catch (\Throwable $e) {
  137. }
  138. }
  139. if($item['dc_id'] == 0){
  140. $item['dc'] = '无';
  141. }else{
  142. $item['dc'] = Db::name('big_warehouse') -> where('id',$item['dc_id']) -> value('warehouse_name');
  143. }
  144. $item['category_id']=(int)$item['category_id'];
  145. $item['status']=(int)$item['status'];
  146. $item['is_best']=(int)$item['is_best'];
  147. }
  148. unset($item);
  149. $merdata= [
  150. ['className'=> 'el-icon-s-order', 'count'=>$count, 'field'=> '个', 'name'=> '商户总数']
  151. ];
  152. return compact('count', 'list','merdata');
  153. }
  154. /**
  155. * @param int|null $id
  156. * @param array $formData
  157. * @return Form
  158. * @throws FormBuilderException
  159. * @author xaboy
  160. * @day 2020-04-16
  161. */
  162. public function form(?int $id = null, array $formData = [])
  163. {
  164. $form = Elm::createForm(is_null($id) ? Route::buildUrl('systemMerchantCreate')->build() : Route::buildUrl('systemMerchantUpdate_1', ['id' => $id])->build());
  165. /** @var MerchantCategoryRepository $make */
  166. $make = app()->make(MerchantCategoryRepository::class);
  167. // $config = systemConfig(['broadcast_room_type', 'broadcast_goods_type']);
  168. $formData['category_id']=(int)$formData['category_id'];
  169. $formData['status']=(int)$formData['status'];
  170. $formData['is_audit']=(int)$formData['is_audit'];
  171. $formData['is_best']=(int)$formData['is_best'];
  172. $rule = [
  173. Elm::input('mer_name', '商户名称')->required(),
  174. // Elm::select('mer_type', '一壶茶会员专区商户')->options(function () {
  175. // return array(
  176. // array('value'=>'0','label'=>'普通商户'),
  177. // array('value'=>'1','label'=>'一壶茶会员专区商户'),
  178. // );
  179. // })->required(),
  180. Elm::select('category_id', '商户分类')->options(function () use ($make) {
  181. $data = $make->allOptions();
  182. $options = [];
  183. foreach ($data as $value => $label) {
  184. $options[] = compact('value', 'label');
  185. }
  186. return $options;
  187. })->requiredNum(),
  188. Elm::input('mer_account', '商户账号')->required()->disabled(!is_null($id))->required(!is_null($id)),
  189. Elm::password('mer_password', '登录密码')->required()->disabled(!is_null($id))->required(!is_null($id)),
  190. Elm::input('real_name', '商户姓名'),
  191. Elm::input('mer_phone', '商户手机号')->col(12),
  192. // Elm::number('commission_rate', '手续费(%)')->col(12),
  193. Elm::input('mer_keyword', '商户关键字')->col(12),
  194. Elm::input('mer_address', '商户地址'),
  195. Elm::input('lng', '经度')->col(12),
  196. Elm::input('lat', '纬度')->col(12),
  197. Elm::textarea('mark', '备注'),
  198. Elm::number('sort', '排序', 0),
  199. Elm::switches('status', '是否开启', 1)->activeValue(1)->inactiveValue(0)->inactiveText('关闭')->activeText('开启')->col(12),
  200. //Elm::switches('is_bro_room', '直播间审核', $config['broadcast_room_type'] == 1 ? 0 : 1)->activeValue(1)->inactiveValue(0)->inactiveText('关闭')->activeText('开启')->col(12),
  201. Elm::switches('is_audit', '产品审核', 1)->activeValue(1)->inactiveValue(0)->inactiveText('关闭')->activeText('开启')->col(12),
  202. //Elm::switches('is_bro_goods', '直播间商品审核', $config['broadcast_goods_type'] == 1 ? 0 : 1)->activeValue(1)->inactiveValue(0)->inactiveText('关闭')->activeText('开启')->col(12),
  203. Elm::switches('is_best', '是否推荐')->activeValue(1)->inactiveValue(0)->inactiveText('关闭')->activeText('开启'),
  204. ];
  205. $form->setRule($rule);
  206. return $form->setTitle(is_null($id) ? '添加商户' : '编辑商户')->formData($formData);
  207. }
  208. /**
  209. * @param array $formData
  210. * @return Form
  211. * @throws FormBuilderException
  212. * @author xaboy
  213. * @day 2020/6/25
  214. */
  215. public function merchantForm(array $formData = [])
  216. {
  217. if(isset($formData['mer_state'])){$formData['mer_state']=(int)$formData['mer_state'];}
  218. $form = Elm::createForm(Route::buildUrl('merchantUpdate')->build());
  219. $rule = [
  220. Elm::input('mer_phone', '手机号')->readonly(true),
  221. Elm::input('email', '邮箱')->readonly(true),
  222. Elm::input('product_score', '商品描述评分')->readonly(true),
  223. Elm::input('service_score', '服务评分')->readonly(true),
  224. Elm::input('postage_score', '物流评分')->readonly(true),
  225. Elm::input('care_count', '关注总数')->readonly(true),
  226. Elm::input('bank_location', '开户所在地')->readonly(true),
  227. Elm::input('bank_deposit', '开户行')->readonly(true),
  228. Elm::input('bank_branch', '开户支行')->readonly(true),
  229. Elm::input('bank_account', '开户账号')->readonly(true),
  230. Elm::input('open_account', '开户许可证')->readonly(true),
  231. // Elm::input('district_addr', '商圈')->required(),
  232. Elm::textarea('mer_info', '店铺简介')->required(),
  233. Elm::input('service_phone', '服务电话')->required(),
  234. Elm::frameImage('mer_banner', '店铺 banner', '/' . config('admin.merchant_prefix') . '/setting/uploadPicture?field=mer_banner&type=1')->modal(['modal' => false])->width('896px')->height('480px')->props(['footer' => false]),
  235. Elm::frameImage('mer_avatar', '店铺头像', '/' . config('admin.merchant_prefix') . '/setting/uploadPicture?field=mer_avatar&type=1')->modal(['modal' => false])->width('896px')->height('480px')->props(['footer' => false]),
  236. Elm::switches('mer_state', '是否开启', 1)->activeValue(1)->inactiveValue(0)->inactiveText('关闭')->activeText('开启')->col(12),
  237. ];
  238. $form->setRule($rule);
  239. return $form->setTitle('编辑店铺信息')->formData($formData);
  240. }
  241. /**
  242. * @param $id
  243. * @return Form
  244. * @throws FormBuilderException
  245. * @throws DataNotFoundException
  246. * @throws DbException
  247. * @throws ModelNotFoundException
  248. * @author xaboy
  249. * @day 2020-04-16
  250. */
  251. public function updateForm($id)
  252. {
  253. $data = $this->dao->get($id)->toArray();
  254. /** @var MerchantAdminRepository $make */
  255. $make = app()->make(MerchantAdminRepository::class);
  256. $data['mer_account'] = $make->merIdByAccount($id);
  257. $data['mer_password'] = '***********';
  258. return $this->form($id, $data);
  259. }
  260. /**
  261. * @param array $data
  262. * @author xaboy
  263. * @day 2020-04-17
  264. */
  265. public function createMerchant(array $data)
  266. {
  267. /** @var MerchantAdminRepository $make */
  268. $make = app()->make(MerchantAdminRepository::class);
  269. Db::transaction(function () use ($data, $make) {
  270. $account = $data['mer_account'];
  271. $password = $data['mer_password'];
  272. unset($data['mer_account'], $data['mer_password']);
  273. $merchant = $this->dao->create($data);
  274. $make->createMerchantAccount($merchant, $account, $password);
  275. app()->make(ShippingTemplateRepository::class)->createDefault($merchant->mer_id);
  276. app()->make(ProductCopyRepository::class)->defaulCopyNum($merchant->mer_id);
  277. });
  278. }
  279. /**
  280. * @Author:Qinii
  281. * @Date: 2020/5/30
  282. * @param $where
  283. * @param $page
  284. * @param $limit
  285. * @return array
  286. */
  287. public function getList($where, $page, $limit, $userInfo)
  288. {
  289. $where['status'] = 1;
  290. $where['mer_state'] = 1;
  291. if ($userInfo && $where['keyword'] !== '')
  292. app()->make(UserVisitRepository::class)->searchMerchant($userInfo['uid'], $where['keyword']);
  293. if($where['category_id'] == 0) {
  294. unset($where['category_id']);
  295. $query = $this->dao->search($where)->with(['showProduct']);
  296. $query = $query->order("create_time desc")->limit("10");
  297. }else{
  298. $query = $this->dao->search($where)->with(['showProduct']);
  299. }
  300. $count = $query->count($this->dao->getPk());
  301. $list = $query->page($page, $limit)->setOption('field', [])
  302. ->field('lat,lng,addr,mer_id,mer_banner,mer_name, mark,mer_avatar,product_score,service_score,postage_score,sales,status,create_time,category_id')
  303. ->select()
  304. ->each(function ($item) {
  305. $data = $item->showProduct->toArray();
  306. unset($item->showProduct);
  307. $recommend = array_slice($data, 0, 3);
  308. return $item['recommend'] = $recommend;
  309. });
  310. return compact('count', 'list');
  311. }
  312. /**
  313. * @Author:Qinii
  314. * @Date: 2020/5/30
  315. * @param int $id
  316. * @return array|\think\Model|null
  317. */
  318. public function merExists(int $id)
  319. {
  320. return ($this->dao->get($id));
  321. }
  322. /**
  323. * @Author:Qinii
  324. * @Date: 2020/5/30
  325. * @param $id
  326. * @param $userInfo
  327. * @return array|\think\Model|null
  328. */
  329. public function detail($id, $userInfo, $lat='', $lng='')
  330. {
  331. $merchant = $this->dao->apiGetOne($id)->hidden(["real_name", "mer_phone", "reg_admin_id", "sort", "is_del", "is_audit", "is_best", "mer_state", "bank", "bank_number", "bank_name", 'update_time']);
  332. $merchant['care'] = false;
  333. $merchant['like']=false;
  334. $merchant['topping']=false;
  335. if ($userInfo){
  336. $like=Db::name('store_fabulous')->where(['uid'=>$userInfo->uid,'mer_id'=>$id])->value('status');
  337. if($like==1){
  338. $like=true;
  339. }else{
  340. $like=false;
  341. }
  342. if($userInfo->mer_topping==$id){
  343. $topping=true;
  344. }else{
  345. $topping=false;
  346. }
  347. $merchant['care'] = $this->getCareByUser($id, $userInfo->uid);
  348. $merchant['like']=$like;
  349. $merchant['topping']=$topping;
  350. }
  351. // Db::name("merchant")->where("mer_id",$cartInfo['mer_id'])->inc('renqizhi', 1)
  352. $this->dao->addrenqi($id);
  353. if($merchant['content_pic']){
  354. $merchant['content_pic'] = explode(',',$merchant['content_pic']);
  355. }
  356. $lat=empty($lat)?"34.73841":$lat;
  357. $lng=empty($lng)?"113.643636":$lng;
  358. $field = 'ACOS(SIN(('.$lat.' * 3.1415) / 180 ) *SIN((lat * 3.1415) / 180 ) +COS(('.$lat.' * 3.1415) / 180 )
  359. * COS((lat * 3.1415) / 180 ) *COS(('.$lng.' * 3.1415) / 180 - (lng * 3.1415) / 180 ) ) * 6380 as distance';
  360. $distance =Db::name("merchant")
  361. ->where("mer_id",$id)
  362. ->field($field)
  363. ->find();
  364. $merchant['distance'] =sprintf("%.2f",$distance['distance']);
  365. return $merchant;
  366. }
  367. /**
  368. * @Author:Qinii
  369. * @Date: 2020/5/30
  370. * @param int $merId
  371. * @param int $userId
  372. * @return bool
  373. */
  374. public function getCareByUser(int $merId, int $userId)
  375. {
  376. if (app()->make(UserRelationRepository::class)->getWhere(['type' => 10, 'type_id' => $merId, 'uid' => $userId]))
  377. return true;
  378. return false;
  379. }
  380. /**
  381. * @Author:Qinii
  382. * @Date: 2020/5/30
  383. * @param $merId
  384. * @param $where
  385. * @param $page
  386. * @param $limit
  387. * @param $userInfo
  388. * @return mixed
  389. */
  390. public function productList($merId, $where, $page, $limit, $userInfo,$type439=false)
  391. {
  392. log::info("========j进入prodctList");
  393. log::info($merId);
  394. log::info( $where);
  395. // log::info($userInfo);
  396. log::info($type439);
  397. $list=app()->make(ProductRepository::class)->getApiSearch($merId, $where, $page, $limit, $userInfo,$type439);
  398. $arr=[];
  399. foreach ($list["list"] as $v){
  400. $k= app()->make(ProductRepository::class)->pvParm($v['product_id']);
  401. // $v['special_merchant']= merchantConfig($v['mer_id'], 'special_merchant');
  402. log::info("==========yang= {$k['yanglao']} ");
  403. $v['yanglao']=($k['yanglao']);
  404. $v['pv']=($k['pv']);
  405. $v['jifen']=($k['jifen']);
  406. $v['gongxian']=($k['gongxian']);
  407. $v['jfduihuan']=($k['jfduihuan']);
  408. $v['jdduihuan']=($k['jdduihuan']);
  409. // $v=($v,$k);
  410. // array_push($v,$k);
  411. $arr[]=$v;
  412. }
  413. $list["list"]=$arr;
  414. return $list;
  415. }
  416. public function productList_yhc($merId, $where, $page, $limit, $userInfo)
  417. {
  418. unset($where['order']);
  419. if (isset($where['price_on']) && $where['price_on'] != '')$where['price'] = [$where['price_asc'], $where['price_desc']];
  420. if (isset($where['brand_id']) && $where['brand_id'] != '') $where['brand_id'] = explode(',', $where['brand_id']);
  421. unset($where['price_on'], $where['price_off']);
  422. //搜索记录
  423. if ($userInfo && isset($where['keyword']) && !empty($where['keyword']))
  424. app()->make(UserVisitRepository::class)->searchProduct($userInfo['uid'], $where['keyword']);
  425. $query = Db::name('store_product')->where('product_id','>',0)->where('mer_id', $merId)->where('type', 17);
  426. $keyArray = $whereArr = [];
  427. unset($where['type']);
  428. foreach ($where as $key => $item) {
  429. if ($item !== '' && $key != 'soft') {
  430. $keyArray[] = $key;
  431. $whereArr[$key] = $item;
  432. }
  433. }
  434. unset($whereArr['order']);
  435. $query->withSearch($keyArray, $whereArr)->where('is_show',1)->where('status',1);
  436. $query->when(isset($where['keyword']) && $where['keyword'] != '', function ($query) use ($where) {
  437. $query->whereLike('store_name', "%{$where['keyword']}%");
  438. unset($where['keyword']);
  439. });
  440. $query->order('sort DESC ,create_time DESC');
  441. $query->with(['merchant'])->with(['issetCoupon']);
  442. $count = $query->count();
  443. // echo DB::getlastsql();die;
  444. $filed = 'product_id,mer_id,brand_id,unit_name,rate,reply_count,store_info,cate_id,ficti,image,slider_image,store_name,keyword,sort,is_show,sales,price,refusal,cost,ot_price,stock,is_gift_bag,care_count,status,is_used,create_time,seckill,plate_mer_profit,type';
  445. $list = $query->page($page, $limit)->setOption('field', [])->field($filed)->select();
  446. $ProductRepository=app()->make(ProductRepository::class);
  447. $list->each(function ($item) use ($ProductRepository) {
  448. $item['sales'] = $item['sales'] + $item['ficti'];
  449. unset($item['ficti']);
  450. $item['yanglao'] = $ProductRepository->yanglaojin($item['product_id']);
  451. return $item;
  452. });
  453. // if ($this->getUserIsPromoter($userInfo)) $list = $list->each(function ($item) {
  454. // return $item['max_extension'] = $item->max_extension;
  455. // });
  456. $list= compact('count', 'list');
  457. $arr=[];
  458. foreach ($list["list"] as $v){
  459. $v['yanglao']= app()->make(ProductRepository::class)->yanglaojin($v['product_id']);
  460. $v['special_merchant']= merchantConfig($v['mer_id'], 'special_merchant');
  461. $arr[]=$v;
  462. }
  463. $list["list"]=$arr;
  464. return $list;
  465. }
  466. /**
  467. * @Author:Qinii
  468. * @Date: 2020/5/30
  469. * @param int $id
  470. * @return mixed
  471. */
  472. public function categoryList(int $id)
  473. {
  474. return app()->make(StoreCategoryRepository::class)->getApiFormatList($id, 1);
  475. }
  476. public function offlineUserQrcode($merId, $userId,$queryString = '/h5payment/index.html', $imgName = '_store_user_offline_wap.jpg')
  477. {
  478. $siteUrl = systemConfig('site_url');
  479. $name = $merId . $userId . $imgName;
  480. $attachmentRepository = app()->make(AttachmentRepository::class);
  481. $imageInfo = $attachmentRepository->getWhere(['attachment_name' => $name]);
  482. if (isset($imageInfo['attachment_src']) && strstr($imageInfo['attachment_src'], 'http') !== false && curl_file_exist($imageInfo['attachment_src']) === false) {
  483. $imageInfo->delete();
  484. $imageInfo = null;
  485. }
  486. if (!$imageInfo) {
  487. $codeUrl = set_http_type(rtrim($siteUrl, '/') . $queryString.'?id=' . $merId . '&user_id=' . $userId, request()->isSsl() ? 0 : 1);//二维码链接
  488. $imageInfo = app()->make(QrcodeService::class)->getQRCodePath($codeUrl, $name);
  489. if (is_string($imageInfo)) throw new ValidateException('二维码生成失败');
  490. $imageInfo['dir'] = path_to_url($imageInfo['dir']);
  491. if (strpos($imageInfo['dir'], 'http') === false) $imageInfo['dir'] = request()->domain() . $imageInfo['dir'];
  492. $attachmentRepository->create(systemConfig('upload_type') ?: 1, -2, $merId, [
  493. 'attachment_category_id' => 0,
  494. 'attachment_name' => $imageInfo['name'],
  495. 'attachment_src' => $imageInfo['dir']
  496. ]);
  497. $urlCode = $imageInfo['dir'];
  498. } else $urlCode = $imageInfo['attachment_src'];
  499. return $urlCode;
  500. }
  501. public function wxQrcode($merId, $queryString = '/pages/store/home/index', $imgName = '_store_wap.jpg', $avatar = '')
  502. {
  503. $siteUrl = systemConfig('site_url');
  504. $name = $merId . $imgName;
  505. $attachmentRepository = app()->make(AttachmentRepository::class);
  506. $imageInfo = $attachmentRepository->getWhere(['attachment_name' => $name]);
  507. if (isset($imageInfo['attachment_src']) && strstr($imageInfo['attachment_src'], 'http') !== false && curl_file_exist($imageInfo['attachment_src']) === false) {
  508. $imageInfo->delete();
  509. $imageInfo = null;
  510. }
  511. if (!$imageInfo) {
  512. $codeUrl = set_http_type(rtrim($siteUrl, '/') . $queryString.'?id=' . $merId, request()->isSsl() ? 0 : 1);//二维码链接
  513. $imageInfo = app()->make(QrcodeService::class)->getQRCodePath($codeUrl, $name, $avatar);
  514. if (is_string($imageInfo)) throw new ValidateException('二维码生成失败');
  515. $imageInfo['dir'] = path_to_url($imageInfo['dir']);
  516. if (strpos($imageInfo['dir'], 'http') === false) $imageInfo['dir'] = request()->domain() . $imageInfo['dir'];
  517. $attachmentRepository->create(systemConfig('upload_type') ?: 1, -2, $merId, [
  518. 'attachment_category_id' => 0,
  519. 'attachment_name' => $imageInfo['name'],
  520. 'attachment_src' => $imageInfo['dir']
  521. ]);
  522. $urlCode = $imageInfo['dir'];
  523. } else $urlCode = $imageInfo['attachment_src'];
  524. return $urlCode;
  525. }
  526. public function routineQrcode($merId, $queryString = '/pages/store/home/index', $imgName = '_store_routine.jpg')
  527. {
  528. $name = $merId . $imgName;
  529. $attachmentRepository = app()->make(AttachmentRepository::class);
  530. $imageInfo = $attachmentRepository->getWhere(['attachment_name' => $name]);
  531. if (isset($imageInfo['attachment_src']) && strstr($imageInfo['attachment_src'], 'http') !== false && curl_file_exist($imageInfo['attachment_src']) === false) {
  532. $imageInfo->delete();
  533. $imageInfo = null;
  534. }
  535. if (!$imageInfo) {
  536. $uploadType = (int)systemConfig('upload_type') ?: 1;
  537. $routineQrcodeRepository = app()->make(RoutineQrcodeRepository::class);
  538. $res = $routineQrcodeRepository->getShareCode($merId, 'store', $queryString.'?id=' . $merId, '');
  539. if (!$res) throw new ValidateException('二维码生成失败');
  540. $upload = UploadService::create($uploadType);
  541. $uploadRes = $upload->to('routine/spread/code')->validate()->stream($res['res'], $name);
  542. if ($uploadRes === false) {
  543. throw new ValidateException($upload->getError());
  544. }
  545. $imageInfo = $upload->getUploadInfo();
  546. $imageInfo['image_type'] = $uploadType;
  547. $imageInfo['dir'] = path_to_url($imageInfo['dir']);
  548. if (strpos($imageInfo['dir'], 'http') === false) $imageInfo['dir'] = request()->domain() . $imageInfo['dir'];
  549. $attachmentRepository->create($uploadType, -2, $merId, [
  550. 'attachment_category_id' => 0,
  551. 'attachment_name' => $imageInfo['name'],
  552. 'attachment_src' => $imageInfo['dir']
  553. ]);
  554. $routineQrcodeRepository->setRoutineQrcodeFind($res['id'], ['status' => 1, 'url_time' => date('Y-m-d H:i:s'), 'qrcode_url' => $imageInfo['dir']]);
  555. $urlCode = $imageInfo['dir'];
  556. } else $urlCode = $imageInfo['attachment_src'];
  557. return $urlCode;
  558. }
  559. public function copyForm(int $id)
  560. {
  561. $form = Elm::createForm(Route::buildUrl('systemMerchantChangeCopy', ['id' => $id])->build());
  562. $form->setRule([
  563. Elm::radio('type', '修改类型', 1)
  564. ->setOptions([
  565. ['value' => 1, 'label' => '增加'],
  566. ['value' => 2, 'label' => '减少'],
  567. ]),
  568. Elm::number('num', '修改数量', $this->dao->getCopyNum($id))->required()
  569. ]);
  570. return $form->setTitle('修改复制商品次数');
  571. }
  572. /**
  573. * @Author:Qinii
  574. * @Date: 2020/10/10
  575. * @param $where
  576. * @param $page
  577. * @param $limit
  578. * @return array
  579. */
  580. public function getLists($data, $page, $limit, $type=1,$where=[])
  581. {
  582. $where['status'] = 1;
  583. $where['mer_state'] = 1;
  584. $field = 'ACOS(SIN(('.$data["lat"].' * 3.1415) / 180 ) *SIN((lat * 3.1415) / 180 ) +COS(('.$data["lat"].' * 3.1415) / 180 )
  585. * COS((lat * 3.1415) / 180 ) *COS(('.$data["lng"].' * 3.1415) / 180 - (lng * 3.1415) / 180 ) ) * 6380 as distance';
  586. $fields =$field.',mer_id,mer_name,mark,mer_avatar,status,create_time,category_id,district_addr,care_count,renqizhi,sales';
  587. if (!empty($data["city_name"])){
  588. $city_id=Db::name("user_city")->where("city_name",$data["city_name"])->value("city_id");
  589. $where['city_id'] = $city_id;
  590. }
  591. if ($type==1) {
  592. if ( empty($where['category_id'])){
  593. $order = "mer_id desc";
  594. }else{
  595. $where['is_best'] = 1;
  596. $order = "mer_id desc,distance asc";
  597. }
  598. }else if($type==2){
  599. if ($data["order"]==1){
  600. $order = "distance asc";
  601. }else{
  602. $order = ['care_count'=>'desc'];
  603. }
  604. }
  605. $query = $this->dao->search($where);
  606. $count = $query->count($this->dao->getPk());
  607. $list =Db::name("merchant")
  608. ->where($where)
  609. ->page($page, $limit)
  610. ->field($fields)
  611. // ->having('distance <= '.'10000')
  612. ->order($order)
  613. ->select()
  614. ->each(function ($item) {
  615. $item['label'] ="人气";
  616. // $item['sales'] =$item["renqizhi"];
  617. $item['distance'] =sprintf("%.2f",$item['distance']);
  618. $item['classify'] =Db::name('merchant_category')->where("merchant_category_id",$item["category_id"])->value("category_name");
  619. // var_dump(merchantConfig($item['mer_id'], 'printing_client_id'));exit();
  620. // $yanglao=merchantConfig($item['mer_id'], 'printing_client_id')/100;
  621. $base_commission=merchantConfig($item['mer_id'], 'base_commission');
  622. if($base_commission <= 0){
  623. $base_commission= systemConfig("extension_one_rate");
  624. }
  625. //商户养老金比例
  626. $yanglao= floatval(merchantConfig($item['mer_id'], 'yanglaojin'));
  627. if(!$yanglao){
  628. //若商户养老金比例不存在 则使用平台配置
  629. $yanglao = systemConfig('yanglaojin_admin');
  630. }
  631. $item['yanglao'] = bcmul($base_commission, $yanglao ,2) * 0.01;
  632. return $item;
  633. })->toArray();
  634. $counts=count($list);
  635. if( $counts<10 && $type==1){
  636. $ids=[];
  637. foreach($list as $v){
  638. $ids[]=$v["mer_id"];
  639. }
  640. $city_id=$where["city_id"];
  641. unset($where["city_id"]);
  642. $lists =Db::name("merchant")
  643. ->where($where)
  644. ->where("city_id","<>",$city_id)
  645. ->where("mer_id","<>",implode(",",$ids))
  646. ->page(1, 10-$counts)
  647. ->field($fields)
  648. ->order("distance asc")
  649. ->select()
  650. ->each(function ($item) {
  651. $item['label'] ="人气";
  652. // $item['sales'] =$item["renqizhi"];
  653. $item['distance'] =sprintf("%.2f",$item['distance']);
  654. $item['classify'] =Db::name('merchant_category')->where("merchant_category_id",$item["category_id"])->value("category_name");
  655. $base_commission=merchantConfig($item['mer_id'], 'base_commission');
  656. if($base_commission <= 0){
  657. $base_commission= systemConfig("extension_one_rate");
  658. }
  659. //商户养老金比例
  660. $yanglao= floatval(merchantConfig($item['mer_id'], 'yanglaojin'));
  661. if(!$yanglao){
  662. //若商户养老金比例不存在 则使用平台配置
  663. $yanglao = systemConfig('yanglaojin_admin');
  664. }
  665. $item['yanglao'] = bcmul($base_commission, $yanglao ,2) * 0.01;
  666. return $item;
  667. })->toArray();
  668. $list=array_merge($list,$lists);
  669. }
  670. return compact('count', 'list','where');
  671. }
  672. /**
  673. * @Date: 2020/10/31
  674. * @param $where
  675. * @param $page
  676. * @param $limit
  677. * @return array
  678. * @author cabbage
  679. */
  680. public function getLst($data, $page, $limit,$where=[])
  681. {
  682. //下过单的店铺
  683. $mer_ids="";
  684. if (!empty($data["user_id"])){
  685. $mer_ids=Db::name('store_order')->where("uid",$data["user_id"])->order("order_id desc")->column("mer_id");
  686. if ($mer_ids){
  687. $mer_ids=array_unique($mer_ids);
  688. }
  689. }
  690. $where['status'] = 1;
  691. $where['mer_state'] = 1;
  692. $where['sxy_submerchant_id'] = 1;
  693. $field = '';
  694. if(!empty($data['lat']) && !empty($data['lng'])){
  695. $field = 'ACOS(SIN(('.$data["lat"].' * 3.1415) / 180 ) *SIN((lat * 3.1415) / 180 ) +COS(('.$data["lat"].' * 3.1415) / 180 )
  696. * COS((lat * 3.1415) / 180 ) *COS(('.$data["lng"].' * 3.1415) / 180 - (lng * 3.1415) / 180 ) ) * 6380 as distance,';
  697. }else{
  698. $field = "mer_id as distance";
  699. }
  700. $fields =$field.'mer_id,mer_name,mark,mer_avatar,sales,status,create_time,category_id,care_count,sales,district_addr,renqizhi';
  701. if ($mer_ids){
  702. $order = [
  703. "FIELD (id,{$mer_ids})"=>'desc',
  704. 'sales'=>'desc', //销量
  705. 'care_count'=>'desc', //关注
  706. 'distance'=>"asc"//距离
  707. ];
  708. }else{
  709. $order = [
  710. 'distance'=>"asc",//距离
  711. 'sales'=>'desc', //销量
  712. 'care_count'=>'desc', //关注
  713. ];
  714. }
  715. if($data["order"]==1){
  716. $order = [
  717. 'distance'=>"asc",//距离
  718. 'sales'=>'asc',
  719. 'care_count'=>'asc',
  720. ];
  721. }
  722. $data['distance'] = ($data['distance']&&$data['distance']!=50)?$data['distance']:8000;
  723. $query = $this->dao->search_mer($where);
  724. $count = $query->count($this->dao->getPk());
  725. $list = $query
  726. ->page($page, $limit)
  727. ->setOption('field', [])
  728. ->field($fields)
  729. ->having('distance <= '.$data['distance'])
  730. ->order($order)
  731. ->select()
  732. ->each(function ($item) {
  733. $item['popularity'] =$item["renqizhi"];
  734. $item['sales'] =$item["sales"];
  735. $item['distance'] =sprintf("%.2f",$item['distance']);
  736. if ( $item['distance']<=10){
  737. $item['label'] ="附近";
  738. }else{
  739. $item['label'] ="附近";
  740. }
  741. $item['classify'] =Db::name('merchant_category')->where("merchant_category_id",$item["category_id"])->value("category_name");
  742. $base_commission=merchantConfig($item['mer_id'], 'base_commission');
  743. if($base_commission <= 0){
  744. $base_commission= systemConfig("extension_one_rate");
  745. }
  746. //商户养老金比例
  747. $yanglao= floatval(merchantConfig($item['mer_id'], 'yanglaojin'));
  748. if(!$yanglao){
  749. //若商户养老金比例不存在 则使用平台配置
  750. $yanglao = systemConfig('yanglaojin_admin');
  751. }
  752. $item['yanglao'] = bcmul($base_commission, $yanglao ,2) * 0.01;
  753. return $item;
  754. });
  755. return compact('count', 'list','order','where');
  756. }
  757. /**
  758. *
  759. * @param $page
  760. * @param $limit
  761. * @return array
  762. */
  763. public function merchantcategory()
  764. {
  765. return Db::name('merchant_category')->where("status",1)->order("order_sort desc,merchant_category_id desc")->select()->toArray();
  766. }
  767. /*商家分类列表*/
  768. public function mercat($page, $limit)
  769. {
  770. $count= Db::name('merchant_category')->count();
  771. $list= Db::name('merchant_category')->where("status",1)->order("order_sort desc,merchant_category_id desc")->select()->toArray();
  772. return compact('count', 'list');
  773. }
  774. /**
  775. * @param $id
  776. * @return Form
  777. * @throws FormBuilderException
  778. * @author xaboy
  779. * @day 2020-05-07
  780. */
  781. public function changeProForm($id)
  782. {
  783. return Elm::createForm(Route::buildUrl('systemMerchantChangePro', compact('id'))->build(), [
  784. Elm::input('spread_account', '推荐人')->required()->value("请填写推荐人手机号")
  785. ])->setTitle('修改商户推荐人');
  786. }
  787. /*
  788. * 修改商户推广人id
  789. *@author cabbage
  790. *@date 2020-10-31
  791. * */
  792. public function update_spread($id,$spread_account){
  793. //推荐人信息
  794. $spread = Db::name("user")->where('account',$spread_account)->value("uid");
  795. $mer_uid = Db::name("merchant")->where(['mer_id'=>$id])->value("uid");
  796. if($spread && $mer_uid){
  797. try{
  798. Db::name("user")->where(["uid"=>$mer_uid])->update(['spread_uid'=>$spread]);
  799. Db::name("merchant")->where(["mer_id"=>$id])->update(['recommender'=>$spread]);
  800. return true;
  801. }catch (Exception $e) {
  802. Db::rollback();
  803. }
  804. }else{
  805. return false;
  806. }
  807. }
  808. /*下架商品*/
  809. public function MerUpdate($where,$data)
  810. {
  811. return Db::name("store_product")->where($where)->update($data);
  812. }
  813. /*推荐商品*/
  814. public function getProductGoodList($id){
  815. $where['is_gift_bag'] = 0;
  816. $where['is_used'] = 1;
  817. $where['product_type'] = 0;
  818. $where['mer_id'] = $id;
  819. $where['is_good'] = 1;
  820. return Db::name("store_product")->where($where)->field("image,product_id,store_name as title,price,ot_price")->select();
  821. }
  822. /*收款二维码*/
  823. public function getQrCode($mer){
  824. $res = Db::name("MerchantQrcode")
  825. -> where("mer_id",$mer -> mer_id)
  826. -> field("qrcode_src as url,show_src")
  827. -> find();
  828. if(!$res || $res['url'] == '' || $res['show_src'] == ''){
  829. Db::name("MerchantQrcode") -> where("mer_id",$mer -> mer_id) -> delete();
  830. $attachmentRepository = app()->make(AttachmentRepository::class);
  831. $siteUrl = systemConfig('site_url');
  832. $merchantIntertion = app() -> make(MerchantIntention::class);
  833. $id = $merchantIntertion -> getNewQrcodeId();
  834. $name = "offlinepay_".$id.".jpg";
  835. $codeUrl = set_http_type(rtrim($siteUrl, '/') .'/offlinepay/index.html?id=' . $id, request()->isSsl() ? 0 : 1);//二维码链接
  836. $imageInfo = app()->make(QrcodeService::class)->getQRCodePath($codeUrl, $name);
  837. if (!is_string($imageInfo)){
  838. $imageInfo['dir'] = path_to_url($imageInfo['dir']);
  839. if (strpos($imageInfo['dir'], 'http') === false) $imageInfo['dir'] = request()->domain() . $imageInfo['dir'];
  840. $attachmentRepository->create(systemConfig('upload_type') ?: 1, 0, 0, [
  841. 'attachment_category_id' => 0,
  842. 'attachment_name' => $imageInfo['name'],
  843. 'attachment_src' => $imageInfo['dir']
  844. ]);
  845. $show_src = $merchantIntertion -> show_src($imageInfo['dir'],$id);
  846. $codeInsert = [
  847. 'uid'=>$mer -> uid,
  848. 'mer_id' =>$mer -> mer_id,
  849. 'code_id'=>$id,
  850. 'qrcode_src' => $imageInfo['dir'],
  851. 'show_src' => $show_src,
  852. ];
  853. Db::name('MerchantQrcode') -> insert($codeInsert);
  854. return ['url'=>$imageInfo['dir'],'show_src'=>$show_src];
  855. }
  856. throw new ValidateException('收款码生成失败');
  857. }
  858. return $res;
  859. }
  860. }