quanwei
18 hours ago c441dea81bd86bdfb12dff35821fed51f4cc91c2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
 
namespace app\operations\model\plus\agent;
 
use app\common\model\plus\agent\Product as AgentProductModel;
use app\common\model\product\Product as ProductModel;
 
/**
 * 分销商用户模型
 */
class Product extends AgentProductModel
{
    public function getList($product_id)
    {
        return $this->where('product_id', '=', $product_id)
            ->select();
    }
 
    /**
     * 保存
     */
    public function edit($params)
    {
        $this->startTrans();
        try {
            (new ProductModel())->where('product_id', '=', $params['product_id'])
                ->save([
                    'is_agent' => $params['is_agent'],
                ]);
            // 参与分销
            if ($params['is_agent'] == 1) {
                // 先删除
                $model = self::detail($params['product_id']);
                if(!$model){
                    $model = new self();
                }
                $model->save(array_merge($params, [
                    'app_id' => self::$app_id
                ]));
            }
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
 
    //设置状态
    public function setAgent($productIds, $is_agent)
    {
        $productIds = explode(",",$productIds);
        if($is_agent == 1){
            // 先删除
            $model = $this;
            $model->where('product_id', 'in', $productIds)->delete();
            foreach ($productIds as $val){
                $save_data=[
                    'is_ind_agent' => 0,
                    'agent_money_type' => 10,
                    'product_id' => $val,
                    'app_id' => self::$app_id,
                ];
                $this->create($save_data);
            }
        }
        return (new ProductModel)->where('product_id', 'in', $productIds)->save(['is_agent' => $is_agent]);
    }
}