MerchantRepository.php 41 KB

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