通常开发的时候,实现功能流程。
有时候,需要测试类,进行数据的模拟产生。
流程的模拟操作。
通过测试类,能够更加清晰数据表的意义。
能够让程序更加健壮。
将涉及到的各个层面的数据,都进行有条不紊的测试。
模拟产生订单,模拟并发订单。
模拟订单发货。
模拟订单收货。
模拟订单评价。
等等。
这些流程的编码,比增删改成有意思多了。
每个项目都很有必要,有个Test文件夹。
我们经常看到开源的项目中有个Test。
估计它的作用就是为了让开源项目健壮无敌吧。
在这里面可以使用各种数据结构与算法。充分的感受编程的创造性和乐趣。
通过程序,通过脚本,可以产生各种想要的数据。这就有点想linux,通过指令操作。远比windows通过界面操作来的爽。
程序员,更多的需要通过脚本操作数据的本领。
<?php
/**
* User: jiqing
* Date: 2017/6/13
* Desc: 订单模拟测试
*/
namespace Test\Controller;
use Model\GoodsModel;
use Model\GoodsPriceModel;
use Model\OrderGoodsJudgeModel;
use Model\OrderGoodsModel;
use Model\OrderModel;
use Model\UserModel;
class OrderController extends BaseController {
public function __construct(){
parent::__construct();
}
// 主函数
public function main() {
// 模拟生成一些数据
//shop_id 8 gid 20 星星 gid 21 熊猫
//shop_id 7 gid 10 篮球 gid 11 哑铃 gid 18 足球(有规格 185 186 187 188) gid 19 男鞋(有规格 189 190 191 192)
// uid 101 小李 102 小张 103 小王 104 小赵 105 小齐
// 单店铺 单商品
// $cartArr = ['shop_id'=>'7','goods'=>['gid'=>11,'count'=>3,'gpid'=>0]];
// $uid = 101;
// 单店铺 多商品
// $cartArr = ['shop_id'=>'7','goods'=>[['gid'=>10,'count'=>2,'gpid'=>0],['gid'=>11,'count'=>5,'gpid'=>0]]];
// $uid = 102;
// 多店铺
// $cartArr = [['shop_id'=>'7','goods'=>[['gid'=>10,'count'=>2,'gpid'=>0],['gid'=>11,'count'=>5,'gpid'=>0]]],['shop_id'=>'8','goods'=>[['gid'=>20,'count'=>2,'gpid'=>0],['gid'=>21,'count'=>3,'gpid'=>0]]]];
// $uid = 103;
//
// if (array_key_exists('shop_id',$cartArr)) { // 单店铺
// $this->createCommonOrder($cartArr,$uid);
// } else { // 多店铺
// foreach ($cartArr as $key => $val) {
// $this->createCommonOrder($val,$uid);
// }
// }
// $this->deliverGoods('sg1802071549051504867');
// $this->receiveGoods('sg1802071549051504867');
// $this->judgeGoods('14',4,'加油!');
}
/**
* 模拟创建一些订单
* 相关表
* order
* order_goods
* goods
* goods_price
* shop
*/
public function createCommonOrder($cartArr = [],$uid = 101){
$goodsModel = new GoodsModel();
$goodsPriceModel = new GoodsPriceModel();
$orderModel = new OrderModel();
$orderGoodsModel = new OrderGoodsModel();
// 单店铺单商品订单
// 单店铺
if (count($cartArr['goods'])==count($cartArr['goods'], 1)) {
// 单商品
$dataOrder = [];
$dataOrderGoods = [];
// 获取商品信息
$goodsData = $goodsModel->findData(['gid'=>$cartArr['goods']['gid']]);
if ($cartArr['goods']['gpid'] == 0) {
$goodsPrice = $goodsData['price'];
$agentPrice = $goodsData['agent_price'];
} else {
$goodsPriceData = $goodsPriceModel->findData(['gpid'=>$cartArr['goods']['gpid']]);
$goodsPrice = $goodsPriceData['price'];
$agentPrice = $goodsPriceData['agent_price'];
}
$allGoodsPrice = $goodsPrice * $cartArr['goods']['count'];
$allAgentPrice = $agentPrice * $cartArr['goods']['count'];
$postPrice = 0; // 包邮
$dataOrder['order_no'] = buildOrderNo('sg'); //订单号生成函数 buildOrderNo('shopgo')
$dataOrder['is_purchase'] = 2; // 非采购订单
$dataOrder['shop_id'] = $cartArr['shop_id'];
$dataOrder['agent_shop_id'] = 0; // 非分销店铺商品
$dataOrder['uid'] = $uid;
$dataOrder['goods_count'] = $cartArr['goods']['count'];
$dataOrder['post_money'] = $postPrice; // 包邮
$dataOrder['order_money'] = $allGoodsPrice + $postPrice; // 订单总价
$dataOrder['pay_money'] = $allGoodsPrice + $postPrice; // 支付金额
$dataOrder['redmoney_purpose'] = 0; // 红包抵扣金额
$dataOrder['bonus_purpose'] = 0; // 赏金抵扣金额
$dataOrder['coupon_purpose'] = 0; // 券优惠金额
$dataOrder['goods_money'] = $allGoodsPrice; // 商品总价
$dataOrder['agent_money'] = $allAgentPrice; // 分销总价
$dataOrder['refund_money'] = 0; // 退款金额
$dataOrder['pay_type'] = 1; // 微信
$dataOrder['add_time'] = date('Y-m-d H:i:s'); // 下单时间
$dataOrder['pay_time'] = date('Y-m-d H:i:s'); // 支付时间
// $dataOrder['receive_time'] = ''; // 订单收货时间
// $dataOrder['deal_time'] = ''; // 订单操作时间
$dataOrder['express_type'] = ''; // 快递类型
$dataOrder['express_no'] = ''; // 快递单号
$dataOrder['pay_status'] = 2; // 已支付
$dataOrder['user_remark'] = "什么时候发货?"; // 用户留言
$dataOrder['shop_remark'] = "马上发货"; // 用户留言
$dataOrder['comment'] = 1; // 未评价
$dataOrder['status'] = 2; // 已付款待发货
$dataOrder['close_reason'] = ''; // 订单关闭原因
$dataOrder['back_status'] = 0; // 退款状态
$dataOrder['is_del'] = 0; // 未删除
$dataOrder['order_type'] = 1; // 普通订单
$dataOrder['group_hours'] = 0; // 成团时间
$dataOrder['group_done'] = 0; // 否
$dataOrder['group_neednum'] = 0; // 拼团差的人数
$dataOrder['group_oid'] = 0; // 拼主订单id
$dataOrder['origin_oid'] = 0; // 原始订单id
$orderModel->startTrans();
$new_oid = $orderModel->addData($dataOrder);
if ($new_oid) { // 生成订单商品数据
$dataOrderGoods['shop_id'] = $cartArr['shop_id'];
$dataOrderGoods['agent_shop_id'] = 0;
$dataOrderGoods['oid'] = $new_oid;
$dataOrderGoods['gid'] = $cartArr['goods']['gid'];
$dataOrderGoods['goods_count'] = $cartArr['goods']['count'];
$dataOrderGoods['back_money_able'] = $allGoodsPrice;
$dataOrderGoods['agent_price'] = $allAgentPrice;
$dataOrderGoods['post_price'] = 0;
$dataOrderGoods['back_status'] = 0;
$dataOrderGoods['sku_goods_json'] = '';
$dataOrderGoods['complain_status'] = 0;
$dataOrderGoods['gpid'] = $cartArr['goods']['gpid'];
$dataOrderGoods['torderno'] = '';
$dataOrderGoods['platform'] = 1; // 拼多多
$res = $orderGoodsModel->addData($dataOrderGoods);
}
if ($new_oid && $res) {
$orderModel->commit();
} else {
$orderModel->rollback();
}
echo "成功生成订单".$new_oid."\n";
} else {
// 多商品
$dataOrder = [];
$dataOrderGoods = [];
$allGoodsPrice = 0;
$allAgentPrice = 0;
$allGoodsCount = 0;
// 获取商品信息
foreach ($cartArr['goods'] as $key => $val) {
$goodsData = $goodsModel->findData(['gid'=>$val['gid']]);
if ($val['gpid'] == 0) {
$goodsPrice = $goodsData['price'];
$agentPrice = $goodsData['agent_price'];
} else {
$goodsPriceData = $goodsPriceModel->findData(['gpid'=>$val['gpid']]);
$goodsPrice = $goodsPriceData['price'];
$agentPrice = $goodsPriceData['agent_price'];
}
$allGoodsPrice += $goodsPrice * $val['count'];
$allAgentPrice += $agentPrice * $val['count'];
$allGoodsCount += $val['count'];
}
$postPrice = 0; // 包邮
$dataOrder['order_no'] = buildOrderNo('sg'); //订单号生成函数 buildOrderNo('shopgo')
$dataOrder['is_purchase'] = 2; // 非采购订单
$dataOrder['shop_id'] = $cartArr['shop_id'];
$dataOrder['agent_shop_id'] = 0; // 非分销店铺商品
$dataOrder['uid'] = $uid;
$dataOrder['goods_count'] = $allGoodsCount;
$dataOrder['post_money'] = $postPrice; // 包邮
$dataOrder['order_money'] = $allGoodsPrice + $postPrice; // 订单总价
$dataOrder['pay_money'] = $allGoodsPrice + $postPrice; // 支付金额
$dataOrder['redmoney_purpose'] = 0; // 红包抵扣金额
$dataOrder['bonus_purpose'] = 0; // 赏金抵扣金额
$dataOrder['coupon_purpose'] = 0; // 券优惠金额
$dataOrder['goods_money'] = $allGoodsPrice; // 商品总价
$dataOrder['agent_money'] = $allAgentPrice; // 分销总价
$dataOrder['refund_money'] = 0; // 退款金额
$dataOrder['pay_type'] = 1; // 微信
$dataOrder['add_time'] = date('Y-m-d H:i:s'); // 下单时间
$dataOrder['pay_time'] = date('Y-m-d H:i:s'); // 支付时间
// $dataOrder['receive_time'] = ''; // 订单收货时间
// $dataOrder['deal_time'] = ''; // 订单操作时间
$dataOrder['express_type'] = ''; // 快递类型
$dataOrder['express_no'] = ''; // 快递单号
$dataOrder['pay_status'] = 2; // 已支付
$dataOrder['user_remark'] = "什么时候发货?"; // 用户留言
$dataOrder['shop_remark'] = "马上发货"; // 用户留言
$dataOrder['comment'] = 1; // 未评价
$dataOrder['status'] = 2; // 已付款待发货
$dataOrder['close_reason'] = ''; // 订单关闭原因
$dataOrder['back_status'] = 0; // 退款状态
$dataOrder['is_del'] = 0; // 未删除
$dataOrder['order_type'] = 1; // 普通订单
$dataOrder['group_hours'] = 0; // 成团时间
$dataOrder['group_done'] = 0; // 否
$dataOrder['group_neednum'] = 0; // 拼团差的人数
$dataOrder['group_oid'] = 0; // 拼主订单id
$dataOrder['origin_oid'] = 0; // 原始订单id
$orderModel->startTrans();
$err_count = 0;
$new_oid = $orderModel->addData($dataOrder);
if (!$new_oid) {
$err_count ++ ;
}
if ($new_oid) { // 生成订单商品数据
foreach ($cartArr['goods'] as $key => $val) {
$goodsData = $goodsModel->findData(['gid'=>$val['gid']]);
if ($val['gpid'] == 0) {
$goodsPrice = $goodsData['price'];
$agentPrice = $goodsData['agent_price'];
} else {
$goodsPriceData = $goodsPriceModel->findData(['gpid'=>$val['gpid']]);
$goodsPrice = $goodsPriceData['price'];
$agentPrice = $goodsPriceData['agent_price'];
}
$dataOrderGoods['shop_id'] = $cartArr['shop_id'];
$dataOrderGoods['agent_shop_id'] = 0;
$dataOrderGoods['oid'] = $new_oid;
$dataOrderGoods['gid'] = $val['gid'];
$dataOrderGoods['goods_count'] = $val['count'];
$dataOrderGoods['back_money_able'] = $goodsPrice * $val['count'];
$dataOrderGoods['agent_price'] = $agentPrice * $val['count'];
$dataOrderGoods['post_price'] = 0;
$dataOrderGoods['back_status'] = 0;
$dataOrderGoods['sku_goods_json'] = '';
$dataOrderGoods['complain_status'] = 0;
$dataOrderGoods['gpid'] = $val['gpid'];
$dataOrderGoods['torderno'] = '';
$dataOrderGoods['platform'] = 1; // 拼多多
$res = $orderGoodsModel->addData($dataOrderGoods);
if (!$res) {
$err_count ++ ;
}
}
}
if ($err_count == 0) {
$orderModel->commit();
} else {
$orderModel->rollback();
}
echo "成功生成订单".$new_oid."\n";
}
}
/**
* 模拟订单发货
* 相关表
* order
*/
public function deliverGoods($order_no,$express_type='shunfeng',$express_no='930905390299') {
// 判断订单是否已经支付
$orderModel = new OrderModel();
$orderInfo = $orderModel->findData(['order_no'=>$order_no]);
$orderData = [];
if (!$orderInfo) {
$this->outData['code'] = 1;
$this->outData['msg'] = "订单不存在";
$this->printOut();
}
if ($orderInfo['status'] == 1) {
$this->outData['code'] = 1;
$this->outData['msg'] = "订单待付款";
$this->printOut();
}
if ($orderInfo['status'] == 3) {
$this->outData['code'] = 1;
$this->outData['msg'] = "订单已发货";
$this->printOut();
}
if ($orderInfo['status'] == 2) {
// 处理订单发货
$orderData['express_type'] = $express_type;
$orderData['express_no'] = $express_no;
$orderData['status'] = 3; // 已发货
$res = $orderModel->editData(['order_no'=>$order_no],$orderData);
if ($res) {
$this->outData['msg'] = "操作成功";
$this->printOut();
}
}
$this->outData['code'] = 1;
$this->outData['msg'] = "操作失败";
$this->printOut();
}
/**
* 模拟订单收货,不涉及到金钱分配
* 相关表
* order
*/
public function receiveGoods($order_no) {
$orderModel = new OrderModel();
$orderInfo = $orderModel->findData(['order_no'=>$order_no]);
if (!$orderInfo) {
$this->outData['code'] = 1;
$this->outData['msg'] = "订单不存在";
$this->printOut();
}
if ($orderInfo['status'] < 3) {
$this->outData['code'] = 1;
$this->outData['msg'] = "订单尚未发货";
$this->printOut();
}
if ($orderInfo['status'] == 4) {
$this->outData['code'] = 1;
$this->outData['msg'] = "订单已完成";
$this->printOut();
}
if ($orderInfo['status'] == 3) {
// 处理订单收货
$orderData['status'] = 4; // 交易完成
$res = $orderModel->editData(['order_no'=>$order_no],$orderData);
if ($res) {
$this->outData['msg'] = "操作成功";
$this->printOut();
}
}
$this->outData['code'] = 1;
$this->outData['msg'] = "操作失败";
$this->printOut();
}
/**
* 模拟实现订单评论
* 相关表
* order
* order_goods
* order_goods_judge
* goods
*/
public function judgeGoods($ogid,$score = 5,$info = "挺好的") {
// 判断订单商品是否存在
$orderGoodsModel = new OrderGoodsModel();
$orderGoodsInfo = $orderGoodsModel->findData(['ogid'=>$ogid]);
if (!$orderGoodsInfo) {
$this->outData['code'] = 1;
$this->outData['msg'] = "订单商品不存在";
$this->printOut();
}
// 判断商品是否已评价
$orderGoodsJudgeModel = new OrderGoodsJudgeModel();
$orderGoodsJudgeInfo = $orderGoodsJudgeModel->findData(['ogid'=>$ogid]);
if ($orderGoodsJudgeInfo) {
$this->outData['code'] = 1;
$this->outData['msg'] = "订单商品已评价";
$this->printOut();
}
// 获取用户信息
$orderModel = new OrderModel();
$orderInfo = $orderModel->findData(['oid'=>$orderGoodsInfo['oid']]);
// 判断订单是否已完成
if ($orderInfo['status'] != 4) {
$this->outData['code'] = 1;
$this->outData['msg'] = "非已完成订单,不可评论";
$this->printOut();
}
$orderGoodsJudgeData = [];
$orderGoodsJudgeData['ogid'] = $ogid;
$orderGoodsJudgeData['shop_id'] = $orderGoodsInfo['shop_id'];
$orderGoodsJudgeData['gid'] = $orderGoodsInfo['gid'];
$orderGoodsJudgeData['score'] = $score;
$orderGoodsJudgeData['info'] = $info;
// 获取用户信息
$userModel = new UserModel();
$userInfo = $userModel->findData(['uid'=>$orderInfo['uid']]);
$orderGoodsJudgeData['uid'] = $userInfo['uid'];
$orderGoodsJudgeData['uname'] = $userInfo['nick_name'];
$orderGoodsJudgeData['add_time'] = date('Y-m-d H:i:s');
$orderGoodsJudgeData['status'] = 1;
$res = $orderGoodsJudgeModel->addData($orderGoodsJudgeData);
if ($res) {
$this->outData['msg'] = "评论成功";
$this->printOut();
}
$this->outData['code'] = 1;
$this->outData['msg'] = "操作失败";
$this->printOut();
}
}