MCommissionGrade.Class.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /**
  3. * 分销商等级Model
  4. * Created by PhpStorm.
  5. * User: haoren
  6. * Date: 2020/07/22
  7. * Time: 15:00
  8. */
  9. namespace JinDouYun\Model\Commission;
  10. use Exception;
  11. use Mall\Framework\Core\ErrorCode;
  12. use Mall\Framework\Core\StatusCode;
  13. use Mall\Framework\Core\ResultWrapper;
  14. use JinDouYun\Cache\CommissionCache;
  15. use JinDouYun\Dao\Commission\DCommissionGrade;
  16. use JinDouYun\Dao\Commission\DCommissionGradeCondition;
  17. use JinDouYun\Model\MBaseModel;
  18. class MCommissionGrade extends MBaseModel
  19. {
  20. private $objDCommissionGrade;
  21. private $objDCommissionGradeCondition;
  22. private $objCommissionCache;
  23. private $userCenterId;
  24. private $enterpriseId;
  25. public function __construct($enterpriseId, $userCenterId)
  26. {
  27. $this->enterpriseId = $enterpriseId;
  28. $this->userCenterId = $userCenterId;
  29. $this->objCommissionCache = new CommissionCache($enterpriseId, $userCenterId);
  30. $this->objDCommissionGrade = new DCommissionGrade();
  31. $this->objDCommissionGradeCondition = new DCommissionGradeCondition();
  32. self::insertGrade();
  33. }
  34. public function insertGrade()
  35. {
  36. $dbResult = $this->objDCommissionGrade->get(['enterpriseId' => $this->enterpriseId,'grade' => 0]);
  37. if($dbResult === false){
  38. return false;
  39. }
  40. if(empty($dbResult)){
  41. //添加分销商默认等级
  42. $insertGrade = [
  43. 'name' => '默认等级',
  44. 'grade' => 0,
  45. 'oneRate' => 30.00,
  46. 'twoRate' => 20.00,
  47. 'threeRate' => 10.00,
  48. 'upgradeMode' => 5,
  49. ];
  50. $modelResult = self::addGrade($insertGrade);
  51. if(!$modelResult->isSuccess()){
  52. return false;
  53. }
  54. unset($modelResult);
  55. }
  56. return true;
  57. }
  58. /**
  59. * 新增等级
  60. * @param $params
  61. * @return ResultWrapper
  62. */
  63. public function addGrade($params)
  64. {
  65. $dbResult = $this->objDCommissionGrade->select(['enterpriseId' => $this->enterpriseId, 'grade' => $params['grade'], 'deleteStatus' => StatusCode::$standard]);
  66. if($dbResult === false){
  67. return ResultWrapper::fail($this->objDCommissionGrade->error(), ErrorCode::$dberror);
  68. }
  69. if(!empty($dbResult)){
  70. return ResultWrapper::fail('等级已存在', ErrorCode::$contentNotExists);
  71. }
  72. unset($dbResult);
  73. $params['enterpriseId'] = $this->enterpriseId;
  74. $params['deleteStatus'] = StatusCode::$standard;
  75. $params['createTime'] = time();
  76. $params['updateTime'] = time();
  77. $params['enableStatus'] = StatusCode::$standard;
  78. isset($params['upgradeCondition']) && $params['upgradeCondition'] = json_encode($params['upgradeCondition']);
  79. $dbResult = $this->objDCommissionGrade->insert($params);
  80. if($dbResult === false){
  81. return ResultWrapper::fail($this->objDCommissionGrade->error(), ErrorCode::$dberror);
  82. }
  83. return ResultWrapper::success($dbResult);
  84. }
  85. /**
  86. * 修改等级
  87. * @param $update
  88. * @param $where
  89. * @return ResultWrapper
  90. */
  91. public function updateGrade($update, $where = [])
  92. {
  93. $where['enterpriseId'] = $this->enterpriseId;
  94. unset($update['enterpriseId']);
  95. if(isset($update['grade'])){
  96. $dbResult = $this->objDCommissionGrade->select(['enterpriseId' => $this->enterpriseId, 'grade' => $update['grade'], 'deleteStatus' => StatusCode::$standard]);
  97. if($dbResult === false){
  98. return ResultWrapper::fail($this->objDCommissionGrade->error(), ErrorCode::$dberror);
  99. }
  100. if(!empty($dbResult)){
  101. $grade = array_shift($dbResult);
  102. if($grade['id'] != $where['id']){
  103. return ResultWrapper::fail('等级已存在', ErrorCode::$contentNotExists);
  104. }
  105. }
  106. }
  107. isset($update['upgradeCondition']) && $update['upgradeCondition'] = json_encode($update['upgradeCondition']);
  108. $dbResult = $this->objDCommissionGrade->update($update, $where);
  109. if($dbResult === false){
  110. return ResultWrapper::fail($this->objDCommissionGrade->error(), ErrorCode::$dberror);
  111. }
  112. //删除缓存
  113. $this->objCommissionCache->delBusinessmanGrade(true);
  114. return ResultWrapper::success($dbResult);
  115. }
  116. /**
  117. * 等级列表
  118. * @param $params
  119. * @return ResultWrapper
  120. */
  121. public function getAllGrade($params = [])
  122. {
  123. $params['deleteStatus'] = StatusCode::$standard;
  124. $params['enterpriseId'] = $this->enterpriseId;
  125. $dbResult = $this->objDCommissionGrade->select($params,'*','grade ASC');
  126. if($dbResult === false){
  127. return ResultWrapper::fail($this->objDCommissionGrade->error(), ErrorCode::$dberror);
  128. }
  129. if(empty($dbResult)){
  130. return ResultWrapper::success($dbResult);
  131. }
  132. $dbResult = self::formatUpgradeCondition($dbResult);
  133. return ResultWrapper::success($dbResult);
  134. }
  135. /**
  136. * 等级详情
  137. * @param $params
  138. * @return ResultWrapper
  139. */
  140. public function getInfoGrade($params)
  141. {
  142. $params['deleteStatus'] = StatusCode::$standard;
  143. $params['enterpriseId'] = $this->enterpriseId;
  144. $dbResult = $this->objDCommissionGrade->get($params);
  145. if($dbResult === false){
  146. return ResultWrapper::fail($this->objDCommissionGrade->error(), ErrorCode::$dberror);
  147. }
  148. if(empty($dbResult)){
  149. return ResultWrapper::success($dbResult);
  150. }
  151. $dbResult = self::formatUpgradeCondition($dbResult);
  152. return ResultWrapper::success($dbResult);
  153. }
  154. /**
  155. * 格式化升级条件
  156. * @param $params
  157. * @return array|mixed
  158. */
  159. public function formatUpgradeCondition($params)
  160. {
  161. if(empty($params)) return [];
  162. $modelResult = self::getAllGradeCondition(['type' => StatusCode::$delete]);
  163. if(!$modelResult->isSuccess()){
  164. return [];
  165. }
  166. $infoData = $modelResult->getData();
  167. unset($modelResult);
  168. $info = [];
  169. foreach($infoData as $value){
  170. $info[$value['id']]['key'] = $value['key'];
  171. $info[$value['id']]['title'] = $value['title'];
  172. }
  173. $format = $params;
  174. if(isset($params['id'])){
  175. $format = [$params];
  176. }
  177. foreach($format as &$value){
  178. if(isset($value['upgradeCondition']) && !empty($value['upgradeCondition'])){
  179. $value['upgradeCondition'] = json_decode($value['upgradeCondition'], true);
  180. if(!empty($value['upgradeCondition'])){
  181. foreach($value['upgradeCondition'] as $key => $v){
  182. if(isset($info[$key])){
  183. $value['upgradeConditionInfo'][] = [
  184. 'id' => $key,
  185. 'key' => $info[$key]['key'],
  186. 'name' => $info[$key]['title'],
  187. 'value' => $v,
  188. ];
  189. }
  190. }
  191. }
  192. }
  193. }
  194. if(isset($params['id'])){
  195. $format = array_shift($format);
  196. }
  197. return $format;
  198. }
  199. /**
  200. * 升级条件
  201. * @param array $where
  202. * @return ResultWrapper
  203. */
  204. public function getAllGradeCondition($where = [])
  205. {
  206. $dbResult = $this->objDCommissionGradeCondition->select($where);
  207. if($dbResult === false){
  208. return ResultWrapper::fail($this->objDCommissionGradeCondition->error(), ErrorCode::$dberror);
  209. }
  210. return ResultWrapper::success($dbResult);
  211. }
  212. }