/* (non-Javadoc)
   * @see com.cloud.erp.dao.SalesContractDao#persistenceSalesContract(com.cloud.erp.entities.table.SalesContract, java.util.Map, java.util.Map)
   */
  @Override
  public boolean persistenceSalesContract(
      SalesContract salesContract,
      Map<String, List<SalesContractEntry>> entries,
      Map<String, List<SalesContractScheme>> schemes) {
    // TODO Auto-generated method stub
    Integer userId = Constants.getCurrentUser().getUserId();
    if (null == salesContract.getInterId() || "".equals(salesContract.getInterId())) {
      salesContract.setCreated(new Date());
      salesContract.setCreater(userId);
      salesContract.setLastmod(new Date());
      salesContract.setModifier(userId);
      salesContract.setStatus(Constants.PERSISTENCE_STATUS);
      baseDao.save(salesContract);
    } else {
      salesContract.setLastmod(new Date());
      salesContract.setModifier(userId);
      baseDao.update(salesContract);
    }
    this.addEntry(salesContract, entries.get(Constants.ENTRY_LIST_TYPE_ADD));
    this.updateEntry(entries.get(Constants.ENTRY_LIST_TYPE_UPD));
    this.delEntry(entries.get(Constants.ENTRY_LIST_TYPE_DEL));

    this.addScheme(salesContract, schemes.get(Constants.ENTRY_LIST_TYPE_ADD));
    this.updateScheme(schemes.get(Constants.ENTRY_LIST_TYPE_UPD));
    this.delScheme(schemes.get(Constants.ENTRY_LIST_TYPE_DEL));

    return true;
  }
 @Override
 public List<SalesContractEntry> findSalesContractEntriesById(Integer contractId) {
   // TODO Auto-generated method stub
   String hql =
       "from SalesContractEntry t where t.status='A' and t.contract.interId=" + contractId;
   return baseDao.find(hql);
 }
 /* (non-Javadoc)
  * @see com.cloud.erp.dao.SalesContractDao#getCount(java.util.Map, com.cloud.erp.utils.PageUtil)
  */
 @Override
 public long getCount(Map<String, Object> params, PageUtil pageUtil) {
   String dataRule = dataPermissionAssign.translate("SalesContract");
   String hql = "select count(*) from SalesContract t where t.status='A'" + dataRule;
   hql += Constants.getSearchConditionsHQL("t", params);
   hql += Constants.getGradeSearchConditionsHQL("t", pageUtil);
   return baseDao.count(hql, params);
 }
 /* (non-Javadoc)
  * @see com.cloud.erp.dao.SalesContractDao#findSalesContracts(java.util.Map, com.cloud.erp.utils.PageUtil)
  */
 @Override
 public List<SalesContract> findSalesContracts(Map<String, Object> params, PageUtil pageUtil) {
   String dataRule = dataPermissionAssign.translate("SalesContract");
   String hql = "from SalesContract t where t.status='A'" + dataRule;
   hql += Constants.getSearchConditionsHQL("t", params);
   hql += Constants.getGradeSearchConditionsHQL("t", pageUtil);
   return baseDao.find(hql, params, pageUtil.getPage(), pageUtil.getRows());
 }
 private boolean updateScheme(List<SalesContractScheme> schemes) {
   Integer userId = Constants.getCurrentUser().getUserId();
   if (null != schemes && !schemes.isEmpty()) {
     for (SalesContractScheme salesContractScheme : schemes) {
       salesContractScheme.setLastmod(new Date());
       salesContractScheme.setModifier(userId);
       baseDao.update(salesContractScheme);
     }
   }
   return true;
 }
 private boolean updateEntry(List<SalesContractEntry> entries) {
   Integer userId = Constants.getCurrentUser().getUserId();
   if (null != entries && !entries.isEmpty()) {
     for (SalesContractEntry salesContractEntry : entries) {
       salesContractEntry.setLastmod(new Date());
       salesContractEntry.setModifier(userId);
       baseDao.update(salesContractEntry);
     }
   }
   return true;
 }
 private boolean delScheme(List<SalesContractScheme> schemes) {
   Integer userId = Constants.getCurrentUser().getUserId();
   if (null != schemes && !schemes.isEmpty()) {
     for (SalesContractScheme salesContractScheme : schemes) {
       salesContractScheme.setLastmod(new Date());
       salesContractScheme.setModifier(userId);
       salesContractScheme.setStatus(Constants.PERSISTENCE_DELETE_STATUS);
       baseDao.deleteToUpdate(salesContractScheme);
     }
   }
   return true;
 }
 private boolean delEntry(List<SalesContractEntry> entries) {
   Integer userId = Constants.getCurrentUser().getUserId();
   if (null != entries && !entries.isEmpty()) {
     for (SalesContractEntry salesContractEntry : entries) {
       salesContractEntry.setLastmod(new Date());
       salesContractEntry.setModifier(userId);
       salesContractEntry.setStatus(Constants.PERSISTENCE_DELETE_STATUS);
       baseDao.deleteToUpdate(salesContractEntry);
     }
   }
   return true;
 }
 /* (non-Javadoc)
  * @see com.cloud.erp.dao.SalesContractDao#delSalesContract(java.lang.Integer)
  */
 @Override
 public boolean delSalesContract(Integer contractId) {
   // TODO Auto-generated method stub
   Integer userId = Constants.getCurrentUser().getUserId();
   String status = Constants.PERSISTENCE_DELETE_STATUS;
   Date date = new Date();
   SalesContract salesContract = (SalesContract) baseDao.get(SalesContract.class, contractId);
   salesContract.setLastmod(date);
   salesContract.setModifier(userId);
   salesContract.setStatus(status);
   baseDao.deleteToUpdate(salesContract);
   String hql =
       "from SalesContractEntry t where t.status='A' and t.contract.interId=" + contractId;
   List<SalesContractEntry> salesContractEntries = baseDao.find(hql);
   for (SalesContractEntry salesContractEntry : salesContractEntries) {
     salesContractEntry.setLastmod(date);
     salesContractEntry.setModifier(userId);
     salesContractEntry.setStatus(status);
     baseDao.deleteToUpdate(salesContractEntry);
   }
   hql = "from SalesContractScheme t where t.status='A' and t.contract.interId=" + contractId;
   List<SalesContractScheme> salesContractSchemes = baseDao.find(hql);
   for (SalesContractScheme salesContractScheme : salesContractSchemes) {
     salesContractScheme.setLastmod(date);
     salesContractScheme.setModifier(userId);
     salesContractScheme.setStatus(status);
     baseDao.deleteToUpdate(salesContractScheme);
   }
   return true;
 }
 @Override
 public boolean updateReference(String billNo, boolean mode) throws UpdateReferenceException {
   if (null == billNo || "".equals(billNo)) {
     return false;
   }
   String hql =
       "update SalesContract t set t.children = t.children + "
           + (mode ? 1 : -1)
           + " where t.billNo='"
           + billNo
           + "'";
   if (baseDao.executeUpdate(hql) > 0) {
     return true;
   } else {
     throw new UpdateReferenceException("更新引用异常");
   }
 }
 @Override
 public boolean updateSalesContract(SalesContract salesContract) {
   // TODO Auto-generated method stub
   baseDao.update(salesContract);
   return true;
 }
 @Override
 public SalesContract findSalesContractById(Integer contractId) {
   // TODO Auto-generated method stub
   return (SalesContract) baseDao.get(SalesContract.class, contractId);
 }