MBasicSetup.Class.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * 基本设置
  4. * Created by PhpStorm.
  5. * User: XiaoMing
  6. * Date: 2019/12/5
  7. * Time: 15:24
  8. */
  9. namespace JinDouYun\Model\System;
  10. use Jindouyun\Cache\EnterpriseCache;
  11. use JinDouYun\Model\SysAreaChina\MSysAreaChina;
  12. use Mall\Framework\Core\ErrorCode;
  13. use Mall\Framework\Core\ResultWrapper;
  14. use JinDouYun\Dao\System\DBasicSetup;
  15. class MBasicSetup
  16. {
  17. private $objDBasicSetup;
  18. private $onlineEnterpriseId;
  19. private $objEnterpriseCache;
  20. public function __construct($enterpriseId)
  21. {
  22. $this->onlineEnterpriseId = $enterpriseId;
  23. $this->objDBasicSetup = new DBasicSetup('default');
  24. $this->objEnterpriseCache = new EnterpriseCache();
  25. }
  26. /**
  27. * @param $params
  28. * @return ResultWrapper
  29. */
  30. public function setting($params)
  31. {
  32. $dbResult = $this->objDBasicSetup->replace($params);
  33. if ($dbResult === false) {
  34. return ResultWrapper::fail($this->objDBasicSetup->error(), ErrorCode::$dberror);
  35. }
  36. //缓存redis企业生产日期设置
  37. $cacheData = json_decode($params['basicData'], true);
  38. $this->objEnterpriseCache->EnterpriseDataCache($this->onlineEnterpriseId, 'shelfLifeSetUp', $cacheData['shelfLifeSetUp']);
  39. //删除企业缓存
  40. $this->objEnterpriseCache->delEnterpriseSetting($this->onlineEnterpriseId);
  41. //缓存企业设置
  42. $cacheResult = $this->objEnterpriseCache->setEnterpriseSetting($this->onlineEnterpriseId, $cacheData);
  43. if(!$cacheResult){
  44. return ResultWrapper::fail('缓存添加失败', ErrorCode::$paramError);
  45. }
  46. return ResultWrapper::success($dbResult);
  47. }
  48. /**
  49. * @param bool $enterpriseId
  50. * @return ResultWrapper
  51. */
  52. public function getBasicSetup($enterpriseId = false)
  53. {
  54. !$enterpriseId && $enterpriseId = $this->onlineEnterpriseId;
  55. //获取缓存企业设置
  56. $cacheResult = $this->objEnterpriseCache->getEnterpriseSetting($enterpriseId);
  57. if($cacheResult){
  58. $format['basicData'] = $cacheResult;
  59. $dbResult = self::formatArea($format);
  60. return ResultWrapper::success($dbResult);
  61. }
  62. unset($cacheResult);
  63. $dbResult = $this->objDBasicSetup->get(['enterpriseId' => $enterpriseId]);
  64. if ($dbResult === false) {
  65. return ResultWrapper::fail($this->objDBasicSetup->error(), ErrorCode::$dberror);
  66. }
  67. if (!empty($dbResult)) {
  68. $dbResult['basicData'] = empty($dbResult['basicData']) ? [] : json_decode($dbResult['basicData'], true);
  69. //缓存企业设置
  70. $cacheResult = $this->objEnterpriseCache->setEnterpriseSetting($enterpriseId, $dbResult['basicData']);
  71. if(!$cacheResult){
  72. return ResultWrapper::fail('缓存添加失败', ErrorCode::$paramError);
  73. }
  74. if (!empty($dbResult['basicData']) && isset($dbResult['basicData'])) {
  75. $dbResult = self::formatArea($dbResult);
  76. }
  77. }
  78. return ResultWrapper::success($dbResult);
  79. }
  80. /**
  81. * 格式化省市区
  82. * @param $params
  83. * @return mixed
  84. */
  85. private function formatArea($params)
  86. {
  87. $objMSysAreaChina = new MSysAreaChina();
  88. $areaName = $objMSysAreaChina->getNameByCode([
  89. isset($params['basicData']['region'][0]) ? $params['basicData']['region'][0] : 0,
  90. isset($params['basicData']['region'][1]) ? $params['basicData']['region'][1] : 0,
  91. isset($params['basicData']['region'][2]) ? $params['basicData']['region'][2] : 0
  92. ]);
  93. if (!empty($areaName)){
  94. if (isset($params['basicData']['region'][0])){
  95. $params['area']['provinceName'] = isset($areaName[$params['basicData']['region'][0]]) ? $areaName[$params['basicData']['region'][0]] : '';
  96. }
  97. if (isset($params['basicData']['region'][1])){
  98. $params['area']['cityName'] = isset($areaName[$params['basicData']['region'][1]]) ? $areaName[$params['basicData']['region'][1]] : '';
  99. }
  100. if (isset($params['basicData']['region'][2])){
  101. $params['area']['districtName'] = isset($areaName[$params['basicData']['region'][2]]) ? $areaName[$params['basicData']['region'][2]] : '';
  102. }
  103. }
  104. return $params;
  105. }
  106. /**
  107. * 获取保质期设置
  108. */
  109. public function getShelfLifeSetUp()
  110. {
  111. $dbResult = $this->objDBasicSetup->get(['enterpriseId' => $this->onlineEnterpriseId]);
  112. if ($dbResult === false) {
  113. return ResultWrapper::fail($this->objDBasicSetup->error(), ErrorCode::$dberror);
  114. }
  115. if (!empty($dbResult)) {
  116. $dbResult['basicData'] = empty($dbResult['basicData']) ? [] : json_decode($dbResult['basicData'], true);
  117. }
  118. $returnData = $dbResult['basicData']['shelfLifeSetUp'];//保质期设置 4:禁用 5:启用
  119. return ResultWrapper::success($returnData);
  120. }
  121. /**
  122. * @param $field
  123. * @return ResultWrapper
  124. */
  125. public function getBasicField($field)
  126. {
  127. $dbResult = $this->objDBasicSetup->get_field('basicData', ['enterpriseId' => $this->onlineEnterpriseId]);
  128. if ($dbResult === false) {
  129. return ResultWrapper::fail($this->objDBasicSetup->error(), ErrorCode::$dberror);
  130. }
  131. $basicData = [];
  132. if (!empty($dbResult)) {
  133. $basicData = empty($dbResult) ? [] : json_decode($dbResult, true);
  134. }
  135. $return = [];
  136. if (is_string($field)) {
  137. $return = [
  138. $field => isset($basicData[$field]) ? $basicData[$field] : '',
  139. ];
  140. } elseif (is_array($field)) {
  141. foreach ($field as $key => $value) {
  142. $return[$value] = isset($basicData[$value]) ? $basicData[$value] : '';
  143. }
  144. }
  145. return ResultWrapper::success($return);
  146. }
  147. }