583 lines
19 KiB
PHP
583 lines
19 KiB
PHP
<?php
|
||
|
||
/**
|
||
* @author Any
|
||
* @description KISS
|
||
* @date 2021-5-17
|
||
* @version 1.0.0
|
||
*
|
||
* _____LOG_____
|
||
*
|
||
*/
|
||
|
||
namespace app\modules\api\models;
|
||
|
||
use function AlibabaCloud\Client\value;
|
||
use app\components\SysConst;
|
||
use app\models\common\payment\PaymentOrder;
|
||
use app\components\SiteHelper;
|
||
use app\models\Detection;
|
||
use app\models\Goods;
|
||
use app\models\Model;
|
||
use app\models\Order;
|
||
use app\models\OrderDetail;
|
||
use app\models\PaymentTypes;
|
||
use app\models\Store;
|
||
use app\models\UniqueOrderNo;
|
||
use app\models\User;
|
||
use app\models\UserBallArm;
|
||
use app\modules\api\components\ApiHelper;
|
||
use app\modules\api\components\GetDistance;
|
||
use yii\data\Pagination;
|
||
|
||
class BallArmForm extends ApiModel
|
||
{
|
||
public $user_id;
|
||
public $cx_mch_id;
|
||
public $page;
|
||
public $limit;
|
||
public $store_id; # 门店id
|
||
public $title; # 球杆名称
|
||
public $data;
|
||
public $id;
|
||
public $type;
|
||
public $order_id;
|
||
|
||
|
||
# 球杆数据key
|
||
public static $DATA_KEY = ['CLUB', 'MODEL', 'LENGTH', 'LOFT', 'LIE', 'S/W', 'T/W', 'SHAFT', 'CPM', 'TQ', 'GRIP', 'SICE', 'TYPE'];
|
||
|
||
public function rules()
|
||
{
|
||
return [
|
||
[['title'], 'trim'],
|
||
[['title', 'data'], 'string'],
|
||
[['user_id', 'page', 'limit', 'store_id', 'id', 'type', 'order_id'], 'integer'],
|
||
// [['user_id', 'cx_mch_id'], 'required'],
|
||
];
|
||
}
|
||
|
||
/**
|
||
* 球杆列表【用户查看列表】
|
||
*/
|
||
public function actionGetList()
|
||
{
|
||
if (!$this->validate()) {
|
||
return $this->getModelError();
|
||
}
|
||
$query = UserBallArm::find()->alias('user_ball')
|
||
->innerJoin(['user' => User::tableName()], 'user_ball.uid=user.id')
|
||
->andWhere([
|
||
'user_ball.is_delete' => 0,
|
||
'user_ball.uid' => $this->user_id,
|
||
])
|
||
->andWhere([
|
||
'in', 'user_ball.status', [3, 4]
|
||
])
|
||
->select('user_ball.id,user_ball.title,user_ball.status,user_ball.created_at,user_ball.updated_at,user.real_name,user.avatar_url,order_id');
|
||
if($this->type == 2){
|
||
$query->groupBy('order_id');
|
||
}
|
||
|
||
$count_query = clone $query;
|
||
$count = $count_query->count();
|
||
|
||
if (empty($this->limit)) {
|
||
$this->limit = 10;
|
||
}
|
||
if (empty($this->page)) {
|
||
$this->page = 1;
|
||
}
|
||
$pagination = new Pagination(['pageSize' => $this->limit, 'totalCount' => $count, 'page' => $this->page - 1]);
|
||
$list = $query->offset($pagination->offset)->limit($pagination->limit)->orderBy(['id' => SORT_DESC])->asArray()->all();
|
||
foreach ($list as $index => $item) {
|
||
$item['avatar_url'] = SiteHelper::getFullUrl($item['avatar_url']);
|
||
$item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
|
||
$item['updated_at'] = date('Y-m-d H:i:s', $item['updated_at']);
|
||
$list[$index] = $item;
|
||
}
|
||
//是否已经全部加载
|
||
$end_flag = $this->page >= $pagination->pageCount ? true : false;
|
||
return [
|
||
'code' => 0,
|
||
'msg' => 'ok',
|
||
'data' => $list,
|
||
'count' => $count,
|
||
'page_size' => $this->limit,
|
||
'page_count' => $pagination->pageCount,
|
||
'page_no' => $this->page,
|
||
'end_flag' => $end_flag
|
||
];
|
||
}
|
||
|
||
/**
|
||
* 球杆列表【检测员查看列表】
|
||
*/
|
||
public function actionGetJcList()
|
||
{
|
||
if (!$this->validate()) {
|
||
return $this->getModelError();
|
||
}
|
||
$find_jc = Detection::findOne([
|
||
'user_id' => $this->user_id,
|
||
'is_delete' => 0,
|
||
]);
|
||
if (empty($find_jc)) {
|
||
return $this->apiReturnError('非检测员不能查看内容');
|
||
}
|
||
|
||
$query = UserBallArm::find()
|
||
->alias('user_ball')
|
||
->innerJoin(['user' => User::tableName()], 'user_ball.uid=user.id')
|
||
->andWhere([
|
||
'user_ball.is_delete' => 0,
|
||
]);
|
||
|
||
|
||
if (intval($this->type) === 2) {
|
||
// 已检测,查找出检测id
|
||
$query->andWhere([
|
||
'user_ball.jc_uid' => $this->user_id,
|
||
])->andWhere([
|
||
'in', 'user_ball.status', [3, 4],
|
||
]);
|
||
} else {
|
||
// 未检测,查找出店铺id
|
||
$query->andWhere([
|
||
'user_ball.store_id' => $find_jc->store_id,
|
||
'user_ball.status' => 2,
|
||
]);
|
||
}
|
||
$query->select('user_ball.id,user_ball.title,user_ball.created_at,user_ball.updated_at,user_ball.status,user.real_name,user.avatar_url,user_ball.order_id')
|
||
->groupBy('order_id');
|
||
$count_query = clone $query;
|
||
$count = $count_query->count();
|
||
|
||
|
||
if (empty($this->limit)) {
|
||
$this->limit = 100;
|
||
}
|
||
if (empty($this->page)) {
|
||
$this->page = 1;
|
||
}
|
||
$pagination = new Pagination(['pageSize' => $this->limit, 'totalCount' => $count, 'page' => $this->page - 1]);
|
||
$list = $query->offset($pagination->offset)->limit($pagination->limit)->orderBy(['id' => SORT_DESC])->asArray()->all();
|
||
|
||
foreach ($list as $index => $item) {
|
||
$item['avatar_url'] = SiteHelper::getFullUrl($item['avatar_url']);
|
||
$item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
|
||
$item['updated_at'] = date('Y-m-d H:i:s', $item['updated_at']);
|
||
$list[$index] = $item;
|
||
}
|
||
//是否已经全部加载
|
||
$end_flag = $this->page >= $pagination->pageCount ? true : false;
|
||
|
||
return [
|
||
'code' => 0,
|
||
'msg' => 'ok',
|
||
'data' => $list,
|
||
'count' => $count,
|
||
'page_size' => $this->limit,
|
||
'page_count' => $pagination->pageCount,
|
||
'page_no' => $this->page,
|
||
'end_flag' => $end_flag,
|
||
];
|
||
}
|
||
|
||
|
||
/**
|
||
* 检测员信息
|
||
* @return array
|
||
*/
|
||
public function getJc()
|
||
{
|
||
//检测员信息
|
||
$find_jc = Detection::findOne([
|
||
'user_id' => $this->user_id,
|
||
'is_delete' => 0,
|
||
]);
|
||
if (empty($find_jc)) {
|
||
return $this->apiReturnError('非检测员不能查看内容');
|
||
}
|
||
$user = ApiHelper::findOneUser($this->user_id);
|
||
$store = Store::findOne(['id' => $find_jc['store_id']]);
|
||
$data['real_name'] = $user['real_name'];
|
||
$data['store_name'] = $store['name'];
|
||
$data['coach_avatar_url'] = SiteHelper::getFullUrl($user['avatar_url']);
|
||
|
||
return [
|
||
'code' => 0,
|
||
'msg' => 'ok',
|
||
'data' => $data,
|
||
];
|
||
|
||
}
|
||
|
||
|
||
/**
|
||
* 获取数据内容[检测员查看]
|
||
*/
|
||
public function actionGetData()
|
||
{
|
||
if (empty($this->order_id)) {
|
||
return $this->apiReturnError('没有球杆');
|
||
}
|
||
$find = UserBallArm::findAll([
|
||
'order_id' => $this->order_id,
|
||
]);
|
||
|
||
if (empty($find)) {
|
||
return $this->apiReturnError('没有球杆数据');
|
||
}
|
||
|
||
$uid = array_unique(array_column($find, 'uid'));
|
||
|
||
if ($uid[0] != $this->user_id) {
|
||
// 判断用户是否为检测员
|
||
$find_jc = Detection::findOne([
|
||
'user_id' => $this->user_id,
|
||
'is_delete' => 0,
|
||
]);
|
||
if (empty($find_jc)) {
|
||
return $this->apiReturnError('非检测员不能查看内容');
|
||
}
|
||
}
|
||
|
||
$jc_name = "";
|
||
$jc_img = "";
|
||
|
||
|
||
foreach ($find as $key => $value) {
|
||
if (!empty($value->jc_uid)) {
|
||
// 获取检测员信息
|
||
$find_jc = Detection::find()
|
||
->alias('d')
|
||
->join('inner join', ['u' => User::tableName()], 'u.id = d.user_id')
|
||
->andWhere([
|
||
'd.is_delete' => 0,
|
||
'u.is_delete' => 0,
|
||
'u.id' => $value->jc_uid,
|
||
])->select('d.name,u.avatar_url')->asArray()->one();
|
||
if (!empty($find_jc)) {
|
||
$jc_name = $find_jc['name'];
|
||
$jc_img = SiteHelper::getFullUrl($find_jc['avatar_url']);
|
||
}
|
||
}
|
||
|
||
$json_de = json_decode($value->data, True);
|
||
$data = $this->redressData($json_de);
|
||
$res['array_data'][] = [
|
||
'id' => $value['id'],
|
||
'jc_name' => $jc_name,
|
||
'jc_img' => $jc_img,
|
||
'title' => $value->title,
|
||
'updated_at' => date('Y-m-d H:i:s', $value->updated_at),
|
||
'data' => $data,
|
||
];
|
||
}
|
||
return $this->apiReturnSuccess('ok', $res);
|
||
}
|
||
|
||
/**
|
||
* 获取数据内容[用户查看]
|
||
*/
|
||
public function actionGetUserData()
|
||
{
|
||
if (empty($this->id)) {
|
||
return $this->apiReturnError('没有球杆');
|
||
}
|
||
$find = UserBallArm::findOne([
|
||
'id' => $this->id,
|
||
]);
|
||
if (empty($find)) {
|
||
return $this->apiReturnError('没有球杆数据');
|
||
}
|
||
if ($find->uid != $this->user_id) {
|
||
// 判断用户是否为检测员
|
||
$find_jc = Detection::findOne([
|
||
'user_id' => $this->user_id,
|
||
'is_delete' => 0,
|
||
]);
|
||
if (empty($find_jc)) {
|
||
return $this->apiReturnError('非检测员不能查看内容');
|
||
}
|
||
}
|
||
$jc_name = "";
|
||
$jc_img = "";
|
||
if (!empty($find->jc_uid)) {
|
||
// 获取检测员信息
|
||
$find_jc = Detection::find()
|
||
->alias('d')
|
||
->join('inner join', ['u' => User::tableName()], 'u.id = d.user_id')
|
||
->andWhere([
|
||
'd.is_delete' => 0,
|
||
'u.is_delete' => 0,
|
||
'u.id' => $find->jc_uid,
|
||
])->select('d.name,u.avatar_url')->asArray()->one();
|
||
if (!empty($find_jc)) {
|
||
$jc_name = $find_jc['name'];
|
||
$jc_img = SiteHelper::getFullUrl($find_jc['avatar_url']);
|
||
}
|
||
}
|
||
$json_de = json_decode($find->data, True);
|
||
$data = $this->redressData($json_de);
|
||
$res = [
|
||
'jc_name' => $jc_name,
|
||
'jc_img' => $jc_img,
|
||
'title' => $find->title,
|
||
'updated_at' => date('Y-m-d H:i:s', $find->updated_at),
|
||
'data' => $data,
|
||
];
|
||
return $this->apiReturnSuccess('ok', $res);
|
||
}
|
||
|
||
/**
|
||
* 购买球杆
|
||
*/
|
||
public function actionBuy()
|
||
{
|
||
if (!$this->validate()) {
|
||
return $this->getModelError();
|
||
}
|
||
|
||
$goods = Goods::find()->where(['plugin_sign' => SysConst::$cxPluginSceneBallArmMall, 'is_delete' => 0, 'status' => 1])->one();
|
||
if (empty($goods)) {
|
||
return $this->apiReturnError('球杆检测服务未配置,请联系教练');
|
||
}
|
||
|
||
if (empty($this->title)) {
|
||
return $this->apiReturnError('请填写球杆名称');
|
||
}
|
||
if (empty($this->store_id)) {
|
||
return $this->apiReturnError('请选择门店');
|
||
}
|
||
$t = \Yii::$app->db->beginTransaction();
|
||
$data = [];
|
||
foreach (self::$DATA_KEY as $key => $val) {
|
||
$data[] = [
|
||
'title' => $val,
|
||
'value' => '',
|
||
];
|
||
}
|
||
$arr = [
|
||
'title' => $this->title,
|
||
'uid' => $this->user_id,
|
||
'store_id' => $this->store_id,
|
||
'data' => json_encode($data, JSON_UNESCAPED_UNICODE),
|
||
'status' => 1,
|
||
'created_at' => time(),
|
||
'updated_at' => time(),
|
||
'is_delete' => 0,
|
||
'deleted_at' => 0,
|
||
'jc_uid' => 0,
|
||
];
|
||
$obj = new UserBallArm();
|
||
foreach ($arr as $key => $val) {
|
||
$obj->{$key} = $val;
|
||
}
|
||
if (!$obj->save()) {
|
||
$t->rollBack();
|
||
return $this->apiReturnError('创建订单错误' . $this->getModelError($obj));
|
||
}
|
||
|
||
$order = new Order();
|
||
$order->cx_mch_id = $this->cx_mch_id;
|
||
$order->user_id = $this->user_id;
|
||
$order->order_no = UniqueOrderNo::generate(UniqueOrderNo::ORDER_TYPE_BALL_ARM, Order::class, 24, 1, 3, 'order_no');
|
||
$order->total_price = 200; //总金额
|
||
$order->total_pay_price = 0; //实际支付
|
||
$order->express_original_price = 0;
|
||
$order->express_price = 0;
|
||
$order->total_goods_price = 200; //优惠后
|
||
$order->total_goods_original_price = 200; //优惠前
|
||
$order->use_user_coupon_id = $obj->id;
|
||
$order->coupon_discount_price = 0;
|
||
$order->name = '';
|
||
$order->mobile = '';
|
||
$order->address = '';
|
||
$order->remark = '';
|
||
$order->seller_words = '';
|
||
$order->seller_remark = '';
|
||
$order->pay_type = SysConst::$cxPayTypeUnionpay;
|
||
$order->plugin_sign = SysConst::$cxPluginSceneBallArmMall;
|
||
if (!$order->save()) {
|
||
$t->rollBack();
|
||
return $this->apiReturnError('创建订单错误' . $this->getModelError($order));
|
||
}
|
||
|
||
$detail = new OrderDetail();
|
||
$detail->cx_mch_id = $this->cx_mch_id;
|
||
$detail->order_id = $order->id;
|
||
$detail->goods_id = $goods->id;
|
||
$detail->num = 1;
|
||
$detail->unit_price = $goods->price;
|
||
$detail->total_original_price = $goods->price;
|
||
$detail->total_price = $goods->price;
|
||
$detail->goods_attr_info = '球杆检测';
|
||
$detail->is_delete = 0;
|
||
$detail->created_at = time();
|
||
$detail->updated_at = time();
|
||
$detail->deleted_at = 0;
|
||
$detail->is_refund = 0;
|
||
$detail->refund_status = 0;
|
||
$detail->plugin_sign = SysConst::$cxPluginSceneBallArmMall;
|
||
$detail->serial_no = '';
|
||
$detail->goods_type = 0;
|
||
if (!$detail->save()) {
|
||
$t->rollBack();
|
||
return $this->apiReturnError('创建订单错误' . $this->getModelError($detail));
|
||
}
|
||
$t->commit();
|
||
return $this->apiReturnSuccess('ok', ['id' => $order->id]);
|
||
|
||
}
|
||
|
||
/**
|
||
* 更新球杆数据【检测员操作】
|
||
*/
|
||
public function actionUpdata($arm_arr)
|
||
{
|
||
// if (!$this->validate()) {
|
||
// return $this->getModelError();
|
||
// }
|
||
// if (empty($this->data)) {
|
||
// return $this->apiReturnError('没有更新数据');
|
||
// }
|
||
// if (empty($this->id)) {
|
||
// return $this->apiReturnError('没有更新id');
|
||
// }
|
||
|
||
if (empty($arm_arr)) {
|
||
return $this->apiReturnError('没有更新数据');
|
||
}
|
||
// 判断用户是否为检测员
|
||
$find_jc = Detection::findOne([
|
||
'user_id' => $this->user_id,
|
||
'is_delete' => 0,
|
||
]);
|
||
if (empty($find_jc)) {
|
||
return $this->apiReturnError('当前用户非检测员');
|
||
}
|
||
$order_id = 0;
|
||
foreach ($arm_arr as $key => $value) {
|
||
$this->id = $value['id'];
|
||
$this->data = $value['data'];
|
||
$data = $this->data;
|
||
|
||
$res_data = $this->redressData($data);
|
||
$find = UserBallArm::findOne([
|
||
'id' => $this->id,
|
||
'is_delete' => 0,
|
||
]);
|
||
if (empty($find)) {
|
||
return $this->apiReturnError('没有找到有关数据');
|
||
}
|
||
/*if ($find->status == 1) {
|
||
return $this->apiReturnError('订单未支付');
|
||
}*/
|
||
$find->data = json_encode($res_data, JSON_UNESCAPED_UNICODE);
|
||
$find->updated_at = time();
|
||
$find->jc_uid = $this->user_id;
|
||
$find->status = 3;
|
||
if (!$find->save()) {
|
||
return $this->apiReturnError('更新数据失败', $this->getModelError($find));
|
||
}
|
||
if(empty($key)){
|
||
$order_id = $find['order_id'];
|
||
// 变更订单已完成
|
||
Order::updateAll([
|
||
'status' => 0,
|
||
'updated_at' => time(),
|
||
],[
|
||
'id' => $order_id,
|
||
]);
|
||
}
|
||
}
|
||
return $this->apiReturnSuccess('ok');
|
||
}
|
||
|
||
/**
|
||
* 更新球杆状态【用户操作】
|
||
*/
|
||
public function actionUpStatus()
|
||
{
|
||
if (!$this->validate()) {
|
||
return $this->getModelError();
|
||
}
|
||
if (empty($this->id)) {
|
||
return $this->apiReturnError('没有更新id');
|
||
}
|
||
$find = UserBallArm::findOne([
|
||
'id' => $this->id,
|
||
'uid' => $this->user_id,
|
||
'is_delete' => 0,
|
||
]);
|
||
if (empty($find)) {
|
||
return $this->apiReturnError('没有找到有关数据');
|
||
}
|
||
if ($find->status == 1) {
|
||
return $this->apiReturnError('订单未支付');
|
||
}
|
||
if ($find->status == 2) {
|
||
return $this->apiReturnError('未检测球杆');
|
||
}
|
||
if ($find->status == 3) {
|
||
$find->status = 4;
|
||
} else {
|
||
$find->status = 3;
|
||
}
|
||
// 更新所有相同的数据
|
||
UserBallArm::updateAll([
|
||
'status' => $find->status,
|
||
'updated_at' => time(),
|
||
],[
|
||
'order_id' => $find->order_id,
|
||
'uid' => $this->user_id,
|
||
]);
|
||
// $find->updated_at = time();
|
||
// if ($find->save()) {
|
||
// }
|
||
return $this->apiReturnSuccess('ok');
|
||
// return $this->apiReturnError('更新数据失败', $this->getModelError($find));
|
||
}
|
||
|
||
/**
|
||
* 纠正数据
|
||
*/
|
||
public function redressData($data = [], $insert = False)
|
||
{
|
||
$res_data = [];
|
||
$temp_key = [];
|
||
$default = '';
|
||
foreach ($data as $key => $val) {
|
||
if (empty($val) || empty($val['title'])) {
|
||
continue;
|
||
}
|
||
if (!in_array($val['title'], self::$DATA_KEY)) {
|
||
continue;
|
||
}
|
||
if (empty($insert)) {// 可以把空数据变更为想要的数据格式
|
||
if (empty($val['value'])) {
|
||
$val['value'] = $default;
|
||
}
|
||
}
|
||
$temp_key[] = $val['title'];
|
||
$res_data[] = $val;
|
||
}
|
||
foreach (self::$DATA_KEY as $key => $val) {
|
||
if (in_array($val, $temp_key)) {
|
||
continue;
|
||
}
|
||
$value = '';
|
||
if (empty($insert)) {// 可以把空数据变更为想要的数据格式
|
||
$value = $default;
|
||
}
|
||
$res_data[] = [
|
||
'title' => $val,
|
||
'value' => $value,
|
||
];
|
||
}
|
||
return $res_data;
|
||
}
|
||
|
||
} |