IncomePageFactory.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. *
  5. * User: king/QQ:995265288
  6. * Date: 2018/5/8 下午2:05
  7. * Email: livsyitian@163.com
  8. */
  9. namespace app\frontend\modules\finance\factories;
  10. use app\backend\modules\member\models\MemberRelation;
  11. use app\common\exceptions\AppException;
  12. use app\common\models\Income;
  13. use app\frontend\modules\finance\interfaces\IIncomePage;
  14. use app\frontend\modules\member\models\MemberModel;
  15. class IncomePageFactory
  16. {
  17. /**
  18. * @var IIncomePage
  19. */
  20. private $_income;
  21. /**
  22. * 会员是否是推客
  23. *
  24. * @var bool
  25. */
  26. private $is_agent;
  27. /**
  28. * 是否开启关系链
  29. *
  30. * @var bool
  31. */
  32. private $is_relation;
  33. private $lang_set;
  34. /**
  35. * 会员收入
  36. * @var array
  37. */
  38. private $total_income;
  39. private $key;
  40. public function __construct(IIncomePage $income, $lang_set, $is_relation = false, $is_agent = false, $total_income,$key)
  41. {
  42. $this->_income = $income;
  43. $this->is_agent = $is_agent;
  44. $this->is_relation = $is_relation;
  45. $this->lang_set = $lang_set;
  46. $this->total_income = $total_income;
  47. $this->key = $key;
  48. }
  49. /**
  50. * 收入模型是否显示
  51. *
  52. * @return bool
  53. */
  54. public function isShow()
  55. {
  56. return $this->_income->isShow();
  57. }
  58. /**
  59. * 是否拥有收入模型使用权限
  60. *
  61. * @return bool
  62. */
  63. public function isAvailable()
  64. {
  65. return $this->_income->isAvailable();
  66. }
  67. /**
  68. * 是否需要验证 是否开启关系链
  69. *
  70. * @return bool
  71. */
  72. public function validatorIsRelation()
  73. {
  74. return $this->_income->needIsRelation();
  75. }
  76. /**
  77. * 是否需要验证 是否是推客
  78. *
  79. * @return bool
  80. */
  81. public function validatorIsAgent()
  82. {
  83. return $this->_income->needIsAgent();
  84. }
  85. /**
  86. * 收入页的前端路由
  87. * @return string 前端路由名
  88. */
  89. public function getAppUrl()
  90. {
  91. return $this->_income->getAppUrl();
  92. }
  93. /**
  94. * 获取收入模型数据
  95. * @param bool $level
  96. * @return array
  97. * @throws AppException
  98. */
  99. public function getIncomeData($level = true)
  100. {
  101. if (!$this->isShow()) {
  102. throw new AppException('IncomeFactory' . $this->_income->getTitle() . 'no use');
  103. }
  104. return [
  105. 'url' => $this->_income->getAppUrl(),
  106. 'icon' => $this->_income->getIcon(),
  107. 'mark' => $this->getMark(),
  108. 'title' => $this->getTitle(),
  109. 'level' => $level ? $this->_income->getLevel() : '', //新增参数控制是否获取等级信息
  110. 'value' => round($this->getValue(), 2),
  111. 'is_agent' => $this->isAgent(),
  112. 'is_relation' => $this->isRelation(),
  113. 'mini_url' => $this->getMiniUrl(),
  114. 'isDiy' => $this->isDiy(),
  115. ];
  116. }
  117. public function getMark()
  118. {
  119. return $this->_income->getMark();
  120. }
  121. private function getMiniUrl(){
  122. if (method_exists($this->_income, 'getMiniUrl')) {
  123. return $this->_income->getMiniUrl();
  124. }
  125. return '';
  126. }
  127. public function getTitle()
  128. {
  129. $mark = $this->_income->getMark();
  130. if (isset($this->lang_set[$mark]['title']) && !empty($this->lang_set[$mark]['title'])) {
  131. return $this->lang_set[$mark]['title'];
  132. }
  133. return $this->_income->getTitle();
  134. }
  135. /**
  136. * 收入模型累计收入值
  137. *
  138. * @return float
  139. */
  140. private function getValue()
  141. {
  142. $value = $this->_income->getTypeValue();//这里是累计金额
  143. if (!is_numeric($value)) {
  144. $total_income = $this->total_income->where('incometable_type', $value)->first();
  145. $value = $total_income ? $total_income->total_amount : '0.00';
  146. }
  147. if ($this->getSet()) {//后台插件金额显示设置
  148. $value = $this->calculation();//计算可提现金额
  149. }
  150. return $value;
  151. }
  152. /**
  153. * @return mixed
  154. * 类型
  155. */
  156. private function getSet()
  157. {
  158. $type_name = $this->_income->getMark();
  159. $type = request()->type;
  160. switch ($type){
  161. case 5: $res = \Setting::get("popularize.wap.".$type_name); break;
  162. case 1: $res = \Setting::get("popularize.wechat.".$type_name); break;
  163. case 2: $res = \Setting::get("popularize.mini.".$type_name); break;
  164. case 9: $res = \Setting::get("popularize.app.".$type_name); break;
  165. case 8: $res = \Setting::get("popularize.alipay.".$type_name); break;
  166. default : $res = \Setting::get("popularize.wechat.".$type_name); break;
  167. }
  168. return $res;
  169. }
  170. /**
  171. * 计算手续费独立提现手续费
  172. */
  173. private function calculation()
  174. {
  175. $value = Income::getIncomes()->where('member_id', \YunShop::app()->getMemberId())
  176. ->where('incometable_type', (string)$this->_income->getTypeValue())
  177. ->where('status', 0)->sum('amount');
  178. return $value;
  179. }
  180. /**
  181. * 如果需要验证关系链,返回关系链状态,默认返回 true
  182. * @return bool
  183. */
  184. private function isRelation()
  185. {
  186. //已经获得权限不需要验证关系链
  187. if ($this->isAvailable()) {
  188. return true;
  189. }
  190. if ($this->_income->needIsRelation()) {
  191. return $this->is_relation;
  192. }
  193. return true;
  194. }
  195. /**
  196. * 如果需要验证是否是推客,返回推客状态,默认返回 true
  197. *
  198. * @return bool
  199. */
  200. private function isAgent()
  201. {
  202. //已经获得权限不需要验证是否是推客
  203. if ($this->isAvailable()) {
  204. return true;
  205. }
  206. if ($this->_income->needIsRelation()) {
  207. return $this->is_agent;
  208. }
  209. return true;
  210. }
  211. private function isDiy(): bool
  212. {
  213. if (method_exists($this->_income, 'isDiy')) {
  214. return $this->_income->isDiy();
  215. }
  216. return false;
  217. }
  218. }