// 模糊搜索剧情文本
    @PostMapping(value="/search/")
    public Result searchPlotText(@RequestParam Map<String, Object> searchForm) {
        String searchText = (String)searchForm.get("searchText");
        Long categoryId = new Long((String)searchForm.get("categoryId"));
        List<Plot> plots = plotDao.getAllPlotByCategoryId(categoryId);
        List<PlotFirstVO> plotFirstVos = new ArrayList<PlotFirstVO>();
        for(int i=0; i<plots.size(); i++) {
            Plot plot = plots.get(i);
            // 0的数据库标识为剧情文本
            if(plotTextDao.search(plot.getId(), new Integer(0), searchText).size()!=0 || plot.getNum().indexOf(searchText)!=-1) {
                PlotFirstVO plotFirstVo = plotFirstPo2Vo(plot.getId());
                plotFirstVos.add(plotFirstVo);
            }
        }
        return Result.success(plotFirstVos);
    }


    @Transactional
    public void deleteBranchRelate(Long id) {
            plotBranchRelateAttrDao.deleteByRelateId(id);
            plotBranchRelateDao.delete(id);
    }
    @Transactional
    public void deleteBranch(Long id) {
        plotBranchMatchFieldDao.deleteByPlotBranchId(id);
        plotBranchPremiseDao.deleteByPlotBranchId(id);
        List<PlotBranchRelate> plotBranchRelates = plotBranchRelateDao.getAllPlotBranchRelateByBranchId(id);
        for(int z=0; z<plotBranchRelates.size(); z++) {
            this.deleteBranchRelate(plotBranchRelates.get(z).getId());
        }
        plotBranchDao.delete(id);
    }
    @Transactional
    public void deleteByPlotId(Long plotId) {
        plotTextDao.deleteByPlotId(plotId);
        List<PlotBranch> plotBranchs = plotBranchDao.getAllBranchByPlotId(plotId);
        for(int j=0; j<plotBranchs.size(); j++) {
            this.deleteBranch(plotBranchs.get(j).getId());
        }
        plotDao.delete(plotId);
    }
    @Transactional
    @DeleteMapping("/{id}/")
    public Result deletePlotFirst(@PathVariable Long id) {
        Result  result = null;
        this.deleteByPlotId(id);
        result = Result.success();
        return result;
    }

    // 删除相关的数据
    @Transactional
    public Result deletePlaceRelationData(Long placeId) {
        // 更新角色的所在地点名称
        // 所在地点中都是id绑定，没有相应名称绑定
        // 更新物品所在地点的名称
        // 所在地点中都是id绑定，没有相应名称绑定
        // 更新文本对话地点名称
        /****** 通过地点id改变相应的地点名称 */
        // 更新附加条件地点名称
        // 绑定id不需要更新
        // 剧情属性更新的地点名称
        // 剧情属性更新地点只绑定的id

        // 角色的所在地点清空
            /***** 角色类别属性 *****/
        npcClassAttrDao.emptyPlaceId(placeId.toString());
            /***** 角色实例属性 *****/
        npcInstanceAttrDao.emptyPlaceId(placeId.toString());

        // 物品的所在地点清空
            /***** 物品类别属性 *****/
        goodsClassAttrDao.emptyPlaceId(placeId.toString());
            /***** 物品实例属性 *****/
        goodsInstanceAttrDao.emptyPlaceId(placeId.toString());
        // 剧情剧情地点清空
        plotDao.emptyPlaceId(placeId);

        // 剧情附加条件地点判断删除
        plotBranchPremiseDao.emptyRelationPlaceId(placeId.toString());

        // 剧情属性更新地点删除
        List<PlotBranchRelate> plotBranchRelates = plotBranchRelateDao.getAllUpdatePlace(placeId.toString());
        for(int i=0; i<plotBranchRelates.size(); i++) {
            PlotBranchRelate plotBranchRelate = plotBranchRelates.get(i);
            plotBranchRelateAttrDao.deleteByRelateId(plotBranchRelate.getId());
            plotBranchRelateDao.delete(plotBranchRelate.getId());
        }
        return null;
    }
    @Transactional


    // 删除goods class service
    @Transactional
    public Result serviceDeleteGoodsClass(Long id) {
        // 删除goods class attr
        goodsClassAttrDao.deleteByClassId(id);
        // 删除本身
        goodsClassDao.delete(id);
        // 删除 npc instance
        List<GoodsInstance> goodsInstances = goodsInstanceDao.getAllByClassId(id);
        for(int i=0; i<goodsInstances.size(); i++) {
            this.serviceDeleteGoodsInstance(goodsInstances.get(i).getId());
        }
        // 删除剧情相关的类下面的属性更新(没有必要暂时不写，因为属性更新的每一条都是以实例来标识的)
        return null;
    }

    public Result deleteGoodsInstanceAttrByClassAttrId(Long classAttrId) {
        List<GoodsInstanceAttr> goodsInstanceAttrs = goodsInstanceAttrDao.getAllByClassAttrId(classAttrId);
        for(int i=0;i<goodsInstanceAttrs.size(); i++) {
            Long goodsInstanceAttrId = goodsInstanceAttrs.get(i).getId();
            goodsInstanceAttrDao.delete(goodsInstanceAttrId);
            // 删除前置条件
            plotBranchPremiseDao.deleteByInstanceAttrId(new Integer(0),goodsInstanceAttrId);
            // 删除属性更新
            plotBranchRelateAttrDao.deleteByAttrId(new Integer(0), goodsInstanceAttrId);
        }
        return null;
    }
    // 删除goods class attr
public Result deleteGoodsClassAttr(Long classAttrId) {
    // 类中的属性删除
    goodsClassAttrDao.delete(classAttrId);
    // 实例中的属性删除
    this.deleteGoodsInstanceAttrByClassAttrId(classAttrId);
    return null;
}
// 更新goods instance attr
    public Result updateGoodsInstanceAttr(Long goodsClassId, String goodsAttrName) {
        // 连带更新实例, 一个类有多个对象
        List<GoodsInstanceAttr> goodsInstanceAttrs = goodsInstanceAttrDao.getAllByClassAttrId(goodsClassId);
        for(int j=0; j<goodsInstanceAttrs.size(); j++) {
            // 更新goodsInstanceAttr value
            GoodsInstanceAttr goodsInstanceAttr = goodsInstanceAttrs.get(j);
            goodsInstanceAttr.setName(goodsAttrName);
            goodsInstanceAttr.setGmtModified(DateUtil.getNowTime());
            goodsInstanceAttrDao.update(goodsInstanceAttr);
            // 更新前置条件
            plotBranchPremiseDao.updateAttrNameByInstanceAttrId(new Integer(1),goodsInstanceAttr.getId(), goodsAttrName);
            // 更新属性更新
            plotBranchRelateAttrDao.updateAttrNameByAttrId(new Integer(1), goodsInstanceAttr.getId(), goodsAttrName);
        }
        return null;
    }
    // 插入goods instance attr
    public Result insertGoodsInstanceAttr(Long classId, GoodsClassAttr goodsClassAttr) {
        GoodsInstanceAttr goodsInstanceAttr = new GoodsInstanceAttr();
        goodsInstanceAttr.setName(goodsClassAttr.getName());
        goodsInstanceAttr.setValue(goodsClassAttr.getValue());
        goodsInstanceAttr.setIsBase(goodsClassAttr.getIsBase());
        goodsInstanceAttr.setGoodsClassAttrId(goodsClassAttr.getId());
        goodsInstanceAttr.setGmtCreate(DateUtil.getNowTime());
        goodsInstanceAttr.setGmtModified(DateUtil.getNowTime());
        List<GoodsInstance> goodsInstances = goodsInstanceDao.getAllByClassId(classId);
        for(int j=0; j<goodsInstances.size(); j++) {
            goodsInstanceAttr.setGoodsInstanceId(goodsInstances.get(j).getId());
            goodsInstanceAttrDao.insert(goodsInstanceAttr);
        }
        return null;
    }

    public Result deletePlotBranchRelateByGoodsInstanceId(Long goodsInstanceId) {
        List<PlotBranchRelate> plotBranchRelates = plotBranchRelateDao.getAllByInstanceId(goodsInstanceId, new Integer(0));
        for(int i=0; i<plotBranchRelates.size(); i++) {
            PlotBranchRelate plotBranchRelate = plotBranchRelates.get(i);
            // 删除属性更新的属性
            plotBranchRelateAttrDao.deleteByRelateId(plotBranchRelate.getId());
            // 删除属性更新的对象
            plotBranchRelateDao.delete(plotBranchRelate.getId());
        }
        return null;
    }
    public Result serviceDeleteGoodsInstance(Long goodsInstanceId) {
        // 删除物品实例
        goodsInstanceDao.delete(goodsInstanceId);
        // 删除物品实例属性
        goodsInstanceAttrDao.deleteByGoodsInstanceId(goodsInstanceId);
        // plot primise 删除
        plotBranchPremiseDao.deleteByInstanceId(new Integer(0), goodsInstanceId);
        // plot relation 删除
        this.deletePlotBranchRelateByGoodsInstanceId(goodsInstanceId);
        return null;
    }

    public Result updatePlotRelateGoodsInstanceName(Long goodsInstanceId, String goodsInstanceName) {
        List<PlotBranchRelate> plotBranchRelates = plotBranchRelateDao.getAllByInstanceId(goodsInstanceId, new Integer(1));
        for(int i=0; i<plotBranchRelates.size(); i++) {
            PlotBranchRelate plotBranchRelate = plotBranchRelates.get(i);
            plotBranchRelate.setInstanceName(goodsInstanceName);
            plotBranchRelateDao.update(plotBranchRelate);
        }
        return null;
    };

    @Transactional
       public Result serviceDeleteNpcClass(Long npcClassId) {
           // 删除npc class attr
           npcClassAttrDao.deleteByClassId(npcClassId);
           // 删除本身
           npcClassDao.delete(npcClassId);
           // 删除 npc instance
           List<NpcInstance> npcInstances = npcInstanceDao.getAllByNpcClassId(npcClassId);
           for(int i=0; i<npcInstances.size(); i++) {
           	NpcInstance npcInstance = npcInstances.get(i);
               this.serviceDeleteNpcInstance(npcInstance.getId());
           }
           // 删除剧情相关的类下面的属性更新(没有必要emptyNpcInstanceId暂时不写，因为属性更新的每一条都是以实例来标识的)
           return null;
       }
       @Transactional
       @DeleteMapping("/class/{id}/")
       public Result deleteNpcClass(@PathVariable Long id) {
           Result  result = null;
           this.serviceDeleteNpcClass(id);
           result = Result.success();
           return result;
       }
       // 删除npc instance attr 通过 class attr id
       public Result deleteNpcInstanceAttrByClassAttrId(Long classAttrId) {
           // 获取删除的属性
           List<NpcInstanceAttr> npcInstanceAttrs = npcInstanceAttrDao.getAllByClassAttrId(classAttrId);
           for(int i=0; i<npcInstanceAttrs.size(); i++) {
               Long npcInstanceAttrId = npcInstanceAttrs.get(i).getId();
               // 删除自身
               npcInstanceAttrDao.delete(npcInstanceAttrId);
               // 删除前置条件
               plotBranchPremiseDao.deleteByInstanceAttrId(new Integer(1),npcInstanceAttrId);
               // 删除属性更新
               plotBranchRelateAttrDao.deleteByAttrId(new Integer(1), npcInstanceAttrId);
           }
           return null;
       }
       // 删除npc class attr
       public Result deleteNpcClassAttr(Long classAttrId) {
           // 类中的属性删除
           npcClassAttrDao.delete(classAttrId);
           // 实例中的属性删除
           this.deleteNpcInstanceAttrByClassAttrId(classAttrId);

           return null;
       }

       // 更新npc instance attr
    public Result updateNpcInstanceAttr(Long npcClassId, String npcAttrName) {
        // 连带更新实例, 一个类有多个对象
        List<NpcInstanceAttr> npcInstanceAttrs = npcInstanceAttrDao.getAllByClassAttrId(npcClassId);
        for(int j=0; j<npcInstanceAttrs.size(); j++) {
            // npcInstanceAttr 属性更新
            NpcInstanceAttr npcInstanceAttr = npcInstanceAttrs.get(j);
            npcInstanceAttr.setName(npcAttrName);
            npcInstanceAttr.setGmtModified(DateUtil.getNowTime());
            npcInstanceAttrDao.update(npcInstanceAttr);
            // 更新前置条件
            plotBranchPremiseDao.updateAttrNameByInstanceAttrId(new Integer(1),npcInstanceAttr.getId(), npcAttrName);
            // 更新属性更新
            plotBranchRelateAttrDao.updateAttrNameByAttrId(new Integer(1), npcInstanceAttr.getId(), npcAttrName);
        }
        return null;
    }
    // 插入npc instance attr
    public Result insertNpcInstanceAttr(Long classId, NpcClassAttr npcClassAttr) {
        NpcInstanceAttr npcInstanceAttr = new NpcInstanceAttr();
        npcInstanceAttr.setName(npcClassAttr.getName());
        npcInstanceAttr.setProjectId(npcClassAttr.getProjectId());
        npcInstanceAttr.setValue(npcClassAttr.getValue());
        npcInstanceAttr.setIsBase(npcClassAttr.getIsBase());
        npcInstanceAttr.setNpcClassAttrId(npcClassAttr.getId());
        npcInstanceAttr.setGmtCreate(DateUtil.getNowTime());
        npcInstanceAttr.setGmtModified(DateUtil.getNowTime());
        List<NpcInstance> npcInstances = npcInstanceDao.getAllByNpcClassId(classId);
        for(int j=0; j<npcInstances.size(); j++) {
            npcInstanceAttr.setNpcInstanceId(npcInstances.get(j).getId());
            npcInstanceAttrDao.insert(npcInstanceAttr);
        }
        return null;
    }
    // 更新npc class attr
    public Result updateNpcClassAttr(Long classId, List<NpcClassAttrVO> npcClassAttrVos) {
        // 获取npc class attr 数据
        List<NpcClassAttr> npcClassAttrs = npcClassAttrDao.getAllByNpcClassId(classId);
        // 整理数据为collection数据
        List<Long> npcClassAttrIds = new ArrayList<Long>();
        for(int i=0; i<npcClassAttrs.size(); i++) {
            npcClassAttrIds.add(npcClassAttrs.get(i).getId());
        }
        List<Long> npcClassAttrVoIds = new ArrayList<Long>();
        for(int i=0; i<npcClassAttrVos.size(); i++) {
            npcClassAttrVoIds.add(npcClassAttrVos.get(i).getId());
        }
        // 取差集
        npcClassAttrIds.removeAll(npcClassAttrVoIds);
        for(int i=0; i<npcClassAttrIds.size(); i++) {
            // 删除npc class attr 和 npc instance attr 数据
            this.deleteNpcClassAttr(npcClassAttrIds.get(i));
        }
        for(int i=0; i<npcClassAttrVos.size(); i++) {
            NpcClassAttrVO npcClassAttrVo = npcClassAttrVos.get(i);
            NpcClassAttr npcClassAttr = new NpcClassAttr();
            npcClassAttr.setName(npcClassAttrVo.getName());
            npcClassAttr.setNpcClassId(classId);
            npcClassAttr.setValue(npcClassAttrVo.getValue());
            npcClassAttr.setIsBase(DataUtil.boolean2Integer(npcClassAttrVo.getIsBase()));
            npcClassAttr.setGmtModified(DateUtil.getNowTime());
            if(npcClassAttrVos.get(i).getId() != null) {
                // 更新 npc class attr
                npcClassAttr.setId(npcClassAttrVos.get(i).getId());
                npcClassAttrDao.update(npcClassAttr);
                // 更新npc instance attr 名称
                this.updateNpcInstanceAttr(npcClassAttr.getId(), npcClassAttr.getName());
            }else {
                // 插入npc class attr
                npcClassAttr.setGmtCreate(DateUtil.getNowTime());
                npcClassAttrDao.insert(npcClassAttr);
                // 插入npc instance attr
                this.insertNpcInstanceAttr(classId, npcClassAttr);
            }
        }
        return null;
    }


    public Result deletePlotBranchRelateByNpcInstanceId(Long npcInstanceId) {
            List<PlotBranchRelate> plotBranchRelates = plotBranchRelateDao.getAllByInstanceId(npcInstanceId, new Integer(1));
            for(int i=0; i<plotBranchRelates.size(); i++) {
                PlotBranchRelate plotBranchRelate = plotBranchRelates.get(i);
                // 删除属性更新的属性
                plotBranchRelateAttrDao.deleteByRelateId(plotBranchRelate.getId());
                // 删除属性更新的对象
                plotBranchRelateDao.delete(plotBranchRelate.getId());
            }
            return null;
        }
        // 删除 npc instance
        public Result serviceDeleteNpcInstance(Long npcInstanceId) {
            // 删除实例
            npcInstanceDao.delete(npcInstanceId);
            // 删除实例属性
            npcInstanceAttrDao.deleteByNpcInstanceId(npcInstanceId);
            // 清空物品类所属角色属性
            goodsClassAttrDao.emptyNpcInstanceId(npcInstanceId.toString());
            // 清空物品实例所属角色属性
            goodsInstanceAttrDao.emptyNpcInstanceId(npcInstanceId.toString());
            // 剧情前置条件删除
            plotBranchPremiseDao.deleteByInstanceId(new Integer(1), npcInstanceId);
            // 剧情更新答案删除
            this.deletePlotBranchRelateByNpcInstanceId(npcInstanceId);
            return null;
        }

    public Result updatePlotRelateNpcInstanceName(Long npcInstanceId, String npcInstanceName) {
        List<PlotBranchRelate> plotBranchRelates = plotBranchRelateDao.getAllByInstanceId(npcInstanceId, new Integer(1));
        for(int i=0; i<plotBranchRelates.size(); i++) {
            PlotBranchRelate plotBranchRelate = plotBranchRelates.get(i);
            plotBranchRelate.setInstanceName(npcInstanceName);
            plotBranchRelateDao.update(plotBranchRelate);
        }
        return null;
    }

    @Transactional
    public void deleteBranchRelate(Long id) {
            plotBranchRelateAttrDao.deleteByRelateId(id);
            plotBranchRelateDao.delete(id);
    }
    @Transactional
    public void deleteBranch(Long id) {
        plotBranchMatchFieldDao.deleteByPlotBranchId(id);
        plotBranchPremiseDao.deleteByPlotBranchId(id);
        List<PlotBranchRelate> plotBranchRelates = plotBranchRelateDao.getAllPlotBranchRelateByBranchId(id);
        for(int z=0; z<plotBranchRelates.size(); z++) {
            this.deleteBranchRelate(plotBranchRelates.get(z).getId());
        }
        plotBranchDao.delete(id);
    }

    // 删除游戏下的所有全局变量
        globalAttrDao.deleteGlobalAttrByProjectId(id);
        List<GoodsClass> goodsClasss = goodsClassDao.getAllByProjectId(id);
        for(int i=0; i<goodsClasss.size(); i++) {
            goodsController.serviceDeleteGoodsClass(goodsClasss.get(i).getId());
        }
        // 删除游戏下的所有地点
        placeDao.deletePlaceByProjectId(id);
        List<NpcClass> NpcClasss = npcClassDao.getAllByProjectId(id);
        for(int i=0; i<NpcClasss.size(); i++) {
            npcController.serviceDeleteNpcClass(NpcClasss.get(i).getId());
        }
        List<PlotCategory> plotCategorys = plotCategoryDao.getAllByGameProjectId(id);
        for(int i=0; i<plotCategorys.size(); i++) {
            plotController.deletePlotByCategoryId(plotCategorys.get(i).getId());
        }
        gameProjectDao.delete(id);
        gameProjectLabelDao.deleteAllByProjectId(id);
        result = Result.success();
        return result;


        Result  result = null;
        GameProjectPO gameProject = gameProjectDao.getOneByBotId(botId);
        if(gameProject == null) {
            result = Result.failure(ResultCode.RESULE_DATA_NONE);
            result.setMsg("游戏不存在!");
            return result;
        }
        Long id = gameProject.getId();
        // 删除游戏下的所有全局变量
        globalAttrDao.deleteGlobalAttrByProjectId(id);
        List<GoodsClass> goodsClasss = goodsClassDao.getAllByProjectId(id);
        for(int i=0; i<goodsClasss.size(); i++) {
            goodsController.deleteGoodsClass(goodsClasss.get(i).getId());
        }
        // 删除游戏下的所有地点
        placeDao.deletePlaceByProjectId(id);
        List<NpcClass> NpcClasss = npcClassDao.getAllByProjectId(id);
        for(int i=0; i<NpcClasss.size(); i++) {
            npcController.serviceDeleteNpcClass(NpcClasss.get(i).getId());
        }
        List<PlotCategory> plotCategorys = plotCategoryDao.getAllByGameProjectId(id);
        for(int i=0; i<plotCategorys.size(); i++) {
            plotController.deletePlotByCategoryId(plotCategorys.get(i).getId());
        }
        gameProjectDao.delete(id);
        result = Result.success();
