123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?php
- namespace app\common\model\member;
- use app\common\enum\member\GoldType;
- use app\common\model\BaseModel;
- use app\common\model\Order;
- use app\common\model\OrderGoods;
- use app\common\model\User;
- /**
- * 满件送赠品活动模型
- * @package app\store\model\coupon
- */
- class GoldRice extends BaseModel
- {
- // 全部
- const LIST_TYPE_ALL = 'all';
- // 收入
- const LIST_TYPE_IN = 'in';
- // 支出
- const LIST_TYPE_OUT = 'out';
- //审核状态
- const AUDIT_STATUS = [0=>'待审核',1=>'审核通过',2=>'审核不通过'];
- //类型 1收入 0支出
- const TYPE = [1=>'收入',0=>'支出'];
- protected $name = 'member_gold_rice';
- protected $append = [];
- /**
- * 详情
- * @param int $id
- * @return null|static
- */
- public static function detail(int $id,$with =[])
- {
- return self::get($id,$with);
- }
- //用户关联
- public function user(){
- return $this->hasOne(User::class,'user_id','user_id');
- }
- //订单关联
- public function order(){
- return $this->hasOne(Order::class,'order_no','order_no');
- }
- /**
- * 添加结构
- * @author: zjwhust
- * @Time: 2021/11/20 16:05
- */
- public static function add($userId,$orderNo,$name,$score,$type=1)
- {
- $data = [
- "user_id" => $userId,//持有用户id
- "name" => $name,//描述
- "order_no" => $orderNo,//订单编号
- "score" => $score,//分值(收入/支出的分值)
- "type" => $type,//类型 1收入 0支出
- ];
- self::create($data);
- }
- /**
- * 获取列表记录
- * @param string $dataType 类型
- * @param array $param
- * @return \think\Paginator
- * @throws \think\db\exception\DbException
- */
- public function getList(string $dataType = self::LIST_TYPE_ALL,array $param = [])
- {
- // 检索查询条件
- $filter = $this->getFilter($param);
- // 设置订单类型条件
- $dataTypeFilter = $this->getFilterDataType($dataType);
- return $this->where($dataTypeFilter)
- ->where($filter)
- ->order('id','desc')
- ->paginate();
- }
- /**
- * 获取查询条件
- * @param array $param
- * @return array
- */
- private function getFilter(array $param = [])
- {
- // 默认查询参数
- $params = $this->setQueryDefaultValue($param, ['userId' => 0]);
- // 检索查询条件
- $filter = [];
- $params['userId']>0 && $filter[] = ['user_id','=', $params['userId']];
- return $filter;
- }
- /**
- * 设置类型条件
- * @param string $dataType
- * @return array
- */
- private function getFilterDataType(string $dataType = self::LIST_TYPE_ALL): array
- {
- // 数据类型
- $filter = [];
- switch ($dataType) {
- case self::LIST_TYPE_ALL:
- $filter = [];
- break;
- case self::LIST_TYPE_IN:
- $filter = [
- ['type', '=', GoldType::IN],
- ];
- break;
- case self::LIST_TYPE_OUT:
- $filter = [
- ['type', '=', GoldType::OUT],
- ];
- break;
- }
- return $filter;
- }
- //操作在途金米粒的处理逻辑
- public static function refundGoldRiceRoad($order,$refund){
- if($refund['gold_rice_amount_output']>0){
- //减少在途金米粒
- User::setDecByField($refund['user_id'],'gold_rice_road',(int)$refund['gold_rice_amount_output']);
- //减少订单表里的gold_rice_amount_input字段数量
- (new Order)->setDec(['order_id'=>$refund['order_id']],'gold_rice_amount_input',(int)$refund['gold_rice_amount_output']);
- //写金米粒出账记录
- self::add($refund['user_id'],$order['order_no'],'退款扣减',(int)$refund['gold_rice_amount_output'],0);
- }
- //
- if($refund['gold_rice_amount']>0){
- //写金米粒入账记录
- GoldRice::add($refund['user_id'],$order['order_no'],'退款返还',$refund['gold_rice_amount'],1);
- //退还抵扣的金米粒
- User::setIncByField($refund['user_id'],'gold_rice',(int)$refund['gold_rice_amount']);
- }
- //减少orderGoods表里的获得金米粒数量
- // if($refund['order_goods_id']>0){//单商品退款
- // (new OrderGoods)->setDec(['order_goods_id'=>$refund['order_goods_id']],'gold_rice_amount_input',(int)$refund['gold_rice_amount']);
- // }else{//整单退款
- // OrderGoods::where('order_id',$refund['order_id'])->update(['gold_rice_amount_input'=>0]);//所有的订单商品获得的金米粒都清零
- // }
- }
- }
|