<?php

namespace app\common\logic;


use app\common\enum\YesNoEnum;
use app\common\logic\BaseLogic;
use app\common\model\dict\DictData;
use app\common\model\user\User;
use app\common\model\user_recharge\UserRecharge;
use app\common\model\user_sign\UserSign;
use app\common\model\user_sign_log\UserSignLog;
use function Symfony\Component\String\s;

/**
 * 会员积分逻辑
 * Class UserSignLogic
 */
class UserSignLogic extends BaseLogic
{

    /**
     * 来自充值
     */
    public static function dealRechargeFrozen($user, $order, $user_ship = 0)
    {
        $total_vip = 0;
        $level_one = 1000;
        $level_two = 2000;
        $level_three = 5000;
        $level_four = 10000;
        $list = DictData::where('type_value', 'recharge')->select();
        foreach( $list as $k=>$v){
            if($v['name']=='level_one'){
                $level_one = bcadd($v['value'],0,2);
            }elseif($v['name']=='level_two'){
                $level_two = bcadd($v['value'],0,2);
            }elseif($v['name']=='level_three' ){
                $level_three = bcadd($v['value'],0,2);
            }elseif($v['name']=='level_four'){
                $level_four = bcadd($v['value'],0,2);
            }
        }
        $price= bcadd($order['price'],0,2);
        switch ($price) {
            case $level_one:
                $total_vip = 249;
                break;
            case $level_two:
                $total_vip = 560;
                break;
            case $level_three:
                $total_vip = 1550;
                break;
            case $level_four:
                $total_vip = 3500;
                break;
            default:
                break;
        }
        if($total_vip==0){
            return false;
        }
        $count = UserRecharge::where(['uid'=>$order->uid,'paid'=>YesNoEnum::YES])->count();
        if ($count ==1 && in_array($user_ship,[1,2,3])) {
            //首充
            $write = self::write($order, $total_vip, 0, 1, 9);
            self::write_log($write, $total_vip, 0, 7);
            self::write_log($write, $total_vip, 0, 9, 0);
            User::where('id', $order->uid)->inc('integral', $total_vip)->update();
        }
        return true;
    }
    /**
     * 充值退款
     */
    public static function RefundRecharge($order)
    {
        $find = UserSign::where('uid', $order['uid'])->where('order_id', $order['order_id'])->find();
        if ($find) {
            $number = UserSignLog::where('sid', $find->id)->where('financial_pm', 0)->sum('number');
            self::write($find, $number, 0, 1, 3, 0);
            User::where('id', $order['uid'])->dec('integral', $number)->update();
        }
    }

    /**
     * 来自订单
     */
    public static function OrderWrite($order)
    {
        $total_vip = bcmul($order['pay_price'], 0.1, 2);

        if ($order['source'] == 0) {
            //冻结礼品券
            if ($order['pay_price'] >= 500) {
                $write = self::write($order, $total_vip, 1, 0, 4);
                self::write_log($write, $total_vip, 1, 1, 1);
            }
        } else {
            //不冻结礼品券
            if ($order['pay_price'] >= 500) {
                $write = self::write($order, $total_vip, 1, 1, 4);
                self::write_log($write, $total_vip, 1, 1);
                self::write_log($write, $total_vip, 1, 2, 0);
                User::where('id', $order->uid)->inc('integral', $total_vip)->update();
            }
        }
    }
    /**
     * 订单退款
     */
    public static function RefundOrder($order)
    {
        $find = UserSign::where('uid', $order['uid'])->where('order_id', $order['order_id'])->find();
        if ($find) {
            $number = UserSignLog::where('sid', $find->id)->where('financial_pm', 0)->sum('number');
            self::write($find, $number, 1, 1, 3, 0);
            User::where('id', $order['uid'])->dec('integral', $number)->update();
        }
    }

    /**
     * 核销
     */
    public static function WriteOff($order)
    {
        $find = UserSign::where(['order_id' => $order['order_id'], 'status' => 0, 'financial_pm' => 1, 'order_type' => 1])->find();
        if ($find) {
            $find->status = 1;
            $find->save();
            User::where('id', $order->uid)->inc('integral', $find['number'])->update();
            self::write_log($find, $find['number'], 1, 2, 0);
            // self::write_log($find, $find['number'], 1, 4);
        }
    }
    /**
     * 储存商品积分结算
     */
    public static function storage($order_id, $price)
    {
        $find = UserSign::where(['order_id' => $order_id, 'status' => 0, 'financial_pm' => 1, 'order_type' => 1])->find();
        if ($find) {
            self::write_log($find, $price, 1, 2, 0);
            self::write_log($find, $price, 1, 2, 1);
            if ($price > $find['number']) {
                $find->status = 1;
                $find->save();
            }
            User::where('id', $find['uid'])->inc('integral', $price)->update();
        }
    }

    /**
     * @param  type 1:购买商品冻结
     * @param  type 2:核销商品解冻
     * @param  type 3:退款扣除冻结
     * @param  type 4:核销商品获得
     * @param  type 5:兑换商品扣除
     * @param  type 6:退款扣除礼品
     * @param  type 7:充值冻结
     * @param  type 8:收银台支付增加
     * @param  type 9:充值解冻
     */
    public static function write($order, $total_vip, $order_type = 0, $status = 0, $type = 4, $pm = 1)
    {
        //礼品券得
        $sing = [
            'uid' => $order['uid'],
            'order_id' => $order['order_id'],
            'type' => $type,
            'financial_pm' => $pm,
            'store_id' => $order['store_id'],
            'status' => $status,
            'number' => $total_vip,
            'back_num' => $total_vip,
            'order_type' => $order_type,

        ];
        return UserSign::create($sing);
    }
    /**
     * @param  type 1:购买商品冻结
     * @param  type 2:核销商品解冻
     * @param  type 3:退款扣除冻结
     * @param  type 4:核销商品获得
     * @param  type 5:兑换商品扣除
     * @param  type 6:退款扣除礼品
     * @param  type 7:充值冻结
     * @param  type 8:收银台支付增加
     * @param  type 9:充值解冻
     */
    public static function write_log($write, $total_vip, $order_type = 0, $type = 4, $pm = 1)
    {
        //礼品券日志记录
        $sing = [
            'uid' => $write['uid'],
            'sid' => $write['id'],
            'order_id' => $write['order_id'],
            'type' => $type,
            'financial_pm' => $pm,
            'order_type' => $order_type,
            'status' => 1,
            'number' => $total_vip,
        ];
        UserSignLog::create($sing);
    }

    //礼品券相关对应文本
    public static function getTitle($type, $amount, $number)
    {
        switch ($type) {
                /**冻结券**/
                //收入
            case 1:
                return "购买商品{$amount}元获得{$number}元冻结券";
            case 7:
                return "充值{$amount}元获得{$number}元冻结券";
                //支出
            case 2:
                return "核销商品{$amount}元解冻{$number}元礼品券";
            case 3:
                return "退款{$amount}元扣除{$number}元冻结券";
                /**礼品券**/
                //收入
            case 4:
                return "核销商品{$amount}元获得{$number}元礼品券";
                //支出
            case 5:
                return "兑换{$amount}元商品扣除{$number}元礼品券";
            case 6:
                return "退款{$amount}元扣除{$number}元礼品券";
            case 8:
                return "收银台支付{$amount}元增加{$number}元礼品券";
            case 9:
                return "充值{$amount}元解冻{$number}元礼品券";
            default:
                return "订单支付{$amount}元";
        }
    }
}