MPurchaseOutGoods.Class.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 Mall\Framework\Core\ErrorCode;
  11. use Mall\Framework\Core\ResultWrapper;
  12. use JinDouYun\Dao\Purchase\DPurchaseOutGoods;
  13. use Mall\Framework\Core\StatusCode;
  14. class MPurchaseOutGoods
  15. {
  16. private $objDPurchaseOutGoods;
  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->objDPurchaseOutGoods = new DPurchaseOutGoods('stock');
  30. $this->objDPurchaseOutGoods->setTable($this->objDPurchaseOutGoods->get_Table() . '_' . $this->onlineEnterpriseId);
  31. }
  32. /**
  33. * 添加采购商品信息
  34. * @param $params
  35. * @return ResultWrapper
  36. */
  37. public function addPurchaseGoods($params)
  38. {
  39. $dbResult = $this->objDPurchaseOutGoods->insert($params,true);
  40. if ($dbResult === false) {
  41. $this->objDPurchaseOutGoods->rollBack();
  42. return ResultWrapper::fail($this->objDPurchaseOutGoods->error(), ErrorCode::$dberror);
  43. }
  44. return ResultWrapper::success($dbResult);
  45. }
  46. /**
  47. * @param $params
  48. * @param $purchaseId
  49. * @param $deleteArray
  50. * @return ResultWrapper
  51. */
  52. public function editPurchaseOutGoods($params,$purchaseId, $deleteArray)
  53. {
  54. foreach ($params as $key => $val) {
  55. if (isset($val['id'])) {
  56. $id = $val['id'];
  57. unset($val['id']);
  58. $dbResult = $this->objDPurchaseOutGoods->update($val, $id);
  59. if($dbResult === false){
  60. return ResultWrapper::fail($this->objDPurchaseOutGoods->error(), ErrorCode::$dberror);
  61. }
  62. } else {
  63. $val['purchaseOutId'] = $purchaseId;
  64. $insert[] = $val;
  65. }
  66. }
  67. if(!empty($deleteArray)){
  68. $dbResult = $this->objDPurchaseOutGoods->update(['deleteStatus' => StatusCode::$delete], ['id' => $deleteArray]);
  69. if($dbResult === false){
  70. return ResultWrapper::fail($this->objDPurchaseOutGoods->error(), ErrorCode::$dberror);
  71. }
  72. }
  73. if (!empty($insert)) {
  74. $dbResult = $this->objDPurchaseOutGoods->insert($insert, true);
  75. if ($dbResult === false) {
  76. return ResultWrapper::fail($this->objDPurchaseOutGoods->error(), ErrorCode::$dberror);
  77. }
  78. }
  79. return ResultWrapper::success(true);
  80. }
  81. /**
  82. * 通过采购退货单id获取采购单下的商品
  83. * @param $ids
  84. * @return ResultWrapper
  85. */
  86. public function getGoodsByPurchaseIds($ids)
  87. {
  88. $dbResult = $this->objDPurchaseOutGoods->select(['purchaseId'=>$ids]);
  89. if ($dbResult === false) {
  90. $this->objDPurchaseOutGoods->rollBack();
  91. return ResultWrapper::fail($this->objDPurchaseOutGoods->error(), ErrorCode::$dberror);
  92. }
  93. return ResultWrapper::success(self::formatJson($dbResult));
  94. }
  95. /**
  96. * @param $data
  97. * @return mixed
  98. */
  99. public function formatJson($data)
  100. {
  101. if (isset($data['id'])) {
  102. $data['unit'] = empty($data['unit']) ? '' : json_decode($data['unit'], true);
  103. return $data;
  104. }
  105. foreach ($data as &$v) {
  106. $v['unit'] = empty($v['unit']) ? '' : json_decode($v['unit'],true);
  107. }
  108. return $data;
  109. }
  110. }