MReward.Class.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /**
  3. * 提成管理Model
  4. * Created by PhpStorm.
  5. * User: haoren
  6. * Date: 2021/03/25
  7. * Time: 16:00
  8. */
  9. namespace JinDouYun\Model\Reward;
  10. use Exception;
  11. use Mall\Framework\Core\ErrorCode;
  12. use Mall\Framework\Core\StatusCode;
  13. use Mall\Framework\Core\ResultWrapper;
  14. use JinDouYun\Dao\UserCenter\DUserCenter;
  15. use JinDouYun\Dao\Reward\DRewardRule;
  16. use JinDouYun\Model\Department\MStaff;
  17. use JinDouYun\Model\MBaseModel;
  18. class MReward extends MBaseModel
  19. {
  20. private $objDRewardRule;
  21. private $userCenterId;
  22. private $enterpriseId;
  23. public function __construct($enterpriseId, $userCenterId = false)
  24. {
  25. $this->enterpriseId = $enterpriseId;
  26. $this->userCenterId = $userCenterId;
  27. $this->objDRewardRule = new DRewardRule('default');
  28. $this->objDRewardRule->setTable('qianniao_reward_rule_' . $enterpriseId);
  29. }
  30. /**
  31. * 提成规则添加
  32. * @param $params
  33. * @return ResultWrapper
  34. */
  35. public function addRewardRule($params)
  36. {
  37. $staffIds = $params['staff'];
  38. unset($params['staff']);
  39. $params['deleteStatus'] = StatusCode::$standard;
  40. $params['enableStatus'] = StatusCode::$standard;
  41. $params['createTime'] = time();
  42. $params['updateTime'] = time();
  43. $dbResult = $this->objDRewardRule->insert($params);
  44. if($dbResult === false){
  45. return ResultWrapper::fail($this->objDRewardRule->error(), ErrorCode::$dberror);
  46. }
  47. $ruleId = $dbResult;
  48. unset($dbResult);
  49. //修改每一个员工的规则绑定
  50. $objMStaff = new MStaff($this->enterpriseId, $this->userCenterId);
  51. $updateStaff = [
  52. 'rewardRuleId' => $ruleId,
  53. 'updateTime' => time(),
  54. ];
  55. $modelResult = $objMStaff->updateStaff($updateStaff, ['id' => $staffIds]);
  56. if(!$modelResult->isSuccess()){
  57. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  58. }
  59. return ResultWrapper::success($ruleId);
  60. }
  61. /**
  62. * 提成规则删除
  63. * @param $where
  64. * @return ResultWrapper
  65. */
  66. public function deleteRewardRule($where)
  67. {
  68. $params = [
  69. 'deleteStatus' => StatusCode::$delete,
  70. 'updateTime' => time(),
  71. ];
  72. $dbResult = $this->objDRewardRule->update($params, $where);
  73. if ($dbResult === false) {
  74. return ResultWrapper::fail($this->objDRewardRule->error(), ErrorCode::$dberror);
  75. }
  76. $returnData = $dbResult;
  77. return ResultWrapper::success($returnData);
  78. }
  79. /**
  80. * 提成规则禁用
  81. * @param $where
  82. * @return ResultWrapper
  83. */
  84. public function enableRewardRule($where)
  85. {
  86. $dbResult = $this->objDRewardRule->get($where);
  87. if ($dbResult === false) {
  88. return ResultWrapper::fail($this->objDRewardRule->error(), ErrorCode::$dberror);
  89. }
  90. $update = [
  91. 'enableStatus' => StatusCode::$standard,
  92. 'updateTime' => time(),
  93. ];
  94. if($dbResult['enableStatus'] == StatusCode::$standard){
  95. $update['enableStatus'] = StatusCode::$delete;
  96. }
  97. $dbResult = $this->objDRewardRule->update($update, $where);
  98. if ($dbResult === false) {
  99. return ResultWrapper::fail($this->objDRewardRule->error(), ErrorCode::$dberror);
  100. }
  101. $returnData = $dbResult;
  102. return ResultWrapper::success($returnData);
  103. }
  104. /**
  105. * 提成规则修改
  106. * @param $updateData
  107. * @param $where
  108. * @return ResultWrapper
  109. */
  110. public function updateRewardRule($updateData, $where)
  111. {
  112. $staffIds = $updateData['staff'];
  113. unset($updateData['staff']);
  114. $dbResult = $this->objDRewardRule->get($where);
  115. if($dbResult === false){
  116. return ResultWrapper::fail($this->objDRewardRule->error(), ErrorCode::$paramError);
  117. }
  118. $rule = $dbResult;
  119. unset($dbResult);
  120. $dbResult = $this->objDRewardRule->update($updateData, $where);
  121. if ($dbResult === false) {
  122. return ResultWrapper::fail($this->objDRewardRule->error(), ErrorCode::$dberror);
  123. }
  124. //修改每一个员工的规则绑定
  125. $objMStaff = new MStaff($this->enterpriseId, $this->userCenterId);
  126. $updateStaff = [
  127. 'rewardRuleId' => $where['id'],
  128. 'updateTime' => time(),
  129. ];
  130. $modelResult = $objMStaff->updateStaff($updateStaff, ['id' => $staffIds]);
  131. if(!$modelResult->isSuccess()){
  132. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  133. }
  134. return ResultWrapper::success($dbResult);
  135. }
  136. /**
  137. * 提成规则列表
  138. * @param $selectParams
  139. * @return ResultWrapper
  140. * @throws Exception
  141. */
  142. public function getAllRewardRule($selectParams)
  143. {
  144. $limit = $selectParams['limit'];
  145. unset($selectParams['limit']);
  146. $offset = $selectParams['offset'];
  147. unset($selectParams['offset']);
  148. $selectParams['deleteStatus'] = StatusCode::$standard;
  149. isset($selectParams['search']) && $selectParams['search'] = ['title' => $selectParams['search']];
  150. $selectParams = self::formatSqlWhere($selectParams);
  151. $dbResult = $this->objDRewardRule->select($selectParams,'*', 'createTime desc', $limit, $offset);
  152. if ($dbResult === false) {
  153. return ResultWrapper::fail($this->objDRewardRule->error(), ErrorCode::$dberror);
  154. }
  155. $countResult = $this->objDRewardRule->count($selectParams);
  156. if ($countResult === false) {
  157. return ResultWrapper::fail($this->objDRewardRule->error(), ErrorCode::$dberror);
  158. }
  159. $return = [
  160. 'data' => self::formatRewardRule($dbResult),
  161. 'total' => ($countResult) ? intval($countResult) : 0,
  162. ];
  163. return ResultWrapper::success($return);
  164. }
  165. /**
  166. * 提成规则详情
  167. * @param $where
  168. * @return ResultWrapper
  169. * @throws Exception
  170. */
  171. public function getRewardRuleInfo($where)
  172. {
  173. $where['deleteStatus'] = StatusCode::$standard;
  174. $dbResult = $this->objDRewardRule->get($where);
  175. if ($dbResult === false) {
  176. return ResultWrapper::fail($this->objDRewardRule->error(), ErrorCode::$dberror);
  177. }
  178. return ResultWrapper::success(self::formatRewardRule($dbResult));
  179. }
  180. /**
  181. * 提成规则格式化
  182. * @param $params
  183. * @return array
  184. */
  185. public function formatRewardRule($params)
  186. {
  187. if(isset($params['id'])){
  188. $data = [$params];
  189. }else{
  190. $data = $params;
  191. }
  192. $ruleIds = array_column($data, 'id');
  193. $objMStaff = new MStaff($this->enterpriseId, $this->userCenterId);
  194. $modelResult = $objMStaff->getAllStaffData(['rewardRuleId' => $ruleIds, 'deleteStatus' => StatusCode::$standard]);
  195. $staff = [];
  196. if($modelResult->isSuccess()){
  197. $staffResult = $modelResult->getData();
  198. foreach($staffResult as $value){
  199. $staff[$value['rewardRuleId']][] = $value;
  200. }
  201. }
  202. foreach($data as &$value){
  203. !empty($value['goods']) && $value['goods'] = json_decode($value['goods'], true);
  204. $value['staff'] = isset($staff[$value['id']]) ? $staff[$value['id']] : [];
  205. }
  206. unset($value);
  207. if(isset($params['id'])){
  208. $return = array_shift($data);
  209. }else{
  210. $return = $data;
  211. }
  212. return $return;
  213. }
  214. /**
  215. * 查询提成规则数据
  216. * @param $where
  217. * @return ResultWrapper
  218. */
  219. public function getRewardRule($where)
  220. {
  221. $where['deleteStatus'] = StatusCode::$standard;
  222. $dbResult = $this->objDRewardRule->get($where);
  223. if ($dbResult === false) {
  224. return ResultWrapper::fail($this->objDRewardRule->error(), ErrorCode::$dberror);
  225. }
  226. return ResultWrapper::success($dbResult);
  227. }
  228. /**
  229. * 查询提成规则数据
  230. * @param $where
  231. * @return ResultWrapper
  232. */
  233. public function selectRewardRule($where)
  234. {
  235. $where['deleteStatus'] = StatusCode::$standard;
  236. $dbResult = $this->objDRewardRule->select($where);
  237. if ($dbResult === false) {
  238. return ResultWrapper::fail($this->objDRewardRule->error(), ErrorCode::$dberror);
  239. }
  240. return ResultWrapper::success($dbResult);
  241. }
  242. }