| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- <?php
- namespace common\models\forms;
- use common\components\Model;
- use common\helpers\Cache;
- use common\helpers\Date;
- use common\helpers\Form;
- use common\helpers\LoggerTool;
- use common\libs\logging\operate\AdminOperate;
- use common\models\ShopGoods;
- use yii\base\Exception;
- /**
- * Login form
- */
- class ShopGoodsForm extends Model
- {
- public $selectedIds;
- public $id;
- public $goodsName;
- public $type;
- public $sellDiscount;
- public $giftType;
- public $sellType;
- public $pvSplit;
- public $goodsNo;
- public $unit;
- public $taxRate;
- public $cover;
- public $sellPrice;
- public $sellPriceStandard;
- public $marketPrice;
- public $pricePv;
- //public $point;
- public $storeNums;
- public $content;
- public $sort;
- public $status;
- public $categoryType;
- private $_model;
- public function init() {
- parent::init();
- $this->adminOperateLogger = new AdminOperate([
- 'fetchClass' => ShopGoods::class,
- ]);
- }
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['id','sellDiscount','giftType','sellType','goodsNo', 'goodsName', 'unit', 'sellPrice', 'marketPrice', 'pricePv', 'storeNums', 'content', 'sort','status','cover'], 'trim'],
- [['goodsName','sellDiscount','giftType','goodsNo', 'storeNums','sellPrice','marketPrice','pricePv', 'sort','status', 'categoryType', 'sellPriceStandard'], 'required'],
- [['id'], 'required', 'on'=>'edit'],
- [['id'], 'exist', 'targetClass'=>ShopGoods::class, 'targetAttribute'=>'ID'],
- [['sellPrice','marketPrice','pricePv', 'sellPriceStandard'], 'price'],
- [['id'], 'initModel'],
- [['selectedIds'], 'isSelected'],
- [['sort'], 'isSort'],
- [['sellDiscount'], 'isDiscount'],
- ];
- }
- public function attributeLabels()
- {
- return [
- 'selectedIds' => 'ID',
- 'id' => 'Product ID',
- 'goodsName' => 'Product Name', // 商品名称
- 'sellDiscount' => 'Discount Rate', // 会员折扣
- 'giftType' => 'Product Type', // 商品类型
- 'categoryType' => 'Product Category', // 商品分类
- 'sellType' => 'SellType', // 出售方式
- 'pvSplit' => 'PV Split', // pv分期
- 'goodsNo' => 'Product Code', // 产品编号
- 'unit' => 'Unit', // 单位
- 'cover' => 'Cover',// 封面
- 'sellPrice' => 'Price', // 销售价格
- 'sellPriceStandard' => 'US price',
- 'marketPrice' => 'Market Price', // 市场价格
- 'pricePv' => 'BV', // 销售PV
- //'point' => '兑换积分',
- 'storeNums' => 'Inventory', // 库存
- 'content' => 'Content',
- 'listOrder' => 'Order',// 排序
- 'taxRate' => 'Tax',// 税率
- ];
- }
- /**
- * 指定场景
- * @return array
- */
- public function scenarios()
- {
- $parentScenarios = parent::scenarios();
- $customScenarios = [
- 'add' => ['goodsName','sellDiscount','giftType', 'sellType','goodsNo','unit','sellPrice','marketPrice','pricePv','storeNums', 'content','sort','cover', 'categoryType', 'sellPriceStandard', 'taxRate', 'taxAmount','pvSplit'],
- 'edit' => ['id','goodsName','sellDiscount','giftType', 'sellType','goodsNo','unit','sellPrice','marketPrice','pricePv', 'storeNums', 'content','sort','cover', 'categoryType', 'sellPriceStandard', 'taxRate','pvSplit'],
- 'changeStatus' => ['selectedIds', 'status'],
- ];
- return array_merge($parentScenarios, $customScenarios);
- }
- /**
- * 初始化model
- * @param $attributes
- */
- public function initModel($attributes) {
- $this->_model = ShopGoods::findOne(['ID' => $this->id]);
- if (!$this->_model) {
- $this->addError($attributes, 'The data does not exist'); // 数据不存在
- }
- }
- /**
- * 前置数据填充和校验.
- * @return bool
- */
- public function beforeValidate()
- {
- if($this->categoryType){
- // 处理sellType
- $categoryType = array_column(ShopGoods::CATEGORY_TYPE, NULL, 'id');
- $sellType = $categoryType[$this->categoryType]['sell_type'] ?? [];
- // print_r($formModel);
- // print_r($this);
- // exit;
- if (!$sellType) {
- $this->addError('add', '商品购买方式错误');
- } else {
- foreach ($sellType as $item) {
- if (!in_array($item['id'], array_keys(ShopGoods::SALE_TYPE))) {
- $this->addError('add', '商品购买方式错误');
- break;
- }
- }
- // 购买方式格式化为','分割的方式
- $this->sellType = implode(',', array_column($sellType, 'id'));
- // 如果是普通商品,有PV,旅游、名车、豪宅商品没有PV
- if ($this->categoryType != 1) {
- $this->pricePv = 0;
- }
- }
- }
- return parent::beforeValidate();
- }
- /**
- * 批量数据
- * @param $attributes
- */
- public function isSelected($attributes) {
- if (!$this->selectedIds) {
- $this->addError($attributes, 'A piece of data must be selected'); // 必须选择一条数据
- }
- if (!is_array($this->selectedIds)) {
- $this->selectedIds = [$this->selectedIds];
- }
- }
- /**
- * 排序需大于等于1
- * @param $attributes
- */
- public function isSort($attributes) {
- if ($this->sort < 1) {
- $this->addError($attributes, '排序请填写大于等于1的数字');
- }
- }
- // 折扣为0-1
- public function isDiscount($attributes) {
- if ($this->sellDiscount < 0 || $this->sellDiscount > 1 || !is_numeric($this->sellDiscount)) {
- $this->addError($attributes, '折扣请填写0-1之间的数');
- }
- }
- /**
- * 添加
- * @return ShopGoods|null
- * @throws \yii\db\Exception
- */
- public function add() {
- if (!$this->validate()) {
- return null;
- }
- $transaction = \Yii::$app->db->beginTransaction();
- try {
- // 添加商品
- $shopGoods = new ShopGoods();
- $shopGoods->GOODS_NAME = $this->goodsName;
- //$shopGoods->TYPE = $this->type;
- $shopGoods->SELL_DISCOUNT = $this->sellDiscount;
- $shopGoods->GIFT_TYPE = implode(',',$this->giftType);
- // $shopGoods->SELL_TYPE = implode(',',$this->sellType);
- $shopGoods->SELL_TYPE = $this->sellType;
- $shopGoods->PV_SPLIT = $this->pvSplit;
- $shopGoods->GOODS_NO = $this->goodsNo;
- $shopGoods->UNIT = $this->unit ? $this->unit : '个';
- $shopGoods->COVER = $this->cover ? $this->cover : '';
- $shopGoods->SELL_PRICE_STANDARD = $this->sellPriceStandard;
- $shopGoods->SELL_PRICE = $this->sellPrice;
- $shopGoods->MARKET_PRICE = $this->marketPrice;
- $shopGoods->PRICE_PV = $this->pricePv;
- //$shopGoods->POINT = $this->point;
- $shopGoods->CONTENT = $this->content;
- $shopGoods->STORE_NUMS = $this->storeNums;
- $shopGoods->TAX_RATE = $this->taxRate;
- $shopGoods->SORT = $this->sort;
- $shopGoods->CATE_ID = '1';
- $shopGoods->CREATED_AT = Date::nowTime();
- $shopGoods->CATEGORY_TYPE = $this->categoryType;
- if (!$shopGoods->save()) {
- throw new Exception(Form::formatErrorsForApi($shopGoods->getErrors()));
- }
- $transaction->commit();
- } catch (Exception $e) {
- $transaction->rollBack();
- $this->addError('add', $e->getMessage());
- return null;
- }
- return $shopGoods;
- }
- /**
- * 编辑商品
- * @return null
- * @throws \yii\db\Exception
- */
- public function edit() {
- if (!$this->validate()) {
- return null;
- }
- $transaction = \Yii::$app->db->beginTransaction();
- try {
- $model = $this->_model;
- $model->GOODS_NAME = $this->goodsName;
- $model->TYPE = 0;
- $model->SELL_DISCOUNT = $this->sellDiscount;
- $model->GIFT_TYPE = implode(',',$this->giftType);
- // $model->SELL_TYPE = implode(',',$this->sellType);
- $model->SELL_TYPE = $this->sellType;
- $model->PV_SPLIT = $this->pvSplit;
- $model->GOODS_NO = $this->goodsNo;
- $model->UNIT = $this->unit ? $this->unit : '个';
- $model->COVER = $this->cover ? $this->cover : '';
- $model->SELL_PRICE_STANDARD = $this->sellPriceStandard;
- $model->SELL_PRICE = $this->sellPrice;
- $model->MARKET_PRICE = $this->marketPrice;
- $model->PRICE_PV = $this->pricePv;
- $model->TAX_RATE = $this->taxRate;
- //$model->POINT = $this->point;
- $model->CONTENT = $this->content;
- $model->STORE_NUMS = $this->storeNums;
- $model->SORT = $this->sort;
- $model->UPDATED_AT = Date::nowTime();
- $model->CATEGORY_TYPE = $this->categoryType;
- if (!$model->save()) {
- throw new Exception(Form::formatErrorsForApi($model->getErrors()));
- }
- $transaction->commit();
- } catch (Exception $e) {
- $transaction->rollBack();
- $this->addError('edit', $e->getMessage());
- return null;
- }
- return $model;
- }
- /**
- * 上下架
- * @return null|static
- * @throws \yii\db\Exception
- */
- public function changeStatus() {
- if (!$this->validate()) {
- return null;
- }
- $db = \Yii::$app->db;
- $transaction = $db->beginTransaction();
- try {
- foreach ($this->selectedIds as $select) {
- $oneGoods = ShopGoods::findOne(['ID' => $select]);
- //判断状态
- if (($msg = ShopGoods::chkAuditStatus($oneGoods->STATUS, $this->status)) != '') {
- throw new Exception($msg);
- }
- $oneGoods->STATUS = $this->status;
- $oneGoods->UPDATED_AT = Date::nowTime();
- if (!$oneGoods->save()) {
- throw new Exception(Form::formatErrorsForApi($oneGoods->getErrors()));
- }
- }
- $transaction->commit();
- } catch (Exception $e) {
- $transaction->rollBack();
- $this->addError('changeStatus', $e->getMessage());
- return null;
- }
- return ['status' => $this->status];
- }
- /**
- * 异步更新商品属性表
- * @return bool
- */
- public function updateAsync(){
- // 汇率
- $exchangeRate = floatval(Cache::getSystemConfig()['exchangeRate']['VALUE'] ?? 0);
- // 商品列表
- $goods = ShopGoods::findAll(['STATUS' => 1]);
- $transaction = \Yii::$app->db->beginTransaction();
- try {
- foreach ($goods as $good) {
- $content = [
- 'id' => $good->ID,
- 'exchangeRate' => $exchangeRate,
- 'priceStandard' => $good->SELL_PRICE_STANDARD,
- 'originSellPrice' => $good->SELL_PRICE,
- 'modernSellPrice' => $good->SELL_PRICE_STANDARD * $exchangeRate,
- ];
- $good->SELL_PRICE = $good->SELL_PRICE_STANDARD * $exchangeRate;
- $good->UPDATED_AT = Date::nowTime();
- if (!$good->save()) {
- throw new Exception(Form::formatErrorsForApi($good->getErrors()));
- }
- $item = json_encode($content);
- echo "商品 {$item} 更新成功!" . PHP_EOL;
- }
- $transaction->commit();
- } catch (Exception $e) {
- $transaction->rollBack();
- $this->addError('update', $e->getMessage());
- return false;
- }
- return true;
- }
- }
|