SaasForm.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use app\models\Admin;
  9. use app\models\GiveIntegralRules;
  10. use app\models\RulesFaceStore;
  11. use app\models\RulesGiveIntegralStore;
  12. use app\models\Store;
  13. use Yii;
  14. use Exception;
  15. use yii\base\Model;
  16. use app\models\Option;
  17. use app\constants\OptionSetting;
  18. class SaasForm extends Model
  19. {
  20. public $rate;
  21. public $name;
  22. public $start_date;
  23. public $end_date;
  24. public $status;
  25. public $face_pay_ids;
  26. public $give_integral_ids;
  27. public $type;
  28. public $rule_id;
  29. public static $bankSetting = [];
  30. public static $bankSettingStore = [];
  31. public static $offlineSetting = [];
  32. public function rules()
  33. {
  34. return [
  35. [['start_date', 'end_date', 'type', 'status'], 'integer'],
  36. [['face_pay_ids', 'give_integral_ids', 'name', 'rule_id'], 'string'],
  37. [['rate'], 'number']
  38. ];
  39. }
  40. public function getStoreList()
  41. {
  42. try {
  43. // $type = $this->type;
  44. // $rule_id = $this->rule_id;
  45. $name = $this->name;
  46. $query = Store::find()->alias('s')->leftJoin(['a' => Admin::tableName()], 's.admin_id = a.id')->where(['s.is_delete' => 0, 'a.is_delete' => 0])->select('s.id, s.name, s.logo');
  47. $store_ids = [];
  48. //排除store_id
  49. // if ($rule_id) {
  50. // if ((int)$type === 0 || empty($type)) {
  51. // $store_ids = RulesFaceStore::find()->where(['rule_id' => $rule_id])->select('store_id')->column();
  52. // }
  53. // if ((int)$type === 1) {
  54. // $store_ids = RulesGiveIntegralStore::find()->where(['rule_id' => $rule_id])->select('store_id')->column();
  55. // }
  56. // }
  57. if ($name) {
  58. $query->andWhere(['like' ,'s.name', $name]);
  59. }
  60. $query->andWhere(['not in', 's.id', $store_ids]);
  61. // $pagination = pagination_make($query);
  62. // $list = $pagination['list'];
  63. $list = $query->asArray()->all();
  64. return [
  65. 'code' => 0,
  66. 'data' => [
  67. 'list' => $list,
  68. // 'pageNo' => $pagination['pageNo'],
  69. // 'totalCount' => $pagination['totalCount']
  70. ]
  71. ];
  72. } catch (\Exception $e) {
  73. return [
  74. 'code' => 1,
  75. 'msg' => $e->getMessage()
  76. ];
  77. }
  78. }
  79. public function getRulesList() {
  80. try {
  81. $status = $this->status;
  82. $name = $this->name;
  83. $end_date = strtotime($this->end_date);
  84. $start_date = strtotime($this->start_date);
  85. $query_main = GiveIntegralRules::find()->where(['is_delete' => 0]);
  86. if (in_array($status, [1, 2])) {
  87. $query_main->andWhere(['status' => $status]);
  88. }
  89. if ($start_date) {
  90. $query_main->andWhere(['>=', 'start_date', $start_date]);
  91. }
  92. if ($end_date) {
  93. $query_main->andWhere(['<=', 'end_date', $end_date]);
  94. }
  95. $pagination = pagination_make($query_main);
  96. $list = $pagination['list'];
  97. foreach ($list as $index => &$item) {
  98. $open = false;
  99. $item['faceStore'] = RulesFaceStore::find()->where(['fs.rule_id' => $item['id']])->alias('fs')
  100. ->leftJoin(['s' => Store::tableName()], 'fs.store_id = s.id')
  101. ->select('s.id, s.name, s.logo, fs.rule_id')->asArray()->all();
  102. foreach ($item['faceStore'] as &$face_item) {
  103. if (!empty($name) && ($face_item['name'] !== $name)) {
  104. $open = true;
  105. }
  106. }
  107. $item['giveIntegralStore'] = RulesGiveIntegralStore::find()->where(['gis.rule_id' => $item['id']])
  108. ->alias('gis')->leftJoin(['s' => Store::tableName()], 'gis.store_id = s.id')
  109. ->select('s.id, s.name, s.logo, gis.rule_id')->asArray()->all();
  110. $item['created_at'] = date("Y-m-d H:i", $item['created_at']);
  111. $item['start_date'] = date("Y-m-d H:i:s", $item['start_date']);
  112. $item['end_date'] = date("Y-m-d H:i:s", $item['end_date']);
  113. if ($open) {
  114. unset($list[$index]);
  115. }
  116. }
  117. return [
  118. 'code' => 0,
  119. 'msg' => 'success',
  120. 'data' => [
  121. 'data' => $list,
  122. 'pageNo' => $pagination['pageNo'],
  123. 'totalCount' => $pagination['totalCount']
  124. ]
  125. ];
  126. } catch (\Exception $e) {
  127. return [
  128. 'code' => 1,
  129. 'msg' => $e->getMessage()
  130. ];
  131. }
  132. }
  133. public function getInfo() {
  134. try {
  135. $rule_id = $this->rule_id;
  136. $rules = GiveIntegralRules::find()->where(['id' => $rule_id, 'is_delete' => 0])->with([
  137. 'faceStore' => function ($query) {
  138. $query->alias('fs')->leftJoin(['s' => Store::tableName()], 'fs.store_id = s.id')->select('s.id, s.name, s.logo, fs.rule_id')->asArray();
  139. },
  140. 'giveIntegralStore' => function ($query) {
  141. $query->alias('gis')->leftJoin(['s' => Store::tableName()], 'gis.store_id = s.id')->select('s.id, s.name, s.logo, gis.rule_id')->asArray();
  142. },
  143. ])->asArray()->one();
  144. $rules['created_at'] = date("Y-m-d H:i", $rules['created_at']);
  145. $rules['start_date'] = date("Y-m-d H:i:s", $rules['start_date']);
  146. $rules['end_date'] = date("Y-m-d H:i:s", $rules['end_date']);
  147. return [
  148. 'code' => 0,
  149. 'msg' => 'success',
  150. 'data' => $rules
  151. ];
  152. } catch (\Exception $e) {
  153. return [
  154. 'code' => 1,
  155. 'msg' => $e->getMessage()
  156. ];
  157. }
  158. }
  159. public function save()
  160. {
  161. try {
  162. $rule_id = $this->rule_id;
  163. $face_pay_ids = explode(',', $this->face_pay_ids);
  164. $give_integral_ids = explode(',', $this->give_integral_ids);
  165. $rules = GiveIntegralRules::findOne(['id' => $rule_id, 'is_delete' => 0]);
  166. if (!$rule_id) {
  167. $rules = new GiveIntegralRules();
  168. $rules->created_at = time();
  169. }
  170. $rules->rate = $this->rate;
  171. $rules->start_date = strtotime($this->start_date);
  172. $rules->end_date = strtotime($this->end_date);
  173. if (!in_array((int)$this->status, [1, 2])) {
  174. throw new \Exception("状态错误");
  175. }
  176. $rules->status = $this->status;
  177. if (!$rules->save()) {
  178. throw new \Exception(json_encode($rules->errors));
  179. }
  180. $rule_id = $rules->id;
  181. RulesFaceStore::deleteAll(['rule_id' => $rule_id]);
  182. RulesGiveIntegralStore::deleteAll(['rule_id' => $rule_id]);
  183. foreach ($face_pay_ids as $item) {
  184. $form = new RulesFaceStore();
  185. $form->store_id = $item;
  186. $form->rule_id = $rule_id;
  187. $form->created_at = time();
  188. if (!$form->save()) {
  189. throw new \Exception(json_encode($form->errors));
  190. }
  191. }
  192. foreach ($give_integral_ids as $item) {
  193. $form = new RulesGiveIntegralStore();
  194. $form->store_id = $item;
  195. $form->rule_id = $rule_id;
  196. $form->created_at = time();
  197. $form->save();
  198. if (!$form->save()) {
  199. throw new \Exception(json_encode($form->errors));
  200. }
  201. }
  202. return [
  203. 'code' => 0,
  204. 'msg' => "保存成功"
  205. ];
  206. } catch (\Exception $e) {
  207. return [
  208. 'code' => 1,
  209. 'msg' => $e->getMessage()
  210. ];
  211. }
  212. }
  213. public function setStatus()
  214. {
  215. try {
  216. $rule_id = $this->rule_id;
  217. if (!is_string($rule_id)) {
  218. throw new \Exception("数据格式错误");
  219. }
  220. $rule_id = explode(',', $rule_id);
  221. $status = (int)$this->status;
  222. $rules = GiveIntegralRules::find()->where(['id' => $rule_id, 'is_delete' => 0])->all();
  223. if (!$rules) {
  224. throw new \Exception("获取数据失败");
  225. }
  226. foreach ($rules as $item) {
  227. if (in_array($status, [1, 2])) {
  228. $item->status = $status;
  229. } elseif ($status === 3) {
  230. $item->is_delete = 1;
  231. }
  232. if (!$item->save()) {
  233. throw new \Exception(json_encode($item->errors));
  234. }
  235. }
  236. return [
  237. 'code' => 0,
  238. 'msg' => "操作成功"
  239. ];
  240. } catch (\Exception $e) {
  241. return [
  242. 'code' => 1,
  243. 'msg' => $e->getMessage()
  244. ];
  245. }
  246. }
  247. public static function yunstSetting($is_saas = 0, $store_id = 0, $refresh = 0){
  248. if(self::$bankSetting && !$refresh){
  249. $oldConf = self::$bankSetting;
  250. }else{
  251. $oldConf = Option::get(OptionSetting::SAAS_YUNST_SETTING, 0, 'saas')['value'];
  252. self::$bankSetting = $oldConf;
  253. }
  254. $oldConf && $oldConf = json_decode($oldConf, true);
  255. return $oldConf ?: null;
  256. }
  257. public static function yunstSettingSave($is_saas = 0, $store_id = 0, $conf = []){
  258. if(empty($conf)){
  259. return false;
  260. }
  261. // print_r($conf);die;
  262. $oldConf = json_decode(Option::get(OptionSetting::SAAS_YUNST_SETTING, 0, 'saas', '{}')['value'], true);
  263. $conf = array_merge($oldConf, $conf);
  264. $set = Option::set(OptionSetting::SAAS_YUNST_SETTING, json_encode($conf), 0, 'saas');
  265. self::yunstSetting($is_saas, $store_id, 1);
  266. return $set;
  267. }
  268. public static function bankSetting($is_saas = 0, $store_id = 0, $refresh = 0){
  269. if($is_saas){
  270. if(self::$bankSetting && !$refresh){
  271. $oldConf = self::$bankSetting;
  272. }else{
  273. $oldConf = Option::get(OptionSetting::SAAS_BANK_SETTING, 0, 'saas')['value'];
  274. self::$bankSetting = $oldConf;
  275. }
  276. }elseif($store_id){
  277. if(self::$bankSettingStore[$store_id] && !$refresh){
  278. $oldConf = self::$bankSettingStore[$store_id];
  279. }else{
  280. $oldConf = Option::get(OptionSetting::STORE_BANK_SETTING, $store_id, 'saas')['value'];
  281. self::$bankSettingStore[$store_id] = $oldConf;
  282. }
  283. }
  284. $oldConf && $oldConf = json_decode($oldConf, true);
  285. return $oldConf ?: null;
  286. }
  287. public static function bankSettingSave($is_saas = 0, $store_id = 0, $conf = []){
  288. /**
  289. *
  290. card_id 银行卡号
  291. card_name 银行卡对应的户名
  292. cert_id 证件号,银行账户类型为对私时,必填
  293. cert_type 证件类型,仅支持:00-身份证,银行账户类型为对私时,必填
  294. tel_no 手机号
  295. bank_code 银行编码,详见附录 银行代码,银行账户类型对公时,必填
  296. bank_name 开户银行名称
  297. bank_acct_type 银行账户类型:1-对公;2-对私
  298. prov_code 银行账户开户银行所在省份编码 (省市编码),银行账户类型为对公时,必填
  299. area_code 银行账户开户银行所在地区编码(省市编码),银行账户类型为对公时,必填
  300. */
  301. if(empty($conf)){
  302. return false;
  303. }
  304. if($is_saas){
  305. $oldConf = json_decode(Option::get(OptionSetting::SAAS_BANK_SETTING, 0, 'saas', '{}')['value'], true);
  306. $conf = array_merge($oldConf, $conf);
  307. $set = Option::set(OptionSetting::SAAS_BANK_SETTING, json_encode($conf), 0, 'saas');
  308. }elseif($store_id){
  309. $oldConf = json_decode(Option::get(OptionSetting::STORE_BANK_SETTING, $store_id, 'saas', '{}')['value'], true);
  310. $conf = array_merge($oldConf, $conf);
  311. $set = Option::set(OptionSetting::STORE_BANK_SETTING, json_encode($conf), $store_id, 'saas');
  312. }
  313. self::bankSetting($is_saas, $store_id, 1);
  314. return $set;
  315. }
  316. public static function bankSettingGet($is_saas, $store_id, $key){
  317. $oldConf = self::bankSetting($is_saas, $store_id);
  318. if(empty($oldConf)){
  319. return null;
  320. }
  321. return $oldConf[$key] ?? null;
  322. }
  323. public static function offlineSetting($store_id = 0, $refresh = 0){
  324. if(self::$offlineSetting[$store_id] && !$refresh){
  325. $oldConf = self::$offlineSetting[$store_id];
  326. }else{
  327. $oldConf = Option::get(OptionSetting::STORE_PAY_OFFLINE, $store_id, 'saas')['value'];
  328. self::$offlineSetting[$store_id] = $oldConf;
  329. }
  330. $oldConf && $oldConf = json_decode($oldConf, true);
  331. return $oldConf ?: [
  332. //
  333. 'open_bank' => '0', //是否开启银行卡支付
  334. 'open_alipay' => '0', //是否开启支付宝支付
  335. 'open_wechat' => '0', //是否开启微信支付
  336. //
  337. 'bank_name' => '', //开户银行名称
  338. 'bank_branch_name' => '', //开户银行支行名称
  339. 'bank_card_id' => '', //银行卡号
  340. 'bank_card_name' => '', //银行卡对应的户名
  341. 'alipay_qrcode' => '', //支付宝收款二维码
  342. 'wechat_qrcode' => '', //微信收款二维码
  343. ];
  344. }
  345. public static function offlineSettingSave($store_id = 0, $conf = []){
  346. /**
  347. *
  348. open_bank 是否开启银行卡支付
  349. open_alipay 是否开启支付宝支付
  350. open_wechat 是否开启微信支付
  351. *
  352. bank_name 开户银行名称
  353. bank_branch_name 开户银行支行名称
  354. bank_card_id 银行卡号
  355. bank_card_name 银行卡对应的户名
  356. alipay_qrcode 支付宝收款二维码
  357. wechat_qrcode 微信收款二维码
  358. */
  359. if(empty($conf)){
  360. return false;
  361. }
  362. $oldConf = json_decode(Option::get(OptionSetting::STORE_PAY_OFFLINE, $store_id, 'saas', '{}')['value'], true);
  363. $conf = array_merge($oldConf, $conf);
  364. $set = Option::set(OptionSetting::STORE_PAY_OFFLINE, json_encode($conf), $store_id, 'saas');
  365. self::offlineSetting($store_id, 1);
  366. return $set;
  367. }
  368. public static function offlineSettingGet($store_id, $key){
  369. $oldConf = self::offlineSetting($store_id);
  370. if(empty($oldConf)){
  371. return null;
  372. }
  373. return $oldConf[$key] ?? null;
  374. }
  375. }