MCommissionSetting.Class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 JinDouYun\Controller\Common\Logger;
  12. use JinDouYun\Model\Customer\MCustomer;
  13. use JinDouYun\Model\MBaseModel;
  14. use Mall\Framework\Core\ErrorCode;
  15. use Mall\Framework\Core\StatusCode;
  16. use Mall\Framework\Core\ResultWrapper;
  17. use JinDouYun\Cache\CommissionCache;
  18. use JinDouYun\Dao\Commission\DCommissionSetting;
  19. use JinDouYun\Dao\Commission\DCommissionDesc;
  20. class MCommissionSetting extends MBaseModel
  21. {
  22. private $objDCommissionSetting;
  23. private $objCommissionCache;
  24. private $objDCommissionDesc;
  25. private $userCenterId;
  26. private $enterpriseId;
  27. public function __construct($enterpriseId, $userCenterId)
  28. {
  29. $this->enterpriseId = $enterpriseId;
  30. $this->userCenterId = $userCenterId;
  31. $this->objCommissionCache = new CommissionCache($enterpriseId, $userCenterId);
  32. $this->objDCommissionSetting = new DCommissionSetting();
  33. $this->objDCommissionDesc = new DCommissionDesc();
  34. }
  35. /**
  36. * 保存设置
  37. * @param $params
  38. * @return ResultWrapper
  39. */
  40. public function saveSetting($params)
  41. {
  42. isset($params['info']) && $params['info'] = json_encode($params['info']);
  43. isset($params['payType']) && $params['payType'] = json_encode($params['payType']);
  44. isset($params['withdrawType']) && $params['withdrawType'] = json_encode($params['withdrawType']);
  45. //Logger::logs(E_USER_ERROR,'saveSetting',__CLASS__,__LINE__,$params);
  46. if(isset($params['buyGoods'])){
  47. $buyGoods = [];
  48. foreach($params['buyGoods'] as $key => $value){
  49. if(!empty($value)){
  50. $buyGoods[$key] = $value;
  51. }
  52. }
  53. $params['buyGoods'] = json_encode($buyGoods, JSON_FORCE_OBJECT);
  54. }
  55. $params['withdrawInfo'] = json_encode($params['withdrawInfo']);
  56. $dbResult = $this->objDCommissionSetting->get(['enterpriseId' => $this->enterpriseId]);
  57. if($dbResult === false){
  58. return ResultWrapper::fail($this->objDCommissionSetting->error(), ErrorCode::$dberror);
  59. }
  60. $setting = $dbResult;
  61. unset($dbResult);
  62. $params['updateTime'] = time();
  63. if(!empty($setting)){
  64. //修改
  65. $dbResult = $this->objDCommissionSetting->update($params, ['enterpriseId' => $this->enterpriseId]);
  66. }else{
  67. //新增
  68. $params['createTime'] = time();
  69. $params['enterpriseId'] = $this->enterpriseId;
  70. $dbResult = $this->objDCommissionSetting->insert($params);
  71. }
  72. if($dbResult === false){
  73. return ResultWrapper::fail($this->objDCommissionSetting->error(), ErrorCode::$dberror);
  74. }
  75. //删除cache
  76. $this->objCommissionCache->delSetting();
  77. $this->objCommissionCache->delBusinessmanSub(true);
  78. //保存cache
  79. $selectResult = $this->objDCommissionSetting->get(['enterpriseId' => $this->enterpriseId]);
  80. if($selectResult === false){
  81. return ResultWrapper::fail($this->objDCommissionSetting->error(), ErrorCode::$dberror);
  82. }
  83. $this->objCommissionCache->saveSetting($selectResult);
  84. return ResultWrapper::success($dbResult);
  85. }
  86. /**
  87. * 查询设置
  88. * @param bool $field //true:查询所有 数组:查询多个字段 字符串:查询单个字段
  89. * @param bool $isCustomerId
  90. * @return ResultWrapper
  91. * @throws Exception
  92. */
  93. public function getSetting($field = true, $isCustomerId = false)
  94. {
  95. $setting = $this->objCommissionCache->getSetting();
  96. if(!$setting){
  97. $setting = $this->objDCommissionSetting->get(['enterpriseId' => $this->enterpriseId]);
  98. if($setting === false){
  99. return ResultWrapper::fail($this->objDCommissionSetting->error(), ErrorCode::$dberror);
  100. }
  101. if(empty($setting)){
  102. return ResultWrapper::success($setting);
  103. }
  104. $this->objCommissionCache->saveSetting($setting);
  105. }
  106. if(!empty($setting['info'])){
  107. $setting['info'] = json_decode($setting['info'], true);
  108. }else{
  109. $setting['info'] = [];
  110. }
  111. if(!empty($setting['payType'])){
  112. $setting['payType'] = json_decode($setting['payType'], true);
  113. }else{
  114. $setting['payType'] = [];
  115. }
  116. if(!empty($setting['withdrawType'])){
  117. $setting['withdrawType'] = json_decode($setting['withdrawType'], true);
  118. }else{
  119. $setting['withdrawType'] = [];
  120. }
  121. if(!empty($setting['withdrawInfo'])){
  122. $setting['withdrawInfo'] = json_decode($setting['withdrawInfo'], true);
  123. }else{
  124. $setting['withdrawInfo'] = [];
  125. }
  126. if(!empty($setting['desc'])){
  127. $setting['desc'] = htmlspecialchars_decode($setting['desc'],ENT_QUOTES);
  128. }
  129. if($isCustomerId){
  130. //查询customerId
  131. $objMCustomer = new MCustomer($this->enterpriseId, $this->userCenterId);
  132. $modelResult = $objMCustomer->getCustomerData(['userCenterId'=>$this->userCenterId]);
  133. if(!$modelResult->isSuccess()){
  134. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  135. }
  136. $customerData = $modelResult->getData();
  137. unset($modelResult);
  138. if(empty($customerData)){
  139. return ResultWrapper::fail('客户查询失败', ErrorCode::$dberror);
  140. }
  141. $customerId = $customerData['id'];
  142. // $customerId = 38;
  143. }else{
  144. $customerId = null;
  145. }
  146. $modelResult = self::formatSettingBuyGoods($setting, $customerId);
  147. if(!$modelResult->isSuccess()){
  148. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  149. }
  150. $setting = $modelResult->getData();
  151. unset($modelResult);
  152. if($field === true){
  153. return ResultWrapper::success($setting);
  154. }
  155. if(is_array($field)){
  156. $return = [];
  157. foreach($field as $value){
  158. $return[$value] = null;
  159. if(isset($field[$value])){
  160. $return[$value] = $field[$value];
  161. }
  162. }
  163. return ResultWrapper::success($return);
  164. }
  165. if(isset($setting[$field])){
  166. return ResultWrapper::success($setting[$field]);
  167. }else{
  168. return ResultWrapper::success([]);
  169. }
  170. }
  171. /**
  172. * 格式化设置购买指定商品字段
  173. * @param $params
  174. * @param null $customerId
  175. * @return ResultWrapper
  176. * @throws Exception
  177. */
  178. public function formatSettingBuyGoods($params, $customerId = null)
  179. {
  180. if(!isset($params['buyGoods']) || empty($params['buyGoods'])) return ResultWrapper::success($params);
  181. $params['buyGoods'] = json_decode($params['buyGoods'], true);
  182. //查询所有等级
  183. $objMCommissionGrade = new MCommissionGrade($this->enterpriseId, $this->userCenterId);
  184. $modelResult = $objMCommissionGrade->getAllGrade();
  185. if(!$modelResult->isSuccess()){
  186. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  187. }
  188. $gradeArray = $modelResult->getData();
  189. unset($modelResult);
  190. if(empty($gradeArray)){
  191. return ResultWrapper::success($params);
  192. }
  193. $gradeData = [];
  194. foreach($gradeArray as $value){
  195. $gradeData[$value['grade']] = $value;
  196. }
  197. unset($gradeArray);
  198. //拿出所有商品id
  199. $goodsData = [];
  200. $buyGoodsIds = [];
  201. foreach($params['buyGoods'] as $grade => $goods){
  202. if(!empty($goods)){
  203. foreach($goods as $v) {
  204. if(!empty($v) && isset($v['skuId']) && isset($v['goodsId'])){
  205. $buyGoodsIds[] = [
  206. 'skuId' => $v['skuId'],
  207. 'goodsId' => $v['goodsId']
  208. ];
  209. }
  210. }
  211. }
  212. }
  213. //查询商品
  214. if(!empty($buyGoodsIds)){
  215. $objMCommissionGoods = new MCommissionGoods($this->enterpriseId, $this->userCenterId);
  216. $modelResult = $objMCommissionGoods->formatGoods($buyGoodsIds, $customerId);
  217. if(!$modelResult->isSuccess()){
  218. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  219. }
  220. $goodsResult = $modelResult->getData();
  221. unset($modelResult);
  222. if(!empty($goodsResult)){
  223. foreach($goodsResult as $value){
  224. $goodsData[$value['skuId']] = $value;
  225. }
  226. }
  227. }
  228. $grades = [];
  229. foreach($gradeData as $value){
  230. $gradeArray = [
  231. 'grade' => $value['grade'],
  232. 'gradeId' => $value['id'],
  233. 'gradeName' => $value['name'],
  234. 'goodsData' => []
  235. ];
  236. if(isset($params['buyGoods'][$value['grade']])){
  237. $buyGoodsData = $params['buyGoods'][$value['grade']];
  238. if(!empty($buyGoodsData)){
  239. foreach($buyGoodsData as $v){
  240. if(!empty($goodsData) && isset($goodsData[$v['skuId']])){
  241. $gradeArray['goodsData'][] = [
  242. 'images' => $goodsData[$v['skuId']]['images'],
  243. 'goodsName' => $goodsData[$v['skuId']]['goodsName'],
  244. 'unitName' => $goodsData[$v['skuId']]['unitName'],
  245. 'specGroup' => $goodsData[$v['skuId']]['specGroup'],
  246. 'salePrice' => $goodsData[$v['skuId']]['salePrice'],
  247. 'inventory' => $goodsData[$v['skuId']]['inventory'],
  248. 'goodsId' => $v['goodsId'],
  249. 'skuId' => $v['skuId'],
  250. ];
  251. }
  252. }
  253. }
  254. }
  255. $grades[$value['grade']] = $gradeArray;
  256. }
  257. ksort($grades);
  258. $params['buyGoods'] = array_values($grades);
  259. return ResultWrapper::success($params);
  260. }
  261. /**
  262. * 保存文本设置
  263. * @param $params
  264. * @return ResultWrapper
  265. */
  266. public function saveTxtSetting($params)
  267. {
  268. $where = [
  269. 'enterpriseId' => $this->enterpriseId,
  270. ];
  271. $dbResult = $this->objDCommissionDesc->get($where);
  272. if($dbResult === false){
  273. return ResultWrapper::fail($this->objDCommissionDesc->error(), ErrorCode::$dberror);
  274. }
  275. $setting = $dbResult;
  276. unset($dbResult);
  277. if(empty($setting)){
  278. //新增
  279. $insert = [
  280. 'enterpriseId' => $this->enterpriseId,
  281. 'desc' => json_encode($params['desc']),
  282. 'updateTime' => time(),
  283. 'createTime' => time()
  284. ];
  285. $dbResult = $this->objDCommissionDesc->insert($insert);
  286. }else{
  287. //修改
  288. $update = [
  289. 'desc' => json_encode($params['desc']),
  290. 'updateTime' => time()
  291. ];
  292. $dbResult = $this->objDCommissionDesc->update($update, $where);
  293. }
  294. if($dbResult === false){
  295. return ResultWrapper::fail($this->objDCommissionDesc->error(), ErrorCode::$dberror);
  296. }
  297. return ResultWrapper::success($dbResult);
  298. }
  299. /**
  300. * 查询文字设置
  301. */
  302. public function getTxtSetting()
  303. {
  304. $where = [
  305. 'enterpriseId' => $this->enterpriseId,
  306. ];
  307. $dbResult = $this->objDCommissionDesc->get($where);
  308. if($dbResult === false){
  309. return ResultWrapper::fail($this->objDCommissionDesc->error(), ErrorCode::$dberror);
  310. }
  311. $returnData = [];
  312. !empty($dbResult) && $returnData = json_decode($dbResult['desc'], true);
  313. return ResultWrapper::success($returnData);
  314. }
  315. /**
  316. * 获取分销商品
  317. * @throws Exception
  318. */
  319. public function getBuyGoods()
  320. {
  321. $modelResult = self::getSetting('buyGoods', true);
  322. if(!$modelResult->isSuccess()){
  323. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  324. }
  325. $buyGoods = $modelResult->getData();
  326. unset($modelResult);
  327. $return = [];
  328. foreach($buyGoods as $value){
  329. foreach($value['goodsData'] as $v){
  330. $v['grade'] = $value['grade'];
  331. $v['gradeName'] = $value['gradeName'];
  332. $return[] = $v;
  333. }
  334. }
  335. return ResultWrapper::success($return);
  336. }
  337. }