123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- <?php
- /**
- * 提成管理Model
- * Created by PhpStorm.
- * User: haoren
- * Date: 2021/03/25
- * Time: 16:00
- */
- namespace JinDouYun\Model\Reward;
- use Exception;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Core\StatusCode;
- use Mall\Framework\Core\ResultWrapper;
- use JinDouYun\Dao\UserCenter\DUserCenter;
- use JinDouYun\Dao\Reward\DRewardRule;
- use JinDouYun\Model\Department\MStaff;
- use JinDouYun\Model\MBaseModel;
- class MReward extends MBaseModel
- {
- private $objDRewardRule;
- private $userCenterId;
- private $enterpriseId;
- public function __construct($enterpriseId, $userCenterId = false)
- {
- $this->enterpriseId = $enterpriseId;
- $this->userCenterId = $userCenterId;
- $this->objDRewardRule = new DRewardRule('default');
- $this->objDRewardRule->setTable('qianniao_reward_rule_' . $enterpriseId);
- }
- /**
- * 提成规则添加
- * @param $params
- * @return ResultWrapper
- */
- public function addRewardRule($params)
- {
- $staffIds = $params['staff'];
- unset($params['staff']);
- $params['deleteStatus'] = StatusCode::$standard;
- $params['enableStatus'] = StatusCode::$standard;
- $params['createTime'] = time();
- $params['updateTime'] = time();
- $dbResult = $this->objDRewardRule->insert($params);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDRewardRule->error(), ErrorCode::$dberror);
- }
- $ruleId = $dbResult;
- unset($dbResult);
- //修改每一个员工的规则绑定
- $objMStaff = new MStaff($this->enterpriseId, $this->userCenterId);
- $updateStaff = [
- 'rewardRuleId' => $ruleId,
- 'updateTime' => time(),
- ];
- $modelResult = $objMStaff->updateStaff($updateStaff, ['id' => $staffIds]);
- if(!$modelResult->isSuccess()){
- return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
- }
- return ResultWrapper::success($ruleId);
- }
- /**
- * 提成规则删除
- * @param $where
- * @return ResultWrapper
- */
- public function deleteRewardRule($where)
- {
- $params = [
- 'deleteStatus' => StatusCode::$delete,
- 'updateTime' => time(),
- ];
- $dbResult = $this->objDRewardRule->update($params, $where);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDRewardRule->error(), ErrorCode::$dberror);
- }
- $returnData = $dbResult;
- return ResultWrapper::success($returnData);
- }
- /**
- * 提成规则禁用
- * @param $where
- * @return ResultWrapper
- */
- public function enableRewardRule($where)
- {
- $dbResult = $this->objDRewardRule->get($where);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDRewardRule->error(), ErrorCode::$dberror);
- }
- $update = [
- 'enableStatus' => StatusCode::$standard,
- 'updateTime' => time(),
- ];
- if($dbResult['enableStatus'] == StatusCode::$standard){
- $update['enableStatus'] = StatusCode::$delete;
- }
- $dbResult = $this->objDRewardRule->update($update, $where);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDRewardRule->error(), ErrorCode::$dberror);
- }
- $returnData = $dbResult;
- return ResultWrapper::success($returnData);
- }
- /**
- * 提成规则修改
- * @param $updateData
- * @param $where
- * @return ResultWrapper
- */
- public function updateRewardRule($updateData, $where)
- {
- $staffIds = $updateData['staff'];
- unset($updateData['staff']);
- $dbResult = $this->objDRewardRule->get($where);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDRewardRule->error(), ErrorCode::$paramError);
- }
- $rule = $dbResult;
- unset($dbResult);
- $dbResult = $this->objDRewardRule->update($updateData, $where);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDRewardRule->error(), ErrorCode::$dberror);
- }
- //修改每一个员工的规则绑定
- $objMStaff = new MStaff($this->enterpriseId, $this->userCenterId);
- $updateStaff = [
- 'rewardRuleId' => $where['id'],
- 'updateTime' => time(),
- ];
- $modelResult = $objMStaff->updateStaff($updateStaff, ['id' => $staffIds]);
- if(!$modelResult->isSuccess()){
- return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 提成规则列表
- * @param $selectParams
- * @return ResultWrapper
- * @throws Exception
- */
- public function getAllRewardRule($selectParams)
- {
- $limit = $selectParams['limit'];
- unset($selectParams['limit']);
- $offset = $selectParams['offset'];
- unset($selectParams['offset']);
- $selectParams['deleteStatus'] = StatusCode::$standard;
- isset($selectParams['search']) && $selectParams['search'] = ['title' => $selectParams['search']];
- $selectParams = self::formatSqlWhere($selectParams);
- $dbResult = $this->objDRewardRule->select($selectParams,'*', 'createTime desc', $limit, $offset);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDRewardRule->error(), ErrorCode::$dberror);
- }
- $countResult = $this->objDRewardRule->count($selectParams);
- if ($countResult === false) {
- return ResultWrapper::fail($this->objDRewardRule->error(), ErrorCode::$dberror);
- }
- $return = [
- 'data' => self::formatRewardRule($dbResult),
- 'total' => ($countResult) ? intval($countResult) : 0,
- ];
- return ResultWrapper::success($return);
- }
- /**
- * 提成规则详情
- * @param $where
- * @return ResultWrapper
- * @throws Exception
- */
- public function getRewardRuleInfo($where)
- {
- $where['deleteStatus'] = StatusCode::$standard;
- $dbResult = $this->objDRewardRule->get($where);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDRewardRule->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success(self::formatRewardRule($dbResult));
- }
- /**
- * 提成规则格式化
- * @param $params
- * @return array
- */
- public function formatRewardRule($params)
- {
- if(isset($params['id'])){
- $data = [$params];
- }else{
- $data = $params;
- }
- $ruleIds = array_column($data, 'id');
- $objMStaff = new MStaff($this->enterpriseId, $this->userCenterId);
- $modelResult = $objMStaff->getAllStaffData(['rewardRuleId' => $ruleIds, 'deleteStatus' => StatusCode::$standard]);
- $staff = [];
- if($modelResult->isSuccess()){
- $staffResult = $modelResult->getData();
- foreach($staffResult as $value){
- $staff[$value['rewardRuleId']][] = $value;
- }
- }
- foreach($data as &$value){
- !empty($value['goods']) && $value['goods'] = json_decode($value['goods'], true);
- $value['staff'] = isset($staff[$value['id']]) ? $staff[$value['id']] : [];
- }
- unset($value);
- if(isset($params['id'])){
- $return = array_shift($data);
- }else{
- $return = $data;
- }
- return $return;
- }
- /**
- * 查询提成规则数据
- * @param $where
- * @return ResultWrapper
- */
- public function getRewardRule($where)
- {
- $where['deleteStatus'] = StatusCode::$standard;
- $dbResult = $this->objDRewardRule->get($where);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDRewardRule->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 查询提成规则数据
- * @param $where
- * @return ResultWrapper
- */
- public function selectRewardRule($where)
- {
- $where['deleteStatus'] = StatusCode::$standard;
- $dbResult = $this->objDRewardRule->select($where);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDRewardRule->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- }
|