MPurchaseGoods.Class.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * 采购订单商品明细数据模型
  4. * Created by PhpStorm.
  5. * User: XiaoMing
  6. * Date: 2019/11/11
  7. * Time: 17:57
  8. */
  9. namespace JinDouYun\Model\Purchase;
  10. use JinDouYun\Dao\Purchase\DPurchaseGoods;
  11. use Mall\Framework\Core\ErrorCode;
  12. use Mall\Framework\Core\ResultWrapper;
  13. use Mall\Framework\Core\StatusCode;
  14. class MPurchaseGoods
  15. {
  16. private $objDPurchaseGoods;
  17. private $onlineUserId;
  18. private $onlineEnterpriseId;
  19. /**
  20. * MPurchaseGoods constructor.
  21. * @param $onlineUserId
  22. * @param $onlineEnterpriseId
  23. * @throws \Exception
  24. */
  25. public function __construct($onlineUserId, $onlineEnterpriseId)
  26. {
  27. $this->onlineUserId = $onlineUserId;
  28. $this->onlineEnterpriseId = $onlineEnterpriseId;
  29. $this->objDPurchaseGoods = new DPurchaseGoods('stock');
  30. $this->objDPurchaseGoods->setTable($this->objDPurchaseGoods->get_Table() . '_' . $this->onlineEnterpriseId);
  31. }
  32. /**
  33. * 添加采购商品信息
  34. * @param $params
  35. * @return ResultWrapper
  36. */
  37. public function addPurchaseGoods($params)
  38. {
  39. $dbResult = $this->objDPurchaseGoods->insert($params, true);
  40. if ($dbResult === false) {
  41. $this->objDPurchaseGoods->rollBack();
  42. return ResultWrapper::fail($this->objDPurchaseGoods->error(), ErrorCode::$dberror);
  43. }
  44. return ResultWrapper::success($dbResult);
  45. }
  46. /**
  47. * @param $params
  48. * @param $actionIds ['deleteArray'=>array,'purchaseId'=>int]
  49. * @return ResultWrapper
  50. */
  51. public function editPurchaseGoods($params, $actionIds)
  52. {
  53. $this->objDPurchaseGoods->beginTransaction();
  54. if (!empty($actionIds['deleteArray'])) {
  55. $dbResult = $this->objDPurchaseGoods->update(['deleteStatus' => StatusCode::$delete], ['id' => $actionIds['deleteArray']]);
  56. if ($dbResult === false) {
  57. $this->objDPurchaseGoods->rollBack();
  58. return ResultWrapper::fail($this->objDPurchaseGoods->error(), ErrorCode::$dberror);
  59. }
  60. }
  61. $insert = [];
  62. foreach ($params as &$details) {
  63. if (isset($details['id']) && !empty($details['id'])) {
  64. $dbResult = $this->objDPurchaseGoods->update($details, ['id' => $details['id']]);
  65. if ($dbResult === false) {
  66. $this->objDPurchaseGoods->rollBack();
  67. return ResultWrapper::fail($this->objDPurchaseGoods->error(), ErrorCode::$dberror);
  68. }
  69. } else {
  70. $insert[] = [
  71. 'basicGoodsId' => $details['basicGoodsId'],
  72. 'goodsCode' => $details['goodsCode'],
  73. 'goodsName' => $details['goodsName'],
  74. 'skuId' => $details['skuId'],
  75. 'buyerNum' => $details['buyerNum'],
  76. 'buyerUnitPrice' => $details['buyerUnitPrice'],
  77. 'subtotalPrice' => $details['subtotalPrice'],
  78. 'couponAmount' => $details['couponAmount'],
  79. 'otherAmount' => $details['otherAmount'],
  80. 'purchaseId' => $actionIds['purchaseId'],
  81. ];
  82. }
  83. }
  84. if (!empty($insert)) {
  85. $dbResult = $this->objDPurchaseGoods->insert($insert, true);
  86. if ($dbResult === false) {
  87. $this->objDPurchaseGoods->rollBack();
  88. return ResultWrapper::fail($this->objDPurchaseGoods->error(), ErrorCode::$dberror);
  89. }
  90. }
  91. $this->objDPurchaseGoods->commit();
  92. return ResultWrapper::success($dbResult);
  93. }
  94. /**
  95. * 通过采购单id获取采购单下的商品
  96. * @param $ids
  97. * @return ResultWrapper
  98. */
  99. public function getGoodsByPurchaseIds($ids)
  100. {
  101. $dbResult = $this->objDPurchaseGoods->select(['purchaseId' => $ids,'deleteStatus'=>StatusCode::$standard]);
  102. if ($dbResult === false) {
  103. $this->objDPurchaseGoods->rollBack();
  104. return ResultWrapper::fail($this->objDPurchaseGoods->error(), ErrorCode::$dberror);
  105. }
  106. return ResultWrapper::success(self::formatJson($dbResult));
  107. }
  108. /**
  109. * 获取采购单信息
  110. */
  111. public function getPurchaseGoodsData($where = [])
  112. {
  113. $where['deleteStatus'] = StatusCode::$standard;
  114. $dbResult = $this->objDPurchaseGoods->select($where);
  115. if($dbResult === false){
  116. return ResultWrapper::fail($this->objDPurchaseGoods->error(), ErrorCode::$dberror);
  117. }
  118. $formatData = [];
  119. foreach($dbResult as $value){
  120. $formatData[$value['skuId']] = $value;
  121. }
  122. return ResultWrapper::success($formatData);
  123. }
  124. /**
  125. * @param $data
  126. * @return mixed
  127. */
  128. public function formatJson($data)
  129. {
  130. if (isset($data['id'])) {
  131. $data['unit'] = empty($data['unit']) ? '' : json_decode($data['unit'], true);
  132. return $data;
  133. }
  134. foreach ($data as &$v) {
  135. $v['unit'] = empty($v['unit']) ? '' : json_decode($v['unit'], true);
  136. }
  137. return $data;
  138. }
  139. }