MBaseModel.Class.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  1. <?php
  2. /**
  3. * 基础Model类
  4. * Created by PhpStorm.
  5. * User: wxj
  6. * Date: 2019/11/27
  7. * Time: 12:01
  8. */
  9. namespace JinDouYun\Model;
  10. use JinDouYun\Controller\Common\Logger;
  11. use JinDouYun\Model\GoodsManage\MGoodsBasic;
  12. use JinDouYun\Model\GoodsManage\MSku;
  13. use JinDouYun\Model\Merchant\MMerchant;
  14. use JinDouYun\Model\Stock\MWarehouse;
  15. use JinDouYun\Model\System\MPrintNum;
  16. use Mall\Framework\Core\ErrorCode;
  17. use Mall\Framework\Core\StatusCode;
  18. use Mall\Framework\Core\ResultWrapper;
  19. use JinDouYun\Cache\GoodsBasicRelevant;
  20. use JinDouYun\Cache\RoleAclCache;
  21. use JinDouYun\Model\Common\MCommon;
  22. use Mall\Framework\Factory;
  23. class MBaseModel
  24. {
  25. private $onlineEnterpriseId;
  26. private $onlineUserId;
  27. //数据域相关
  28. private $objRoleAclCache;
  29. private static $dataField = [];
  30. static $salesManIds = [];//业务员ids
  31. static $staffUserCenterIds;//业务员对应的uids
  32. static $shopIds = [];//商铺ids
  33. static $warehouseIds = [];//仓库ids
  34. public function __construct($onlineEnterpriseId = '', $onlineUserId = '')
  35. {
  36. $this->onlineEnterpriseId = $onlineEnterpriseId;
  37. $this->onlineUserId = $onlineUserId;
  38. $this->objRoleAclCache = new RoleAclCache();
  39. }
  40. /**
  41. * 营销活动(领取优惠券)
  42. * @param $data
  43. * @param $className
  44. */
  45. protected static function coupon($data, $className)
  46. {
  47. $i = 1;
  48. do {
  49. $postData = [
  50. 'topicName' => 'MyJob',
  51. 'topicClass' => "Jobs\Model\MTopic\Market\\".$className,
  52. 'topicMethon' => 'push',
  53. 'topicMethonParams' => [
  54. 'userCenterId' => $data['userCenterId'],
  55. 'enterpriseId' => $data['enterpriseId'],
  56. 'customerId' => $data['customerId'],
  57. 'payAmount' => isset($data['payAmount']) ? $data['payAmount'] : 0,
  58. ],
  59. ];
  60. $url = QIANNIAO_QUEUE . '/CAddJob/add';
  61. $result = request($url, $postData);
  62. $i++;
  63. } while ($result['httpcode'] != 200 && $i <= 3);
  64. }
  65. /**
  66. * 登录注册日志
  67. * @param $data
  68. */
  69. public static function saveLoginLog($data)
  70. {
  71. $i = 1;
  72. do {
  73. $postData = [
  74. 'topicName' => 'MyJob',
  75. 'topicClass' => 'Jobs\Model\MTopic\Log\MLoginLog',
  76. 'topicMethon' => 'push',
  77. 'topicMethonParams' => [
  78. 'userCenterId' => $data['userCenterId'],
  79. 'enterpriseId' => $data['enterpriseId'],
  80. 'mobile' => $data['mobile'],
  81. 'createTime' => $data['createTime'],//操作时间
  82. 'source' => $data['source'], // 来源:
  83. 'actionType' => $data['actionType'], // 操作类型:登录/注册
  84. 'operationData' => $data['operationData'], //本次操作请求的数据(可空) json格式
  85. ],
  86. ];
  87. $url = QIANNIAO_QUEUE . '/CAddJob/add';
  88. $result = request($url, $postData);
  89. $i++;
  90. } while ($result['httpcode'] != 200 && $i <= 3);
  91. }
  92. /**
  93. * 除登录注册之外的日志记录
  94. */
  95. public static function saveLog($data)
  96. {
  97. $i = 1;
  98. do {
  99. $postData = [
  100. 'topicName' => 'MyJob',
  101. 'topicClass' => 'Jobs\Model\MTopic\Log\MLog',
  102. 'topicMethon' => 'push',
  103. 'topicMethonParams' => [
  104. 'userCenterId' => $data['userCenterId'],
  105. 'enterpriseId' => $data['enterpriseId'],
  106. 'createTime' => $data['createTime'],//操作时间
  107. 'no' => $data['no'], // 如:单据编号D563875634856 订单编号N42332435435
  108. 'actionType' => $data['actionType'], // 操作类型:下单 支付 取消订单
  109. 'operationData' => $data['operationData'], //本次操作请求的数据(可空) json格式
  110. ],
  111. ];
  112. $url = QIANNIAO_QUEUE . '/CAddJob/add';
  113. $result = request($url, $postData);
  114. $i++;
  115. } while ($result['httpcode'] != 200 && $i <= 3);
  116. }
  117. /**
  118. * 格式化skuId
  119. * @param $enterpriseId
  120. * @param $value
  121. * @return mixed
  122. * @throws \Exception
  123. */
  124. public function formatSkuId($enterpriseId, $value)
  125. {
  126. if(empty($enterpriseId) || empty($value)) return $value;
  127. $objMSku = new MSku(false, $enterpriseId);
  128. if (isset($value['skuId']) && !empty($value['skuId']) && !isset($value['unitName']) && !isset($value['skuName'])){
  129. $modelResult = $objMSku->getSkuDataBySkuIds($value['skuId']);
  130. $value['unitId'] = 0;
  131. $value['unitName'] = '';
  132. $value['skuName'] = '';
  133. $value['barCode'] = '';
  134. if($modelResult->isSuccess()){
  135. $skuData = $modelResult->getData();
  136. $value['skuName'] = $skuData[$value['skuId']]['skuName'];
  137. $value['unitId'] = $skuData[$value['skuId']]['unitId'];
  138. $value['unitName'] = $skuData[$value['skuId']]['unitName'];
  139. $value['isMaster'] = $skuData[$value['skuId']]['isMaster'];
  140. $value['barCode'] = $skuData[$value['skuId']]['barCode'];
  141. }
  142. }
  143. return $value;
  144. }
  145. /**
  146. * 究极无敌格式化订单数据方法
  147. * @param $enterpriseId
  148. * @param $array
  149. * @param bool $orderType
  150. * @param bool $isFormatNo
  151. * @return array
  152. * @throws \Exception
  153. */
  154. static function formatOrderMan($enterpriseId, $array, $orderType = false, $isFormatNo = true)
  155. {
  156. if (empty($array)) return $array;
  157. $objGoodsBasicRelevantCache = new GoodsBasicRelevant($enterpriseId);
  158. $objMPrintNum = new MPrintNum($enterpriseId);
  159. $objMCommon = new MCommon();
  160. $objMGoodsBasic = new MGoodsBasic(false, $enterpriseId);
  161. $objMSku = new MSku(false, $enterpriseId);
  162. $objMWarehouse = new MWarehouse($enterpriseId);
  163. $objMMerchant = new MMerchant($enterpriseId);
  164. $formatData = isset($array['id']) ? [$array] : $array;
  165. foreach ($formatData as &$value) {
  166. // if (isset($value['no']) && !empty($value['no'])){
  167. // $value['no'] = StatusCode::$noPrefix[13].'-'.$value['no'];
  168. // }
  169. if (isset($value['deliveryType']) && !empty($value['deliveryType'])) $value['deliveryName'] = isset(StatusCode::$deliveryType[$value['deliveryType']]) ? StatusCode::$deliveryType[$value['deliveryType']] : '';
  170. if ($orderType) $value['type'] = $orderType;
  171. if (isset($value['type']) && !empty($value['type'])) $value['no'] = StatusCode::$noPrefix[$value['type']] .'-'. $value['no'];
  172. if (isset($value['type']) && !empty($value['type'])) $value['typeName'] = $objMCommon->formatOrderSource($value['type']);
  173. if (isset($value['source']) && !empty($value['source'])) $value['sourceName'] = $objMCommon->formatOrderSource($value['source']);
  174. if (isset($value['source']) && !empty($value['source']) && $isFormatNo){
  175. $value['sourceNo'] = StatusCode::$noPrefix[$value['source']].'-' . $value['sourceNo'];
  176. isset($value['originNo']) && $value['originNo'] = StatusCode::$noPrefix[$value['source']].'-' . $value['originNo'];
  177. }
  178. if(isset($value['skuId']) && !empty($value['skuId'])){
  179. $modelResult = $objMSku->getSkuDataBySkuIds($value['skuId']);
  180. if($modelResult->isSuccess()){
  181. $skuData = $modelResult->getData();
  182. if(!isset($value['unitId']) || empty($value['unitId'])){
  183. $value['unitId'] = isset($skuData[$value['skuId']]['unitId'])?$skuData[$value['skuId']]['unitId']:'';
  184. }
  185. if(!isset($value['unitName']) || empty($value['unitName'])){
  186. $value['unitName'] = isset($skuData[$value['skuId']]['unitName'])?$skuData[$value['skuId']]['unitName']:'';
  187. }
  188. if(!isset($value['skuName']) || empty($value['skuName'])){
  189. $value['skuName'] = isset($skuData[$value['skuId']]['skuName'])?$skuData[$value['skuId']]['skuName']:'';
  190. }
  191. if(!isset($value['isMaster']) || empty($value['isMaster'])){
  192. $value['isMaster'] = isset($skuData[$value['skuId']]['isMaster'])?$skuData[$value['skuId']]['isMaster']:'';
  193. }
  194. if(!isset($value['skuBarCode']) || empty($value['skuBarCode'])){
  195. $value['skuBarCode'] = isset($skuData[$value['skuId']]['barCode'])?$skuData[$value['skuId']]['barCode']:'';
  196. }
  197. }else{
  198. $value['unitId'] = 0;
  199. $value['unitName'] = '';
  200. $value['skuName'] = '';
  201. $value['skuBarCode'] = '';
  202. }
  203. }
  204. if (isset($value['categoryId']) && !empty($value['categoryId']) && !isset($value['categoryName'])) {
  205. $value['categoryName'] = $objGoodsBasicRelevantCache->getNameByCategoryId($value['categoryId']);
  206. } else {
  207. if (isset($value['materielId']) && !isset($value['categoryName'])) {
  208. $value['categoryName'] = $objGoodsBasicRelevantCache->getNameByBasicId($value['materielId'], 'categoryName');
  209. }
  210. }
  211. //基础资料信息
  212. if ((isset($value['goodsBasicId']) && !empty($value['goodsBasicId'])) || (isset($value['basicGoodsId']) && !empty($value['basicGoodsId']))){
  213. $goodsBasicId = isset($value['goodsBasicId']) ? $value['goodsBasicId'] : $value['basicGoodsId'];
  214. $modelResult = $objMGoodsBasic->getBasicGoodsFieldByIds([$goodsBasicId]);
  215. $basicData = [];
  216. if($modelResult->isSuccess()){
  217. $basicData = $modelResult->getData();
  218. $basicData = $basicData[$goodsBasicId];
  219. }
  220. if (isset($basicData['categoryId']) && !isset($value['categoryName'])){
  221. unset($categoryName);
  222. $categoryName = $objGoodsBasicRelevantCache->getNameByCategoryId($basicData['categoryId']);
  223. if($categoryName){
  224. $value['categoryName'] = $categoryName;
  225. }
  226. }
  227. if(!isset($value['images']) || empty($value['images'])){
  228. $value['images'] = isset($basicData['images']) ? $basicData['images'] : null;
  229. }
  230. if (!isset($value['storage'])){
  231. $value['storage'] = '';
  232. if(isset($basicData['extends']) && isset($basicData['extends']['storage'])){
  233. $value['storage'] = $basicData['extends']['storage'];
  234. }
  235. }
  236. if (!isset($value['goodsBasicName']) && isset($basicData['title'])){
  237. $value['goodsBasicName'] = $basicData['title'];
  238. }
  239. if (!isset($value['isEq'])){
  240. $value['isEq'] = !empty($basicData['isEq']) ? $basicData['isEq'] : 4;
  241. }
  242. }
  243. //基础资料信息
  244. if (isset($value['materielId'])){
  245. $modelResult = $objMGoodsBasic->getBasicGoodsFieldByIds([$value['materielId']]);
  246. $basicData = [];
  247. if($modelResult->isSuccess()){
  248. $basicData = $modelResult->getData();
  249. $basicData = $basicData[$value['materielId']];
  250. }
  251. if($basicData){
  252. if(!isset($value['images']) || empty($value['images'])){
  253. $value['images'] = isset($basicData['images']) ? $basicData['images'] : null;
  254. }
  255. if (!isset($value['materielName'])){
  256. $value['materielName'] = $basicData['title'];
  257. }
  258. if (!isset($value['storage'])){
  259. $value['storage'] = '';
  260. if(isset($basicData['extends']) && isset($basicData['extends']['storage'])){
  261. $value['storage'] = $basicData['extends']['storage'];
  262. }
  263. }
  264. if (!isset($value['isEq'])){
  265. $value['isEq'] = !empty($basicData['isEq']) ? $basicData['isEq'] : 4;
  266. }
  267. }else{
  268. $value['images'] = null;
  269. $value['materielName'] = '';
  270. $value['storage'] = '';
  271. $value['isEq'] = 4;
  272. }
  273. }
  274. if((isset($value['extends']) && !empty($value['extends'])) || (isset($value['extend']) && !empty($value['extend']))){
  275. if(isset($value['extend'])){
  276. $extends = is_array($value['extend'])?$value['extend']:json_decode($value['extend'], true);
  277. }else if(isset($value['extends'])){
  278. $extends = is_array($value['extend'])?$value['extend']:json_decode($value['extends'], true);
  279. }else{
  280. $extends = [];
  281. }
  282. if(!empty($extends)){
  283. foreach($extends as $keys => $item){
  284. $value[$keys] = $item;
  285. }
  286. }
  287. }
  288. if( isset($value['batch']) && !empty($value['batch']) ){
  289. if(is_string($value['batch'] )){
  290. $value['batch'] = json_decode($value['batch'], true);
  291. }
  292. }
  293. if (isset($value['brandId']) && !empty($value['brandId']) && !isset($value['brandName'])) $value['brandName'] = $objGoodsBasicRelevantCache->getNameByBrandId($value['brandId']);
  294. if (isset($value['warehouseId']) && !empty($value['warehouseId']) && (!isset($value['warehouseName']) || empty($value['warehouseName']))) $value['warehouseName'] = $objMWarehouse->getWarehouseName($value['warehouseId']);
  295. if (isset($value['inWarehouseId']) && !empty($value['inWarehouseId']) && (!isset($value['inWarehouseName']) || empty($value['warehouseName']))) $value['inWarehouseName'] = $objMWarehouse->getWarehouseName($value['inWarehouseId']);
  296. if (isset($value['shopId']) && !empty($value['shopId']) && !isset($value['shopName'])) $value['shopName'] = $objGoodsBasicRelevantCache->getNameByShopId($value['shopId']);
  297. if (isset($value['details']) && !empty($value['details'])) $value['details'] = self::formatOrderMan($enterpriseId, $value['details']);
  298. if (isset($value['no'])) $value['printNum'] = $objMPrintNum->getObjectPrintNum($value['no']);
  299. if(isset($value['merchantId']) && (!isset($value['merchantName']) || empty($value['merchantName']))){
  300. $merchant = $objMMerchant->getMerchant($value['merchantId']);
  301. $value['merchantName'] = isset($merchant['name']) ? $merchant['name'] : '';
  302. }
  303. }
  304. unset($value);
  305. $returnData = isset($array['id']) ? array_shift($formatData) : $formatData;
  306. return $returnData;
  307. }
  308. /**
  309. * 格式化打印机数据
  310. * @param $params
  311. * @param $type 1:佳博打印机 2:飞鹅打印机
  312. * @return string
  313. */
  314. public function formatMsg($params, $type = 1)
  315. {
  316. $msgDetail = '';
  317. switch ($type){
  318. case 1:
  319. $details = $params['details'];
  320. $msgDetail .= '<gpWord Align=1 Bold=1 Wsize=1 Hsize=1 Reverse=0 Underline=0>'.$params['shopName'].'</gpWord><gpBr/>';//商铺名称
  321. $msgDetail .= '<gpWord Align=1 Bold=1 Wsize=0 Hsize=0 Reverse=0 Underline=0>'.$params['title'].'</gpWord><gpBr/>';//标题
  322. // $msgDetail .= '<gpBarCode Align=1 Type=7 Width=2 Height=80 Position=0>'.$params['code'].'</gpBarCode>';//单号
  323. $msgDetail .= '<gpWord Align=0 Bold=0 Wsize=0 Hsize=0 Reverse=0 Underline=0>'.$params['codeName'].':'.$params['code'].'</gpWord>';//单号详情
  324. $msgDetail .= '<gpWord Align=0 Bold=0 Wsize=0 Hsize=0 Reverse=0 Underline=0>订单日期:'.date('Y-m-d H:i:s', $params['createTime']).'</gpWord>';//订单日期
  325. $msgDetail .= '<gpWord Align=0 Bold=0 Wsize=0 Hsize=0 Reverse=0 Underline=0>顾客姓名:'.$params['userName'].'</gpWord>';//买家姓名
  326. $msgDetail .= '<gpWord Align=0 Bold=0 Wsize=0 Hsize=0 Reverse=0 Underline=0>顾客电话:'.$params['mobile'].'</gpWord>';//买家手机号
  327. $msgDetail .= '<gpWord Align=0 Bold=0 Wsize=0 Hsize=0 Reverse=0 Underline=0>收货地址:'.$params['address'].'</gpWord>';//买家手机号
  328. // $msgDetail .= '<gpWord Align=0 Bold=1 Wsize=0 Hsize=1 Reverse=0 Underline=0>买家留言:发顺丰,尽快发货,谢谢</gpWord>';//买家留言
  329. // $msgDetail .= '<gpWord Align=0 Bold=1 Wsize=0 Hsize=1 Reverse=0 Underline=0>卖家备注:发顺丰,优先处理</gpWord>';//卖家备注
  330. // $msgDetail .= '<gpWord Align=0 Bold=1 Wsize=0 Hsize=1 Reverse=0 Underline=0>买就送信息:送U盘</gpWord>';//活动信息 活动说明
  331. $msgDetail .= '<gpWord Align=0 Bold=0 Wsize=0 Hsize=0 Reverse=0 Underline=0>--------------------------------</gpWord>';
  332. $msgDetail .= '<gpTR4 Type=0><td>商品</td><td>单价</td><td>数量</td><td>金额</td></gpTR4>';//商品名称 商品单价 商品数量 商品总价
  333. //循环商品信息
  334. foreach($details as $value) {
  335. if(count($value['skuData']) > 1){
  336. $msgDetail .= '<gpTR4 Type=0><td>'.$value['title'].'</td><td></td><td></td><td></td></gpTR4>';
  337. foreach($value['skuData'] as $vv){
  338. $msgDetail .= '<gpTR4 Type=0><td> '.$vv['skuName'].'</td><td>'.$vv['unitPrice'].'</td><td>'.$vv['num'].$vv['unitName'].'</td><td>'.$vv['amountPrice'].'</td></gpTR4>';
  339. }
  340. }else{
  341. foreach($value['skuData'] as $vv){
  342. $msgDetail .= '<gpTR4 Type=0><td>'.$value['title'].' '.$vv['skuName'].'</td><td>'.$vv['unitPrice'].'</td><td>'.$vv['num'].$vv['unitName'].'</td><td>'.$vv['amountPrice'].'</td></gpTR4>';
  343. }
  344. }
  345. }
  346. $msgDetail .= '<gpWord Align=0 Bold=0 Wsize=0 Hsize=0 Reverse=0 Underline=0>-------------------------------- </gpWord>';
  347. $msgDetail .= '<gpTR2 Type=0><td>优惠:</td><td>-'.$params['preferential'].'元</td></gpTR2>';//优惠金额
  348. $msgDetail .= '<gpTR2 Type=0><td>合计:</td><td>'.$params['totalMoney'].'元</td></gpTR2>';//合计金额
  349. $msgDetail .= '<gpTR2 Type=0><td>应收:</td><td>'.$params['payAmount'].'元</td></gpTR2>';//应收金额
  350. $msgDetail .= '<gpCut/>';
  351. // $msgDetail .= '<gpWord Align=1 Bold=1 Wsize=1 Hsize=1 Reverse=0 Underline=0>扫码关注千鸟云商</gpWord><gpBr/>';//二维码说明
  352. // $msgDetail .= '<gpImg Align=1>http://onlineimg.qianniao.vip/b8ca10e4ce35fded735c980403dfedf7/gh_16db94f79967_430.jpg</gpImg>';//二维码地址
  353. $msgDetail .= '<gpCut/><gpBr/>';
  354. break;
  355. case 2:
  356. $details = $params['details'];
  357. $msgDetail .= '<CB>'.$params['shopName'].'</CB><BR>';
  358. $msgDetail .= '<CB>'.$params['title'].'</CB><BR>';
  359. $msgDetail .= $params['codeName'].':'.$params['code'].'<BR>';
  360. $msgDetail .= '订单日期:'.date('Y-m-d H:i:s', $params['createTime']).'<BR>';
  361. $msgDetail .= '顾客姓名:'.$params['userName'].'<BR>';
  362. $msgDetail .= '顾客电话:'.$params['mobile'].'<BR>';
  363. $msgDetail .= '收货地址:'.$params['address'].'<BR>';
  364. $msgDetail .= '--------------------------------<BR>';
  365. $msgDetail .= '商品      单价 数量 金额<BR>';
  366. foreach($details as $value) {
  367. if (count($value['skuData']) > 1) {
  368. $msgDetail .= $value['title'].'<BR>';
  369. $msgDetail .= self::formatText($value['skuData']);
  370. }else{
  371. $msgDetail .= self::formatText($value['skuData'], $value['title']);
  372. }
  373. }
  374. $msgDetail .= '--------------------------------<BR>';
  375. $msgDetail .= '优惠:<RIGHT>-'.$params['preferential'].'元</RIGHT><BR>';
  376. $msgDetail .= '合计:<RIGHT>'.$params['totalMoney'].'元</RIGHT><BR>';
  377. $msgDetail .= '应收:<RIGHT>'.$params['payAmount'].'元</RIGHT><BR>';
  378. $msgDetail .= '<CUT>';
  379. break;
  380. }
  381. return $msgDetail;
  382. }
  383. /**
  384. * 格式化飞鹅打印内容
  385. * @param $params
  386. * @param bool $title
  387. * @param int $A
  388. * @param int $B
  389. * @param int $C
  390. * @param int $D
  391. * @return string
  392. */
  393. public function formatText($params, $title = false, $A = 13,$B = 6,$C = 4,$D = 6)
  394. {
  395. $orderInfo = '';
  396. foreach ($params as $k5 => $v5) {
  397. $name = $title ? $title.' '.$v5['skuName'] : ' '.$v5['skuName'];
  398. $price = $v5['unitPrice'];
  399. $num = $v5['num'].$v5['unitName'];
  400. $prices = $v5['amountPrice'];
  401. $kw3 = '';
  402. $kw1 = '';
  403. $kw2 = '';
  404. $kw4 = '';
  405. $str = $name;
  406. $blankNum = $A;//名称控制为14个字节
  407. $lan = mb_strlen($str,'utf-8');
  408. $m = 0;
  409. $j=1;
  410. $blankNum++;
  411. $result = array();
  412. if(strlen($price) < $B){
  413. $k1 = $B - strlen($price);
  414. for($q=0;$q<$k1;$q++){
  415. $kw1 .= ' ';
  416. }
  417. $price = $price.$kw1;
  418. }
  419. if(strlen($num) < $C){
  420. $k2 = $C - strlen($num);
  421. for($q=0;$q<$k2;$q++){
  422. $kw2 .= ' ';
  423. }
  424. $num = $num.$kw2;
  425. }
  426. if(strlen($prices) < $D){
  427. $k3 = $D - strlen($prices);
  428. for($q=0;$q<$k3;$q++){
  429. $kw4 .= ' ';
  430. }
  431. $prices = $prices.$kw4;
  432. }
  433. for ($i=0;$i<$lan;$i++){
  434. $new = mb_substr($str,$m,$j,'utf-8');
  435. $j++;
  436. if(mb_strwidth($new,'utf-8')<$blankNum) {
  437. if($m+$j>$lan) {
  438. $m = $m+$j;
  439. $tail = $new;
  440. $lenght = mb_convert_encoding($new,'GBK','UTF-8');
  441. $k = $A - strlen($lenght);
  442. for($q=0;$q<$k;$q++){
  443. $kw3 .= ' ';
  444. }
  445. if($m==$j){
  446. $tail .= $kw3.' '.$price.' '.$num.' '.$prices;
  447. }else{
  448. $tail .= $kw3.'<BR>';
  449. }
  450. break;
  451. }else{
  452. $next_new = mb_substr($str,$m,$j,'utf-8');
  453. if(mb_strwidth($next_new,'utf-8')<$blankNum)
  454. continue;
  455. else{
  456. $m = $i+1;
  457. $result[] = $new;
  458. $j=1;
  459. }
  460. }
  461. }
  462. }
  463. $head = '';
  464. foreach ($result as $key=>$value) {
  465. if($key < 1){
  466. $v_lenght = mb_convert_encoding($value, 'GBK', 'UTF-8');
  467. $v_lenght = strlen($v_lenght);
  468. if($v_lenght == 13) $value = $value." ";
  469. $head .= $value.' '.$price.' '.$num.' '.$prices;
  470. }else{
  471. $head .= $value.'<BR>';
  472. }
  473. }
  474. $orderInfo .= $head.$tail;
  475. }
  476. return $orderInfo;
  477. }
  478. /**
  479. * 拼接esId
  480. * @param $enterpriseId //企业id
  481. * @param $id //自增id
  482. * @param $title //表名
  483. * @return string
  484. */
  485. public function setEsId($enterpriseId, $title, $id)
  486. {
  487. return 'EnterpriseId_' . $enterpriseId . '_'.$title.'_' . $id;
  488. }
  489. /**
  490. * 判断es返回值
  491. * @param $result
  492. * @return ResultWrapper
  493. */
  494. public function isResult($result)
  495. {
  496. if(empty($result)){
  497. return ResultWrapper::success($result);
  498. }
  499. if (isset($result['_shards']) && isset($result['_shards']['successful']) && $result['_shards']['successful'] == 1) {
  500. return ResultWrapper::success(isset($result['_id']) ? $result['_id'] : false);
  501. }
  502. // return ResultWrapper::fail($result['error']['reason'], ErrorCode::$paramError);
  503. return ResultWrapper::fail('ES添加失败', ErrorCode::$paramError);
  504. }
  505. /**
  506. * @param $enterpriseId
  507. * @param $params
  508. * @return array
  509. */
  510. protected static function formatBasic($enterpriseId, $params)
  511. {
  512. if (empty($params)) return [];
  513. }
  514. /**
  515. * 映射 分类名称,基础商品名称,品牌名称,规格名称
  516. * @param $enterpriseId
  517. * @param $params ['skuId'=>[1,2,..],'categoryId'=>[1,2,..],'brandId'=>[1,2,..],'goodsBasicId'=>[1,2,...]]
  518. * @return array
  519. */
  520. protected static function formatMapping($enterpriseId, $params)
  521. {
  522. if (empty($params)) return [];
  523. $mapping = [];
  524. if (isset($params['skuId']) && !empty($params['skuId'])) $mapping['skuData'] = self::getSkuNameByIds($enterpriseId, $params['skuId']);
  525. if (isset($params['categoryId']) && !empty($params['categoryId'])) $mapping['categoryData'] = self::getCategoryNameByIds($enterpriseId, $params['categoryId']);
  526. if (isset($params['brandId']) && !empty($params['brandId'])) $mapping['brandData'] = self::getBrandNameByIds($enterpriseId, $params['brandId']);
  527. if (isset($params['goodsBasicId']) && !empty($params['goodsBasicId'])) $mapping['basicData'] = self::getBasicNameByIds($enterpriseId, $params['goodsBasicId']);
  528. return $mapping;
  529. }
  530. /**
  531. * 获取skuName
  532. * @param $enterpriseId
  533. * @param $params
  534. * @return array
  535. */
  536. protected static function getSkuNameByIds($enterpriseId, $params)
  537. {
  538. if (empty($params)) return [];
  539. $objGoodsBasicRelevantCache = new GoodsBasicRelevant($enterpriseId);
  540. $allSkuData = [];
  541. foreach ($params as $skuId) {
  542. $name = $objGoodsBasicRelevantCache->getNameBySkuId($skuId);
  543. $allSkuData[$skuId] = $name;
  544. }
  545. return $allSkuData;
  546. }
  547. /**
  548. * 格式化查询语句
  549. * @param array $where
  550. * @param string $whereSql
  551. * @return string
  552. */
  553. public function formatSqlWhere($where = [], $whereSql = '')
  554. {
  555. foreach($where as $key => $value){
  556. if(empty($value)){
  557. continue;
  558. }
  559. switch ($key){
  560. case 'createTime':
  561. if(is_array($value)){
  562. $end = $value['end'];
  563. if(isset($value['star'])){
  564. $start = $value['star'];
  565. }else{
  566. $start = $value['start'];
  567. }
  568. $whereSql .= (!empty($whereSql) ? ' and ' : '').'('.$key.' between '.$start.' and '.$end.')';
  569. }else{
  570. $whereSql .= (!empty($whereSql) ? ' and ' : '').$key.' = '.$value;
  571. }
  572. break;
  573. case 'search':
  574. if(is_array($value)){
  575. $likeSql = '';
  576. foreach($value as $k => $v){
  577. $likeSql .= (!empty($likeSql) ? ' or ': '').$k.' like "%'.$v.'%"';
  578. }
  579. $whereSql .= (!empty($whereSql) ? ' and ' : '').'('.$likeSql.')';
  580. }else{
  581. $whereSql .= (!empty($whereSql) ? ' and ' : '').$value;
  582. }
  583. break;
  584. case 'neq':
  585. if(isset($value[0]) && isset($value[1])){
  586. $whereSql .= (!empty($whereSql) ? ' and ' : '').$value[0].' <> '.$value[1];
  587. }
  588. break;
  589. default:
  590. if(is_array($value)){
  591. $whereSql .= (!empty($whereSql) ? ' and ' : '').$key.' in ('.implode(",", $value).')';
  592. }else{
  593. $whereSql .= (!empty($whereSql) ? ' and ' : '').$key.' = '.$value;
  594. }
  595. break;
  596. }
  597. }
  598. return $whereSql;
  599. }
  600. /**
  601. * categoryName
  602. * @param $enterpriseId
  603. * @param $params
  604. * @return array
  605. */
  606. protected static function getCategoryNameByIds($enterpriseId, $params)
  607. {
  608. if (empty($params)) return [];
  609. $objGoodsBasicRelevantCache = new GoodsBasicRelevant($enterpriseId);
  610. $allCategoryData = [];
  611. foreach ($params as $categoryId) {
  612. $name = $objGoodsBasicRelevantCache->getNameByCategoryId($categoryId);
  613. $allCategoryData[$categoryId] = $name;
  614. }
  615. return $allCategoryData;
  616. }
  617. /**
  618. * brandName
  619. * @param $enterpriseId
  620. * @param $params
  621. * @return array
  622. */
  623. protected static function getBrandNameByIds($enterpriseId, $params)
  624. {
  625. if (empty($params)) return [];
  626. $objGoodsBasicRelevantCache = new GoodsBasicRelevant($enterpriseId);
  627. $allBrandData = [];
  628. foreach ($params as $brandId) {
  629. $name = $objGoodsBasicRelevantCache->getNameByBrandId($brandId);
  630. $allBrandData[$brandId] = isset($name) ? $name : '';
  631. }
  632. return $allBrandData;
  633. }
  634. /**
  635. * BasicName
  636. * @param $enterpriseId
  637. * @param $params
  638. * @return array
  639. */
  640. protected static function getBasicNameByIds($enterpriseId, $params)
  641. {
  642. if (empty($params)) return [];
  643. $objGoodsBasicRelevantCache = new GoodsBasicRelevant($enterpriseId);
  644. $allBasicData = [];
  645. foreach ($params as $basicId) {
  646. $name = $objGoodsBasicRelevantCache->getNameByBasicId($basicId);
  647. $allBasicData[$basicId] = isset($name) ? $name : '';
  648. }
  649. return $allBasicData;
  650. }
  651. /**
  652. * 数据域查询条件 业务员ID 数据库存的是staff的id用这个
  653. * @param mixed $selectParams
  654. * @param string $fieldsNameOfStaffId
  655. * @return mixed|null
  656. */
  657. public function getSalesManQueryParams($selectParams = null, $fieldsNameOfStaffId = 'salesManId')
  658. {
  659. // 限制查看的业务员ids
  660. // self::getAccessSalesManIds();
  661. if (empty(self::$salesManIds)) {
  662. return $selectParams;
  663. }
  664. if (is_array($selectParams)) {
  665. $selectParams[$fieldsNameOfStaffId] = self::$salesManIds;
  666. return $selectParams;
  667. }
  668. $condition = '';
  669. if (!empty($selectParams)) {
  670. $condition .= ' AND ';
  671. }
  672. $selectParams .= $condition;
  673. $selectParams .= $fieldsNameOfStaffId;
  674. !empty(self::$salesManIds) ? self::$salesManIds : '';
  675. $selectParams .= is_array(self::$salesManIds) ? ' in (' . implode(',', self::$salesManIds) . ')' : '=' . self::$salesManIds;
  676. return $selectParams;
  677. }
  678. /**
  679. * 数据域查询条件 商铺ID
  680. * @param null $selectParams
  681. * @param string $fieldsNameOfShopId
  682. * @return array|string|null
  683. */
  684. public function getShopIdQueryParams($selectParams = null, $fieldsNameOfShopId = 'shopId')
  685. {
  686. self::getAccessShopIds();
  687. if (empty(self::$shopIds)) {
  688. return $selectParams;
  689. }
  690. if (is_array($selectParams)) {
  691. $selectParams[$fieldsNameOfShopId] = self::$shopIds;
  692. return $selectParams;
  693. }
  694. $condition = '';
  695. if (!empty($selectParams)) {
  696. $condition .= ' AND ';
  697. }
  698. $selectParams .= $condition;
  699. $selectParams .= $fieldsNameOfShopId;
  700. $selectParams .= ' in (' . implode(',', self::$shopIds) . ')';
  701. return $selectParams;
  702. }
  703. /**
  704. * 数据域查询条件
  705. * @param null $selectParams
  706. * @param string $field
  707. * @return array|string|null
  708. */
  709. public function getWarehouseIdQueryParams($selectParams = null, $field = 'warehouseId')
  710. {
  711. self::getAccessWarehouseIds($field);
  712. if(!empty(self::$warehouseIds)){
  713. $fieldData = self::$warehouseIds;
  714. }else{
  715. $fieldData = self::$shopIds;
  716. }
  717. if(empty($fieldData)){
  718. return $selectParams;
  719. }
  720. if (is_array($selectParams)) {
  721. $selectParams[$field] = $fieldData;
  722. return $selectParams;
  723. }
  724. $condition = '';
  725. if (!empty($selectParams)) {
  726. $condition .= ' AND ';
  727. }
  728. $selectParams .= $condition;
  729. $selectParams .= $field;
  730. $selectParams .= ' in (' . implode(',', $fieldData) . ')';
  731. return $selectParams;
  732. }
  733. /**
  734. * 数据域查询条件 数据库存的是staff的uid用这个
  735. * @param null $selectParams
  736. * @param string $field
  737. * @return array|string|null
  738. */
  739. public function getStaffQueryParams($selectParams = null, $field = 'salesManId')
  740. {
  741. self::getAccessStaffUserCenterIds();
  742. if (empty(self::$staffUserCenterIds)) {
  743. return $selectParams;
  744. }
  745. if (is_array($selectParams)) {
  746. $selectParams[$field] = self::$staffUserCenterIds;
  747. return $selectParams;
  748. }
  749. $condition = '';
  750. if (!empty($selectParams)) {
  751. $condition .= ' AND ';
  752. }
  753. $selectParams .= $condition;
  754. $selectParams .= $field;
  755. $selectParams .= ' in (' . implode(',', self::$staffUserCenterIds) . ')';
  756. return $selectParams;
  757. }
  758. //限制查看的业务员ids
  759. public function getAccessSalesManIds()
  760. {
  761. // 查询当前登录用户的数据域
  762. if (empty(self::$dataField)) {
  763. self::getDataFieldOfOnlineUser();
  764. }
  765. $dataField = self::$dataField;
  766. if (!isset($dataField['dataField'])) {
  767. self::$salesManIds = [];
  768. return;
  769. }
  770. //设置了数据域
  771. switch ($dataField['dataField']) {
  772. case StatusCode::$dataFieldType['person'] : // 个人
  773. $staffId = $this->objRoleAclCache->getStaffIdAndUserCenterId($this->onlineEnterpriseId, $this->onlineUserId);
  774. self::$salesManIds = (!empty($staffId)) ? [$staffId] : [];
  775. break;
  776. case StatusCode::$dataFieldType['staff']: // 员工
  777. self::$salesManIds = isset($dataField['staffIds']) ? $dataField['staffIds'] : [];
  778. break;
  779. case StatusCode::$dataFieldType['all']: // 所有
  780. self::$salesManIds = [];
  781. break;
  782. default:
  783. self::$salesManIds = [];
  784. }
  785. }
  786. //限制查看的商铺ids
  787. public function getAccessShopIds()
  788. {
  789. if (empty(self::$dataField)) {
  790. self::getDataFieldOfOnlineUser();
  791. }
  792. $dataField = self::$dataField;
  793. if (!isset($dataField['shopIds'])) {
  794. self::$shopIds = [];
  795. return;
  796. }
  797. self::$shopIds = $dataField['shopIds'];
  798. }
  799. //限制查看的仓库ids
  800. public function getAccessWarehouseIds($field = '')
  801. {
  802. if (empty(self::$dataField)) {
  803. self::getDataFieldOfOnlineUser();
  804. }
  805. if (empty(self::$shopIds)) {
  806. self::getAccessShopIds();
  807. }
  808. if (empty(self::$shopIds)) {
  809. self::$warehouseIds = [];
  810. return self::$warehouseIds;
  811. }
  812. if(!empty($field) && $field == 'shopId'){
  813. return self::$shopIds;
  814. }
  815. //查出商铺下的仓库ids
  816. foreach (self::$shopIds as $shopId) {
  817. $warehouseId = $this->objRoleAclCache->getShopIdAndWarehouseId($this->onlineEnterpriseId, $shopId);
  818. if ($warehouseId) {
  819. self::$warehouseIds[] = $warehouseId;
  820. }
  821. }
  822. return self::$warehouseIds;
  823. }
  824. //限制查看的业务员对应的userCenterIds
  825. public function getAccessStaffUserCenterIds()
  826. {
  827. if (empty(self::$dataField)) {
  828. self::getDataFieldOfOnlineUser();
  829. }
  830. if (empty(self::$salesManIds)) {
  831. self::getAccessSalesManIds();
  832. }
  833. if (empty(self::$salesManIds)) {
  834. self::$staffUserCenterIds = [];
  835. return self::$staffUserCenterIds;
  836. }
  837. if (is_array(self::$salesManIds)) {
  838. //查出员工id对应的uid
  839. foreach (self::$salesManIds as $staffId) {
  840. $uid = $this->objRoleAclCache->getUserCenterIdAndStaffId($this->onlineEnterpriseId, $staffId);
  841. if ($uid) {
  842. self::$staffUserCenterIds[] = $uid;
  843. }
  844. }
  845. } else {
  846. $userCenterId = $this->objRoleAclCache->getUserCenterIdAndStaffId($this->onlineEnterpriseId, self::$salesManIds);
  847. if ($userCenterId) {
  848. self::$staffUserCenterIds[] = $userCenterId;
  849. }
  850. }
  851. return self::$staffUserCenterIds;
  852. }
  853. //获取当前登录客户的数据域
  854. private function getDataFieldOfOnlineUser()
  855. {
  856. $dataField = $this->objRoleAclCache->getStaffUidAndDataField($this->onlineEnterpriseId, $this->onlineUserId);
  857. self::$dataField = $dataField;
  858. }
  859. /**
  860. * 格式化es搜索查询数据
  861. * @param $data
  862. * @param bool $enterpriseId
  863. * @param bool $orderType
  864. * @return mixed
  865. * @throws \Exception
  866. */
  867. static function formatEsSelectData($data, $enterpriseId = false, $orderType = false)
  868. {
  869. if (empty($data)) return $data['data'] = [];
  870. $returnData['data'] = [];
  871. $returnData['total'] = $data['hits']['total'];
  872. foreach ($data['hits']['hits'] as $value) {
  873. $returnData['data'][] = $value['_source'];
  874. }
  875. $enterpriseId && $returnData['data'] = self::formatOrderMan($enterpriseId, $returnData['data'], $orderType, false);
  876. return $returnData;
  877. }
  878. /**
  879. * Doc: (des="websocket推送消息")
  880. * User: XMing
  881. * Date: 2020/11/10
  882. * Time: 9:44 上午
  883. * @param int $shopId 店铺id
  884. * @param string $control 控制器
  885. * @return ResultWrapper
  886. * @throws \Exception
  887. */
  888. public static function sendMessage(int $shopId,string $control,$source = 'order')
  889. {
  890. try {
  891. echo '-1';
  892. $swooleServerConfigData = Factory::config()->getAppoint('swooleServer', 'order');
  893. if (empty($swooleServerConfigData)) {
  894. return ResultWrapper::fail('swoole服务端链接信息配置项错误',ErrorCode::$paramError);
  895. }
  896. echo '-3';
  897. var_dump($swooleServerConfigData);
  898. $swooleClient = Factory::swoole($swooleServerConfigData, 'WebsocketClient');
  899. // 触发商家新订单通知
  900. $data = [
  901. 'controller' => $control,
  902. 'action' => 'autoSend',
  903. 'params' => [
  904. 'platfrom' => SOOOLE_SOURCE,
  905. 'shop_id' => $shopId,
  906. 'source' => $source
  907. ]
  908. ];
  909. echo '-3';
  910. $swooleClient->sendJson($data);
  911. echo '-4';
  912. return ResultWrapper::success('发送消息成功!');
  913. }catch (\Throwable $exception){
  914. echo '-error:'.$exception->getMessage();
  915. return ResultWrapper::fail($exception->getMessage(),ErrorCode::$serviceError);
  916. }
  917. }
  918. }