123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- <?php namespace App\Models;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- use App\Models\Traits\Orders\Status as OrdersStatus;
- /**
- * 需求模型
- *
- */
- class Orders extends Model
- {
- use HasFactory,OrdersStatus;
- // 与模型关联的表名
- protected $table = 'orders';
- // 是否主动维护时间戳
- public $timestamps = false;
- // 定义时间戳字段名
- // const CREATED_AT = 'insert_time';
- // const UPDATED_AT = 'update_time';
- /**
- * 添加数据
- *
- */
- public function add($data)
- {
- // 时间
- if( empty($data['insert_time']) ) $data['insert_time'] = time();
- if( empty($data['update_time']) ) $data['update_time'] = time();
- // 写入数据表
- $id = $this->query()->insertGetId($data);
- // 返回结果
- return $id;
- }
- /**
- * 添加数据
- *
- */
- public function edit($id,$data)
- {
- // 更新时间
- if( empty($data['update_time']) ) $data['update_time'] = time();
- // 写入数据表
- $result = $this->query()->where([['id','=',$id]])->update($data);
- // 返回结果
- return $id;
- }
- /**
- * 编码转id
- *
- * @param string $code 编码
- *
- */
- public function codeToId($code){
- return intval(str_ireplace('kldd','',$code));
- }
-
- /**
- * id转编码
- *
- * @param int $id 编码
- *
- */
- public function idToCode($id){
- return 'kldd'. str_pad($id, 9, '0', STR_PAD_LEFT);;
- }
- /**
- * 取消订单
- *
- * @param int $id 订单ID
- * @param int $status 订单状态
- * @param OrdersProduct $OrdersProduct 子订单模型
- */
- public function setOrderStatus($id,$status,$OrdersProduct){
- // 查询数据
- $result = $this->edit($id,['status'=>$status]);
- // 提示新增失败
- if( !$result ) {
- // 提示信息
- return ['error'=>'订单修改失败'];
- }
- // 查询数据
- $result = $OrdersProduct->query()->where([['order_id','=',$id]])->update(['status'=>$status,'update_time'=>time()]);
- // 提示新增失败
- if( !$result ) {
- // 提示信息
- return ['error'=>'子订单修改失败'];json_send(['code'=>'error','msg'=>'取消失败','data'=>['error'=>'']]);
- }
- // 进行操作
- return ['success'=>'成功'];
- }
- /**
- * 取消优惠券关联订单
- *
- * @param int $orderTime 订单时间
- * @param int $uid 客户ID
- * @param OrdersProduct $OrdersProduct 子订单模型
- * @param CustomScore $CustomScore 客户积分
- */
- public function cancelRelate($orderTime,$uid,$OrdersProduct,$CustomScore){
- // 模型实例
- $CouponRewardRule = new \App\Models\CouponRewardRule();
- $CustomCoupon = new \App\Models\CustomCoupon();
- $CouponRewardProduct = new \App\Models\CouponRewardProduct();
- // 获取配置列表
- $ruleList = $CouponRewardRule->query()->where([['start_time','<=',$orderTime],['end_time','>=',$orderTime]])->get(['id','name','std_num','remove_custom','city_ids','status','start_time','end_time','coupon_id']);
- // 如果有活动列表
- foreach ($ruleList as $value) {
- // 查询客户是否有该优惠券
- $havaCoupon = $CustomCoupon->query()->where([['custom_uid','=',$uid],['coupon_id','=',$value['coupon_id']]])->first(['id','status','order_id']);
- // 没有优惠券,跳过
- if( !$havaCoupon ) continue;
- // 通过配置ID获取对应的商品范围
- $productList = $CouponRewardProduct->getListByRule($value['id']);
- // 获取客户 规定时段内订单的商品ID以及购买数量
- $orderList = $OrdersProduct->query()->where([['custom_uid','=',$uid],['status','=',1],['insert_time','>=',$value['start_time']],['insert_time','<=',$value['end_time']]])->get(['product_id','buy_num'])->toArray();
- // 计算商品总量
- $total = 0;
- // 循环商品范围
- foreach ($productList as $scope) {
- // 循环订单产品
- foreach ($orderList as $order) {
- // 如果产品不相等
- if( $scope['product_id'] != $order['product_id'] ) continue;
- // 相等的计算总量
- $total += $scope['product_units'] * $order['buy_num'];
- }
- }
- // 判断总数还是达标,跳过
- if( $total >= $value['std_num'] ) continue;
- // 剩余的达不到条件,如果有优惠券,未使用的的作废
- if( $havaCoupon['status'] == 0 ) {
- // 作废
- $CustomCoupon->edit($havaCoupon['id'],['status'=>4]);
- // 作废成功
- continue;
- }
- // 优惠券有订单信息
- if( $havaCoupon['order_id'] ) {
- // 获取优惠券关联的订单
- $oldData = $this->query()->where([['id','=',$havaCoupon['order_id']],['custom_uid','=',$uid]])->first(['id','order_score','status','custom_uid','insert_time']);
- // 如果订单不存在
- if( !$oldData ) continue;
- // 如果订单状态已取消
- if( $oldData['status'] == 4 ) continue;
- // 取消订单
- $result = $this->setOrderStatus($havaCoupon['order_id'],4,$OrdersProduct);
- // 取消失败
- if( isset($result['error']) ) return ['error'=>$result['error']];
- // 撤回订单
- if( $oldData['order_score'] > 0 ) {
- // 如果扣减失败
- $result = $CustomScore->trade($oldData['custom_uid'],$oldData['id'],($oldData['order_score']*-1),6,1);
- // 提示新增失败
- if( isset($result['error']) ) return ['error'=>$result['error']];
- }
- }
- }
- // 默认成功
- return ['success'=>'操作成功'];
- }
- }
|