MerchantRepository.php 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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']='';
  404. $v['pv']='';
  405. $v['jifen']='';
  406. $v['gongxian']='';
  407. $v['jfduihuan']='';
  408. $v['jdduihuan']='';
  409. //会员专区中VR或者现金和VR+现金形式
  410. //支付组合
  411. $v['fzone_paytype'] = '';
  412. //$v['vr'] = 0;
  413. $num_yl = 0;
  414. if($v['price'] == '1180'){
  415. $num_yl = 1000;
  416. }else if($v['price'] == '10620'){
  417. $num_yl = 9000;
  418. }else if($v['price'] == '11800'){
  419. $num_yl = 10000;
  420. }else if($v['price'] == '2360'){
  421. $num_yl = 2000;
  422. }else if($v['price'] == '9440'){
  423. $num_yl = 8000;
  424. }
  425. if($merId == 496){
  426. $v['yanglao'] = round($num_yl * 0.035, 2);
  427. $v['pv']=$num_yl *0.7;
  428. //$v['vr']=$v['price'];
  429. //1:为会员专区,
  430. $v['fzone_paytype']= [
  431. 1=>[
  432. 1=>$v['price'],//VR
  433. 2=>$v['price']//现金
  434. ]
  435. ];
  436. }else if($merId == 439){
  437. //精彩生活和商贸区
  438. $jf = $v['price'] - round($v['concession_pri']*0.3, 2) . ' + ' . round($v['concession_pri']*0.3, 2);
  439. $jd = $v['price'] - round($v['concession_pri']*0.35, 2) . ' + ' . round($v['concession_pri']*0.35, 2);
  440. if($v['concession_pri'] !=null || $v['concession_pri'] !=0){
  441. $v['yanglao'] = round($v['concession_pri'] * 0.3, 2);
  442. $v['pv'] = round($v['concession_pri'] * 0.35, 2);
  443. $v['jfduihuan'] =$jf;
  444. $v['jdduihuan'] = $jd;
  445. $v['jifen'] = round($v['concession_pri'] * 0.35, 2);
  446. $v['gongxian'] = round($v['concession_pri'] * 0.35, 2);
  447. }
  448. }else if($merId == 491){
  449. $num_yl = $v['price'];
  450. $v['yanglao'] = round($num_yl * 0.035, 2);
  451. $v['pv']=$num_yl *0.7;
  452. $v['fzone_paytype']= [
  453. 1=>[
  454. 1=>$v['price'],//VR
  455. 2=>$v['price']//现金
  456. ]
  457. ];
  458. }
  459. // $v=($v,$k);
  460. // array_push($v,$k);
  461. $arr[]=$v;
  462. }
  463. $list["merId"]=$merId;
  464. $list["list"]=$arr;
  465. return $list;
  466. }
  467. public function productList_yhc($merId, $where, $page, $limit, $userInfo)
  468. {
  469. unset($where['order']);
  470. if (isset($where['price_on']) && $where['price_on'] != '')$where['price'] = [$where['price_asc'], $where['price_desc']];
  471. if (isset($where['brand_id']) && $where['brand_id'] != '') $where['brand_id'] = explode(',', $where['brand_id']);
  472. unset($where['price_on'], $where['price_off']);
  473. //搜索记录
  474. if ($userInfo && isset($where['keyword']) && !empty($where['keyword']))
  475. app()->make(UserVisitRepository::class)->searchProduct($userInfo['uid'], $where['keyword']);
  476. $query = Db::name('store_product')->where('product_id','>',0)->where('mer_id', $merId)->where('type', 17);
  477. $keyArray = $whereArr = [];
  478. unset($where['type']);
  479. foreach ($where as $key => $item) {
  480. if ($item !== '' && $key != 'soft') {
  481. $keyArray[] = $key;
  482. $whereArr[$key] = $item;
  483. }
  484. }
  485. unset($whereArr['order']);
  486. $query->withSearch($keyArray, $whereArr)->where('is_show',1)->where('status',1);
  487. $query->when(isset($where['keyword']) && $where['keyword'] != '', function ($query) use ($where) {
  488. $query->whereLike('store_name', "%{$where['keyword']}%");
  489. unset($where['keyword']);
  490. });
  491. $query->order('sort DESC ,create_time DESC');
  492. $query->with(['merchant'])->with(['issetCoupon']);
  493. $count = $query->count();
  494. // echo DB::getlastsql();die;
  495. $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';
  496. $list = $query->page($page, $limit)->setOption('field', [])->field($filed)->select();
  497. $ProductRepository=app()->make(ProductRepository::class);
  498. $list->each(function ($item) use ($ProductRepository) {
  499. $item['sales'] = $item['sales'] + $item['ficti'];
  500. unset($item['ficti']);
  501. $item['yanglao'] = $ProductRepository->yanglaojin($item['product_id']);
  502. return $item;
  503. });
  504. // if ($this->getUserIsPromoter($userInfo)) $list = $list->each(function ($item) {
  505. // return $item['max_extension'] = $item->max_extension;
  506. // });
  507. $list= compact('count', 'list');
  508. $arr=[];
  509. foreach ($list["list"] as $v){
  510. $v['yanglao']= app()->make(ProductRepository::class)->yanglaojin($v['product_id']);
  511. $v['special_merchant']= merchantConfig($v['mer_id'], 'special_merchant');
  512. $arr[]=$v;
  513. }
  514. $list["list"]=$arr;
  515. return $list;
  516. }
  517. /**
  518. * @Author:Qinii
  519. * @Date: 2020/5/30
  520. * @param int $id
  521. * @return mixed
  522. */
  523. public function categoryList(int $id)
  524. {
  525. return app()->make(StoreCategoryRepository::class)->getApiFormatList($id, 1);
  526. }
  527. public function offlineUserQrcode($merId, $userId,$queryString = '/h5payment/index.html', $imgName = '_store_user_offline_wap.jpg')
  528. {
  529. $siteUrl = systemConfig('site_url');
  530. $name = $merId . $userId . $imgName;
  531. $attachmentRepository = app()->make(AttachmentRepository::class);
  532. $imageInfo = $attachmentRepository->getWhere(['attachment_name' => $name]);
  533. if (isset($imageInfo['attachment_src']) && strstr($imageInfo['attachment_src'], 'http') !== false && curl_file_exist($imageInfo['attachment_src']) === false) {
  534. $imageInfo->delete();
  535. $imageInfo = null;
  536. }
  537. if (!$imageInfo) {
  538. $codeUrl = set_http_type(rtrim($siteUrl, '/') . $queryString.'?id=' . $merId . '&user_id=' . $userId, request()->isSsl() ? 0 : 1);//二维码链接
  539. $imageInfo = app()->make(QrcodeService::class)->getQRCodePath($codeUrl, $name);
  540. if (is_string($imageInfo)) throw new ValidateException('二维码生成失败');
  541. $imageInfo['dir'] = path_to_url($imageInfo['dir']);
  542. if (strpos($imageInfo['dir'], 'http') === false) $imageInfo['dir'] = request()->domain() . $imageInfo['dir'];
  543. $attachmentRepository->create(systemConfig('upload_type') ?: 1, -2, $merId, [
  544. 'attachment_category_id' => 0,
  545. 'attachment_name' => $imageInfo['name'],
  546. 'attachment_src' => $imageInfo['dir']
  547. ]);
  548. $urlCode = $imageInfo['dir'];
  549. } else $urlCode = $imageInfo['attachment_src'];
  550. return $urlCode;
  551. }
  552. public function wxQrcode($merId, $queryString = '/pages/store/home/index', $imgName = '_store_wap.jpg', $avatar = '')
  553. {
  554. $siteUrl = systemConfig('site_url');
  555. $name = $merId . $imgName;
  556. $attachmentRepository = app()->make(AttachmentRepository::class);
  557. $imageInfo = $attachmentRepository->getWhere(['attachment_name' => $name]);
  558. if (isset($imageInfo['attachment_src']) && strstr($imageInfo['attachment_src'], 'http') !== false && curl_file_exist($imageInfo['attachment_src']) === false) {
  559. $imageInfo->delete();
  560. $imageInfo = null;
  561. }
  562. if (!$imageInfo) {
  563. $codeUrl = set_http_type(rtrim($siteUrl, '/') . $queryString.'?id=' . $merId, request()->isSsl() ? 0 : 1);//二维码链接
  564. $imageInfo = app()->make(QrcodeService::class)->getQRCodePath($codeUrl, $name, $avatar);
  565. if (is_string($imageInfo)) throw new ValidateException('二维码生成失败');
  566. $imageInfo['dir'] = path_to_url($imageInfo['dir']);
  567. if (strpos($imageInfo['dir'], 'http') === false) $imageInfo['dir'] = request()->domain() . $imageInfo['dir'];
  568. $attachmentRepository->create(systemConfig('upload_type') ?: 1, -2, $merId, [
  569. 'attachment_category_id' => 0,
  570. 'attachment_name' => $imageInfo['name'],
  571. 'attachment_src' => $imageInfo['dir']
  572. ]);
  573. $urlCode = $imageInfo['dir'];
  574. } else $urlCode = $imageInfo['attachment_src'];
  575. return $urlCode;
  576. }
  577. public function routineQrcode($merId, $queryString = '/pages/store/home/index', $imgName = '_store_routine.jpg')
  578. {
  579. $name = $merId . $imgName;
  580. $attachmentRepository = app()->make(AttachmentRepository::class);
  581. $imageInfo = $attachmentRepository->getWhere(['attachment_name' => $name]);
  582. if (isset($imageInfo['attachment_src']) && strstr($imageInfo['attachment_src'], 'http') !== false && curl_file_exist($imageInfo['attachment_src']) === false) {
  583. $imageInfo->delete();
  584. $imageInfo = null;
  585. }
  586. if (!$imageInfo) {
  587. $uploadType = (int)systemConfig('upload_type') ?: 1;
  588. $routineQrcodeRepository = app()->make(RoutineQrcodeRepository::class);
  589. $res = $routineQrcodeRepository->getShareCode($merId, 'store', $queryString.'?id=' . $merId, '');
  590. if (!$res) throw new ValidateException('二维码生成失败');
  591. $upload = UploadService::create($uploadType);
  592. $uploadRes = $upload->to('routine/spread/code')->validate()->stream($res['res'], $name);
  593. if ($uploadRes === false) {
  594. throw new ValidateException($upload->getError());
  595. }
  596. $imageInfo = $upload->getUploadInfo();
  597. $imageInfo['image_type'] = $uploadType;
  598. $imageInfo['dir'] = path_to_url($imageInfo['dir']);
  599. if (strpos($imageInfo['dir'], 'http') === false) $imageInfo['dir'] = request()->domain() . $imageInfo['dir'];
  600. $attachmentRepository->create($uploadType, -2, $merId, [
  601. 'attachment_category_id' => 0,
  602. 'attachment_name' => $imageInfo['name'],
  603. 'attachment_src' => $imageInfo['dir']
  604. ]);
  605. $routineQrcodeRepository->setRoutineQrcodeFind($res['id'], ['status' => 1, 'url_time' => date('Y-m-d H:i:s'), 'qrcode_url' => $imageInfo['dir']]);
  606. $urlCode = $imageInfo['dir'];
  607. } else $urlCode = $imageInfo['attachment_src'];
  608. return $urlCode;
  609. }
  610. public function copyForm(int $id)
  611. {
  612. $form = Elm::createForm(Route::buildUrl('systemMerchantChangeCopy', ['id' => $id])->build());
  613. $form->setRule([
  614. Elm::radio('type', '修改类型', 1)
  615. ->setOptions([
  616. ['value' => 1, 'label' => '增加'],
  617. ['value' => 2, 'label' => '减少'],
  618. ]),
  619. Elm::number('num', '修改数量', $this->dao->getCopyNum($id))->required()
  620. ]);
  621. return $form->setTitle('修改复制商品次数');
  622. }
  623. /**
  624. * @Author:Qinii
  625. * @Date: 2020/10/10
  626. * @param $where
  627. * @param $page
  628. * @param $limit
  629. * @return array
  630. */
  631. public function getLists($data, $page, $limit, $type=1,$where=[])
  632. {
  633. $where['status'] = 1;
  634. $where['mer_state'] = 1;
  635. $field = 'ACOS(SIN(('.$data["lat"].' * 3.1415) / 180 ) *SIN((lat * 3.1415) / 180 ) +COS(('.$data["lat"].' * 3.1415) / 180 )
  636. * COS((lat * 3.1415) / 180 ) *COS(('.$data["lng"].' * 3.1415) / 180 - (lng * 3.1415) / 180 ) ) * 6380 as distance';
  637. $fields =$field.',mer_id,mer_name,mark,mer_avatar,status,create_time,category_id,district_addr,care_count,renqizhi,sales';
  638. if (!empty($data["city_name"])){
  639. $city_id=Db::name("user_city")->where("city_name",$data["city_name"])->value("city_id");
  640. $where['city_id'] = $city_id;
  641. }
  642. if ($type==1) {
  643. if ( empty($where['category_id'])){
  644. $order = "mer_id desc";
  645. }else{
  646. $where['is_best'] = 1;
  647. $order = "mer_id desc,distance asc";
  648. }
  649. }else if($type==2){
  650. if ($data["order"]==1){
  651. $order = "distance asc";
  652. }else{
  653. $order = ['care_count'=>'desc'];
  654. }
  655. }
  656. $query = $this->dao->search($where);
  657. $count = $query->count($this->dao->getPk());
  658. $list =Db::name("merchant")
  659. ->where($where)
  660. ->page($page, $limit)
  661. ->field($fields)
  662. // ->having('distance <= '.'10000')
  663. ->order($order)
  664. ->select()
  665. ->each(function ($item) {
  666. $item['label'] ="人气";
  667. // $item['sales'] =$item["renqizhi"];
  668. $item['distance'] =sprintf("%.2f",$item['distance']);
  669. $item['classify'] =Db::name('merchant_category')->where("merchant_category_id",$item["category_id"])->value("category_name");
  670. // var_dump(merchantConfig($item['mer_id'], 'printing_client_id'));exit();
  671. // $yanglao=merchantConfig($item['mer_id'], 'printing_client_id')/100;
  672. $base_commission=merchantConfig($item['mer_id'], 'base_commission');
  673. if($base_commission <= 0){
  674. $base_commission= systemConfig("extension_one_rate");
  675. }
  676. //商户养老金比例
  677. $yanglao= floatval(merchantConfig($item['mer_id'], 'yanglaojin'));
  678. if(!$yanglao){
  679. //若商户养老金比例不存在 则使用平台配置
  680. $yanglao = systemConfig('yanglaojin_admin');
  681. }
  682. $item['yanglao'] = bcmul($base_commission, $yanglao ,2) * 0.01;
  683. return $item;
  684. })->toArray();
  685. $counts=count($list);
  686. if( $counts<10 && $type==1){
  687. $ids=[];
  688. foreach($list as $v){
  689. $ids[]=$v["mer_id"];
  690. }
  691. $city_id=$where["city_id"];
  692. unset($where["city_id"]);
  693. $lists =Db::name("merchant")
  694. ->where($where)
  695. ->where("city_id","<>",$city_id)
  696. ->where("mer_id","<>",implode(",",$ids))
  697. ->page(1, 10-$counts)
  698. ->field($fields)
  699. ->order("distance asc")
  700. ->select()
  701. ->each(function ($item) {
  702. $item['label'] ="人气";
  703. // $item['sales'] =$item["renqizhi"];
  704. $item['distance'] =sprintf("%.2f",$item['distance']);
  705. $item['classify'] =Db::name('merchant_category')->where("merchant_category_id",$item["category_id"])->value("category_name");
  706. $base_commission=merchantConfig($item['mer_id'], 'base_commission');
  707. if($base_commission <= 0){
  708. $base_commission= systemConfig("extension_one_rate");
  709. }
  710. //商户养老金比例
  711. $yanglao= floatval(merchantConfig($item['mer_id'], 'yanglaojin'));
  712. if(!$yanglao){
  713. //若商户养老金比例不存在 则使用平台配置
  714. $yanglao = systemConfig('yanglaojin_admin');
  715. }
  716. $item['yanglao'] = bcmul($base_commission, $yanglao ,2) * 0.01;
  717. return $item;
  718. })->toArray();
  719. $list=array_merge($list,$lists);
  720. }
  721. return compact('count', 'list','where');
  722. }
  723. /**
  724. * @Date: 2020/10/31
  725. * @param $where
  726. * @param $page
  727. * @param $limit
  728. * @return array
  729. * @author cabbage
  730. */
  731. public function getLst($data, $page, $limit,$where=[])
  732. {
  733. //下过单的店铺
  734. $mer_ids="";
  735. if (!empty($data["user_id"])){
  736. $mer_ids=Db::name('store_order')->where("uid",$data["user_id"])->order("order_id desc")->column("mer_id");
  737. if ($mer_ids){
  738. $mer_ids=array_unique($mer_ids);
  739. }
  740. }
  741. $where['status'] = 1;
  742. $where['mer_state'] = 1;
  743. $where['sxy_submerchant_id'] = 1;
  744. $field = '';
  745. if(!empty($data['lat']) && !empty($data['lng'])){
  746. $field = 'ACOS(SIN(('.$data["lat"].' * 3.1415) / 180 ) *SIN((lat * 3.1415) / 180 ) +COS(('.$data["lat"].' * 3.1415) / 180 )
  747. * COS((lat * 3.1415) / 180 ) *COS(('.$data["lng"].' * 3.1415) / 180 - (lng * 3.1415) / 180 ) ) * 6380 as distance,';
  748. }else{
  749. $field = "mer_id as distance";
  750. }
  751. $fields =$field.'mer_id,mer_name,mark,mer_avatar,sales,status,create_time,category_id,care_count,sales,district_addr,renqizhi';
  752. if ($mer_ids){
  753. $order = [
  754. "FIELD (id,{$mer_ids})"=>'desc',
  755. 'sales'=>'desc', //销量
  756. 'care_count'=>'desc', //关注
  757. 'distance'=>"asc"//距离
  758. ];
  759. }else{
  760. $order = [
  761. 'distance'=>"asc",//距离
  762. 'sales'=>'desc', //销量
  763. 'care_count'=>'desc', //关注
  764. ];
  765. }
  766. if($data["order"]==1){
  767. $order = [
  768. 'distance'=>"asc",//距离
  769. 'sales'=>'asc',
  770. 'care_count'=>'asc',
  771. ];
  772. }
  773. $data['distance'] = ($data['distance']&&$data['distance']!=50)?$data['distance']:8000;
  774. $query = $this->dao->search_mer($where);
  775. $count = $query->count($this->dao->getPk());
  776. $list = $query
  777. ->page($page, $limit)
  778. ->setOption('field', [])
  779. ->field($fields)
  780. ->having('distance <= '.$data['distance'])
  781. ->order($order)
  782. ->select()
  783. ->each(function ($item) {
  784. $item['popularity'] =$item["renqizhi"];
  785. $item['sales'] =$item["sales"];
  786. $item['distance'] =sprintf("%.2f",$item['distance']);
  787. if ( $item['distance']<=10){
  788. $item['label'] ="附近";
  789. }else{
  790. $item['label'] ="附近";
  791. }
  792. $item['classify'] =Db::name('merchant_category')->where("merchant_category_id",$item["category_id"])->value("category_name");
  793. $base_commission=merchantConfig($item['mer_id'], 'base_commission');
  794. if($base_commission <= 0){
  795. $base_commission= systemConfig("extension_one_rate");
  796. }
  797. //商户养老金比例
  798. $yanglao= floatval(merchantConfig($item['mer_id'], 'yanglaojin'));
  799. if(!$yanglao){
  800. //若商户养老金比例不存在 则使用平台配置
  801. $yanglao = systemConfig('yanglaojin_admin');
  802. }
  803. $item['yanglao'] = bcmul($base_commission, $yanglao ,2) * 0.01;
  804. return $item;
  805. });
  806. return compact('count', 'list','order','where');
  807. }
  808. /**
  809. *
  810. * @param $page
  811. * @param $limit
  812. * @return array
  813. */
  814. public function merchantcategory()
  815. {
  816. return Db::name('merchant_category')->where("status",1)->order("order_sort desc,merchant_category_id desc")->select()->toArray();
  817. }
  818. /*商家分类列表*/
  819. public function mercat($page, $limit)
  820. {
  821. $count= Db::name('merchant_category')->count();
  822. $list= Db::name('merchant_category')->where("status",1)->order("order_sort desc,merchant_category_id desc")->select()->toArray();
  823. return compact('count', 'list');
  824. }
  825. /**
  826. * @param $id
  827. * @return Form
  828. * @throws FormBuilderException
  829. * @author xaboy
  830. * @day 2020-05-07
  831. */
  832. public function changeProForm($id)
  833. {
  834. return Elm::createForm(Route::buildUrl('systemMerchantChangePro', compact('id'))->build(), [
  835. Elm::input('spread_account', '推荐人')->required()->value("请填写推荐人手机号")
  836. ])->setTitle('修改商户推荐人');
  837. }
  838. /*
  839. * 修改商户推广人id
  840. *@author cabbage
  841. *@date 2020-10-31
  842. * */
  843. public function update_spread($id,$spread_account){
  844. //推荐人信息
  845. $spread = Db::name("user")->where('account',$spread_account)->value("uid");
  846. $mer_uid = Db::name("merchant")->where(['mer_id'=>$id])->value("uid");
  847. if($spread && $mer_uid){
  848. try{
  849. Db::name("user")->where(["uid"=>$mer_uid])->update(['spread_uid'=>$spread]);
  850. Db::name("merchant")->where(["mer_id"=>$id])->update(['recommender'=>$spread]);
  851. return true;
  852. }catch (Exception $e) {
  853. Db::rollback();
  854. }
  855. }else{
  856. return false;
  857. }
  858. }
  859. /*下架商品*/
  860. public function MerUpdate($where,$data)
  861. {
  862. return Db::name("store_product")->where($where)->update($data);
  863. }
  864. /*推荐商品*/
  865. public function getProductGoodList($id){
  866. $where['is_gift_bag'] = 0;
  867. $where['is_used'] = 1;
  868. $where['product_type'] = 0;
  869. $where['mer_id'] = $id;
  870. $where['is_good'] = 1;
  871. return Db::name("store_product")->where($where)->field("image,product_id,store_name as title,price,ot_price")->select();
  872. }
  873. /*收款二维码*/
  874. public function getQrCode($mer){
  875. $res = Db::name("MerchantQrcode")
  876. -> where("mer_id",$mer -> mer_id)
  877. -> field("qrcode_src as url,show_src")
  878. -> find();
  879. if(!$res || $res['url'] == '' || $res['show_src'] == ''){
  880. Db::name("MerchantQrcode") -> where("mer_id",$mer -> mer_id) -> delete();
  881. $attachmentRepository = app()->make(AttachmentRepository::class);
  882. $siteUrl = systemConfig('site_url');
  883. $merchantIntertion = app() -> make(MerchantIntention::class);
  884. $id = $merchantIntertion -> getNewQrcodeId();
  885. $name = "offlinepay_".$id.".jpg";
  886. $codeUrl = set_http_type(rtrim($siteUrl, '/') .'/offlinepay/index.html?id=' . $id, request()->isSsl() ? 0 : 1);//二维码链接
  887. $imageInfo = app()->make(QrcodeService::class)->getQRCodePath($codeUrl, $name);
  888. if (!is_string($imageInfo)){
  889. $imageInfo['dir'] = path_to_url($imageInfo['dir']);
  890. if (strpos($imageInfo['dir'], 'http') === false) $imageInfo['dir'] = request()->domain() . $imageInfo['dir'];
  891. $attachmentRepository->create(systemConfig('upload_type') ?: 1, 0, 0, [
  892. 'attachment_category_id' => 0,
  893. 'attachment_name' => $imageInfo['name'],
  894. 'attachment_src' => $imageInfo['dir']
  895. ]);
  896. $show_src = $merchantIntertion -> show_src($imageInfo['dir'],$id);
  897. $codeInsert = [
  898. 'uid'=>$mer -> uid,
  899. 'mer_id' =>$mer -> mer_id,
  900. 'code_id'=>$id,
  901. 'qrcode_src' => $imageInfo['dir'],
  902. 'show_src' => $show_src,
  903. ];
  904. Db::name('MerchantQrcode') -> insert($codeInsert);
  905. return ['url'=>$imageInfo['dir'],'show_src'=>$show_src];
  906. }
  907. throw new ValidateException('收款码生成失败');
  908. }
  909. return $res;
  910. }
  911. }