cxfoot/models/common/CommonPaymentSettingForm.php
2023-10-27 14:25:12 +08:00

137 lines
4.0 KiB
PHP

<?php
/**
* @author Any
* @description KISS
* @date 2021年8月12日
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\models\common;
use app\models\PaymentTypes;
use app\models\Model;
class CommonPaymentSettingForm extends Model
{
public $pay_types;
public $cx_mch_id;
public function rules()
{
return [
[['cx_mch_id'], 'integer'],
[['pay_types'],'required', 'on' => 'edit'],
[['cx_mch_id'], 'required'],
];
}
public function attributeLabels(){
return [
'pay_types' => '支付类型',
'cx_mch_id' => '平台商户ID',
];
}
public function save()
{
if(!$this->validate()){
return $this->getModelError();
}
$t = \Yii::$app->db->beginTransaction();
$res = $this->savePayType();
if($res['code'] != 0){
$t->rollBack();
return $this->apiReturnError($res['msg']);
}
$t->commit();
return $this->apiReturnSuccess('保存成功');
}
private function savePayType()
{
PaymentTypes::updateAll(['is_open' => 0],[
'cx_mch_id' => $this->cx_mch_id,
'is_open' => 1,
]);
$pay_types_default = PaymentTypes::payTypes();
foreach ($this->pay_types as $pay_type){
$pay_type = trim($pay_type);
$model = PaymentTypes::findOne(['cx_mch_id' => $this->cx_mch_id, 'short_name' => $pay_type]);
if($model == null){
$model = new PaymentTypes();
$model->cx_mch_id = $this->cx_mch_id;
}
$model->name = $pay_types_default[$pay_type]['name'];
$model->short_name = $pay_types_default[$pay_type]['short_name'];
$model->is_show = 1;
$model->is_open = 1;
if(!$model->save()){
return $this->getModelError($model);
}
}
return $this->apiReturnSuccess();
}
private function initPayType()
{
$pay_types_default = PaymentTypes::payTypes();
$pay_types = [];
foreach ($pay_types_default as $key => $val){
array_push($pay_types,$val['short_name']);
}
$this->pay_types = $pay_types;
$this->savePayType();
$list = PaymentTypes::find()
->where([
'cx_mch_id' => $this->cx_mch_id,
])
->select('name,short_name,is_show,is_open')->asArray()->all();
return $list;
}
public function search()
{
if(!$this->validate()){
return $this->getModelError();
}
$pay_types = PaymentTypes::find()
->where([
'cx_mch_id' => $this->cx_mch_id,
])
->select('name,short_name,is_show,is_open')->asArray()->all();
if(!$pay_types){
$pay_types = $this->initPayType();
}
$pay_types_default = PaymentTypes::payTypes();
foreach($pay_types_default as $key => $val){
if(!in_array($val['short_name'], array_column($pay_types, 'short_name'))){
$pay_type_item = [
'name' => $val['name'],
'short_name' => $val['short_name'],
'is_show' => $val['disabled'] ? false : true,
'is_open' => $val['disabled'] ? false : true,
];
array_push($pay_types, $pay_type_item);
}
}
foreach ($pay_types as $index => $item){
$item['is_show'] = $pay_types_default[$item['short_name']]['disabled'] ? false : true;
$item['is_open'] = isset($item['is_open']) && $item['is_open'] == 1 ? true : false;
$pay_types[$index] = $item;
}
$data = [];
$data['pay_types'] = $pay_types;
return $data;
}
}