GoodsLog.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2018/10/26
  6. * Time: 10:11
  7. */
  8. namespace app\common\services\operation;
  9. use app\common\models\OperationLog;
  10. class GoodsLog extends OperationBase
  11. {
  12. public $modules = 'goods';
  13. public $type = 'update';
  14. public $modify_fields;
  15. public function __construct($model, $type = null)
  16. {
  17. parent::__construct($model, $type);
  18. }
  19. protected function modifyDefault()
  20. {
  21. $this->setLog('mark', $this->model->id);
  22. }
  23. /**
  24. * 获取模型需要记录的字段
  25. * @return mixed
  26. */
  27. protected function recordField()
  28. {
  29. return [
  30. 'title' => '商品标题',
  31. 'price' => '现价',
  32. 'market_price' => '原价',
  33. 'cost_price' => '成本价',
  34. 'type' => ['field_name' => '商品类型',1=> '实体', 2=>' 虚拟'],
  35. 'is_recommand' => ['field_name' => '商品属性',0=>'取消推荐', 1=>'推荐'],
  36. 'is_new' => ['field_name' => '商品属性',0=>'取消新品', 1=>'新品'],
  37. 'is_hot' => ['field_name' => '商品属性',0=>'取消热卖', 1=>'热卖'],
  38. 'is_discount' => ['field_name' => '商品属性',0=>'取消促销', 1=>'促销'],
  39. 'weight' => '商品重量',
  40. 'stock' => '商品库存',
  41. 'virtual_sales'=> '商品虚拟销量',
  42. 'reduce_stock_method' => ['field_name' => '减库存方式',0=>'拍下减库存',1=>'付款减库存',2=>'永不减库存'],
  43. 'status' => ['field_name' => '上下架',0=>'下架',1=>'上架'],
  44. ];
  45. }
  46. /**
  47. * 获取模型修改了哪些字段
  48. * @return array
  49. */
  50. protected function modifyField()
  51. {
  52. $model = $this->model;
  53. foreach ($this->recordField() as $key => $item) {
  54. if ($model->isDirty($key)) {
  55. $this->modify_fields[$key]['old_content'] = $model->getOriginal($key);
  56. $this->modify_fields[$key]['new_content'] = $model->getDirty()[$key];
  57. }
  58. }
  59. return $this->modify_fields;
  60. }
  61. protected function createLog()
  62. {
  63. $model = $this->model;
  64. $this->setLog('type', 'create');
  65. $this->setLog('field', 'id');
  66. $this->setLog('field_name', '商品ID');
  67. $this->setLog('old_content', $model->id);
  68. $this->setLog('new_content', $model->id);
  69. OperationLog::create($this->logs);
  70. }
  71. }