<?php

namespace app\common\model\hu;

use think\Exception;
use think\Model;
use traits\model\SoftDelete;
use think\Db;
class WholesaleGoods extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'hu_wholesale_goods';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [

    ];

    /**
     * @param int $user_id   用户ID
     * @param int $order_id  订单ID
     * @return bool
     */
    public function add(int $user_id, int $order_id){
        try {
            //查询生成数目
            $shop_num = Db::name('config')->where("name","shop_nums")->value('value');
            $shop_num = isset($shop_num)?$shop_num:2;
           //查询当前订单的商品
            $list = Db::name("hu_order_goods")->where('order_id',$order_id)->where('user_id',$user_id)->select();

            //增加批发商品表数据
            foreach ($list as $key=>$value){
                $goods_ratio = Db::name("hu_goods")->where("id",$value['goods_id'])->value("ratio");
                $goods_ratio = $goods_ratio/100;
                $price = bcmul((string)$goods_ratio,(string)$value['price'],2);
                $data['user_id'] = $user_id;
                $data['goods_id'] = $value['goods_id'];
                $data['goods_name'] = $value['goods_name'];
                $data['cover_image'] = $value['cover_image'];
                $data['details'] = $value['details'];
                $data['price'] = $price;
                $data['num'] = $value['buy_number']*$shop_num;
                $data['createtime'] = time();

                self::insert($data);
            }
            return true;
        }catch (Exception $e) {
            return false;
        }

    }

    /**
     * 到时间自动售出
     * @param  $shop_days 应到期天数
     * @return void
     */
    public function sale($shop_days){

       $list = self::where('status',1)->select();

       $times = time();

       //天数转时间戳
        $shop_days_time = $shop_days*86400;

        if(!empty($list)){
            foreach ($list as $key=>$value){
                $end_time = $value['createtime']+$shop_days_time;
                if($end_time<$times){
                    //到期
                    $upd = self::where('id',$value['id'])->update(['status'=>0]);
                    if($upd){
                        //增加积分/金额
                       $update_money = bcmul((string)$value['num'],(string)$value['price'],2);
                       $money = Db::name("user")->where('id',$value['user_id'])->value('money');
                       $sum = bcadd((string)$update_money,(string)$money,2);
                       $money_upd = Db::name("user")->where('id',$value['user_id'])->update(['money'=>$sum]);
                       if(!$money_upd){
                           Logs('批发商品ID为:'.$value['id'].'增加积分失败','Task');
                       }else{
                           //写入流水
                           $is_log = setUserMoneyLog($value['user_id'],$money,$update_money,$sum,1,"批发商品售出");
                           if($is_log  == 2){
                               Logs('批发商品ID为:'.$value['id'].'增加积分流水记录失败','Task');
                           }
                       }
                    }else{
                        //写入失败 写入记录日志
                        Logs('批发商品ID为:'.$value['id'].'自动售出失败','Task');
                    }


                }
            }
        }


    }
    

}
