<?php


namespace wstmart\admin\model;


use think\Db;

class TradeRule extends Base
{
    protected $createTime = "create_time";
    protected $dateFormat = false;
    protected $updateTime = false;
    /**
     * 分页
     */

    public function pageQuery(){
        $key = input('key');
        $sort = input('sort');
        $where = [];
        $where['a.dataFlag'] = 1;
        if($key!='')$where['a.title'] = ['like','%'.$key.'%'];
        $order = 'a.id desc';
        if($sort){
            $sort =  str_replace('.',' ',$sort);
            $order = $sort;
        }

        $page = Db::name('trade_rule')->alias('a')
            ->where($where)
            ->field(true)
            ->order($order)
            ->paginate(input('post.limit/d'))->toArray();

        if(count($page['Rows'])>0){
            foreach ($page['Rows'] as $key => $v){
                $page['Rows'][$key]['content'] = strip_tags(htmlspecialchars_decode($v['content']));
            }
        }
        return $page;

    }

    /**
     * 新增
     */
    public function add(){
        $data = input('post.');
        WSTUnset($data,'id,dataFlag,isShow');
        $data['content'] = htmlspecialchars_decode($data['content']);
        $data['create_time'] = date('Y-m-d H:i:s');
        Db::startTrans();
        try{
            $result = $this->validate('TradeRule.add')->allowField(true)->save($data);
            if(false !== $result){
                WSTClearAllCache();
                Db::commit();
                return WSTReturn("新增成功", 1);
            }
        }catch(\Exception $e){
            Db::rollback();errLog($e);
        }
        return WSTReturn($this->getError(),-1);
    }
    /**
     * 获取指定对象
     */
    public function getById($id){
        $single = $this->where(['id'=>$id,'dataFlag'=>1])->find();
        $single['content'] = htmlspecialchars_decode($single['content']);
        return $single;
    }

    /**
     * 编辑
     */
    public function edit(){
        $id = input('post.id/d');
        $data = input('post.');
        $data['content'] = htmlspecialchars_decode($data['content']);
        WSTUnset($data,'id,dataFlag,isShow,create_time');
        Db::startTrans();
        try{
            $result = $this->validate('TradeRule.edit')->allowField(true)->save($data,['id'=>$id]);
            if(false !== $result){
                WSTClearAllCache();
                Db::commit();
                return WSTReturn("修改成功", 1);
            }
        }catch(\Exception $e){
            Db::rollback();errLog($e);
        }
        return WSTReturn($this->getError(),-1);
    }

    /**
     * 删除
     */
    public function del(){
        $id = input('post.id/d');
        $data = [];
        $data['dataFlag'] = -1;
        Db::startTrans();
        try{
            $result = $this->where(['id'=>$id])->update($data);
            if(false !== $result){
                WSTClearAllCache();
                Db::commit();
                return WSTReturn("删除成功", 1);
            }
        }catch (\Exception $e) {
            Db::rollback();errLog($e);
        }
        return WSTReturn('删除失败',-1);
    }

    /**
     * 批量删除
     */
    public function delByBatch(){
        $ids = input('post.ids');
        $data = [];
        $data['dataFlag'] = -1;
        Db::startTrans();
        try{
            $result = $this->where(['articleId'=>['in',$ids]])->update($data);
            if(false !== $result){
                WSTClearAllCache();
                Db::commit();
                return WSTReturn("删除成功", 1);
            }
        }catch (\Exception $e) {
            Db::rollback();errLog($e);
        }
        return WSTReturn('删除失败',-1);
    }


    /**

     * 显示是否显示/隐藏

     */
    public function editiIsShow(){
        $id = input('post.id/d');
        $isShow = (input('post.isShow/d')==1)?1:0;
        $result = $this->where(['id'=>$id])->update(['isShow' => $isShow]);
        if(false !== $result){
            WSTClearAllCache();
            return WSTReturn("操作成功", 1);
        }else{
            return WSTReturn($this->getError(),-1);
        }

    }

}