MerchantRepository.php 42 KB

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