MUnits.Class.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * 计量单位
  4. * Created by PhpStorm.
  5. * User: XiaoMing
  6. * Date: 2020/3/17
  7. * Time: 17:06
  8. */
  9. namespace JinDouYun\Model\GoodsManage;
  10. use JinDouYun\Controller\Common\Logger;
  11. use JinDouYun\Dao\GoodsManage\DUnits;
  12. use Mall\Framework\Core\ErrorCode;
  13. use Mall\Framework\Core\ResultWrapper;
  14. use Mall\Framework\Core\StatusCode;
  15. class MUnits
  16. {
  17. private $onlineUserId;
  18. private $onlineEnterpriseId;
  19. private $objDUnits;
  20. /**
  21. * MUnits constructor.
  22. * @param $onlineUserId
  23. * @param $onlineEnterpriseId
  24. * @throws \Exception
  25. */
  26. public function __construct($onlineUserId, $onlineEnterpriseId)
  27. {
  28. $this->onlineUserId = $onlineUserId;
  29. $this->onlineEnterpriseId = $onlineEnterpriseId;
  30. $this->objDUnits = new DUnits('default');
  31. $this->objDUnits->setTable( 'qianniao_units_' . $onlineEnterpriseId);
  32. self::before();
  33. }
  34. /**
  35. * 添加计量单位
  36. *
  37. * @param $params
  38. * @return ResultWrapper
  39. */
  40. public function add($params)
  41. {
  42. $dbResult = $this->objDUnits->get_field('id', ['deleteStatus' => StatusCode::$standard, 'unitName' => $params['unitName']]);
  43. if ($dbResult === false) return ResultWrapper::fail($this->objDUnits->error(), ErrorCode::$dberror);
  44. if (!empty($dbResult)) return ResultWrapper::fail($params['unitName'] . '计量单位已存在', ErrorCode::$paramError);
  45. unset($dbResult);
  46. $dbResult = $this->objDUnits->insert($params);
  47. if ($dbResult === false) return ResultWrapper::fail($this->objDUnits->error(), ErrorCode::$dberror);
  48. return ResultWrapper::success($dbResult);
  49. }
  50. /**
  51. * 删除计量单位
  52. *
  53. * @param $id
  54. * @return ResultWrapper
  55. */
  56. public function del($id)
  57. {
  58. $dbResult = $this->objDUnits->update(['deleteStatus'=>StatusCode::$delete],$id);
  59. if ($dbResult===false) return ResultWrapper::fail($this->objDUnits->error(),ErrorCode::$dberror);
  60. return ResultWrapper::success($dbResult);
  61. }
  62. /**
  63. * 跟新启用/禁用状态
  64. *
  65. * @param $params
  66. * @return ResultWrapper
  67. */
  68. public function updateEnableStatus($params)
  69. {
  70. $dbResult = $this->objDUnits->update(['enableStatus' => $params['enableStatus']], $params['id']);
  71. if ($dbResult===false) return ResultWrapper::fail($this->objDUnits->error(),ErrorCode::$dberror);
  72. return ResultWrapper::success($dbResult);
  73. }
  74. /**
  75. * 编辑计量单位
  76. *
  77. * @param $params
  78. * @return ResultWrapper
  79. */
  80. public function edit($params)
  81. {
  82. $dbResult = $this->objDUnits->get_field('id', ['deleteStatus' => StatusCode::$standard, 'unitName' => $params['unitName']]);
  83. if ($dbResult === false) return ResultWrapper::fail($this->objDUnits->error(), ErrorCode::$dberror);
  84. if (!empty($dbResult)) return ResultWrapper::fail($params['unitName'] . '计量单位已存在', ErrorCode::$paramError);
  85. unset($dbResult);
  86. $id = $params['id'];
  87. unset($params['id']);
  88. $dbResult = $this->objDUnits->update($params,$id);
  89. if ($dbResult === false) return ResultWrapper::fail($this->objDUnits->error(),ErrorCode::$dberror);
  90. return ResultWrapper::success($dbResult);
  91. }
  92. /**
  93. * 获取单位列表
  94. *
  95. * @param $selectParams
  96. * @return ResultWrapper
  97. */
  98. public function getAll($selectParams)
  99. {
  100. $limit = $selectParams['limit'];
  101. unset($selectParams['limit']);
  102. $offset = $selectParams['offset'];
  103. unset($selectParams['offset']);
  104. $selectParams['deleteStatus'] = StatusCode::$standard;
  105. if ($selectParams['enableStatus']==StatusCode::$delete){
  106. $selectParams['enableStatus']=StatusCode::$standard;
  107. }else{
  108. unset($selectParams['enableStatus']);
  109. }
  110. $dbResult = $this->objDUnits->select($selectParams, '*', 'createTime DESC', $limit, $offset);
  111. if ($dbResult === false) return ResultWrapper::fail($this->objDUnits->error(), ErrorCode::$dberror);
  112. $total = $this->objDUnits->count($selectParams);
  113. $return = [
  114. 'data' => $dbResult,
  115. 'total' => ($total) ? intval($total) : 0,
  116. ];
  117. return ResultWrapper::success($return);
  118. }
  119. /**
  120. * @return bool
  121. */
  122. private function before()
  123. {
  124. $sql = "SELECT a.title FROM qianniao_admin_setting as a
  125. LEFT JOIN qianniao_units_".$this->onlineEnterpriseId." as u
  126. ON a.title = u.unitName
  127. WHERE a.type= ". StatusCode::$adminSettingType['unit']." AND u.unitName IS NULL";
  128. $dbResult = $this->objDUnits->query($sql);
  129. if ($dbResult === false){
  130. Logger::logs(E_USER_ERROR,'查询单位失败',__CLASS__,__LINE__,$this->objDUnits->error());
  131. return false;
  132. }
  133. if (empty($dbResult)){
  134. return true;
  135. }
  136. $insert = [];
  137. foreach ($dbResult as $value){
  138. $insert[] = [
  139. 'unitName' => $value['title'],
  140. 'createTime' => time()
  141. ];
  142. }
  143. //写入数据
  144. $dbResult = $this->objDUnits->insert($insert,true);
  145. if ($dbResult === false){
  146. Logger::logs(E_USER_ERROR,'初始化单位失败',__CLASS__,__LINE__,$this->objDUnits->error());
  147. return false;
  148. }
  149. return true;
  150. }
  151. }