condition单表查询(重写updatePatch接口)

updatePatch方法

 @Override
    public int updatePatch(Object model, Object key) {
        //后台已经封装 CompanyCapTable的po类,将请求参数model强转为CompanyCapTable类型
        CompanyCapTable companyCapTable = ((CompanyCapTable) model); 
       //判断如果BizType是company,则BizId=companyId。BizType类里已经封装company
        if (BizType.COMPANY.equals(companyCapTable.getBizType())) {
       //生成condition用于直接单表查询,CompanyInfoRela.class为要查询的表格
            Condition condition = new Condition(CompanyInfoRela.class);
       //定义condition的查询条件,createCriteria相当于where,后面的and都是查询条件
            condition.createCriteria().andEqualTo("companyId",companyCapTable.getBizId())
                                      .andEqualTo("relaType", "self");
      //需要查询表对应的service层接口来执行查询companyInfoRelaService,将condition条件放进去
            List<CompanyInfoRela> list = companyInfoRelaService.queryByCondition(condition);
     //遍历集合,将查询结果赋值给当前所在表,companyCatTable
            String bizId = list.iterator().next().getBizId();
            String bizType = list.iterator().next().getBizType();
            companyCapTable.setCompanyId(companyCapTable.getBizId());
            companyCapTable.setBizId(bizId);
            companyCapTable.setBizType(bizType);
       //CapId为主键,判断主键是否存在,如果不存在为新增,存在为编辑
            if (companyCapTable.getCapId() == null) {
        //给head字段赋值,调用下面的compareCapDate方法        companyCapTable.setHead(this.compareCapDate(companyCapTable.getCompanyId(), bizId, bizType, companyCapTable.getCapDate()));
       //调用新增方法,因为继承了父类,没有重写add方法,this执行的为父类方法
                this.add(companyCapTable);
            } else {
                companyCapTable.setHead(this.compareCapDate(companyCapTable.getCompanyId(), bizId, bizType, companyCapTable.getCapDate()));
      //因为本方法属于重写updatePatch方法,如果用this为调用当前方法,所以用super才能调父类的
                super.updatePatch(companyCapTable, companyCapTable.getCapId());
            }
        } else {
    //如果bizType不为company,则需要跟着bizId和bizType去CompanyInfoRela中查询companyId
            Condition condition = new Condition(CompanyInfoRela.class);
            condition.createCriteria().andEqualTo("bizId", companyCapTable.getBizId())
                .andEqualTo("bizType", companyCapTable.getBizType())
                .andEqualTo("relaType", "self");
  //将CompanyInfoRela表中查询companyId赋值回本表
            String companyId = companyInfoRelaService.queryByCondition(condition).iterator().next().getCompanyId();
            companyCapTable.setCompanyId(companyId);
            if (companyCapTable.getCapId() == null) {
                companyCapTable.setHead(this.compareCapDate(companyId, companyCapTable.getBizId(), companyCapTable.getBizType(), companyCapTable.getCapDate()));
                this.add(companyCapTable);
            } else {
                companyCapTable.setHead(this.compareCapDate(companyId, companyCapTable.getBizId(), companyCapTable.getBizType(), companyCapTable.getCapDate()));
                super.updatePatch(companyCapTable, companyCapTable.getCapId());
            }

        }
//根据方法的返回值类型进行返回,与操作表的过程无关
        return super.updatePatch(model, key);
    }
compareCapDate方法
 //根据 updatePatch方法需要的业务给本方法传参,方便之后调用
private String compareCapDate(String companyId, String bizId, String bizType, Date capDate) {
        Condition condition = new Condition(CompanyCapTable.class);
        condition.createCriteria().andEqualTo("CompanyId", companyId)
            .andEqualTo("bizId", bizId)
            .andEqualTo("bizType", bizType)
//CommonConstant.YES为公司定义好的常量"Y"
            .andEqualTo("head", CommonConstant.YES);
//condition可以多次添加条件,根据capDate倒序排序   
        condition.orderBy("capDate").desc();
//将查询结构用集合接收
        List<CompanyCapTable> companyCapTables = this.queryByCondition(condition);
//判断companyCapTables不为空,CollUtil可以规避空指针,为胡涂语法
        if (CollUtil.isNotEmpty(companyCapTables)) {
//compareTo为对象比较大小,结果为0:相等;1:大于;-1:小于   get(0)是根据下标获取
            if (capDate != null && capDate.compareTo(companyCapTables.get(0).getCapDate()) >= 0) {
                for (CompanyCapTable companyCapTable : companyCapTables) {
//CommonConstant.NO为公司封装的常量"N"
                    companyCapTable.setHead(CommonConstant.NO);
//updateSelective 对括号内定义的内容进行批量修改
                    this.updateSelective(companyCapTable);
                }
            } else {
                return CommonConstant.NO;
            }
        }
        return CommonConstant.YES;
    }


版权声明:本文为zhangzhanpu原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。