MerchantRepository.php 42 KB

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