MerchantRepository.php 42 KB

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