MerchantRepository.php 41 KB

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