MerchantRepository.php 41 KB

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