MerchantRepository.php 40 KB

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