<?php

namespace app\common\model\unishop;

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

    use SoftDelete;

    

    // 表名
    protected $name = 'unishop_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("unishop_order_product")->where('order_id',$order_id)->where('user_id',$user_id)->select();

            //增加批发商品表数据

            foreach ($list as $key=>$value){
                $goods_ratio = Db::name("unishop_product")->where("id",$value['product_id'])->value("ratio");
                $goods_ratio = $goods_ratio/100;
                $goods_info = Db::name("unishop_product")->where("id",$value['product_id'])->find("ratio");
                $price = bcmul((string)$goods_ratio,(string)$value['price'],2);
                $data['user_id'] = $user_id;
                $data['goods_id'] = $value['product_id'];
                $data['title'] = $value['title'];
                $data['image'] = $value['image'];
                $data['images'] = $value['images'];
                $data['desc'] = '用户下单支付生成批发商品';
                $data['market_price'] = $goods_info['market_price']??0;
                $data['sales_price'] = $price??0;
                $data['sales'] = $goods_info['sales']??0;
                $data['stock'] = $value['number']*$shop_num;
                $data['weigh'] = $goods_info['weigh']??0;
                $data['switch'] = $goods_info['switch']??1;
                $data['createtime'] = time();

               self::insert($data);

            }
            return true;
        }catch (Exception $e) {

            return false;
        }

    }

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

        $not_time = Db::name("config")->where("name","not_time")->value("value");
        if(!empty($not_time)){
            $not_time_arr = explode(',',$not_time);
        }
        $list = self::where('switch',1)->order('createtime asc')->select();



       $times = time();

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

        if(!empty($list)){
            foreach ($list as $key=>$value){
                 $num = 0;
                 foreach ($not_time_arr as $k=>$val){
                     $num = $num + getweeknum(date('Y-m-d',$value['createtime']),$val);
                 }
                 //根据天数增加时间戳
                 $days = $num*86400;
                 //结束时间 = 创建时间戳+过期天数+休息日
                $end_time = $value['createtime']+$shop_days_time+$days;
                if($end_time<$times){
                    //到期
                    $upd = self::where('id',$value['id'])->update(['switch'=>0]);
                    if($upd){
                        //增加积分/金额
                       $update_money = bcmul((string)$value['stock'],(string)$value['sales_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');
                    }


                }
            }
        }


    }
    

}
