<?php

namespace common\models;

/**
 * This is the model class for table "{{%SHOP_GOODS}}".
 *
 * @property string $ID
 * @property string $GOODS_NAME 商品名称
 * @property string $CATE_ID 所属分类ID
 * @property string $TYPE 商品来源
 * @property string $GIFT_TYPE 商品类型
 * @property string $SELL_TYPE 允许购买方式
 * @property int $GOODS_NO 商品编号
 * @property string $UNIT 单位
 * @property string $COVER 商品封面
 * @property int $IMAGES 商品图片
 * @property string $CONTENT 商品内容
 * @property string $SELL_PRICE 销售价格
 * @property string $MARKET_PRICE 市场价格
 * @property int $PRICE_PV 价格PV
 * @property int $POINT 兑换积分
 * @property int $STORE_NUMS 库存
 * @property int $IS_DEL 是否删除
 * @property int $CREATED_AT 创建时间
 * @property int $UPDATED_AT 更新时间
 * @property int $DELETED_AT 删除时间
 * @property int $STATUS 状态
 * @property string $SORT 排序
 */
class ShopGoods extends \common\components\ActiveRecord
{
    const SALE_TYPE = [
        1 => [
            'name' => '余额购买',
        ],
        2 => [
            'name' => '复消兑换',
        ],
        3 => [
            'name' => '积分兑换'
        ],
    ];

    const LOCAL_PAY = [
        1 => [
            'name' => '余额购买',
        ],
        2 => [
            'name' => '复消兑换',
        ],
        3 => [
            'name' => '积分兑换'
        ],
    ];

    const ONLINE_PAY = [
        4 => [
            'name' => 'IPay88'
        ],
        5 => [
            'name' => '微信'
        ],
        6 => [
            'name' => '支付宝'
        ],
    ];

    const BANK_CODE = [
        'WECHAT'        => 'wechat',
        'WAPWECHAT'     => 'wechat',
        'ALIPAY'        => 'alipay',
        'WEBSITEALIPAY' => 'alipay',
    ];

    const GOODS_TYPE = [
        1 =>[
            'name'=>'国内商品',
            'discount'=>'50',
        ],
        2 =>[
            'name'=>'进口商品',
            'discount'=>'60',
        ],
    ];
    const GIFT_TYPE = [
        1 =>[
            'name'=>'报单区',
        ],
        2 =>[
            'name'=>'复消区',
        ],
        3 =>[
            'name'=>'工作室报单',
        ],
        4 =>[
            'name'=>'工作室复消',
        ],
    ];
    const STATUS_NAME = [
        1 => '已上架',
        0 => '已下架',
    ];
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%SHOP_GOODS}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['SELL_DISCOUNT','GOODS_NAME', 'CATE_ID','GIFT_TYPE', 'GOODS_NO', 'SELL_PRICE','MARKET_PRICE','PRICE_PV','STORE_NUMS'], 'required'],
            [['STORE_NUMS'], 'integer'],
            [['SELL_PRICE','MARKET_PRICE','PRICE_PV','POINT'], 'number'],
            [['ID','CATE_ID', 'GOODS_NO','SELL_TYPE','GIFT_TYPE'], 'string', 'max' => 32],
            [['UNIT'], 'string', 'max' => 16],
            [['COVER', 'GOODS_NAME'], 'string', 'max' => 255],
            [['IMAGES','CONTENT'], 'string', 'max' => 4000],
            [['GOODS_NAME'], 'unique'],
            [['ID'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'ID' => 'ID',
            'GOODS_NAME' => '商品名称',
            'CATE_ID' => '所属分类ID',
            // 'TYPE' => '商品来源',
            'SELL_DISCOUNT' => '会员折扣',
            'GIFT_TYPE' => '商品类型',
            'SELL_TYPE' => '出售方式',
            'GOODS_NO' => '商品编号',
            'UNIT' => '商品单位',
            'COVER' => '商品封面',
            'IMAGES' => '商品图片',
            'CONTENT' => '商品内容',
            'SELL_PRICE' => '销售价格',
            'MARKET_PRICE' => '市场价格',
            'PRICE_PV' => '价格PV',
            'POINT' => '兑换点数',
            'STORE_NUMS' => '库存',
            'STATUS' => '是否上架',
            'IS_DEL' => '是否删除',
            'CREATED_AT' => '创建时间',
            'UPDATED_AT' => '更新时间',
            'DELETED_AT' => '更新时间',
            'SORT' => '排序',
        ];
    }

    /**
     * 判断提现状态
     * @param $nowStatus
     * @param $toStatus
     * @return string
     */
    public static function chkAuditStatus($nowStatus, $toStatus) {
        $statusName = self::STATUS_NAME;
        $msg = '当前商品状态为【' . $statusName[$nowStatus] . '】，无法设置为【' . $statusName[$toStatus] . '】';
        switch ($toStatus) {
            // 下架
            case 0:
                if ($nowStatus == 1) {
                    $msg = '';
                }
                break;
            //上架
            case 1:
                if ($nowStatus == 0) {
                    $msg = '';
                }
                break;
            default:
        }
        return $msg;
    }

    /**
     * 支付方式
     * @return array
     */
    public static function payTypes(){
        return [
            'cash'=>[
                'name'=>'消费点数支付'
            ],
            'point'=>[
                'name'=>'复销点数支付'
            ],
            'exchange' => [
                'name' => '无业绩兑换点数'
            ],
            'online' => [
                'name' => 'IPay88'
            ],
            'wechat'  => [
                'name' => '微信',
            ],
            'alipay'  => [
                'name' => '支付宝',
            ],
        ];
    }

    public static function getPayMethod($device = 'local'): array
    {
        $result = [];
        if ($device == 'website') {
            // 正扫/主扫
            $result = [
                'WECHAT'  => [
                    'name' => '微信',
                ],
//                'ALIPAY'  => [
//                    'name' => '支付宝',
//                ],
            ];
        }
        if ($device == 'mobile') {
            // H5/WAP
            $result = [
                'WAPWECHAT'  => [
                    'name' => '微信',
                ],
//                'WEBSITEALIPAY'  => [
//                    'name' => '支付宝',
//                ],
            ];
        }
        if ($device == 'local') {
            // 系统支付
            $result = [
                'cash'=>[
                    'name'=>'消费点数支付'
                ],
                'point'=>[
                    'name'=>'复销点数支付'
                ],
                'exchange' => [
                    'name' => '无业绩兑换点数'
                ],
            ];
        }

        return $result;
    }

    public static function bankCode2PayType($payCode): string
    {
        return self::BANK_CODE[$payCode];
    }
}
