MovieRepository.php 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075
  1. <?php
  2. /**
  3. * @package merchant
  4. * @Author: Qinii
  5. * @Date: 2020/5/8
  6. */
  7. namespace app\common\repositories\movie;
  8. use app\common\dao\store\product\ProductDao as dao;
  9. use app\common\model\store\product\Product;
  10. use app\common\model\user\User;
  11. use app\common\repositories\BaseRepository;
  12. use app\common\repositories\merchant\MerchantRepository;
  13. use app\common\repositories\store\coupon\StoreCouponRepository;
  14. use app\common\repositories\store\order\StoreOrderRepository;
  15. use app\common\repositories\store\product\ProductAttrRepository;
  16. use app\common\repositories\store\product\ProductAttrValueRepository;
  17. use app\common\repositories\store\product\ProductCateRepository;
  18. use app\common\repositories\store\product\ProductContentRepository;
  19. use app\common\repositories\store\product\ProductReplyRepository;
  20. use app\common\repositories\store\shipping\ShippingTemplateRepository;
  21. use app\common\repositories\store\StoreBrandRepository;
  22. use app\common\repositories\store\StoreCategoryRepository;
  23. use app\common\repositories\store\StoreSeckillActiveRepository;
  24. use app\common\repositories\store\StoreSeckillTimeRepository;
  25. use app\common\repositories\user\UserRelationRepository;
  26. use app\common\repositories\user\UserVisitRepository;
  27. use app\controller\merchant\gong\Goods;
  28. use app\traits\ShouxinyiApiRequest;
  29. use crmeb\exceptions\ApiException;
  30. use crmeb\services\QrcodeService;
  31. use crmeb\services\SwooleTaskService;
  32. use think\Exception;
  33. use think\exception\ValidateException;
  34. use think\facade\Db;
  35. use think\facade\Log;
  36. /**
  37. * Class MovieRepository
  38. * @package app\common\repositories\movie
  39. * @author xaboy
  40. * @mixin dao
  41. */
  42. class MovieRepository extends BaseRepository
  43. {
  44. protected $dao;
  45. protected $filed = 'concession_pri,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';
  46. /**
  47. * ProductRepository constructor.
  48. * @param dao $dao
  49. */
  50. public function __construct(dao $dao)
  51. {
  52. $this->dao = $dao;
  53. }
  54. /**
  55. * @Author:Qinii
  56. * @Date: 2020/5/11
  57. * @param int $id
  58. * @param int $merId
  59. * @return mixed
  60. */
  61. public function CatExists(int $id)
  62. {
  63. return (app()->make(StoreCategoryRepository::class))->merExists(0, $id);
  64. }
  65. /**
  66. * @Author:Qinii
  67. * @Date: 2020/5/20
  68. * @param $ids
  69. * @param int $merId
  70. * @return bool
  71. */
  72. public function merCatExists($ids, int $merId)
  73. {
  74. if (!is_array($ids ?? '')) return true;
  75. foreach ($ids as $id) {
  76. if (!(app()->make(StoreCategoryRepository::class))->merExists($merId, $id))
  77. return false;
  78. }
  79. return true;
  80. }
  81. /**
  82. * @Author:Qinii
  83. * @Date: 2020/5/11
  84. * @param int $merId
  85. * @param int $id
  86. * @return mixed
  87. */
  88. public function merShippingExists(int $merId, int $id)
  89. {
  90. $make = app()->make(ShippingTemplateRepository::class);
  91. return $make->merExists($merId, $id);
  92. }
  93. /**
  94. * @Author:Qinii
  95. * @Date: 2020/5/11
  96. * @param int $id
  97. * @return mixed
  98. */
  99. public function merBrandExists(int $id)
  100. {
  101. $make = app()->make(StoreBrandRepository::class);
  102. return $make->meExists($id);
  103. }
  104. /**
  105. * @Author:Qinii
  106. * @Date: 2020/5/11
  107. * @param int $merId
  108. * @param int $id
  109. * @return bool
  110. */
  111. public function merExists(?int $merId, int $id)
  112. {
  113. return $this->dao->merFieldExists($merId, $this->getPk(), $id);
  114. }
  115. public function merDeleteExists(int $merId, int $id)
  116. {
  117. return $this->dao->getDeleteExists($merId, $id);
  118. }
  119. /**
  120. * @Author:Qinii
  121. * @Date: 2020/5/30
  122. * @param int $merId
  123. * @param int $id
  124. * @return bool
  125. */
  126. public function apiExists(?int $merId, int $id)
  127. {
  128. return $this->dao->apiFieldExists($merId, $this->getPk(), $id);
  129. }
  130. /**
  131. * @param int $merId
  132. * @param int $tempId
  133. * @return bool
  134. * @author Qinii
  135. */
  136. public function merTempExists(int $merId, int $tempId)
  137. {
  138. return $this->dao->merFieldExists($merId, 'temp_id', $tempId);
  139. }
  140. /**
  141. * @Author:Qinii
  142. * @Date: 2020/5/11
  143. * @param array $data
  144. */
  145. public function create(array $data)
  146. {
  147. return Db::transaction(function () use ($data) {
  148. $pension = bcsub($data['attrValue'][0]['price'], bcmul($data['attrValue'][0]['price'], $data['commission'], 2), 2);
  149. if ($data['pension'] > $pension) {
  150. return ['code' => 201, 'msg' => '养老金金额和佣金金额总和不能超过商品单价'];
  151. }
  152. $param['attr'] = $data['attr'] ?? '';
  153. $param['attrValue'] = $data['attrValue'] ?? '';
  154. $param['cate'] = $data['mer_cate_id'] ?? '';
  155. $content = $data['content'];
  156. $data['slider_image'] = implode(',', $data['slider_image']);
  157. $give_coupon_ids = '';
  158. if (isset($data['give_coupon_ids']) && !empty($data['give_coupon_ids'])) {
  159. $gcoupon_ids = array_unique($data['give_coupon_ids']);
  160. $give_coupon_ids = implode(',', $gcoupon_ids);
  161. }
  162. $data['give_coupon_ids'] = $give_coupon_ids;
  163. unset($data['attr']);
  164. unset($data['attrValue']);
  165. unset($data['content']);
  166. unset($data['mer_cate_id']);
  167. $data['dc_id'] = Db::name('merchant')->where('mer_id', $data['mer_id'])->value('dc_id');
  168. if ($data['type'] == 7) $data['temp_id'] = 105;
  169. $result = $this->dao->create($data);
  170. $product_id = $result['product_id'];
  171. $settleParams = $this->settleParams($param, $product_id, $data['mer_id']);
  172. $this->save($product_id, $settleParams, $content);
  173. return ['code' => 200, 'msg' => $product_id];
  174. });
  175. }
  176. /**
  177. * TODO 秒杀商品创建
  178. * @param array $data
  179. * @author Qinii
  180. * @day 2020-08-04
  181. */
  182. public function createSeckill(array $data)
  183. {
  184. $dat = [
  185. 'start_day' => $data['start_day'],
  186. 'end_day' => $data['end_day'],
  187. 'start_time' => (int)$data['start_time'],
  188. 'end_time' => (int)$data['end_time'],
  189. 'mer_id' => $data['mer_id'],
  190. 'status' => 1,
  191. 'once_pay_count' => (int)$data['once_pay_count'],
  192. 'all_pay_count' => (int)$data['all_pay_count'],
  193. ];
  194. unset($data['start_day']);
  195. unset($data['end_day']);
  196. unset($data['start_time']);
  197. unset($data['end_time']);
  198. unset($data['once_pay_count']);
  199. unset($data['all_pay_count']);
  200. Db::transaction(function () use ($data, $dat) {
  201. $dat['product_id'] = $this->create($data);
  202. app()->make(StoreSeckillActiveRepository::class)->create($dat);
  203. });
  204. }
  205. /**
  206. * TODO 基础更新
  207. * @param int $id
  208. * @param array $data
  209. * @return int
  210. * @author Qinii
  211. * @day 2020-06-04
  212. */
  213. public function baseUpdate(int $id, array $data)
  214. {
  215. return $this->dao->update($id, $data);
  216. }
  217. /**
  218. * @Author:Qinii
  219. * @Date: 2020/5/11
  220. * @param int $id
  221. * @param array $data
  222. */
  223. public function update(int $id, array $data, $merId)
  224. {
  225. Db::transaction(function () use ($id, $data, $merId) {
  226. $param['attr'] = $data['attr'] ?? '';
  227. $param['attrValue'] = $data['attrValue'] ?? '';
  228. $param['cate'] = $data['mer_cate_id'] ?? '';
  229. $content = $data['content'];
  230. $data['slider_image'] = implode(',', $data['slider_image']);
  231. $give_coupon_ids = '';
  232. if (isset($data['give_coupon_ids']) && !empty($data['give_coupon_ids'])) {
  233. $gcoupon_ids = array_unique($data['give_coupon_ids']);
  234. $give_coupon_ids = implode(',', $gcoupon_ids);
  235. }
  236. $data['give_coupon_ids'] = $give_coupon_ids;
  237. unset($data['attr'], $data['attrValue'], $data['content'], $data['mer_cate_id']);
  238. (app()->make(ProductAttrRepository::class))->clearAttr($id);
  239. (app()->make(ProductAttrValueRepository::class))->clearAttr($id);
  240. (app()->make(ProductContentRepository::class))->clearAttr($id);
  241. (app()->make(ProductCateRepository::class))->clearAttr($id);
  242. $data['dc_id'] = Db::name('merchant')->where('mer_id', $merId)->value('dc_id');
  243. $settleParams = $this->settleParams($param, $id, $merId, $data['type']);
  244. $this->save($id, $settleParams, $content, $data);
  245. });
  246. }
  247. public function updateSeckill(int $id, array $data, $merId)
  248. {
  249. $dat = [
  250. 'start_day' => $data['start_day'],
  251. 'end_day' => $data['end_day'],
  252. 'start_time' => $data['start_time'] . ':00:00',
  253. 'end_time' => $data['end_time'] . ':00:00',
  254. 'status' => 1,
  255. 'once_pay_count' => $data['once_pay_count'],
  256. 'all_pay_count' => $data['all_pay_count'],
  257. ];
  258. unset($data['start_day']);
  259. unset($data['end_day']);
  260. unset($data['start_time']);
  261. unset($data['end_time']);
  262. unset($data['once_pay_count']);
  263. unset($data['all_pay_count']);
  264. Db::transaction(function () use ($data, $dat, $id, $merId) {
  265. $this->update($id, $data, $merId);
  266. app()->make(StoreSeckillActiveRepository::class)->updateByProduct($id, $dat);
  267. });
  268. }
  269. public function destory($id)
  270. {
  271. (app()->make(ProductAttrRepository::class))->clearAttr($id);
  272. (app()->make(ProductAttrValueRepository::class))->clearAttr($id);
  273. (app()->make(ProductContentRepository::class))->clearAttr($id);
  274. (app()->make(ProductCateRepository::class))->clearAttr($id);
  275. $this->dao->delete($id, true);
  276. }
  277. /**
  278. * @Author:Qinii
  279. * @Date: 2020/5/20
  280. * @param $id
  281. * @param $spec_type
  282. * @param $settleParams
  283. * @param $content
  284. * @return int
  285. */
  286. public function save($id, $settleParams, $content, $data = [])
  287. {
  288. if (isset($settleParams['cate']))
  289. (app()->make(ProductCateRepository::class)->insert($settleParams['cate']));
  290. if (isset($settleParams['attr']))
  291. (app()->make(ProductAttrRepository::class))->insert($settleParams['attr']);
  292. if (isset($settleParams['attrValue']))
  293. (app()->make(ProductAttrValueRepository::class))->insert($settleParams['attrValue']);
  294. $this->dao->createContent($id, ['content' => $content]);
  295. $data['price'] = $settleParams['data']['price'];
  296. $data['ot_price'] = $settleParams['data']['ot_price'];
  297. $data['dc_price'] = $settleParams['data']['dc_price'];
  298. $data['cost_price'] = $settleParams['data']['cost_price'] ?? 0;
  299. $data['gong_sku_id'] = $settleParams['data']['gong_sku_id'] ?? 0;
  300. $data['cost'] = $settleParams['data']['cost'];
  301. $data['stock'] = $settleParams['data']['stock'];
  302. $data['concession_pri'] = $settleParams['data']['concession_pri'];
  303. return $this->dao->update($id, $data);
  304. }
  305. /**
  306. * @Author:Qinii
  307. * @Date: 2020/5/18
  308. * @param int $id
  309. * @param array $data
  310. * @return int
  311. */
  312. public function adminUpdate(int $id, array $data)
  313. {
  314. Db::transaction(function () use ($id, $data) {
  315. (app()->make(ProductContentRepository::class))->clearAttr($id);
  316. $this->dao->createContent($id, ['content' => $data['content']]);
  317. unset($data['content']);
  318. return $this->dao->update($id, $data);
  319. });
  320. }
  321. /**
  322. * @Author:Qinii
  323. * @Date: 2020/5/11
  324. * @param array $data
  325. * @param int|null $id
  326. * @return array
  327. */
  328. public function settleParams(array $data, int $id, $merId, $type = 0)
  329. {
  330. $result = $attr_name = [];
  331. if (isset($data['cate']) && is_array($data['cate'])) {
  332. foreach ($data['cate'] as $value) {
  333. $result['cate'][] = [
  334. 'product_id' => $id,
  335. 'mer_cate_id' => $value,
  336. 'mer_id' => $merId,
  337. ];
  338. }
  339. }
  340. if (isset($data['attr']) && is_array($data['attr'])) {
  341. foreach ($data['attr'] as $value) {
  342. $result['attr'][] = [
  343. 'type' => 0,
  344. 'product_id' => $id,
  345. "attr_name" => $value['value'],
  346. 'attr_values' => implode('-!-', $value['detail']),
  347. ];
  348. }
  349. }
  350. $extension_status = systemConfig('extension_status');
  351. $pric = $stock = $ot_price = $cost = $dc_price = $concession_pri = 0;
  352. if (isset($data['attrValue']) && is_array($data['attrValue'])) {
  353. foreach ($data['attrValue'] as $value) {
  354. $sku = '';
  355. if (isset($value['detail']) && is_array($value['detail'])) {
  356. $sku = implode(',', $value['detail']);
  357. }
  358. $product_data = Db::name('store_product')->where('product_id', $id)->value('spu_id');
  359. if ($product_data) {
  360. if (!in_array($merId, ['319', '320', '321', '322', '323', '324', '325', '326', '327', '328'])) {
  361. if ($value['price'] < $value['cost']) {
  362. throw new ValidateException('售价不得小于成本价');
  363. }
  364. if ($type == 5) {
  365. // if ($value['dacang_price'] < $value['cost']) {
  366. // throw new ValidateException('大仓价格不得小于成本价');
  367. // }
  368. }
  369. if ($value['ot_price'] < $value['cost']) {
  370. throw new ValidateException('原价不得小于成本价');
  371. }
  372. }
  373. }
  374. $unique = $this->setUnique($id, $sku, 0);
  375. $value['dacang_price'] = isset($value['dacang_price']) ? (($value['dacang_price'] < 0) ? 0 : $value['dacang_price']) : 0;
  376. if ($product_data) {
  377. $result['attrValue'][] = [
  378. 'detail' => json_encode($value['detail'] ?? ''),
  379. "bar_code" => $value["bar_code"] ?? '',
  380. "image" => $value["image"],
  381. "cost" => $value['cost'] ? (($value['cost'] < 0) ? 0 : $value['cost']) : 0,
  382. "price" => $value['price'] ? (($value['price'] < 0) ? 0 : $value['price']) : 0,
  383. "volume" => $value['volume'] ? (($value['volume'] < 0) ? 0 : $value['volume']) : 0,
  384. "weight" => $value['weight'] ? (($value['weight'] < 0) ? 0 : $value['weight']) : 0,
  385. "stock" => $value['stock'] ? (($value['stock'] < 0) ? 0 : $value['stock']) : 0,
  386. "sales" => (isset($value['sales']) && $value['sales'] && $value['sales'] > 0) ? $value['sales'] : 0,
  387. "ot_price" => $value['ot_price'] ? (($value['ot_price'] < 0) ? 0 : $value['ot_price']) : 0,
  388. "extension_one" => $extension_status ? ($value['extension_one'] ?? 0) : 0,
  389. "extension_two" => $extension_status ? ($value['extension_two'] ?? 0) : 0,
  390. "product_id" => $id,
  391. "type" => 0,
  392. "sku" => $sku,
  393. "unique" => $unique,
  394. "dacang_price" => $value['dacang_price'],
  395. "cost_price" => $value['cost_price'],
  396. "gong_sku_id" => $value['gong_sku_id'],
  397. "pension_num" => 0,
  398. "share_num" => 0,
  399. "stock_num" => 0,
  400. "shares_num" => 0,
  401. 'gong_mer_profit' => $value['gong_mer_profit'] ?? 0.00,
  402. 'gong_market_price' => $value['gong_market_price'] ?? 0.00,
  403. 'plate_mer_profit' => $value['plate_mer_profit'] ?? 0.00,
  404. ];
  405. } else {
  406. $result['attrValue'][] = [
  407. 'detail' => json_encode($value['detail'] ?? ''),
  408. "bar_code" => $value["bar_code"] ?? '',
  409. "image" => $value["image"],
  410. "cost" => $value['cost'] ? (($value['cost'] < 0) ? 0 : $value['cost']) : 0,
  411. "price" => $value['price'] ? (($value['price'] < 0) ? 0 : $value['price']) : 0,
  412. "volume" => $value['volume'] ? (($value['volume'] < 0) ? 0 : $value['volume']) : 0,
  413. "weight" => $value['weight'] ? (($value['weight'] < 0) ? 0 : $value['weight']) : 0,
  414. "stock" => $value['stock'] ? (($value['stock'] < 0) ? 0 : $value['stock']) : 0,
  415. "sales" => (isset($value['sales']) && $value['sales'] && $value['sales'] > 0) ? $value['sales'] : 0,
  416. "ot_price" => $value['ot_price'] ? (($value['ot_price'] < 0) ? 0 : $value['ot_price']) : 0,
  417. "extension_one" => $extension_status ? ($value['extension_one'] ?? 0) : 0,
  418. "extension_two" => $extension_status ? ($value['extension_two'] ?? 0) : 0,
  419. "product_id" => $id,
  420. "type" => 0,
  421. "sku" => $sku,
  422. "unique" => $unique,
  423. "dacang_price" => $value['dacang_price'],
  424. "pension_num" => (isset($value['pension_num']) && $value['pension_num'] !== '') ? $value['pension_num'] : 0,
  425. "share_num" => (isset($value['share_num']) && $value['share_num'] !== '') ? $value['share_num'] : 0,
  426. "stock_num" => (isset($value['stock_num']) && $value['stock_num'] !== '') ? $value['stock_num'] : 0,
  427. "shares_num" => (isset($value['shares_num']) && $value['shares_num'] !== '') ? $value['shares_num'] : 0,
  428. 'gong_mer_profit' => $value['gong_mer_profit'] ?? 0.00,
  429. 'gong_market_price' => $value['gong_market_price'] ?? 0.00,
  430. 'plate_mer_profit' => $value['plate_mer_profit'] ?? 0.00,
  431. ];
  432. }
  433. if (!$pric) $pric = $value['price'];
  434. if (!$ot_price) $ot_price = $value['ot_price'];
  435. if (!$dc_price) $dc_price = $value['dacang_price'];
  436. if (!$cost) $cost = $value['cost'];
  437. if (!$concession_pri) $concession_pri = $value['plate_mer_profit'] ? (($value['plate_mer_profit'] < 0) ? 0 : $value['plate_mer_profit']) : 0;
  438. $ot_price = ($ot_price > $value['ot_price']) ? $value['ot_price'] : $ot_price;
  439. $concession_pri = ($pric > $value['price']) ? $value['plate_mer_profit'] : $concession_pri;
  440. $pric = ($pric > $value['price']) ? $value['price'] : $pric;
  441. $dc_price = ($dc_price > $value['dacang_price']) ? $value['dacang_price'] : $dc_price;
  442. $cost = ($cost > $value['cost']) ? $value['cost'] : $cost;
  443. $stock = $stock + $value['stock'];
  444. }
  445. }
  446. $result['data'] = ['price' => $pric, 'stock' => $stock, 'ot_price' => $ot_price, 'cost' => $cost, 'dc_price' => $dc_price,'concession_pri'=>$concession_pri];
  447. return $result;
  448. }
  449. /**
  450. * @Author:Qinii
  451. * @Date: 2020/5/11
  452. * @param int $id
  453. * @param string $sku
  454. * @param int $type
  455. * @return string
  456. */
  457. public function setUnique(int $id, $sku, int $type)
  458. {
  459. $unique = substr(md5($sku . $id), 12, 11) . $type;
  460. $has = (app()->make(ProductAttrValueRepository::class))->merUniqueExists(null, $unique);
  461. return $has ? false : $unique;
  462. }
  463. /**
  464. * @Author:Qinii
  465. * @Date: 2020/5/18
  466. * @param $id
  467. * @return array
  468. */
  469. public function get($id)
  470. {
  471. $with = ['attr', 'attrValue', 'content', 'merCateId.category', 'storeCategory', 'brand', 'temp', 'seckillActive'];
  472. $data = $this->dao->getWhere(['product_id' => $id], '*', $with)->append(['seckill_status']);
  473. $where = [['coupon_id', 'in', $data['give_coupon_ids']]];
  474. $data['coupon'] = app()->make(StoreCouponRepository::class)->selectWhere($where, 'coupon_id,title')->toArray();
  475. $arr = [];
  476. if (isset($data['merCateId'])) {
  477. foreach ($data['merCateId'] as $i) {
  478. $arr[] = $i['mer_cate_id'];
  479. }
  480. }
  481. $data['mer_cate_id'] = $arr;
  482. foreach ($data['attr'] as $k => $v) {
  483. $data['attr'][$k] = [
  484. 'value' => $v['attr_name'],
  485. 'detail' => $v['attr_values']
  486. ];
  487. }
  488. if ($data['product_type'] == 1)
  489. $make = app()->make(ProductAttrValueRepository::class);
  490. foreach ($data['attrValue'] as $key => $item) {
  491. $sku = explode(',', $item['sku']);
  492. if ($data['product_type'] == 1)
  493. $item['old_stock'] = $make->search(['product_id' => $data['old_product_id'], 'sku' => $item['sku']])->value('stock');
  494. foreach ($sku as $k => $v) {
  495. $item['value' . $k] = $v;
  496. }
  497. $data['attrValue'][$key] = $item;
  498. }
  499. foreach ($data['attrValue'] as $k => &$v) {
  500. $v['dacang_cost_price'] = bcadd(bcmul($v['cost_price'], 0.06, 2), $v['cost_price'], 2);
  501. $v['no_dacang_cost_price'] = bcadd(bcmul($v['cost_price'], 0.1, 2), $v['cost_price'], 2);
  502. }
  503. $content = $data['content']['content'];
  504. unset($data['content']);
  505. $data['content'] = $content;
  506. return $data;
  507. }
  508. /**
  509. * @Author:Qinii
  510. * @Date: 2020/5/18
  511. * @param $type
  512. * @param int|null $merId
  513. * @return array
  514. */
  515. public function switchType($type, ?int $merId = 0, $productType = 0, $is_gift_bag = 0)
  516. {
  517. $stock = 0;
  518. if ($merId) $stock = merchantConfig($merId, 'mer_store_stock');
  519. switch ($type) {
  520. case 1:
  521. $where = ['is_show' => 1, 'status' => 1, 'mer_status' => 1];
  522. break;
  523. case 2:
  524. $where = ['is_show' => 0, 'status' => 1];
  525. if (!$merId && $productType == 0) $where['is_gift_bag'] = 0;
  526. break;
  527. case 3:
  528. $where = ['is_show' => 1, 'stock' => 0, 'status' => 1];
  529. break;
  530. case 4:
  531. $where = ['is_show' => 1, 'stock' => is_int($stock) ? $stock : 0, 'status' => 1];
  532. break;
  533. case 5:
  534. $where = ['soft' => true];
  535. break;
  536. case 6:
  537. $where = ['status' => 0, 'is_gift_bag' => $is_gift_bag];
  538. break;
  539. case 7:
  540. $where = ['status' => -1, 'is_gift_bag' => $is_gift_bag];
  541. break;
  542. default:
  543. $where = ['is_show' => 1, 'status' => 1];
  544. break;
  545. }
  546. if ($productType == 0) {
  547. $where['product_type'] = $productType;
  548. }
  549. if ($productType == 1) {
  550. $where['product_type'] = $productType;
  551. }
  552. if ($productType == 3) {
  553. $where['is_gift_bag'] = 1;
  554. }
  555. return $where;
  556. }
  557. /**
  558. * @Author:Qinii
  559. * @Date: 2020/5/18
  560. * @param int|null $merId
  561. * @return array
  562. */
  563. public function getFilter(?int $merId, $name = '商品', $productType = 0, $is_gift_bag = 0)
  564. {
  565. $result = [];
  566. $result[] = [
  567. 'type' => 1,
  568. 'name' => '出售中' . $name,
  569. 'count' => $this->dao->search($merId, $this->switchType(1, $merId, $productType))->whereNull('spu_id')->count()
  570. ];
  571. $result[] = [
  572. 'type' => 2,
  573. 'name' => '仓库中' . $name,
  574. 'count' => $this->dao->search($merId, $this->switchType(2, $merId, $productType))->whereNull('spu_id')->count()
  575. ];
  576. if ($merId) {
  577. $result[] = [
  578. 'type' => 3,
  579. 'name' => '已售罄' . $name,
  580. 'count' => $this->dao->search($merId, $this->switchType(3, $merId, $productType))->whereNull('spu_id')->count()
  581. ];
  582. $result[] = [
  583. 'type' => 4,
  584. 'name' => '警戒库存',
  585. 'count' => $this->dao->search($merId, $this->switchType(4, $merId, $productType))->whereNull('spu_id')->count()
  586. ];
  587. $result[] = [
  588. 'type' => 5,
  589. 'name' => '回收站' . $name,
  590. 'count' => $this->dao->search($merId, $this->switchType(5, $merId, $productType))->whereNull('spu_id')->count()
  591. ];
  592. }
  593. $result[] = [
  594. 'type' => 6,
  595. 'name' => '待审核' . $name,
  596. 'count' => $this->dao->search($merId, $this->switchType(6, $merId, $productType, $is_gift_bag))->whereNull('spu_id')->count()
  597. ];
  598. $result[] = [
  599. 'type' => 7,
  600. 'name' => '审核未通过' . $name,
  601. 'count' => $this->dao->search($merId, $this->switchType(7, $merId, $productType, $is_gift_bag))->whereNull('spu_id')->count()
  602. ];
  603. return $result;
  604. }
  605. public function getGongFilter(?int $merId, $name = '商品', $productType = 0)
  606. {
  607. $make = app()->make(Goods::class);
  608. $gongProduct439 = $make->getJoinProduct439();
  609. if ($merId == 496) {
  610. // 出售中
  611. $result[] = [
  612. 'type' => 1,
  613. 'name' => '出售中' . $name,
  614. 'count' => $this->dao->search($merId, $this->switchType(1, $merId, $productType))->whereIn('spu_id', $gongProduct439)->whereNotNull('spu_id')->count()
  615. ];
  616. // 仓库中
  617. $result[] = [
  618. 'type' => 2,
  619. 'name' => '仓库中' . $name,
  620. 'count' => $this->dao->search($merId, $this->switchType(2, $merId, $productType))->whereIn('spu_id', $gongProduct439)->whereNotNull('spu_id')->count()
  621. ];
  622. return $result;
  623. }
  624. // 出售中
  625. $result[] = [
  626. 'type' => 1,
  627. 'name' => '出售中' . $name,
  628. 'count' => $this->dao->search($merId, $this->switchType(1, $merId, $productType))->whereNotIn('spu_id', $gongProduct439)->whereNotNull('spu_id')->count()
  629. ];
  630. // 仓库中
  631. $result[] = [
  632. 'type' => 2,
  633. 'name' => '仓库中' . $name,
  634. 'count' => $this->dao->search($merId, $this->switchType(2, $merId, $productType))->whereNotIn('spu_id', $gongProduct439)->whereNotNull('spu_id')->count()
  635. ];
  636. return $result;
  637. }
  638. /**
  639. * TODO 商户商品列表
  640. * @Author:Qinii
  641. * @Date: 2020/5/11
  642. * @param int $merId
  643. * @param array $where
  644. * @param int $page
  645. * @param int $limit
  646. * @return array
  647. */
  648. public function getList(?int $merId, array $where, int $page, int $limit)
  649. {
  650. $query = $this->dao->search($merId, $where)->whereNull('spu_id')->with(['merCateId.category', 'storeCategory', 'brand']);
  651. $count = $query->count($this->dao->getPk());
  652. $list = $query->page($page, $limit)->setOption('field', [])->field($this->filed)->select();
  653. $list->append(['max_extension', 'min_extension']);
  654. return compact('count', 'list');
  655. }
  656. public function getGongProductList(?int $merId, array $where, int $page, int $limit)
  657. {
  658. $make = app()->make(Goods::class);
  659. $gongProduct439 = $make->getJoinProduct439();
  660. $query = $this->dao->search($merId, $where)->whereNotNull('spu_id')->with(['attrValue']);
  661. if ($merId == 496) {
  662. $count = $query->whereIn('spu_id', $gongProduct439)->count($this->dao->getPk());
  663. $list = $query->whereIn('spu_id', $gongProduct439)->page($page, $limit)->setOption('field', [])->field($this->filed)->select();
  664. } else {
  665. $count = $query->whereNotIn('spu_id', $gongProduct439)->count($this->dao->getPk());
  666. $list = $query->whereNotIn('spu_id', $gongProduct439)->page($page, $limit)->setOption('field', [])->field($this->filed)->select();
  667. }
  668. foreach ($list as $key => $value) {
  669. foreach ($value['attrValue'] as &$v) {
  670. $v['min_market_price'] = bcsub($v['gong_market_price'], $v['gong_mer_profit'], 2);
  671. }
  672. $list[$key]['spec_type'] = count($value['attrValue']) > 1;
  673. }
  674. return compact('count', 'list');
  675. }
  676. /**
  677. * TODO 商户秒杀商品列表
  678. * @param int|null $merId
  679. * @param array $where
  680. * @param int $page
  681. * @param int $limit
  682. * @return array
  683. * @author Qinii
  684. * @day 2020-08-04
  685. */
  686. public function getSeckillList(?int $merId, array $where, int $page, int $limit)
  687. {
  688. $make = app()->make(StoreOrderRepository::class);
  689. $query = $this->dao->search($merId, $where)->with(['merCateId.category', 'storeCategory', 'brand', 'seckillActive']);
  690. $count = $query->count($this->dao->getPk());
  691. $list = $query->page($page, $limit)->setOption('field', [])->field($this->filed)->select()
  692. ->each(function ($item) use ($make) {
  693. $item['sales'] = $make->seckillOrderCounut($item['product_id']);
  694. return $item;
  695. });
  696. $list->append(['seckill_status']);
  697. return compact('count', 'list');
  698. }
  699. /**
  700. * TODO 平台商品列表
  701. * @Author:Qinii
  702. * @Date: 2020/5/11
  703. * @param int $merId
  704. * @param array $where
  705. * @param int $page
  706. * @param int $limit
  707. * @return array
  708. */
  709. public function getAdminList(?int $merId, array $where, int $page, int $limit)
  710. {
  711. $query = $this->dao->search($merId, $where)->with(['merCateId.category', 'storeCategory', 'brand', 'merchant']);
  712. $count = $query->count($this->dao->getPk());
  713. $list = $query->page($page, $limit)->select();
  714. foreach ($list as &$v) {
  715. $v['is_used'] = (int)$v['is_used'];
  716. }
  717. $list->append(['max_extension', 'min_extension']);
  718. return compact('count', 'list');
  719. }
  720. /**
  721. * TODO 平台秒杀商品列表
  722. * @param int|null $merId
  723. * @param array $where
  724. * @param int $page
  725. * @param int $limit
  726. * @return array
  727. * @author Qinii
  728. * @day 2020-08-04
  729. */
  730. public function getAdminSeckillList(?int $merId, array $where, int $page, int $limit)
  731. {
  732. $make = app()->make(StoreOrderRepository::class);
  733. $query = $this->dao->search($merId, $where)->with(['merCateId.category', 'storeCategory', 'brand', 'merchant', 'seckillActive']);
  734. $count = $query->count($this->dao->getPk());
  735. $list = $query->page($page, $limit)->setOption('field', [])->field($this->filed)->select()
  736. ->each(function ($item) use ($make) {
  737. $item['sales'] = $make->seckillOrderCounut($item['product_id']);
  738. return $item;
  739. });;
  740. $list->append(['seckill_status']);
  741. return compact('count', 'list');
  742. }
  743. /**
  744. * @Author:Qinii
  745. * @Date: 2020/5/28
  746. * @param array $where
  747. * @param int $page
  748. * @param int $limit
  749. * @param $userInfo
  750. * @return array
  751. */
  752. public function getApiSearch($merId, array $where, int $page, int $limit, $userInfo, $type439 = false)
  753. {
  754. unset($where['order']);
  755. if (isset($where['price_on']) && $where['price_on'] != '') $where['price'] = [$where['price_asc'], $where['price_desc']];
  756. if (isset($where['brand_id']) && $where['brand_id'] != '') $where['brand_id'] = explode(',', $where['brand_id']);
  757. unset($where['price_on'], $where['price_off']);
  758. $where = array_merge($where, $this->dao->productShow());
  759. $cateList = [];
  760. if(isset($where['cate_id']) && $where['cate_id']!= ''){
  761. $cate_name = Db::name('store_category')->where('pid', 564)->where('store_category_id', $where['cate_id'])->value('cate_name');
  762. $url = "https://dc-api.hxxfb.com/douhuomallCategory/getCateAll";
  763. $result = curlGet($url);
  764. if (!isset($result['status']) || $result['status'] != 200) {
  765. $cateList = [$where['cate_id']];
  766. }else{
  767. $cateIds = [];
  768. foreach($result['data'] as $item){
  769. if($item['cateName'] == $cate_name){
  770. $cateIds = explode(",", $item['cateIds']);
  771. break;
  772. }
  773. }
  774. $cateList = array_merge([$where['cate_id']], $cateIds);
  775. }
  776. unset($where['cate_id']);
  777. }
  778. //搜索记录
  779. if ($userInfo && isset($where['keyword']) && !empty($where['keyword'])){
  780. log::info("======sousuojilu====");
  781. app()->make(UserVisitRepository::class)->searchProduct($userInfo['uid'], $where['keyword']);
  782. }
  783. $query = $this->dao->search($merId, $where)->with(['merchant']);
  784. $query->with(['issetCoupon']);
  785. if ($type439 == true) {
  786. $make2 = app()->make(Goods::class);
  787. $gongProduct439 = $make2->getJoinProduct439();
  788. $query->where(function ($query2) use ($gongProduct439) {
  789. $query2->whereNotIn('spu_id', $gongProduct439)->whereOr("spu_id is null");
  790. });
  791. }
  792. if(count($cateList) > 0){
  793. $query->where(function ($query2) use ($cateList) {
  794. $query2->whereIn('cate_id', $cateList)->whereOr("cate_id is null");
  795. });
  796. }
  797. $count = $query->count($this->dao->getPk());
  798. // echo DB::getlastsql();die;
  799. $list = $query->page($page, $limit)->setOption('field', [])->field($this->filed)->select();
  800. $list->each(function ($item) {
  801. $item['sales'] = $item['sales'] + $item['ficti'];
  802. unset($item['ficti']);
  803. $item['yanglao'] = $this->yanglaojin($item['product_id']);
  804. return $item;
  805. });
  806. if ($this->getUserIsPromoter($userInfo)) $list = $list->each(function ($item) {
  807. return $item['max_extension'] = $item->max_extension;
  808. });
  809. return compact('count', 'list');
  810. }
  811. /**
  812. * TODO 秒杀列表
  813. * @param array $where
  814. * @param int $page
  815. * @param int $limit
  816. * @return array
  817. * @author Qinii
  818. * @day 2020-08-04
  819. */
  820. public function getApiSeckill(array $where, int $page, int $limit)
  821. {
  822. $field = 'Product.product_id,Product.mer_id,is_new,keyword,brand_id,image,store_name,sort,rate,reply_count,sales,price,cost,ficti,ot_price,stock,extension_type,care_count,unit_name,create_time';
  823. $make = app()->make(StoreOrderRepository::class);
  824. $res = app()->make(StoreSeckillTimeRepository::class)->getBginTime($where);
  825. $count = 0;
  826. $list = [];
  827. if ($res) {
  828. $where = [
  829. 'start_time' => $res['start_time'],
  830. 'end_time' => $res['end_time'],
  831. 'day' => date('Y-m-d', time())
  832. ];
  833. $query = $this->dao->seckillSearch($where)->with(['seckillActive']);
  834. $count = $query->count();
  835. $list = $query->page($page, $limit)->setOption('field', [])->field($field)->select()
  836. ->each(function ($item) use ($make) {
  837. $item['sales'] = $make->seckillOrderCounut($item['product_id']);
  838. $item['stop'] = $item->end_time;
  839. //unset($item['seckillActive']);
  840. unset($item['ficti']);
  841. return $item;
  842. });
  843. }
  844. return compact('count', 'list');
  845. }
  846. /**
  847. * TODO 平台礼包列表
  848. * @param array $where
  849. * @param int $page
  850. * @param int $limit
  851. * @return array
  852. * @author Qinii
  853. * @day 2020-06-01
  854. */
  855. public function getBagList(array $where, int $page, int $limit)
  856. {
  857. $query = $this->dao->search(null, $where)->with(['merCateId.category', 'storeCategory', 'brand']);
  858. $count = $query->count($this->dao->getPk());
  859. $list = $query->page($page, $limit)->setOption('field', [])->field($this->filed)->select();
  860. return compact('count', 'list');
  861. }
  862. /**
  863. * @Author:Qinii
  864. * @Date: 2020/5/28
  865. * @param array $where
  866. * @return mixed
  867. */
  868. public function getBrandByCategory(array $where)
  869. {
  870. $query = $this->dao->search(null, $where);
  871. return $query->group('brand_id')->column('brand_id');
  872. }
  873. /**
  874. * @Author:Qinii
  875. * @Date: 2020/5/30
  876. * @param $id
  877. * @param $userInfo
  878. */
  879. public function detail($id, $userInfo)
  880. {
  881. $field = 'product_id,mer_id,image,slider_image,store_name,store_info,unit_name,price,cost,ot_price,stock,sales,ficti,video_link,product_type,type,dc_price,seckill,spu_id';
  882. $with = [
  883. 'attr', 'attrValue', 'content', 'topReply.orderProduct' => function ($query) {
  884. $query->field('reply_id,uid,nickname,merchant_reply_content,avatar,order_product_id,product_id,product_score,service_score,postage_score,comment,pics,create_time');
  885. }, 'merchant.recommend'
  886. ];
  887. $is_seckill = Db::name('store_product')->where('product_id', $id)->value('seckill');
  888. if ($is_seckill == 1) {
  889. $is_hide = 1;
  890. } else {
  891. $is_hide = 0;
  892. }
  893. $res = $this->dao->getWhere(['is_show' => 1, 'status' => 1, 'product_id' => $id, 'is_used' => 1, 'is_hide' => $is_hide], $field, $with);
  894. if (!$res) return null;
  895. if ($userInfo) {
  896. $isRelation = app()->make(UserRelationRepository::class)->getUserRelation(['type_id' => $res['product_id'], 'type' => 1], $userInfo['uid']);
  897. if ($this->getUserIsPromoter($userInfo)) $res->append(['max_extension', 'min_extension']);
  898. }
  899. $res['isRelation'] = $isRelation ?? false;
  900. $res['sku'] = $this->detailAttrValue($res['attrValue'], $userInfo, $res['product_type']);
  901. $res['sales'] = $res['sales'] + $res['ficti'];
  902. $res['replayData'] = app()->make(ProductReplyRepository::class)->getReplyRate($res['product_id']);
  903. // 应前端需求 加上对应价格sku的unique
  904. $res['unique'] = Db::name('store_product_attr_value')->where('product_id', $id)->where('price', $res['price'])->value('unique');
  905. if ($res['topReply']) {
  906. $res['topReply']['sku'] = $res['topReply']['orderProduct']['cart_info']['productAttr']['sku'];
  907. unset($res['topReply']['orderProduct']);
  908. }
  909. unset($res['attrValue']);
  910. unset($res['ficti']);
  911. unset($res['attr']);
  912. unset($res['attr']);
  913. $res['attr'] = $this->detailAttr($res['attr']);
  914. $k=$this->pvParm($res['product_id']);
  915. $res['jfduihuan']=0;
  916. $res['jdduihuan']=0;
  917. $res['yanglao']=($k['yanglao']);
  918. if($res['mer_id'] != 496){
  919. if($res['mer_id'] != 491){
  920. $res['jfduihuan']=($k['jfduihuan']);
  921. $res['jdduihuan']=($k['jdduihuan']);
  922. }
  923. }
  924. if($res['mer_id'] == 496){
  925. $s = 0;
  926. if($res['price']==1180){
  927. $s = 1000;
  928. }else if($res['price']==10620){
  929. $s = 9000;
  930. }else if($res['price']==11800){
  931. $s = 10000;
  932. }else if($res['price'] == 2360){
  933. $s= 2000;
  934. }else if($res['price'] == 9440){
  935. $s = 8000;
  936. }
  937. $res['yanglao'] = round($s * 0.035,2);
  938. }
  939. $res['pv']=($k['pv']);
  940. $res['jifen']=($k['jifen']);
  941. $res['gongxian']=($k['gongxian']);
  942. $res['fzone_paytype']=($k['fzone_paytype']);
  943. foreach ($res['merchant']['recommend'] as $k => $v) {
  944. // $is_seckill=Db::name('store_product')->where('product_id',$v['product_id'])->value('seckill');
  945. // if($is_seckill==1){
  946. // unset($res['merchant']['recommend'][$k]);
  947. // continue;
  948. // }
  949. if ($res['merchant']['recommend'][$k]['mer_id'] == 276 || $res['merchant']['recommend'][$k]['mer_id'] == 236 || $res['merchant']['recommend'][$k]['mer_id'] == 290) {
  950. if ($res['merchant']['recommend'][$k]['mer_id'] == '236') {
  951. $res['merchant']['recommend'][$k]['yanglao'] = bcmul($res['merchant']['recommend'][$k]['price'], bcmul('10', 0.01, 2), 2);
  952. } else {
  953. $res['merchant']['recommend'][$k]['yanglao'] = bcmul($res['merchant']['recommend'][$k]['price'], bcmul('2', 0.01, 2), 2);
  954. }
  955. } else {
  956. $res['merchant']['recommend'][$k]['yanglao'] = $this->yanglaojin($v['product_id']);
  957. }
  958. // $res['merchant']['recommend'][$k]['yanglao'] = $this->yanglaojin($v['product_id']);
  959. }
  960. //店铺商品在售数量
  961. $res['product_count'] = Db::name("store_product")->where(['mer_id' => $res['mer_id'], 'status' => '1', 'is_show' => 1])->count();
  962. $res['merchant']["business_id"] = Db::name("merchant")->where(['mer_id' => $res['mer_id']])->value("business_id");
  963. $res['merchant']["sxy_submerchant_id"] = Db::name("merchant")->where(['mer_id' => $res['mer_id']])->value("sxy_submerchant_id");
  964. $merchant_uid = Db::name("merchant")->where(['mer_id' => $res['merchant']['mer_id']])->value('uid');
  965. $res['merchant']['im'] = Db::name('merchant_admin')->where(['mer_id' => $res['merchant']['mer_id']])->field('uid,im_uuid,account,im_user_id')->find();
  966. Db::name("merchant")->where(['mer_id' => $res['mer_id']])->inc('renqizhi', 1)->update();
  967. return $res;
  968. }
  969. public function getopenid($code)
  970. {
  971. $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . env('WECHAT.APPID') . "&secret=" . env('WECHAT.SECRET') . "&code=" . $code . "&grant_type=authorization_code";
  972. $get = curlGet($url);
  973. if (!isset($get['openid']) || empty($get['openid'])) {
  974. return '授权失败';
  975. }
  976. return $get['openid'];
  977. }
  978. public function pay($price, $openid)
  979. {
  980. $productDetails = array(
  981. 'name' => $product ?? "储蓄保",//商品名称。需传递真实产品名称。
  982. 'quantity' => 1,//商品数量
  983. 'amount' => strval($price * 100),//商品单价
  984. );
  985. $data = [
  986. 'openId' => $openid,
  987. 'requestId' => time() . rand(10000, 99999) . "_" . rand(10, 99),
  988. 'orderAmount' => strval($price * 100),
  989. 'callbackUrl' => 'https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=Mzg4NzQ4NDgzNQ==&scene=124&uin=&key=&devicetype=Windows+10+x64&version=6302019c&lang=zh_CN&a8scene=7&fontgear=2',
  990. 'paymentModeCode' => 'WECHAT-OFFICIAL_PAY-P2P',
  991. 'productDetails' => [$productDetails],
  992. 'clientIp' => $this->getRealIP(),
  993. ];
  994. $data['notifyUrl'] = '/api/notify/pay_5upay_h5';
  995. $insertdata = [
  996. 'data' => json_encode($data),
  997. 'time' => time()
  998. ];
  999. Db::name('gzh_h5_pay')->insert($insertdata);
  1000. $res = ShouxinyiApiRequest::onlinePay($data);
  1001. $return = array(
  1002. 'redirectUrl' => $res['redirectUrl'] ?? '',//重定向地址
  1003. 'status' => $res['status'],
  1004. 'scanCode' => $res['scanCode'] ?? '',//Base64二维码
  1005. 'appParams' => $res['appParams'] ?? '',//App调用码
  1006. 'jsString' => $res['jsString'] ?? '',//微信小程序及公众号支付调用串
  1007. 'wechatId' => $res['wechatId'] ?? '',//微信原始ID串
  1008. 'tradeNo' => $res['tradeNo'] ?? '',//支付宝生活号调用id
  1009. 'token' => $res['token'] ?? '', //微包支付调用token
  1010. );
  1011. return $return;
  1012. }
  1013. //客户端IP
  1014. public function getRealIP()
  1015. {
  1016. $forwarded = request()->header("x-forwarded-for");
  1017. if ($forwarded) {
  1018. $ip = explode(',', $forwarded)[0];
  1019. } else {
  1020. $ip = request()->ip();
  1021. }
  1022. return $ip;
  1023. }
  1024. /**
  1025. * TODO 单商品属性
  1026. * @param $data
  1027. * @return array
  1028. * @author Qinii
  1029. * @day 2020-08-05
  1030. */
  1031. public function detailAttr($data)
  1032. {
  1033. $attr = [];
  1034. foreach ($data as $key => $item) {
  1035. $attr[$key] = $item;
  1036. $arr = [];
  1037. foreach ($item['attr_values'] as $i => $value) {
  1038. $arr[] = [
  1039. 'attr' => $value,
  1040. 'check' => false
  1041. ];
  1042. }
  1043. $attr[$key]['attr_value'] = $arr;
  1044. }
  1045. return $attr;
  1046. }
  1047. /**
  1048. * TODO 单商品sku
  1049. * @param $data
  1050. * @param $userInfo
  1051. * @return array
  1052. * @author Qinii
  1053. * @day 2020-08-05
  1054. */
  1055. public function detailAttrValue($data, $userInfo, $productType = 0)
  1056. {
  1057. $sku = [];
  1058. $make = app()->make(StoreOrderRepository::class);
  1059. foreach ($data as $k => $value) {
  1060. $sku[$value['sku']] = [
  1061. 'sku' => $value['sku'],
  1062. 'price' => set_price_rtrim($value['price']),
  1063. 'stock' => $value['stock'],
  1064. 'image' => $value['image'],
  1065. 'bar_code' => $value['bar_code'],
  1066. 'weight' => $value['weight'],
  1067. 'volume' => $value['volume'],
  1068. 'ot_price' => set_price_rtrim($value['ot_price']),
  1069. 'unique' => $value['unique'],
  1070. 'dacang' => $value['dacang_price'] ? set_price_rtrim($value['dacang_price']) : 'null'
  1071. ];
  1072. if ($productType) {
  1073. $sku[$value['sku']]['stock'] = $value['stock'] - $make->seckillSkuOrderCounut($value['unique']); //获取sku的销量
  1074. }
  1075. if ($this->getUserIsPromoter($userInfo)) {
  1076. $sku[$value['sku']]['extension_one'] = $value->bc_extension_one;
  1077. $sku[$value['sku']]['extension_two'] = $value->bc_extension_two;
  1078. }
  1079. }
  1080. return $sku;
  1081. }
  1082. /**
  1083. * TODO 秒杀商品详情
  1084. * @param int $id
  1085. * @author Qinii
  1086. * @day 2020-08-05
  1087. */
  1088. public function seckillDetail(int $id)
  1089. {
  1090. $field = 'product_id,mer_id,image,slider_image,store_name,store_info,unit_name,price,cost,ot_price,stock,sales,video_link,old_product_id,product_type';
  1091. $with = [
  1092. 'attr', 'attrValue', 'content', 'topReply' => function ($query) {
  1093. $query->field('reply_id,uid,nickname,merchant_reply_content,avatar,order_product_id,product_id,product_score,service_score,postage_score,comment,create_time');
  1094. }, 'merchant.recommend', 'seckillActive' => function ($query) {
  1095. $query->field('start_day,end_day,start_time,end_time,product_id');
  1096. }
  1097. ];
  1098. $where = $this->seckillShow();
  1099. $where['product_id'] = $id;
  1100. $res = $this->dao->getWhere($where, $field, $with);
  1101. if (!$res) throw new ValidateException('商品不存在');
  1102. $res['isRelation'] = $isRelation ?? false;
  1103. $res['sku'] = $this->detailAttrValue($res['attrValue'], null, $res['product_type']);
  1104. $res['replayData'] = app()->make(ProductReplyRepository::class)->getReplyRate($res['product_id']);
  1105. unset($res['attrValue'], $res['ficti'], $res['attr']);
  1106. $res['attr'] = $this->detailAttr($res['attr']);
  1107. //活动是否结束
  1108. $res->append(['seckill_status']);
  1109. $res['stop'] = strtotime(date('Y-m-d', time()) . $res['seckillActive']['end_time'] . ':00:00');
  1110. unset($res['seckillActive']);
  1111. $res['sales'] = app()->make(StoreOrderRepository::class)->seckillOrderCounut($id);
  1112. $res['quota'] = $this->seckillStock($id);
  1113. $oldPro = $this->dao->productShow();
  1114. $oldPro['product_id'] = $res['old_product_id'];
  1115. $old_status = 0;
  1116. if ($this->dao->getWhere($oldPro)) $old_status = 1;
  1117. $res['old_status'] = $old_status;
  1118. $res['yanglao'] = $this->yanglaojin($res['product_id']);
  1119. foreach ($res['merchant']['recommend'] as $k => $v) {
  1120. $res['merchant']['recommend'][$k]['yanglao'] = $this->yanglaojin($v['product_id']);
  1121. }
  1122. return $res;
  1123. }
  1124. /**
  1125. * TODO 秒杀商品库存检测
  1126. * @param int $productId
  1127. * @return bool|int
  1128. * @author Qinii
  1129. * @day 2020-08-05
  1130. */
  1131. public function seckillStock(int $productId)
  1132. {
  1133. $product = $this->dao->getWhere(['product_id' => $productId], '*', ['attrValue']);
  1134. $count = app()->make(StoreOrderRepository::class)->seckillOrderCounut($productId);
  1135. if ($product['stock'] > $count) {
  1136. $make = app()->make(ProductAttrValueRepository::class);
  1137. foreach ($product['attrValue'] as $item) {
  1138. $attr = [
  1139. ['sku', '=', $item['sku']],
  1140. ['product_id', '=', $product['old_product_id']],
  1141. ['stock', '>', 0],
  1142. ];
  1143. if ($make->getWhereCount($attr)) return true;
  1144. }
  1145. }
  1146. return false;
  1147. }
  1148. /**
  1149. * @Author:Qinii
  1150. * @Date: 2020/5/30
  1151. * @param $userInfo
  1152. * @return bool
  1153. */
  1154. public function getUserIsPromoter($userInfo)
  1155. {
  1156. return (isset($userInfo['is_promoter']) && $userInfo['is_promoter'] && systemConfig('extension_status')) ? true : false;
  1157. }
  1158. /**
  1159. * @Author:Qinii
  1160. * @Date: 2020/5/30
  1161. * @param $userInfo
  1162. * @param int|null $merId
  1163. * @param $page
  1164. * @param $limit
  1165. * @return array
  1166. */
  1167. public function recommend($userInfo, ?int $merId, $page, $limit, $volunteer = null, $sort = '', $type = '')
  1168. {
  1169. // $where = ['order' => 'sales'];
  1170. if ($sort)
  1171. $where['order'] = $sort;
  1172. if (!is_null($userInfo)) {
  1173. $cate_ids = app()->make(UserVisitRepository::class)->getRecommend($userInfo['uid']);
  1174. if ($cate_ids) $where = ['cate_ids' => $cate_ids];
  1175. }
  1176. if ($volunteer) {
  1177. $where['volunteer'] = 1;
  1178. } else {
  1179. $where['volunteer'] = 0;
  1180. }
  1181. $where['price'] = ['>', 0];
  1182. $where = array_merge($where, $this->switchType(1, $merId, 0));
  1183. $make2 = app()->make(Goods::class);
  1184. $gongProduct439 = $make2->getJoinProduct439();
  1185. $query = $this->dao->search($merId, $where)->when($type && $merId == 321, function ($query) use ($type) {
  1186. $query->where("type", $type);
  1187. })->when($merId == 321, function ($q1) use ($gongProduct439) {
  1188. $q1->where(function ($query2) use ($gongProduct439) {
  1189. $query2->whereNotIn('spu_id', $gongProduct439)->whereOr("spu_id is null");
  1190. });
  1191. });
  1192. $count = $query->count($this->dao->getPk());
  1193. // var_dump($where);
  1194. // echo Db::getlastsql();die;
  1195. $list = $query->page($page, $limit)->setOption('field', [])->with(['issetCoupon'])->field('volunteer,product_id,cate_id,mer_id,image,store_name,sort,sales,price,create_time,keyword')->select();
  1196. $arr=[];
  1197. foreach ($list["list"] as $v) {
  1198. $k = $this->pvParm($v['product_id']);
  1199. // $v['yanglao'] = $yanglao;
  1200. // $v["yanglao"]
  1201. $v['yanglao']=($k['yanglao']);
  1202. $v['pv']=($k['pv']);
  1203. $v['jifen']=($k['jifen']);
  1204. $v['gongxian']=($k['gongxian']);
  1205. $v['jfduihuan']=($k['jfduihuan']);
  1206. $v['jdduihuan']=($k['jdduihuan']);
  1207. // $v=($v,$k);
  1208. // array_push($v,$k);
  1209. $arr[]=$v;
  1210. }
  1211. $list["list"]=$arr;
  1212. return compact('count', 'list');
  1213. }
  1214. /**
  1215. * @param $page
  1216. * @param $limit
  1217. * @param $key
  1218. * @param $cate_id
  1219. * @return array
  1220. * @throws \think\db\exception\DataNotFoundException
  1221. * @throws \think\db\exception\DbException
  1222. * @throws \think\db\exception\ModelNotFoundException
  1223. * @author tang
  1224. */
  1225. public function search_recommend($page, $limit, $key, $cate_id, $uid)
  1226. {
  1227. $topping = Db::name('user')->where('uid', $uid)->value('mer_topping');
  1228. $where = [];
  1229. // if($cate_id!='')
  1230. // $where['cate_id'] = $cate_id;
  1231. $where['sp.volunteer'] = 1;
  1232. $list = [];
  1233. if ($key != '') {
  1234. $list = Db::name('store_product')
  1235. ->alias('sp')
  1236. ->join('merchant m', 'm.mer_id = sp.mer_id')
  1237. ->page($page, $limit)
  1238. ->where($where)
  1239. ->group('sp.mer_id')
  1240. ->whereLike('sp.store_name', "%" . $key . "%")
  1241. ->where('m.mer_state', 1)
  1242. ->where('m.is_del', 0)
  1243. ->where('sp.volunteer', 1)
  1244. ->where('sp.is_show', 1)
  1245. ->where('sp.is_used ', 1)
  1246. ->select();
  1247. if (!$list->isEmpty()) {
  1248. $list = $list->toArray();
  1249. }
  1250. foreach ($list as $k => &$v) {
  1251. if ($v['mer_id'] == $topping) {
  1252. $a = $v;
  1253. unset($list[$k]);
  1254. } else {
  1255. $a = [];
  1256. }
  1257. }
  1258. if (!empty($a))
  1259. array_unshift($list, $a);
  1260. $list = array_merge($list);
  1261. } else {
  1262. $whereCate = [];
  1263. if ($cate_id > 0) {
  1264. $whereCate['m.category_id'] = $cate_id;
  1265. }
  1266. $list = Db::name('store_product')
  1267. ->alias('sp')
  1268. ->join('merchant m', 'm.mer_id = sp.mer_id')
  1269. ->where('m.mer_state', 1)
  1270. ->where('m.is_del', 0)
  1271. ->group('sp.mer_id')
  1272. ->where('sp.volunteer', 1)
  1273. ->where('sp.is_show', 1)
  1274. ->where('sp.is_used ', 1)
  1275. ->where($whereCate)
  1276. ->page($page, $limit)
  1277. ->field('sp.*')
  1278. ->select();
  1279. Log::info($list->isEmpty());
  1280. if (!$list->isEmpty()) {
  1281. $list = $list->toArray();
  1282. }
  1283. Log::info(json_encode($list));
  1284. $a = [];
  1285. foreach ($list as $k => &$v) {
  1286. if ($v['mer_id'] == $topping) {
  1287. $a = $v;
  1288. unset($list[$k]);
  1289. }
  1290. }
  1291. if (!empty($a))
  1292. array_unshift($list, $a);
  1293. $list = array_merge($list);
  1294. }
  1295. $count = 0;
  1296. foreach ($list as &$v) {
  1297. $count++;
  1298. $yanglao = $this->yanglaojin($v['product_id']);
  1299. $v['yanglao'] = $yanglao;
  1300. }
  1301. return compact('count', 'list');
  1302. }
  1303. /**
  1304. * 检测是否有效
  1305. * @Author:Qinii
  1306. * @Date: 2020/6/1
  1307. * @param $id
  1308. * @return mixed
  1309. */
  1310. public function getOne($id)
  1311. {
  1312. $data = ($this->dao->getWhere([$this->dao->getPk() => $id]));
  1313. if (!is_null($data) && $data->check()) return $data;
  1314. return false;
  1315. }
  1316. public function switchStatus(array $id, $data)
  1317. {
  1318. $status = $data['status'] ?? 0;
  1319. $is_show = $data['is_show'] ?? 0;
  1320. $make = app()->make(ProductAttrValueRepository::class);
  1321. if (($status == 1 || $is_show == 1) && systemConfig('extension_status')) {
  1322. foreach ($id as $i) {
  1323. if ($this->getWhereCount(['product_id' => $i, 'extension_type' => 1])) {
  1324. $sku = Db::name("store_product_attr_value")->where(['product_id' => $i])->column("extension_one");
  1325. foreach ($sku as $k) {
  1326. if ($k <= systemConfig('extension_one_rate'))
  1327. throw new ValidateException('设置返利请高于系统设置的' . systemConfig('extension_one_rate') . '%比例');
  1328. }
  1329. }
  1330. }
  1331. }
  1332. $this->dao->updates($id, $data);
  1333. $res = $this->dao->get($id[0]);
  1334. if (isset($data['status'])) {
  1335. SwooleTaskService::merchant('notice', [
  1336. 'type' => 'product',
  1337. 'data' => [
  1338. 'title' => '新审核结果',
  1339. 'message' => '您有一条新的商品审核',
  1340. 'id' => ''
  1341. ]
  1342. ], $res['mer_id']);
  1343. }
  1344. }
  1345. /**
  1346. * TODO 执行检测所有线上商品
  1347. * @author Qinii
  1348. * @day 2020-06-24
  1349. */
  1350. public function checkProductByExtension()
  1351. {
  1352. if (systemConfig('extension_status')) {
  1353. $where = ['status' => 1, 'is_show' => 1, 'product_type' => 0];
  1354. $make = app()->make(ProductAttrValueRepository::class);
  1355. $query = $this->dao->search(null, $where)->where('extension_type', 1);
  1356. $query->chunk(100, function ($prduct) use ($make) {
  1357. foreach ($prduct as $item) {
  1358. if (!$make->checkExtensionById($item['product_id'])) {
  1359. $item->status = -2;
  1360. $item->refusal = '因佣金比例调整,商品佣金低于系统比例';
  1361. $item->save();
  1362. }
  1363. }
  1364. });
  1365. }
  1366. }
  1367. public function wxQrCode(Product $product, User $user)
  1368. {
  1369. $name = $product->product_id . '_product_detail_' . $user->uid . '_is_promoter_' . $user['is_promoter'] . '_wap.jpg';
  1370. return app()->make(QrcodeService::class)->getWechatQrcodePath($name, '/pages/goods_details/index?id=' . $product->product_id . '&spread=' . $user['uid']);
  1371. }
  1372. public function routineQrCode(Product $product, User $user)
  1373. {
  1374. //小程序
  1375. $name = $product->product_id . '_' . $user['uid'] . '_' . $user['is_promoter'] . '_product.jpg';
  1376. $data = 'id=' . $product->product_id;
  1377. if ($user['is_promoter'] || systemConfig('store_brokerage_statu') == 2) $data .= '&pid=' . $user['uid'];
  1378. return app()->make(QrcodeService::class)->getRoutineQrcodePath($name, 'pages/goods_details/index', $data);
  1379. }
  1380. /**
  1381. * TODO 礼包是否超过数量限制
  1382. * @param $merId
  1383. * @return bool
  1384. * @author Qinii
  1385. * @day 2020-06-25
  1386. */
  1387. public function checkMerchantBagNumber($merId)
  1388. {
  1389. $where = ['is_gift_bag' => 1];
  1390. $promoter_bag_number = systemConfig('promoter_bag_number');
  1391. $count = $this->dao->search($merId, $where)->count();
  1392. if ($promoter_bag_number > $count) return true;
  1393. return false;
  1394. }
  1395. /**
  1396. * TODO
  1397. * @param int $id
  1398. * @param array $status
  1399. * @author Qinii
  1400. * @day 2020-07-17
  1401. */
  1402. public function changeUsed(int $id, array $status)
  1403. {
  1404. return $this->dao->update($id, $status);
  1405. }
  1406. public function orderProductIncStock($cart)
  1407. {
  1408. Db::transaction(function () use ($cart) {
  1409. $productAttrValueRepository = app()->make(ProductAttrValueRepository::class);
  1410. if (!$productAttrValueRepository->attrExists($cart['product_id'], $cart['cart_info']['productAttr']['unique'])) return;
  1411. $productAttrValueRepository->incStock($cart['product_id'], $cart['cart_info']['productAttr']['unique'], $cart['product_num']);
  1412. if (!$this->exists($cart['product_id'])) return;
  1413. $this->dao->incStock($cart['product_id'], $cart['product_num']);
  1414. if (isset($cart['cart_info']['product']['old_product_id'])) {
  1415. $oldId = $cart['cart_info']['product']['old_product_id'];
  1416. if (!$productAttrValueRepository->skuExists($oldId, $cart['cart_info']['productAttr']['sku'])) return;
  1417. $productAttrValueRepository->incSkuStock($oldId, $cart['cart_info']['productAttr']['sku'], $cart['product_num']);
  1418. if ($this->exists($oldId))
  1419. $this->incStock($oldId, $cart['product_num']);
  1420. }
  1421. });
  1422. }
  1423. /**
  1424. * TODO 商品验证
  1425. * @param $data
  1426. * @author Qinii
  1427. * @day 2020-08-01
  1428. */
  1429. public function check($data, $merId)
  1430. {
  1431. if ($data['share_is'] == 1 || $data['pension_is'] == 1) {
  1432. $member_settings = merchantConfig($merId, 'member_settings');
  1433. $where['mer_id'] = $merId;
  1434. $where['state'] = 1;
  1435. $yeji = Db::name('enterprise_performance_divvy')->where($where)->sum('gdp');
  1436. $daili = Db::name('enterprise_agent_divvy')->where($where)->sum('gdp');
  1437. foreach ($data['attrValue'] as $v) {
  1438. if ($data['share_is'] == 1) {
  1439. if ($v['share_num'] < 0) {
  1440. throw new ValidateException('分享奖不能小于0');
  1441. }
  1442. $member_settings['tjj'] = $v['share_num'];
  1443. }
  1444. if ($data['pension_is'] == 1) {
  1445. if ($v['pension_num'] < 0) {
  1446. throw new ValidateException('养老金不能小于0');
  1447. }
  1448. $member_settings['ylj'] = $v['pension_num'];
  1449. }
  1450. // 推荐奖比例+养老金比例+业绩分红总比例+代理商分红总比例≤100
  1451. $bili1 = $member_settings['tjj'] + $member_settings['ylj'] + $yeji + $daili;
  1452. if ($bili1 > 100) {
  1453. throw new ValidateException('推荐奖比例+养老金比例+业绩分红总比例+代理商分红总比例≤100');
  1454. return app('json')->fail('推荐奖比例+养老金比例+业绩分红总比例+代理商分红总比例≤100');
  1455. }
  1456. // 运营中心比例+推荐代理商收益≤100-(推荐奖比例+养老金比例+业绩分红总比例+代理商分红总比例)
  1457. $bili2 = $member_settings['yy_one'] + $member_settings['dls'];
  1458. if ($member_settings['yy_kg'] != 1) {
  1459. $bili2 = $member_settings['dls'];
  1460. }
  1461. if ($bili2 <= (100 - $bili1)) {
  1462. } else {
  1463. throw new ValidateException('运营中心比例+推荐代理商收益≤100-(推荐奖比例+养老金比例+业绩分红总比例+代理商分红总比例)');
  1464. return app('json')->fail('运营中心比例+推荐代理商收益≤100-(推荐奖比例+养老金比例+业绩分红总比例+代理商分红总比例)');
  1465. }
  1466. }
  1467. }
  1468. // if(!$this->merBrandExists($data['brand_id']))
  1469. // throw new ValidateException('品牌不存在');
  1470. // if(!$this->CatExists($data['cate_id']))
  1471. // throw new ValidateException('平台分类不存在');
  1472. // if(isset($data['mer_cate_id']) && !$this->merCatExists($data['mer_cate_id'],$merId))
  1473. // throw new ValidateException('不存在的商户分类');
  1474. if (!$this->merShippingExists($merId, $data['temp_id']) && $data['temp_id'] != 105)
  1475. throw new ValidateException('运费模板不存在');
  1476. }
  1477. /**
  1478. * 通过产品id,价格,商户id,获取pv,贡献值,积分,养老金等
  1479. * @param int $good_id
  1480. * @param int $price
  1481. * @param int $dc
  1482. * @param null $product_attr_unique
  1483. * @return string
  1484. */
  1485. public function pvParm($good_id = 0, $price = 0,$mer_id=0, $dc = 0, $product_attr_unique = null)
  1486. {
  1487. $yanglao = 0;//养老金
  1488. $pv = 0;//pv值
  1489. $gongxian = 0;//贡献值
  1490. $jifen = 0;//积分
  1491. $jfduihuan = "";//积分+现金兑换
  1492. $jdduihuan = "";//京豆+现金兑换
  1493. $total_price = 0;//产品价格
  1494. $srtArry[]="";//返回的数组
  1495. $fzone_paytype = [];
  1496. // $extension_bili = 0;
  1497. //线上商品
  1498. if ($good_id > 0) {
  1499. if ($good_id == 15323) {
  1500. //19800商品固定200养老金
  1501. return 200;
  1502. }
  1503. $product = Db::name('store_product')->where('product_id', $good_id)->find();
  1504. //var_dump($product['mer_id']);
  1505. if ($price) {
  1506. $total_price = $price;
  1507. } else {
  1508. $total_price = $product['price'];
  1509. }
  1510. //plate_mer_profit 产品获得的佣金 数据库添加商品已经算好的佣金
  1511. // $pv=decimal2($product['plate_mer_profit']*0.5);//pv=佣金的50%
  1512. // $dui_jifen=decimal2($product['plate_mer_profit']*0.4);
  1513. // $dui_jingdou=decimal2($product['plate_mer_profit']*0.5);
  1514. // $dui_price=$total_price-$dui_jifen;//用积分兑换,所需要的现金 佣金的*40%
  1515. // $jidou__price=$total_price-$dui_jingdou;//用京豆兑换,所需要的现金 佣金的*50%
  1516. // $jfduihuan="{$dui_price} + {$dui_jifen}";
  1517. // $jdduihuan="{$jidou__price} + {$dui_jingdou}";
  1518. // $srtArry['jfduihuan']=$jfduihuan;
  1519. // $srtArry['jdduihuan']=$jdduihuan;
  1520. // $yanglao='';
  1521. // $pv='';
  1522. // $jifen='';
  1523. // $gongxian='';
  1524. // $jfduihuan='';
  1525. // $jdduihuan='';
  1526. //$v['vr'] = 0;
  1527. $num_yl = 0;
  1528. if($product['price'] == '1180'){
  1529. $num_yl = 1000;
  1530. }else if($product['price'] == '10620'){
  1531. $num_yl = 9000;
  1532. }else if($product['price'] == '11800'){
  1533. $num_yl = 10000;
  1534. }else if($product['price'] == '2360'){
  1535. $num_yl = 2000;
  1536. }else if($product['price'] == '9440'){
  1537. $num_yl = 8000;
  1538. }
  1539. if($product['mer_id'] == 496){
  1540. $yanglao = round($num_yl * 0.035, 2);
  1541. $pv=$num_yl *0.7;
  1542. //$v['vr']=$v['price'];
  1543. //1:为会员专区,
  1544. $fzone_paytype= [
  1545. 1=>[
  1546. 1=>$product['price'],//VR
  1547. 2=>$product['price']//现金
  1548. ]
  1549. ];
  1550. }else if($product['mer_id'] == 439){
  1551. //精彩生活和商贸区
  1552. $jf = $product['price'] - round($product['concession_pri']*0.3, 2) . ' + ' . round($product['concession_pri']*0.3, 2);
  1553. $jd = $product['price'] - round($product['concession_pri']*0.35, 2) . ' + ' . round($product['concession_pri']*0.35, 2);
  1554. if($product['concession_pri'] !=null || $product['concession_pri'] !=0){
  1555. $yanglao = round($product['concession_pri'] * 0.3, 2);
  1556. $pv = round($product['concession_pri'] * 0.35, 2);
  1557. $srtArry['jfduihuan'] =$jf;
  1558. $srtArry['jdduihuan'] = $jd;
  1559. $jifen = round($product['concession_pri'] * 0.35, 2);
  1560. $gongxian = round($product['concession_pri'] * 0.35, 2);
  1561. $jcvr = $product['price'] - round($product['concession_pri']*0.3, 2);
  1562. $jcjf = round($product['concession_pri']*0.3, 2);
  1563. $jcprice = $product['price'] - round($product['concession_pri']*0.3, 2);
  1564. $jcjf1 = round($product['concession_pri']*0.3, 2);
  1565. $jcvr1 = $product['price'] - round($product['concession_pri']*0.35, 2);
  1566. $jcjd = round($product['concession_pri']*0.35, 2);
  1567. $jcprice1 = $product['price'] - round($product['concession_pri']*0.35, 2);
  1568. $jcjd1 = round($product['concession_pri']*0.35, 2);
  1569. //支付组合
  1570. // $fzone_paytype= [
  1571. // 2=>[
  1572. // 1=>[$jcvr,$jcjf],//VR+积分
  1573. // 2=>[$jcprice,$jcjf1],//现金+积分
  1574. // 3=>[$jcvr1,$jcjd],//VR+京豆
  1575. // 4=>[$jcprice1,$jcjd1],//现金+金豆
  1576. // 5=>$product['price'],//VR
  1577. // 6=>$product['price'],//现金
  1578. // ]
  1579. // ];
  1580. $fzone_paytype= [
  1581. ['type'=>'b1','price'=>$jcvr,'other'=>$jcjf],
  1582. ['type'=>'b2','price'=>$jcprice,'other'=>$jcjf1],
  1583. ['type'=>'b3','price'=>$jcvr1,'other'=>$jcjd],
  1584. ['type'=>'b4','price'=>$jcprice1,'other'=>$jcjd1],
  1585. ['type'=>'b5','price'=>$product['price'],'other'=>0],
  1586. ['type'=>'b6','price'=>$product['price'],'other'=>0],
  1587. ];
  1588. }
  1589. }else if($product['mer_id'] == 491){
  1590. // $num_yl = $product['price'];
  1591. // $pv= $num_yl * 0.3;
  1592. // 获取商品sku
  1593. $productSku = Db::name('store_product_attr_value')->where('product_id', $good_id)->order('price asc')->find();
  1594. $pv = $productSku['plate_mer_profit']??0.00;
  1595. $yanglao = round($pv * 0.5, 2);
  1596. //$v['vr']=$v['price'];
  1597. //1:为会员专区,
  1598. $fzone_paytype= [
  1599. 1=>[
  1600. 1=>$product['price'],//VR
  1601. 2=>$product['price'],//现金
  1602. 3=>$product['price']//复购金
  1603. ]
  1604. ];
  1605. } else {
  1606. // 商贸区
  1607. /** @var MerchantRepository $merchant */
  1608. $merchant = app()->make(MerchantRepository::class);
  1609. $merchantInfo = $merchant->get($product['mer_id']);
  1610. // 1企业2个体3个人
  1611. if ($merchantInfo && in_array($merchantInfo['type'],[2,3])) {
  1612. //商贸区
  1613. $jf = $product['price'] - round($product['concession_pri']*0.3, 2) . ' + ' . round($product['concession_pri']*0.3, 2);
  1614. $jd = $product['price'] - round($product['concession_pri']*0.35, 2) . ' + ' . round($product['concession_pri']*0.35, 2);
  1615. if($product['concession_pri'] !=null || $product['concession_pri'] !=0){
  1616. $yanglao = round($product['concession_pri'] * 0.3, 2);
  1617. $pv = round($product['concession_pri'] * 0.35, 2);
  1618. $srtArry['jfduihuan'] =$jf;
  1619. $srtArry['jdduihuan'] = $jd;
  1620. $jifen = round($product['concession_pri'] * 0.35, 2);
  1621. $gongxian = round($product['concession_pri'] * 0.35, 2);
  1622. $jcvr = $product['price'] - round($product['concession_pri']*0.3, 2);
  1623. $jcjf = round($product['concession_pri']*0.3, 2);
  1624. $jcprice = $product['price'] - round($product['concession_pri']*0.3, 2);
  1625. $jcjf1 = round($product['concession_pri']*0.3, 2);
  1626. $jcvr1 = $product['price'] - round($product['concession_pri']*0.35, 2);
  1627. $jcjd = round($product['concession_pri']*0.35, 2);
  1628. $jcprice1 = $product['price'] - round($product['concession_pri']*0.35, 2);
  1629. $jcjd1 = round($product['concession_pri']*0.35, 2);
  1630. $fzone_paytype= [
  1631. // ['type'=>'b1','price'=>$jcvr,'other'=>$jcjf],
  1632. ['type'=>'b2','price'=>$jcprice,'other'=>$jcjf1],
  1633. // ['type'=>'b3','price'=>$jcvr1,'other'=>$jcjd],
  1634. ['type'=>'b4','price'=>$jcprice1,'other'=>$jcjd1],
  1635. // ['type'=>'b5','price'=>$product['price'],'other'=>0],
  1636. ['type'=>'b6','price'=>$product['price'],'other'=>0],
  1637. ];
  1638. }
  1639. }
  1640. }
  1641. }else{//线下商品
  1642. if ($mer_id>0){
  1643. $base_commission= merchantConfig($mer_id, 'base_commission') ?: [
  1644. 'base_commission' => 5
  1645. ];// 如果没有查到商户的设置,系统默认最小5%让利
  1646. $rangli=$total_price*($base_commission/100);//商户让利的金额
  1647. $pv=$rangli;//线下的pv等与让利100%
  1648. }
  1649. }
  1650. $jifen=$pv;
  1651. $gongxian=$pv;
  1652. $yanglao=$yanglao;//养老金=pv的5%
  1653. $srtArry['pv']=$pv;
  1654. $srtArry['jifen']=$jifen;
  1655. $srtArry['gongxian']=$gongxian;
  1656. $srtArry['yanglao']=$yanglao;
  1657. $srtArry['fzone_paytype']=$fzone_paytype;
  1658. // $srtArry['per_balance']=$fzone_paytype;//个人余额
  1659. // $srtArry['per_vr']=$fzone_paytype;//个人VR
  1660. // $srtArry['per_jingdou']=$fzone_paytype;//个人京豆
  1661. // $srtArry['per_score']=$fzone_paytype;//个人积分
  1662. return $srtArry;
  1663. }
  1664. /*
  1665. *
  1666. @good_id 商品id
  1667. @cabbage
  1668. @date 2020-11-18
  1669. */
  1670. /**
  1671. * 计算商品的养老金
  1672. * @param int $good_id
  1673. * @param int $price
  1674. * @param int $dc
  1675. * @param null $product_attr_unique
  1676. * @return string
  1677. */
  1678. public function yanglaojin($good_id = 0, $price = 0, $dc = 0, $product_attr_unique = null): string
  1679. {
  1680. $yanglao = 0;
  1681. $total_price = 0;
  1682. $extension_bili = 0;
  1683. if ($good_id > 0) {
  1684. if ($good_id == 15323) {
  1685. //19800商品固定200养老金
  1686. return 200;
  1687. }
  1688. $product = Db::name('store_product')->where('product_id', $good_id)->find();
  1689. if ($price) {
  1690. $total_price = $price;
  1691. } else {
  1692. $total_price = $product['price'];
  1693. }
  1694. $mer_id = $product['mer_id'];
  1695. if ($product['spu_id']) {
  1696. // 大仓商品
  1697. // 佣金设置
  1698. $commission_rate = systemConfig('commission_rate');
  1699. $yanglao = $total_price * $commission_rate['yanglao_rate'] / 100;
  1700. } else {
  1701. // 平台商品 走商户后台
  1702. // 推荐创客收益设置
  1703. $shop_bonus_setting = merchantConfig($mer_id, 'shop_bonus_setting') ?: [
  1704. 'shop_bonus_rate' => 30,
  1705. 'yanglao_rate' => 5,
  1706. ];
  1707. $shop_bonus_setting['yanglao_rate'] = $shop_bonus_setting['yanglao_rate'] ?? 5;
  1708. // if ($mer_id == 496) {
  1709. // $yanglao = $total_price * 5 / 100;
  1710. // } else {
  1711. $yanglao = $total_price * $shop_bonus_setting['yanglao_rate'] / 100;
  1712. // }
  1713. }
  1714. $yanglao= decimal2($yanglao);
  1715. } else {
  1716. $extension_bili= (float)systemConfig('extension_tao_rate');
  1717. $logs['extension_tao_rate'] = $extension_bili;
  1718. $extension['price'] = $price;
  1719. }
  1720. //养老金比例
  1721. $yanglaojin_admin= systemConfig('yanglaojin_admin');
  1722. $logs['yanglaojin_admin'] = $yanglaojin_admin;
  1723. if ($extension_bili>0){
  1724. $yanglao = $extension['price'] * $extension_bili / 100 ; //除去刷卡费 乘以返利比例
  1725. if($good_id==0){
  1726. // 三方手续费要计算刷卡手续费,前台显示要和实际给到保持一致
  1727. //刷卡手续费
  1728. $souxufees=0;
  1729. $souxufee =floatval(systemConfig('yanglao'));
  1730. $logs['yanglao'] = $souxufee;
  1731. if ($souxufee){
  1732. $souxufees = bcmul($yanglao, $souxufee * 0.01, 3);
  1733. }
  1734. //返佣比例
  1735. $base_commission= floatval(systemConfig('extension_tao_rate'));
  1736. $logs['extension_tao_rate'] = $base_commission;
  1737. //返佣总额 返利总额=(交易金额-手续费)*返利比例
  1738. log::info("计算养老金 good_id: {$good_id},yanglao : $yanglao");
  1739. $yanglao=bcmul($yanglao-$souxufees,$base_commission/100,3);
  1740. log::info("计算养老金 good_id: {$good_id},yanglao-$souxufees 保留三位$yanglao");
  1741. }
  1742. $yanglao = $yanglao * $yanglaojin_admin /100 ;//养老金比例
  1743. log::info("计算养老金 good_id: {$good_id},yanglao : $yanglao");
  1744. $yanglao= decimal2($yanglao);// 取两位小数 后面舍去
  1745. }
  1746. log::info(json_encode($logs));
  1747. log::info("计算养老金 good_id: {$good_id},price{$total_price},yanglao:{$yanglao}");
  1748. return set_price_rtrim($yanglao);
  1749. }
  1750. public function editPrice($product_id, $skuArr, $type = 1)
  1751. {
  1752. if (empty($skuArr))
  1753. throw new ApiException('参数错误');
  1754. try {
  1755. // 获取商品的规格信息
  1756. $attrInfo = Db::name('store_product_attr_value')
  1757. ->field('gong_sku_id,gong_mer_profit,price,cost,ot_price,gong_market_price,gong_pension')
  1758. ->where('product_id', $product_id)
  1759. ->select()->toArray();
  1760. $attrInfo = array_column($attrInfo, null, 'gong_sku_id');
  1761. $updateData = [];
  1762. // 组装数据
  1763. foreach ($skuArr as $key => $value) {
  1764. if (isset($attrInfo[$value['sku_id']])) {
  1765. if ($value['edit_price'] > $attrInfo[$value['sku_id']]['gong_market_price']) {
  1766. // 上调价格计算创客利润
  1767. $diff = bcsub($value['edit_price'], $attrInfo[$value['sku_id']]['gong_market_price'], 2);
  1768. $diff_profit = bcmul($diff, 0.7, 2);
  1769. $plate_mer_profit = bcadd($attrInfo[$value['sku_id']]['gong_mer_profit'], $diff_profit, 2);
  1770. } else {
  1771. // 下调价格计算创客利润
  1772. $diff = bcsub($attrInfo[$value['sku_id']]['gong_market_price'], $value['edit_price'], 2);
  1773. $plate_mer_profit = bcsub($attrInfo[$value['sku_id']]['gong_mer_profit'], $diff, 2);
  1774. if ($plate_mer_profit < 0) {
  1775. throw new ApiException('销售价格填写不正确');
  1776. }
  1777. }
  1778. $updateData[] = [
  1779. 'gong_sku_id' => $value['sku_id'],
  1780. 'price' => $value['edit_price'],
  1781. 'plate_mer_profit' => $plate_mer_profit,
  1782. 'concession_pri' => $plate_mer_profit,
  1783. 'cost' => $attrInfo[$value['sku_id']]['cost'],
  1784. 'ot_price' => $attrInfo[$value['sku_id']]['ot_price'],
  1785. 'gong_pension' => $attrInfo[$value['sku_id']]['gong_pension']
  1786. ];
  1787. }
  1788. }
  1789. if (empty($updateData))
  1790. throw new ApiException('数据错误');
  1791. $sortPrice = array_column($updateData, 'price');
  1792. array_multisort($sortPrice, SORT_ASC, $updateData);
  1793. Db::startTrans();
  1794. // 修改入库
  1795. foreach ($updateData as $udv) {
  1796. $skuModel = Db::name('store_product_attr_value')->where('product_id', $product_id)->where('gong_sku_id', $udv['gong_sku_id']);
  1797. $skuOld = $skuModel->select()->toArray();
  1798. Log::info("商家修改供应链商品sku前的数据:" . json_encode($skuOld));
  1799. $skuModel->update([
  1800. 'price' => $udv['price'],
  1801. 'plate_mer_profit' => $udv['plate_mer_profit']
  1802. ]);
  1803. }
  1804. // 同步商品库中的信息
  1805. $productModel = Db::name('store_product')->where('product_id', $product_id);
  1806. $productOld = $productModel->select()->toArray();
  1807. Log::info("商家修改供应链商品基本信息前的数据:" . json_encode($productOld));
  1808. $yanglaojin_scale = (!empty($updateData[0]['gong_pension']) && !empty($updateData[0]['price'])) ? bcdiv($updateData[0]['gong_pension'], $updateData[0]['price'], 2) : 0;
  1809. $productModel->update([
  1810. 'price' => $updateData[0]['price'],
  1811. 'cost' => $updateData[0]['cost'],
  1812. 'ot_price' => $updateData[0]['ot_price'],
  1813. 'plate_mer_profit' => $updateData[0]['plate_mer_profit'],
  1814. 'concession_pri' => $updateData[0]['plate_mer_profit'],
  1815. 'is_show' => 0,
  1816. 'yanglaojin_scale' => $yanglaojin_scale,
  1817. 'type' => $type
  1818. ]);
  1819. Db::commit();
  1820. } catch (Exception $e) {
  1821. Db::rollback();
  1822. throw new ApiException('系统繁忙');
  1823. }
  1824. }
  1825. // 判断创客利润是否大于0
  1826. public function checkGongProfit($ids)
  1827. {
  1828. foreach ($ids as $id) {
  1829. $skuInfo = Db::name('store_product_attr_value')->field('price,gong_market_price,gong_mer_profit')->where('product_id', $id)->select()->toArray();
  1830. foreach ($skuInfo as $value) {
  1831. if ($value['price'] < $value['gong_market_price']) {
  1832. $diff = bcsub($value['gong_market_price'], $value['price'], 2);
  1833. $plate_mer_profit = bcsub($value['gong_mer_profit'], $diff, 2);
  1834. if ($plate_mer_profit < 0) {
  1835. return false;
  1836. }
  1837. }
  1838. }
  1839. }
  1840. return true;
  1841. }
  1842. // 供应链商品删除
  1843. public function changeDelete($ids)
  1844. {
  1845. $data = ['is_show' => 0, 'is_del' =>1];
  1846. $this->dao->update($ids, $data);
  1847. }
  1848. // 供应链商品上下架
  1849. public function changeStatus($ids, $is_show)
  1850. {
  1851. $data = ['is_show' => $is_show];
  1852. $this->dao->updates($ids, $data);
  1853. foreach ($ids as $id) {
  1854. $res = $this->dao->get($id);
  1855. if (isset($data['status'])) {
  1856. SwooleTaskService::merchant('notice', [
  1857. 'type' => 'product',
  1858. 'data' => [
  1859. 'title' => '新审核结果',
  1860. 'message' => '您有一条新的商品审核',
  1861. 'id' => ''
  1862. ]
  1863. ], $res['mer_id']);
  1864. }
  1865. }
  1866. }
  1867. public function thirdYanglaojin($good_id = 0, $price = 0)
  1868. {
  1869. //返佣比例
  1870. $base_commission= floatval(systemConfig('extension_tao_rate'));
  1871. //刷卡手续费
  1872. $souxufees=0;
  1873. $souxufee =floatval(systemConfig('yanglao'));
  1874. if ($souxufee){
  1875. $souxufees = bcmul($price, $souxufee * 0.01, 3);
  1876. }
  1877. //返佣总额 返利总额=(交易金额-手续费)*返利比例
  1878. return decimal2(bcmul($price-$souxufees,$base_commission/100,3));
  1879. }
  1880. }