PHP上移下移,置顶置底操作

 /**
     * 上移下移,置顶置底操作
     *
     */
    public function goodsResort(){

        $this->load->model('CmsFilmGoods');
        //获取参数
        $params = $this->input->post(NULL, true);
        //商品SKU
        $goods_sku = $params['sku'];
        //影片ID
        $film_id = $params['film_id'];
        //操作类型
        $sort_type = $params['type'];

        if(!$film_id || !$goods_sku || !$sort_type){

            $this->response('300000201', "参数错误");
        }
        //获取当前需要移动的商品数据
        $goods_info = $this->CmsFilmGoods->getRow(['film_id' => $film_id, 'goods_sku' => $goods_sku, 'status' => 0], 'id, sort');
        if(!isset($goods_info['id'])){
            $this->response('300000202', "数据错误");
        }
        //执行移动操作
        switch ($sort_type)
        {
            case 1:
                //上移
                $goods_before = $this->CmsFilmGoods->getOne('film_id = '.$film_id.' and goods_sku > 0 and status=0 and sort < '.$goods_info['sort'], 'id, sort', " sort desc");
                        var_dump($goods_before);

                if($goods_info['sort'] == 1 || !isset($goods_before['id'])){
                    //当前操作商品已被排到第一位
                    $this->response('300000203', "已置顶");
                }
                $sql = 'UPDATE cms_film_goods SET sort = CASE id WHEN '.$goods_info['id'].' THEN '.$goods_before['sort'].' WHEN '.$goods_before['id'].' THEN '.$goods_info['sort'].' END WHERE id IN ('.$goods_info['id'].', '.$goods_before['id'].')';
                $res = $this->CmsFilmGoods->query($sql);
                $this->db->last_query();die;
                if(!$res){
                    $this->response('300000204', "操作失败");
                }
                break;
            case 2:
                //下移
                $goods_after = $this->CmsFilmGoods->getOne('film_id = '.$film_id.' and goods_sku > 0 and status=0 and sort > '.$goods_info['sort'], 'id, sort', " sort asc");
                if(!isset($goods_after['id'])){
                    //当前操作商品已被排到第一位
                    $this->response('300000203', "已置底");
                }
                $sql = 'UPDATE cms_film_goods SET sort = CASE id WHEN '.$goods_info['id'].' THEN '.$goods_after['sort'].' WHEN '.$goods_after['id'].' THEN '.$goods_info['sort'].' END WHERE id IN ('.$goods_info['id'].', '.$goods_after['id'].')';
                $res = $this->CmsFilmGoods->query($sql);

                if(!$res){
                    $this->response('300000204', "操作失败");
                }
                break;
            case 3:
                //置顶
                if($goods_info['sort'] == 1){
                    $this->response('0', "已置顶");
                }
                $goods_first = $this->CmsFilmGoods->getOne('film_id = '.$film_id.' and goods_sku > 0 and status=0', 'id, sort', " sort asc");
                if($goods_first && $goods_first['sort'] > 1){
                    //如果当前排序第一条的商品序列值不是1的时候
                    $ret = $this->CmsFilmGoods->update(['id'=>$goods_info['id']], ['sort'=>1]);
                    if(!$ret){
                        $this->response('300000204', "操作失败");
                    }
                }else{
                    //序列值小于当前商品的都+1,然后将当前商品序列值设定1
                    $this->CmsFilmGoods->startTrans();
                    $down = $this->CmsFilmGoods->format('sort < '.$goods_info['sort'], array('sort'=>'sort+1'));
                    if(!$down){
                        $this->response('300000204', "操作失败");
                    }
                    $set = $this->CmsFilmGoods->update('id = '.$goods_info['id'], array('sort'=>'1'));
                    if(!$set){
                        $this->CmsFilmGoods->rollback();
                        $this->response('300000204', "操作失败");
                    }
                    $this->CmsFilmGoods->commit();
                }
                break;
            case 4:
                //置底
                $goods_last = $this->CmsFilmGoods->getOne('film_id = '.$film_id.' and goods_sku > 0 and status=0', 'id, sort', " sort desc");
                if($goods_last && $goods_last['id'] == $goods_info['id']){
                    $this->response('0', "已置底");
                }
                $this->CmsFilmGoods->startTrans();
                $up = $this->CmsFilmGoods->format('sort > '.$goods_info['sort'], array('sort'=>'sort-1'));
                if(!$up){
                    $this->response('300000204', "操作失败");
                }
                $set_down = $this->CmsFilmGoods->update('id = '.$goods_info['id'], array('sort'=>$goods_last['sort']));
                if(!$set_down){
                    $this->CmsFilmGoods->rollback();
                    $this->response('300000204', "操作失败");
                }
                $this->CmsFilmGoods->commit();
                break;
            default:
                $this->response('300000201', "参数错误");
        }

        $this->response('0', "操作成功");
    }

你可能感兴趣的:(PHP上移下移,置顶置底操作)