MCustomerPriceAdjustment.Class.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. <?php
  2. /**
  3. * 客户调价单管理
  4. * Created by PhpStorm.
  5. * User: phperstar
  6. * Date: 2019/4/26
  7. * Time: 9:41 AM
  8. */
  9. namespace JinDouYun\Model\Price;
  10. use JinDouYun\Cache\PriceCache;
  11. use JinDouYun\Controller\Common\Logger;
  12. use JinDouYun\Dao\Customer\DCustomer;
  13. use JinDouYun\Model\GoodsManage\MSku;
  14. use Mall\Framework\Core\ErrorCode;
  15. use Mall\Framework\Core\ResultWrapper;
  16. use Mall\Framework\Core\StatusCode;
  17. use JinDouYun\Model\MBaseModel;
  18. use JinDouYun\Dao\Price\DCustomerPriceEffective;
  19. use JinDouYun\Dao\Price\DCustomerPriceAdjustment;
  20. class MCustomerPriceAdjustment extends MBaseModel
  21. {
  22. private $onlineUserId;
  23. private $onlineEnterpriseId;
  24. private $objDCustomerPriceAdjustment;
  25. private $objPriceCache;
  26. /**
  27. * MCustomerPriceAdjustment constructor.
  28. * @param $onlineUserId
  29. * @param $onlineEnterpriseId
  30. * @throws \Exception
  31. */
  32. public function __construct($onlineUserId, $onlineEnterpriseId)
  33. {
  34. $this->onlineUserId = $onlineUserId;
  35. $this->onlineEnterpriseId = $onlineEnterpriseId;
  36. parent::__construct($this->onlineEnterpriseId, $this->onlineUserId);
  37. $this->objPriceCache = new PriceCache($this->onlineEnterpriseId);
  38. $this->objDCustomerPriceAdjustment = new DCustomerPriceAdjustment();//DCustomerPriceAdjustment
  39. $this->objDCustomerPriceAdjustment->setTable($this->objDCustomerPriceAdjustment->get_Table() . '_' . $this->onlineEnterpriseId . '_' . date('Y') . '_' . ceil(date('m') / 3));
  40. $this->objDCustomerPriceAdjustment->setSearchIndex('customerPriceAdjustmentSheet_search')->setType('customerPriceAdjustmentSheet');
  41. }
  42. /**
  43. * 客户调价单添加
  44. */
  45. public function add($sheetData)
  46. {
  47. // 保存调单价数据
  48. $no = createOrderSn(StatusCode::$source['manage'], StatusCode::$orderType['customerPriceAdjustment'], $this->onlineUserId);
  49. foreach ($sheetData as $key => $value) {
  50. unset($sheetData[$key]['effective']);
  51. $sheetData[$key]['no'] = $no;
  52. }
  53. $dbResult = $this->objDCustomerPriceAdjustment->insert($sheetData, true);
  54. if ($dbResult === false) {
  55. return ResultWrapper::fail($this->objDCustomerPriceAdjustment->error(), ErrorCode::$dberror);
  56. }
  57. if (is_array($dbResult)) {
  58. foreach ($dbResult as $key => $id) {
  59. if (isset($sheetData[$key])) {
  60. $sheetData[$key]['id'] = $id;
  61. }
  62. }
  63. }
  64. self::updateEsData($sheetData);
  65. return ResultWrapper::success($dbResult);
  66. }
  67. /**
  68. * 更新ES
  69. * @param $data
  70. * @return mixed
  71. */
  72. public function updateEsData($data)
  73. {
  74. if (empty($data)) return $data;
  75. foreach ($data as $key => $val) {
  76. $esData = [
  77. 'enterpriseId' => $this->onlineEnterpriseId,
  78. 'id' => $val['id'],//自增id,
  79. 'no' => $val['no'],//调价单号,
  80. 'goodsCode' => isset($val['goodsCode']) ? $val['goodsCode'] : createCode(StatusCode::$code['goodsBasic']['prefix'], $val['goodsId'], StatusCode::$code['goodsBasic']['length']),//商品编号
  81. 'goodsName' => $val['goodsName'],//商品名称
  82. 'goodsId' => $val['goodsId'],//商品id
  83. 'shopId' => $val['shopId'],//店铺id
  84. 'shopName' => $val['shopName'],//店铺名称
  85. 'createUserId' => $val['createUserId'],//创建人id
  86. 'createUserName' => $val['createUserName'],//创建人姓名
  87. 'effectiveUserName' => isset($val['effectiveUserName']) ? $val['effectiveUserName'] : '',//生效人姓名
  88. 'effectiveUserId' => isset($val['effectiveUserId']) ? $val['effectiveUserId'] : '',//生效人id
  89. 'deleteStatus' => StatusCode::$standard,//删除状态 4删除 5正常
  90. 'effectiveStatus' => isset($val['effectiveStatus']) ? $val['effectiveStatus'] : StatusCode::$auditStatus['auditPass'],//生效状态 1.待审 2审核通过 3未通过 4审核中
  91. 'createTime' => isset($val['createTime']) ? $val['createTime'] : time(),//创建时间
  92. 'updateTime' => isset($val['updateTime']) ? $val['updateTime'] : time(),//修改时间
  93. 'salePrice' => json_decode($val['salePrice'], true),//销售价格
  94. 'customerId' => $val['customerId'],
  95. 'customerName' => $val['customerName'],
  96. 'customerCode' => '',
  97. ];
  98. $_id = self::createEsDocumentId($val['id']);
  99. $result = $this->objDCustomerPriceAdjustment->addUpSearchIndexDocument($esData, $_id);
  100. if (isset($result['_shards']) && isset($result['_shards']['successful']) && $result['_shards']['successful'] == 1) {
  101. //echo "es操作成功";
  102. //return;
  103. }
  104. //echo "es操作失败";
  105. }
  106. }
  107. /**
  108. * 生成文档id
  109. * @param $id
  110. * @return string
  111. */
  112. private function createEsDocumentId($id)
  113. {
  114. return 'EnterpriseId_' . $this->onlineEnterpriseId . '_id_' . $id;
  115. }
  116. /**
  117. * 生成单据编号
  118. */
  119. public function createNo($createUserId)
  120. {
  121. return date('YmdHis') . substr(strval($createUserId + 100), -2) . substr(microtime(), 2, 4);
  122. }
  123. /**
  124. * 获取指定用户区域可售商品
  125. */
  126. public function goodsLists($params)
  127. {
  128. // 判断用户所在区域是否在店铺经营范围内
  129. $objDCloudShopRegion = new DCloudShopRegion();
  130. $condition = 'shop_id = ' . $params['shopId'] . ' and FIND_IN_SET(' . $params['provinceId'] . ', province_id) and FIND_IN_SET(' . $params['cityId'] . ', city_id) and FIND_IN_SET(' . $params['districtId'] . ', district)';
  131. $shop_region = $objDCloudShopRegion->get($condition, 'id');
  132. if ($shop_region == false) {
  133. return ResultWrapper::fail($objDCloudShopRegion->error(), ErrorCode::$dberror);
  134. } else if (empty($shop_region)) {
  135. return ResultWrapper::success([]);
  136. }
  137. // 查询店铺中在该区域内的商品
  138. $pageData = pageToOffset($params['page'], $params['pageSize']);
  139. $condition = 'shop_id = ' . $params['shopId'] . ' and is_delete = 0 and ( is_area_sale = 1 or is_area_sale = 2 and FIND_IN_SET(' . $params['provinceId'] . ', province_id) and FIND_IN_SET(' . $params['cityId'] . ', city_id) and FIND_IN_SET(' . $params['districtId'] . ', district_id)) ';
  140. if (isset($params['keyword'])) {
  141. $condition = $condition . 'and goods_name like "%' . $params['keyword'] . '%"';
  142. }
  143. $total = $this->objDNsGoods->count($condition);
  144. $dbResult = $this->objDNsGoods->select($condition, 'materialCode,marketCode,goods_name,state', 'goods_id desc', $pageData['limit'], $pageData['offset']);
  145. if ($dbResult === false) {
  146. return ResultWrapper::fail($this->objDNsGoods->error(), ErrorCode::$dberror);
  147. } else {
  148. $return = [
  149. 'data' => $dbResult,
  150. 'total' => $total,
  151. ];
  152. return ResultWrapper::success($return);
  153. }
  154. }
  155. /**
  156. * 获取所有调价单
  157. */
  158. public function getAll($params,$export = 0)
  159. {
  160. $pageData = pageToOffset($params['page'], $params['pageSize']);
  161. if($export){
  162. $pageData['limit'] = null;
  163. $pageData['offset'] = null;
  164. }
  165. $condition = null;
  166. if ($params['keyword']) {
  167. $condition = "(goodsName like '%" . $params['keyword'] . "%' or goodsCode like '%" . $params['keyword'] . "%' or no like '%".$params['keyword']."%')";
  168. }
  169. if ($params['startTime'] && $params['endTime']) {
  170. if ($condition !== null) {
  171. $condition .= ' and ';
  172. }
  173. $condition .= " createTime >= " . $params['startTime'] . " and createTime <=" . $params['endTime'];
  174. }
  175. if ($params['effectiveStatus']) {
  176. if ($condition !== null) {
  177. $condition .= ' and ';
  178. }
  179. $condition .= " effectiveStatus = " . $params['effectiveStatus'];
  180. }
  181. if (isset($params['goodsId']) && !empty($params['goodsId'])) {
  182. if ($condition !== null) {
  183. $condition .= ' and ';
  184. }
  185. $condition .= " goodsId = " . $params['goodsId'];
  186. }
  187. if (isset($params['shopId']) && !empty($params['shopId'])){
  188. if ($condition !== null) {
  189. $condition .= ' and ';
  190. }
  191. $condition .= " shopId = " . $params['shopId'];
  192. }
  193. $condition = parent::getShopIdQueryParams($condition);
  194. $total = $this->objDCustomerPriceAdjustment->count($condition);
  195. $dbResult = $this->objDCustomerPriceAdjustment->select($condition, '*', 'id desc', $pageData['limit'], $pageData['offset']);
  196. if ($dbResult === false) {
  197. return ResultWrapper::fail($this->objDCustomerPriceAdjustment->error(), ErrorCode::$dberror);
  198. }
  199. $return = [
  200. 'data' => self::format($dbResult),
  201. 'total' => $total,
  202. ];
  203. //客户调价单导出
  204. if($export){
  205. self::exportCustomerPriceSheet($return['data']);
  206. exit;
  207. }
  208. return ResultWrapper::success($return);
  209. }
  210. /**
  211. * 格式化返回数据
  212. * @param $data
  213. * @return mixed
  214. * @throws \Exception
  215. */
  216. public function format($data)
  217. {
  218. $skuIds = [];
  219. foreach ($data as $key => &$val) {
  220. if (!is_array($val['salePrice'])){
  221. $val['salePrice'] = json_decode($val['salePrice'], true);
  222. }
  223. foreach ($val['salePrice'] as $row){
  224. $skuIds[] = $row['skuId'];
  225. }
  226. }
  227. $objMSku = new MSku($this->onlineUserId,$this->onlineEnterpriseId);
  228. $skuResult = $objMSku->getSpecNameBySkuId($skuIds);
  229. $allSkuData = [];
  230. if ($skuResult->isSuccess()){
  231. $allSkuData = $skuResult->getData();
  232. }
  233. foreach ($data as &$value){
  234. foreach ($value['salePrice'] as &$row){
  235. $row['unitName'] = isset($allSkuData[$row['skuId']]) ? $allSkuData[$row['skuId']]['unitName'] : '';
  236. $row['specGroup'] = isset($allSkuData[$row['skuId']]) ? $allSkuData[$row['skuId']]['specGroup'] : [];
  237. if (empty($row['specGroup'])){
  238. $value['specType'] = StatusCode::$specType['single'];
  239. }else{
  240. $value['specType'] = StatusCode::$specType['multiple'];
  241. }
  242. }
  243. }
  244. return $data;
  245. }
  246. /**
  247. * 生效操作
  248. * @param $id
  249. * @param $params
  250. * @return ResultWrapper
  251. * @throws \Exception
  252. */
  253. public function effective($id, $params)
  254. {
  255. // 计算客户调价单分表
  256. $tableName = 'qianniao_customerPriceAdjustmentSheet_' . $this->onlineEnterpriseId . '_' . date('Y', $params['createTime']) . '_' . ceil(date('m', $params['createTime']) / 3);
  257. $this->objDCustomerPriceAdjustment->setTable($tableName);
  258. $priceAdjustment = $this->objDCustomerPriceAdjustment->get($id);
  259. if ($priceAdjustment === false) {
  260. return ResultWrapper::fail($this->objDCustomerPriceAdjustment->error(), ErrorCode::$dberror);
  261. }
  262. if (empty($priceAdjustment)) {
  263. return ResultWrapper::fail('要生效的调价单不存在', ErrorCode::$contentNotExists);
  264. }
  265. $this->objDCustomerPriceAdjustment->beginTransaction();
  266. // 将调价单变更为生效状态
  267. $dbResult = $this->objDCustomerPriceAdjustment->update(
  268. [
  269. 'effectiveStatus' => StatusCode::$auditStatus['auditPass'],
  270. 'effectiveUserName' => $params['effectiveUserName'],
  271. 'effectiveUserId' => $params['effectiveUserId'],
  272. ]
  273. , $id);
  274. if ($dbResult === false) {
  275. $this->objDCustomerPriceAdjustment->rollBack();
  276. return ResultWrapper::fail($this->objDCustomerPriceAdjustment->error(), ErrorCode::$dberror);
  277. }
  278. // 记录最后生效数据
  279. unset($dbResult);
  280. $objDCustomerPriceEffective = new DCustomerPriceEffective();
  281. $objDCustomerPriceEffective->setTable('qianniao_customerPriceEffective_' . $this->onlineEnterpriseId);
  282. $data = [
  283. 'no' => $priceAdjustment['no'],
  284. 'goodsId' => $priceAdjustment['goodsId'],
  285. 'shopId' => $priceAdjustment['shopId'],
  286. 'salePrice' => $priceAdjustment['salePrice'],
  287. 'startTime' => $priceAdjustment['startTime'],
  288. 'endTime' => $priceAdjustment['endTime'],
  289. 'customerId' => $priceAdjustment['customerId'],
  290. ];
  291. $dbResult = $objDCustomerPriceEffective->replace($data);
  292. if ($dbResult === false) {
  293. $this->objDCustomerPriceAdjustment->rollBack();
  294. return ResultWrapper::fail($objDCustomerPriceEffective->error(), ErrorCode::$dberror);
  295. }
  296. $_id = self::createEsDocumentId($id);
  297. $this->objDCustomerPriceAdjustment->esupdateTypeFieldVaule([
  298. 'effectiveStatus' => StatusCode::$auditStatus['auditPass'],
  299. 'effectiveUserName' => isset($params['effectiveUserName']) ? $params['effectiveUserName'] : '',
  300. 'effectiveUserId' => isset($params['effectiveUserId']) ? $params['effectiveUserId'] : '',
  301. ], $_id);
  302. //删除缓存
  303. $this->objPriceCache->delCustomerAdj($priceAdjustment['goodsId'],$priceAdjustment['customerId']);
  304. $this->objDCustomerPriceAdjustment->commit();
  305. return ResultWrapper::success($dbResult);
  306. }
  307. /**
  308. * 获取指定用户的可用的调价单
  309. * @param int $shopId 店铺id
  310. * @param int $customerId 客户id
  311. * @param array $goodsIds 店铺下商品集合
  312. * @return ResultWrapper
  313. */
  314. public function getCustomerPriceByIds($shopId, $customerId, $goodsIds)
  315. {
  316. $objDCustomerPriceEffective = new DCustomerPriceEffective();
  317. $objDCustomerPriceEffective->setTable('qianniao_customerPriceEffective_' . $this->onlineEnterpriseId);
  318. $where = 'shopId = ' . $shopId . ' and customerId = ' . $customerId. ' and goodsId in ('.implode(',', $goodsIds).')';
  319. $dbResult = $objDCustomerPriceEffective->select($where, 'goodsId,salePrice', 'endTime desc');
  320. if ($dbResult === false) {
  321. return ResultWrapper::fail($objDCustomerPriceEffective->error(), ErrorCode::$dberror);
  322. }
  323. return ResultWrapper::success($dbResult);
  324. }
  325. /**
  326. * 客户调价单搜索
  327. * @param $selectParams
  328. * @return ResultWrapper
  329. */
  330. public function search($selectParams)
  331. {
  332. $defaultDSL = [
  333. 'from' => $selectParams['offset'],
  334. 'size' => $selectParams['limit'],
  335. 'sort' => [
  336. 'createTime' => [
  337. 'order' => 'desc'
  338. ],
  339. ],
  340. ];
  341. $dsl = [];
  342. $dsl['query']['bool']['must'][] = [
  343. 'term' => ['enterpriseId' => $this->onlineEnterpriseId],
  344. ];
  345. //店铺id
  346. parent::getAccessShopIds();
  347. if (parent::$shopIds) {
  348. $dsl['query']['bool']['filter'][] = [
  349. 'terms' => ['shopId' => parent::$shopIds]
  350. ];
  351. }
  352. //订单编号,收货人,商品名称
  353. if (!empty($selectParams['keyword'])) {
  354. $dsl['query']['bool']['must'][] = [
  355. 'multi_match' => [
  356. 'fields' => ['goodsCode', 'goodsName'],
  357. 'query' => $selectParams['keyword'],
  358. 'fuzziness' => 'AUTO',
  359. ],
  360. ];
  361. }
  362. //生效状态
  363. if (!empty($selectParams['effectiveStatus'])) {
  364. $dsl['query']['bool']['filter'][] =
  365. ['term' => ['effectiveStatus' => $selectParams['effectiveStatus']]];
  366. }
  367. //时间
  368. if (!empty($selectParams['startTime']) && !empty($selectParams['endTime'])) {
  369. $dsl['query']['bool']['must'][] = [
  370. 'range' => [
  371. 'createTime' => [
  372. 'gte' => $selectParams['startTime'],
  373. 'lte' => $selectParams['endTime'],
  374. ]
  375. ]
  376. ];
  377. } else {
  378. if (!empty($selectParams['startTime'])) {
  379. $dsl['query']['bool']['must'][] = [
  380. 'range' => [
  381. 'createTime' => [
  382. 'gte' => $selectParams['startTime'],
  383. ]
  384. ]
  385. ];
  386. }
  387. if (!empty($selectParams['endTime'])) {
  388. $dsl['query']['bool']['must'][] = [
  389. 'range' => [
  390. 'createTime' => [
  391. 'lte' => $selectParams['endTime'],
  392. ]
  393. ]
  394. ];
  395. }
  396. }
  397. $dsl = array_merge($defaultDSL, $dsl);
  398. $result = $this->objDCustomerPriceAdjustment->getSearchQueryDsl($dsl);
  399. if (isset($result['status']) && $result['status'] == 400) {
  400. return ResultWrapper::fail('获取数据失败' . $result['error']['reason'], ErrorCode::$apiNotResult);
  401. }
  402. $total = $result['hits']['total'];
  403. $dbResult = $result['hits']['hits'];
  404. $list = [];
  405. foreach ($dbResult as $key => &$value) {
  406. $data = [];
  407. $data = $value['_source'];
  408. $list[] = $data;
  409. }
  410. $return = [
  411. 'data' => self::format($list),
  412. 'total' => ($total) ? intval($total) : 0,
  413. ];
  414. return ResultWrapper::success($return);
  415. }
  416. /**
  417. * 客户调价单导出方法
  418. * @param $priceSheetData
  419. */
  420. private static function exportCustomerPriceSheet($priceSheetData)
  421. {
  422. //导出到本地
  423. header("Content-type:application/vnd.ms-excel");
  424. header("Content-Disposition:filename=客户调价单明细表记录.csv");
  425. header('Cache-Control: max-age=0');
  426. $fp = fopen('php://output', 'a');
  427. $head = ['调价单编码','客户名称','商品名称','调价时间','调整后价格规格','调整市场价格','调整人','相关店铺', '状态']; //定义标题
  428. foreach ($head as $i => $v) {
  429. $head[$i] = mb_convert_encoding($v, 'GBK', 'utf-8'); //将中文标题转换编码,否则乱码
  430. }
  431. fputcsv($fp, $head);
  432. $limit = 10000;
  433. $num = 0;//计数器
  434. foreach ($priceSheetData as $v) {
  435. $price = ''; //市场价格
  436. $unitName = ''; //规格
  437. $specValueName = '';
  438. if(!empty($v['salePrice'])){
  439. foreach ($v['salePrice'] as $key => $value){
  440. $price = $value['price'];
  441. foreach ($value['specGroup'] as $K => $V){
  442. $specValueName = $V['specValueName'];
  443. }
  444. $unitName = $value['unitName'].';'.$specValueName;
  445. }
  446. }
  447. //循环数据
  448. $num++;
  449. if ($num == $limit) {
  450. ob_flush();//释放内存
  451. flush();
  452. }
  453. $rows['no'] = isset($v['no']) ? $v['no'] : '';//调价单编码
  454. $rows['customerName'] = isset($v['customerName']) ? $v['customerName'] : '';//客户名称
  455. $rows['goodsName'] = isset($v['goodsName']) ? $v['goodsName'] : '';//商品名称
  456. $rows['createTime'] = isset($v['createTime']) ? date('Y-m-d H:i:s',$v['createTime']) : '';//调价时间
  457. $rows['unitName'] = $unitName;//调整规格
  458. $rows['price'] = '-¥'.$price;//调整市场价格
  459. $rows['createUserName'] = isset($v['createUserName']) ? $v['createUserName'] : '';//调整人
  460. $rows['shopName'] = isset($v['shopName']) ? $v['shopName'] : '';//相关店铺
  461. $rows['effectiveStatus'] = $v['effectiveStatus'] === 2 ? '生效' : '未生效';//状态
  462. foreach ($rows as $kk => $vv) {
  463. $rs[$kk] = mb_convert_encoding($vv, 'GBK', 'utf-8'); //转译编码
  464. }
  465. fputcsv($fp, $rs);
  466. $rows = [];
  467. }
  468. exit;
  469. }
  470. /**
  471. * 获取指定商品的最后生效的客户调价单数据
  472. * @param $params
  473. * @return ResultWrapper
  474. */
  475. public function getCustomerPriceByGoodsIds($params)
  476. {
  477. $pageData = pageToOffset($params['page'], $params['pageSize']);
  478. $objDCustomerPriceEffective = new DCustomerPriceEffective();
  479. $objDCustomerPriceEffective->setTable('qianniao_customerPriceEffective_' . $this->onlineEnterpriseId);
  480. $conditon = [
  481. 'goodsId' => $params['goodsId'],
  482. ];
  483. $dbResult = $objDCustomerPriceEffective->select($conditon, 'id,customerId,salePrice', 'id desc',$pageData['limit'],$pageData['offset']);
  484. if ($dbResult === false) {
  485. return ResultWrapper::fail($objDCustomerPriceEffective->error(), ErrorCode::$dberror);
  486. }
  487. $count = $objDCustomerPriceEffective->count($conditon);
  488. if ($count === false) {
  489. return ResultWrapper::fail($objDCustomerPriceEffective->error(), ErrorCode::$dberror);
  490. }
  491. $data = self::formatCustomerName($dbResult);
  492. if(!$data->isSuccess()){
  493. return ResultWrapper::fail($objDCustomerPriceEffective->error(), ErrorCode::$dberror);
  494. }
  495. $dbResult = $data->getData();
  496. foreach ($dbResult as $key =>$value){
  497. $dbResult[$key]['salePrice'] = json_decode($value['salePrice']);
  498. }
  499. $return = [
  500. 'data' => $dbResult,
  501. 'total' => $count,
  502. ];
  503. return ResultWrapper::success($return);
  504. }
  505. /**
  506. * 删除指定商品的最后生效的客户调价单数据
  507. * @param array $params
  508. * @return ResultWrapper
  509. */
  510. public function delCustomerPrice($params)
  511. {
  512. $objDCustomerPriceEffective = new DCustomerPriceEffective();
  513. $objDCustomerPriceEffective->setTable('qianniao_customerPriceEffective_' . $this->onlineEnterpriseId);
  514. $dbResult = $objDCustomerPriceEffective->select($params['id']);
  515. if ($dbResult === false) {
  516. return ResultWrapper::fail($objDCustomerPriceEffective->error(), ErrorCode::$dberror);
  517. }
  518. if(empty($dbResult)){
  519. return ResultWrapper::fail('要作废的调价单不存在', ErrorCode::$contentNotExists);
  520. }
  521. foreach ($dbResult as $key => $value){
  522. $value['salePrice'] = json_decode($value['salePrice'], true);
  523. foreach ($value['salePrice'] as $k => $v){
  524. if($v['skuId'] == $params['skuId']){
  525. unset($value['salePrice'][$k]);
  526. continue;
  527. }
  528. }
  529. $value['salePrice'] = json_encode( $value['salePrice']);
  530. $updateDbResult = $objDCustomerPriceEffective->update(['salePrice'=>$value['salePrice']], $value['id']);
  531. if ($updateDbResult === false) {
  532. return ResultWrapper::fail($objDCustomerPriceEffective->error(), ErrorCode::$dberror);
  533. }
  534. }
  535. return ResultWrapper::success($updateDbResult);
  536. }
  537. /**
  538. * 格式化客户名称
  539. * @return array
  540. */
  541. public function formatCustomerName($data)
  542. { //customerId
  543. //根据客户id进行渲染客户名称
  544. $customerIdBindCustomerName = [];
  545. $customerIds = [];
  546. foreach ($data as $k => $v){
  547. if(!empty($v['customerId'])){
  548. $customerIds[] = $v['customerId'];
  549. }
  550. }
  551. if(!empty($customerIds)){
  552. $objDCustomer = new DCustomer();
  553. $objDCustomer->setTable('qianniao_customer_' . $this->onlineEnterpriseId);
  554. $customerData = $objDCustomer->select($customerIds, 'id,name');
  555. if($customerData == false){
  556. return ResultWrapper::fail($objDCustomer->error(), ErrorCode::$dberror);
  557. }
  558. if(!empty($customerData)){
  559. foreach ($customerData as $k => $v){
  560. $customerIdBindCustomerName[$v['id']] = $v['name'];
  561. }
  562. }
  563. // 循环主数据,进行对应的字段映射操作
  564. foreach ($data as $key => $value){
  565. $data[$key]['customerName'] = isset($customerIdBindCustomerName[$value['customerId']]) ? $customerIdBindCustomerName[$value['customerId']] :'';
  566. }
  567. }
  568. return ResultWrapper::success($data);
  569. }
  570. }