<?php



namespace app\controller\admin\store;

use app\common\repositories\store\product\ProductPresellRepository as repository;
use app\common\repositories\system\CacheRepository;
use ln\basic\BaseController;
use ln\jobs\ChangeSpuStatusJob;
use ln\services\SwooleTaskService;
use think\App;
use app\validate\merchant\StoreProductAdminValidate as validate;

class StoreProductPresell extends BaseController
{
    protected  $repository ;

    /**
     * Product constructor.
     * @param App $app
     * @param repository $repository
     */
    public function __construct(App $app ,repository $repository)
    {
        parent::__construct($app);
        $this->repository = $repository;
    }

    /**
     * TODO 列表
     * @return mixed
     * @author Qinii
     * @day 2020-10-12
     */
    public function lst()
    {
        [$page, $limit] = $this->getPage();
        $where = $this->request->params(['product_status','keyword','status','type','presell_type','mer_id','is_trader','us_status','star','product_presell_id']);
        $data = $this->repository->getAdminList($where,$page,$limit);
        return app('json')->success($data);
    }

    /**
     * TODO 详情
     * @param $id
     * @return mixed
     * @author Qinii
     * @day 2020-10-12
     */
    public function detail($id)
    {
        $data = $this->repository->detail(null,$id);
        return app('json')->success($data);
    }

    /**
     * TODO 获取商品
     * @param $id
     * @return mixed
     * @author Qinii
     * @day 2020-11-02
     */
    public function get($id)
    {
        $data = $this->repository->get($id);
        if(!$data) return app('json')->fail('数据不存在');
        return app('json')->success($data);
    }

    /**
     * TODO 编辑商品
     * @param $id
     * @param validate $validate
     * @return mixed
     * @author Qinii
     * @day 2020-11-02
     */
    public function update($id,validate $validate)
    {
        $data = $this->checkParams($validate);
        if(!$this->repository->getWhereCount([$this->repository->getPk() => $id]))
            return  app('json')->fail('数据不存在');
        $this->repository->updateProduct($id,$data);
        return app('json')->success('编辑成功');

    }
    public function switchStatus($id)
    {
        $status = $this->request->param('status', 0) == 1 ? 1 : 0;
        if(!$ret = $this->repository->get($id)) return app('json')->fail('数据不存在');
        $this->repository->update($id, ['status' => $status]);
//        if(!$status) SwooleTaskService::merchant('notice', [
//            'type' => 'product_presell',
//            'data' => [
//                'title' => '下架提醒',
//                'message' => '您有一个预售商品被下架',
//                'id' => $id
//            ]
//        ], $ret->mer_id);
        queue(ChangeSpuStatusJob::class,['id' => $id,'product_type' => 2]);
        return app('json')->success('修改成功');
    }

    public function saveAgree()
    {
        $agree = $this->request->param('agree');
        app()->make(CacheRepository::class)->save('sys_product_presell_agree',$agree);
        return app('json')->success('保存成功');
    }

    public function getAgree()
    {
        $make = app()->make(CacheRepository::class);
        return app('json')->success(['sys_product_presell_agree' => $make->getResult('sys_product_presell_agree')]);
    }

    public function checkParams(validate $validate)
    {
        $data = $this->request->params(['is_hot','is_best','is_benefit','is_new','store_name','keyword','content','rank','star']);
        $validate->check($data);
        return $data;
    }

    public function productStatus()
    {
        $id = $this->request->param('id');
        if(!$ret = $this->repository->get($id))
            return  app('json')->fail('数据不存在');
        $data = $this->request->params(['status','refusal']);
        if($data['status'] == -1 && empty($data['refusal']))
            return app('json')->fail('请填写拒绝理由');
        if(!is_array($id)) $id = [$id];
        $status = 0;
        if($data['status'] == 1){
            $status = 1;
            $title = '审核结果';
            $message = '审核通过';
            $type = 'product_presell_success';
        }
        if($data['status'] == -1){
            $title = '审核结果';
            $message = '审核失败';
            $type = 'product_presell_fail';
        }
        if($data['status'] == -2){
            $title = '下架提醒';
            $message = '被下架';
            $type = 'product_presell_fail';
        }
        $this->repository->updates($id,['product_status' => $data['status'],'refusal' => $data['refusal'],'status' => $status]);
        SwooleTaskService::merchant('notice', [
            'type' => $type,
            'data' => [
                'title' => $title,
                'message' => '您有一个预售商品'. $message,
                'id' => $id[0],
                'type' => $ret['presell_type'],
            ]
        ], $ret->mer_id);
        foreach ($id as $item){
            queue(ChangeSpuStatusJob::class,['id' => $item,'product_type' => 2]);
        }
        return app('json')->success('操作成功');
    }

}
