MCustomerDemand.Class.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: kang
  5. * Date: 2021/3/8
  6. * Time: 15:05
  7. */
  8. namespace JinDouYun\Model\Customer;
  9. use Mall\Framework\Core\ErrorCode;
  10. use Mall\Framework\Core\StatusCode;
  11. use Mall\Framework\Core\ResultWrapper;
  12. use JinDouYun\Dao\Customer\DCustomer;
  13. use JinDouYun\Dao\Customer\DCustomerDemand;
  14. class MCustomerDemand
  15. {
  16. private $objDCustomerDemand;
  17. private $enterpriseId;
  18. private $userCenterId;
  19. private $objDCustomer;
  20. public function __construct($enterpriseId, $userCenterId)
  21. {
  22. $this->enterpriseId = $enterpriseId;
  23. $this->userCenterId = $userCenterId;
  24. $this->objDCustomerDemand = new DCustomerDemand('default');
  25. $this->objDCustomer = new DCustomer();
  26. $this->objDCustomer->setTable('qianniao_customer_'.$enterpriseId);
  27. }
  28. /**
  29. * 添加客户需求
  30. * @param $params
  31. * @return ResultWrapper
  32. * @throws Exception
  33. */
  34. public function addCustomerdemand($demandData)
  35. {
  36. $demandData = $this->objDCustomerDemand->insert($demandData);
  37. if($demandData === false){
  38. return ResultWrapper::fail($this->objDCustomerDemand->error(), ErrorCode::$dberror);
  39. }
  40. return ResultWrapper::success($demandData);
  41. }
  42. /**
  43. * 获取所有的客户需求提报
  44. */
  45. public function getAllCustomerdemand($selectParams)
  46. {
  47. $limit = $selectParams['limit'];
  48. unset($selectParams['limit']);
  49. $offset = $selectParams['offset'];
  50. unset($selectParams['offset']);
  51. $selectParams['deleteStatus'] = StatusCode::$standard;
  52. $returnData = [
  53. 'data' => [],
  54. 'total' => 0,
  55. ];
  56. $whereSql = '';
  57. if (isset($selectParams['beginTime']) && !empty($selectParams['beginTime'])) {
  58. $where = empty($whereSql) ? '' : ' AND ';
  59. $whereSql .= $where . ' createTime >=' . $selectParams['beginTime'];
  60. }
  61. if (isset($selectParams['endTime']) && !empty($selectParams['endTime'])) {
  62. $where = empty($whereSql) ? '' : ' AND ';
  63. $whereSql .= $where . ' createTime <= '. $selectParams['endTime'];
  64. }
  65. $where = empty($whereSql) ? '' : ' AND ';
  66. $whereSql .= $where . 'deleteStatus = ' . StatusCode::$standard.' AND enterpriseId ='.$this->enterpriseId;
  67. $dbResult = $this->objDCustomerDemand->select($whereSql,'*','createTime desc',$limit,$offset);
  68. if ($dbResult === false) {
  69. return ResultWrapper::fail($this->objDCustomerDemand->error(), ErrorCode::$dberror);
  70. }
  71. if(empty($dbResult)){
  72. return ResultWrapper::success($returnData);
  73. }
  74. $dbTotalResult = $this->objDCustomerDemand->count($whereSql);
  75. if ($dbTotalResult === false) {
  76. return ResultWrapper::fail($this->objDCustomerDemand->error(), ErrorCode::$dberror);
  77. }
  78. $return = [
  79. 'data' => $this->format($dbResult),
  80. 'total' => $dbTotalResult
  81. ];
  82. return ResultWrapper::success($return);
  83. }
  84. /**
  85. * 格式化
  86. * @return array
  87. */
  88. public function format($data)
  89. {
  90. // 映射客户id对应的名字
  91. $customerIdBindName = [];
  92. $customerIds = array_column($data, 'customerId');
  93. $customerData = $this->objDCustomer->select($customerIds, 'id,name');
  94. if( !empty($customerData) ){
  95. foreach ($customerData as $key => $value){
  96. $customerIdBindName[$value['id']] = $value['name'];
  97. }
  98. }
  99. foreach ($data as $key => $value){
  100. $data[$key]['name'] = '';
  101. if( isset($customerIdBindName[$value['customerId']]) ){
  102. $data[$key]['name'] = $customerIdBindName[$value['customerId']];
  103. }
  104. }
  105. return $data;
  106. }
  107. /**
  108. * 获取指定的客户需求提报
  109. */
  110. public function getCustomerdemandInfo($receiptRequisitionId)
  111. {
  112. $dbResult = $this->objDCustomerDemand->select($receiptRequisitionId);
  113. if($dbResult === false){
  114. return ResultWrapper::fail($this->objDCustomerDemand->error(), ErrorCode::$dberror);
  115. }
  116. if( empty($dbResult) ){
  117. return ResultWrapper::success([]);
  118. }
  119. $returnData = self::format($dbResult);
  120. return ResultWrapper::success( array_pop($returnData) );
  121. }
  122. }