package com.doublechaintech.retailscm.levelthreecategory;

import com.doublechaintech.retailscm.*;
import com.doublechaintech.retailscm.BaseViewPage;
import com.doublechaintech.retailscm.RetailscmUserContextImpl;
import com.doublechaintech.retailscm.iamservice.*;
import com.doublechaintech.retailscm.levelthreecategory.LevelThreeCategory;
import com.doublechaintech.retailscm.leveltwocategory.CandidateLevelTwoCategory;
import com.doublechaintech.retailscm.leveltwocategory.LevelTwoCategory;
import com.doublechaintech.retailscm.product.Product;
import com.doublechaintech.retailscm.secuser.SecUser;
import com.doublechaintech.retailscm.services.IamService;
import com.doublechaintech.retailscm.tree.*;
import com.doublechaintech.retailscm.treenode.*;
import com.doublechaintech.retailscm.userapp.UserApp;
import com.doublechaintech.retailscm.utils.ModelAssurance;
import com.terapico.caf.BlobObject;
import com.terapico.caf.DateTime;
import com.terapico.caf.Images;
import com.terapico.caf.Password;
import com.terapico.caf.baseelement.PlainText;
import com.terapico.caf.viewpage.SerializeScope;
import com.terapico.uccaf.BaseUserContext;
import com.terapico.utils.*;
import java.math.BigDecimal;
import java.util.*;
import com.doublechaintech.retailscm.search.Searcher;

public class LevelThreeCategoryManagerImpl extends CustomRetailscmCheckerManager
    implements LevelThreeCategoryManager, BusinessHandler {

  // Only some of ods have such function

  // To test
  public BlobObject exportExcelFromList(
      RetailscmUserContext userContext, String id, String listName) throws Exception {

    Map<String, Object> tokens =
        LevelThreeCategoryTokens.start().withTokenFromListName(listName).done();
    LevelThreeCategory levelThreeCategory =
        (LevelThreeCategory) this.loadLevelThreeCategory(userContext, id, tokens);
    // to enrich reference object to let it show display name
    List<BaseEntity> entityListToNaming = levelThreeCategory.collectReferencesFromLists();
    levelThreeCategoryDaoOf(userContext).alias(entityListToNaming);

    return exportListToExcel(userContext, levelThreeCategory, listName);
  }

  @Override
  public BaseGridViewGenerator gridViewGenerator() {
    return new LevelThreeCategoryGridViewGenerator();
  }

  private static final String SERVICE_TYPE = "LevelThreeCategory";

  @Override
  public LevelThreeCategoryDAO daoOf(RetailscmUserContext userContext) {
    return levelThreeCategoryDaoOf(userContext);
  }

  @Override
  public String serviceFor() {
    return SERVICE_TYPE;
  }

  protected void throwExceptionWithMessage(String value) throws LevelThreeCategoryManagerException {

    Message message = new Message();
    message.setBody(value);
    throw new LevelThreeCategoryManagerException(message);
  }

  protected LevelThreeCategory saveLevelThreeCategory(
      RetailscmUserContext userContext, LevelThreeCategory levelThreeCategory, String[] tokensExpr)
      throws Exception {
    // return getLevelThreeCategoryDAO().save(levelThreeCategory, tokens);

    Map<String, Object> tokens = parseTokens(tokensExpr);

    return saveLevelThreeCategory(userContext, levelThreeCategory, tokens);
  }

  protected LevelThreeCategory saveLevelThreeCategoryDetail(
      RetailscmUserContext userContext, LevelThreeCategory levelThreeCategory) throws Exception {

    return saveLevelThreeCategory(userContext, levelThreeCategory, allTokens());
  }

  public LevelThreeCategory loadLevelThreeCategory(
      RetailscmUserContext userContext, String levelThreeCategoryId, String[] tokensExpr)
      throws Exception {

    checkerOf(userContext).checkIdOfLevelThreeCategory(levelThreeCategoryId);

    checkerOf(userContext).throwExceptionIfHasErrors(LevelThreeCategoryManagerException.class);

    Map<String, Object> tokens = parseTokens(tokensExpr);

    LevelThreeCategory levelThreeCategory =
        loadLevelThreeCategory(userContext, levelThreeCategoryId, tokens);
    // do some calc before sent to customer?
    return present(userContext, levelThreeCategory, tokens);
  }

  public LevelThreeCategory searchLevelThreeCategory(
      RetailscmUserContext userContext,
      String levelThreeCategoryId,
      String textToSearch,
      String[] tokensExpr)
      throws Exception {

    checkerOf(userContext).checkIdOfLevelThreeCategory(levelThreeCategoryId);

    checkerOf(userContext).throwExceptionIfHasErrors(LevelThreeCategoryManagerException.class);

    Map<String, Object> tokens =
        tokens()
            .allTokens()
            .searchEntireObjectText(tokens().startsWith(), textToSearch)
            .initWithArray(tokensExpr);

    LevelThreeCategory levelThreeCategory =
        loadLevelThreeCategory(userContext, levelThreeCategoryId, tokens);
    // do some calc before sent to customer?
    return present(userContext, levelThreeCategory, tokens);
  }

  protected LevelThreeCategory present(
      RetailscmUserContext userContext,
      LevelThreeCategory levelThreeCategory,
      Map<String, Object> tokens)
      throws Exception {

    addActions(userContext, levelThreeCategory, tokens);

    LevelThreeCategory levelThreeCategoryToPresent =
        levelThreeCategoryDaoOf(userContext).present(levelThreeCategory, tokens);

    List<BaseEntity> entityListToNaming = levelThreeCategoryToPresent.collectReferencesFromLists();
    levelThreeCategoryDaoOf(userContext).alias(entityListToNaming);

    renderActionForList(userContext, levelThreeCategory, tokens);

    return levelThreeCategoryToPresent;
  }

  public LevelThreeCategory loadLevelThreeCategoryDetail(
      RetailscmUserContext userContext, String levelThreeCategoryId) throws Exception {
    LevelThreeCategory levelThreeCategory =
        loadLevelThreeCategory(userContext, levelThreeCategoryId, allTokens());
    return present(userContext, levelThreeCategory, allTokens());
  }

  public Object prepareContextForUserApp(BaseUserContext userContext, Object targetUserApp)
      throws Exception {

    UserApp userApp = (UserApp) targetUserApp;
    return this.view((RetailscmUserContext) userContext, userApp.getAppId());
  }

  public Object view(RetailscmUserContext userContext, String levelThreeCategoryId)
      throws Exception {
    LevelThreeCategory levelThreeCategory =
        loadLevelThreeCategory(userContext, levelThreeCategoryId, viewTokens());
    markVisited(userContext, levelThreeCategory);
    return present(userContext, levelThreeCategory, viewTokens());
  }

  public Object summaryView(RetailscmUserContext userContext, String levelThreeCategoryId)
      throws Exception {
    LevelThreeCategory levelThreeCategory =
        loadLevelThreeCategory(userContext, levelThreeCategoryId, viewTokens());
    levelThreeCategory.summarySuffix();
    markVisited(userContext, levelThreeCategory);
    return present(userContext, levelThreeCategory, summaryTokens());
  }

  public Object analyze(RetailscmUserContext userContext, String levelThreeCategoryId)
      throws Exception {
    LevelThreeCategory levelThreeCategory =
        loadLevelThreeCategory(userContext, levelThreeCategoryId, analyzeTokens());
    markVisited(userContext, levelThreeCategory);
    return present(userContext, levelThreeCategory, analyzeTokens());
  }

  protected LevelThreeCategory saveLevelThreeCategory(
      RetailscmUserContext userContext,
      LevelThreeCategory levelThreeCategory,
      Map<String, Object> tokens)
      throws Exception {

    return levelThreeCategoryDaoOf(userContext).save(levelThreeCategory, tokens);
  }

  protected LevelThreeCategory loadLevelThreeCategory(
      RetailscmUserContext userContext, String levelThreeCategoryId, Map<String, Object> tokens)
      throws Exception {
    checkerOf(userContext).checkIdOfLevelThreeCategory(levelThreeCategoryId);

    checkerOf(userContext).throwExceptionIfHasErrors(LevelThreeCategoryManagerException.class);

    return levelThreeCategoryDaoOf(userContext).load(levelThreeCategoryId, tokens);
  }

  protected <T extends BaseEntity> void addActions(
      RetailscmUserContext userContext,
      LevelThreeCategory levelThreeCategory,
      Map<String, Object> tokens) {
    super.addActions(userContext, levelThreeCategory, tokens);
  } // end method of protected<T extends BaseEntity> void addActions(RetailscmUserContext
    // userContext, LevelThreeCategory levelThreeCategory, Map<String, Object> tokens){

  @Override
  public List<LevelThreeCategory> searchLevelThreeCategoryList(
      RetailscmUserContext ctx, LevelThreeCategoryRequest pRequest) {
    pRequest.setUserContext(ctx);
    List<LevelThreeCategory> list = daoOf(ctx).search(pRequest);
    Searcher.enhance(list, pRequest);
    return list;
  }

  @Override
  public LevelThreeCategory searchLevelThreeCategory(
      RetailscmUserContext ctx, LevelThreeCategoryRequest pRequest) {
    pRequest.limit(0, 1);
    List<LevelThreeCategory> list = searchLevelThreeCategoryList(ctx, pRequest);
    if (list == null || list.isEmpty()) {
      return null;
    }
    return list.get(0);
  }

  public LevelThreeCategory createLevelThreeCategory(
      RetailscmUserContext userContext, String parentCategoryId, String name) throws Exception {

    checkerOf(userContext).checkNameOfLevelThreeCategory(name);

    checkerOf(userContext).throwExceptionIfHasErrors(LevelThreeCategoryManagerException.class);

    LevelThreeCategory levelThreeCategory = createNewLevelThreeCategory();

    LevelTwoCategory parentCategory =
        loadLevelTwoCategory(userContext, parentCategoryId, emptyOptions());
    levelThreeCategory.setParentCategory(parentCategory);

    levelThreeCategory.setName(name);

    levelThreeCategory = saveLevelThreeCategory(userContext, levelThreeCategory, emptyOptions());

    onNewInstanceCreated(userContext, levelThreeCategory);
    return levelThreeCategory;
  }

  protected LevelThreeCategory createNewLevelThreeCategory() {

    return new LevelThreeCategory();
  }

  protected void checkParamsForUpdatingLevelThreeCategory(
      RetailscmUserContext userContext,
      String levelThreeCategoryId,
      int levelThreeCategoryVersion,
      String property,
      String newValueExpr,
      String[] tokensExpr)
      throws Exception {

    checkerOf(userContext).checkIdOfLevelThreeCategory(levelThreeCategoryId);
    checkerOf(userContext).checkVersionOfLevelThreeCategory(levelThreeCategoryVersion);

    if (LevelThreeCategory.NAME_PROPERTY.equals(property)) {

      checkerOf(userContext).checkNameOfLevelThreeCategory(parseString(newValueExpr));
    }

    checkerOf(userContext).throwExceptionIfHasErrors(LevelThreeCategoryManagerException.class);
  }

  public LevelThreeCategory clone(RetailscmUserContext userContext, String fromLevelThreeCategoryId)
      throws Exception {

    return levelThreeCategoryDaoOf(userContext).clone(fromLevelThreeCategoryId, this.allTokens());
  }

  public LevelThreeCategory internalSaveLevelThreeCategory(
      RetailscmUserContext userContext, LevelThreeCategory levelThreeCategory) throws Exception {
    return internalSaveLevelThreeCategory(userContext, levelThreeCategory, allTokens());
  }

  public LevelThreeCategory internalSaveLevelThreeCategory(
      RetailscmUserContext userContext,
      LevelThreeCategory levelThreeCategory,
      Map<String, Object> options)
      throws Exception {
    // checkParamsForUpdatingLevelThreeCategory(userContext, levelThreeCategoryId,
    // levelThreeCategoryVersion, property, newValueExpr, tokensExpr);

    synchronized (levelThreeCategory) {
      // will be good when the levelThreeCategory loaded from this JVM process cache.
      // also good when there is a ram based DAO implementation
      // make changes to LevelThreeCategory.
      if (levelThreeCategory.isChanged()) {}

      // checkerOf(userContext).checkAndFixLevelThreeCategory(levelThreeCategory);
      levelThreeCategory = saveLevelThreeCategory(userContext, levelThreeCategory, options);
      return levelThreeCategory;
    }
  }

  public LevelThreeCategory updateLevelThreeCategory(
      RetailscmUserContext userContext,
      String levelThreeCategoryId,
      int levelThreeCategoryVersion,
      String property,
      String newValueExpr,
      String[] tokensExpr)
      throws Exception {
    checkParamsForUpdatingLevelThreeCategory(
        userContext,
        levelThreeCategoryId,
        levelThreeCategoryVersion,
        property,
        newValueExpr,
        tokensExpr);

    LevelThreeCategory levelThreeCategory =
        loadLevelThreeCategory(userContext, levelThreeCategoryId, allTokens());
    if (levelThreeCategory.getVersion() != levelThreeCategoryVersion) {
      String message =
          "The target version("
              + levelThreeCategory.getVersion()
              + ") is not equals to version("
              + levelThreeCategoryVersion
              + ") provided";
      throwExceptionWithMessage(message);
    }
    synchronized (levelThreeCategory) {
      // will be good when the levelThreeCategory loaded from this JVM process cache.
      // also good when there is a ram based DAO implementation
      // make changes to LevelThreeCategory.

      levelThreeCategory.changeProperty(property, newValueExpr);
      levelThreeCategory = saveLevelThreeCategory(userContext, levelThreeCategory, tokens().done());
      return present(userContext, levelThreeCategory, mergedAllTokens(tokensExpr));
      // return saveLevelThreeCategory(userContext, levelThreeCategory, tokens().done());
    }
  }

  public LevelThreeCategory updateLevelThreeCategoryProperty(
      RetailscmUserContext userContext,
      String levelThreeCategoryId,
      int levelThreeCategoryVersion,
      String property,
      String newValueExpr,
      String[] tokensExpr)
      throws Exception {
    checkParamsForUpdatingLevelThreeCategory(
        userContext,
        levelThreeCategoryId,
        levelThreeCategoryVersion,
        property,
        newValueExpr,
        tokensExpr);

    LevelThreeCategory levelThreeCategory =
        loadLevelThreeCategory(userContext, levelThreeCategoryId, allTokens());
    if (levelThreeCategory.getVersion() != levelThreeCategoryVersion) {
      String message =
          "The target version("
              + levelThreeCategory.getVersion()
              + ") is not equals to version("
              + levelThreeCategoryVersion
              + ") provided";
      throwExceptionWithMessage(message);
    }
    synchronized (levelThreeCategory) {
      // will be good when the levelThreeCategory loaded from this JVM process cache.
      // also good when there is a ram based DAO implementation
      // make changes to LevelThreeCategory.

      levelThreeCategory.changeProperty(property, newValueExpr);

      levelThreeCategory = saveLevelThreeCategory(userContext, levelThreeCategory, tokens().done());
      return present(userContext, levelThreeCategory, mergedAllTokens(tokensExpr));
      // return saveLevelThreeCategory(userContext, levelThreeCategory, tokens().done());
    }
  }

  protected Map<String, Object> emptyOptions() {
    return tokens().done();
  }

  protected LevelThreeCategoryTokens tokens() {
    return LevelThreeCategoryTokens.start();
  }

  protected Map<String, Object> parseTokens(String[] tokensExpr) {
    return tokens().initWithArray(tokensExpr);
  }

  protected Map<String, Object> allTokens() {
    return LevelThreeCategoryTokens.all();
  }

  protected Map<String, Object> analyzeTokens() {
    return tokens().allTokens().analyzeAllLists().done();
  }

  protected Map<String, Object> summaryTokens() {
    return tokens().allTokens().done();
  }

  protected Map<String, Object> viewTokens() {
    return tokens().allTokens().sortProductListWith(Product.ID_PROPERTY, sortDesc()).done();
  }

  protected Map<String, Object> mergedAllTokens(String[] tokens) {
    return LevelThreeCategoryTokens.mergeAll(tokens).done();
  }

  protected void checkParamsForTransferingAnotherParentCategory(
      RetailscmUserContext userContext, String levelThreeCategoryId, String anotherParentCategoryId)
      throws Exception {

    checkerOf(userContext).checkIdOfLevelThreeCategory(levelThreeCategoryId);
    checkerOf(userContext)
        .checkIdOfLevelTwoCategory(anotherParentCategoryId); // check for optional reference

    checkerOf(userContext).throwExceptionIfHasErrors(LevelThreeCategoryManagerException.class);
  }

  public LevelThreeCategory transferToAnotherParentCategory(
      RetailscmUserContext userContext, String levelThreeCategoryId, String anotherParentCategoryId)
      throws Exception {
    checkParamsForTransferingAnotherParentCategory(
        userContext, levelThreeCategoryId, anotherParentCategoryId);

    LevelThreeCategory levelThreeCategory =
        loadLevelThreeCategory(userContext, levelThreeCategoryId, allTokens());
    synchronized (levelThreeCategory) {
      // will be good when the levelThreeCategory loaded from this JVM process cache.
      // also good when there is a ram based DAO implementation
      LevelTwoCategory parentCategory =
          loadLevelTwoCategory(userContext, anotherParentCategoryId, emptyOptions());
      levelThreeCategory.updateParentCategory(parentCategory);

      levelThreeCategory = saveLevelThreeCategory(userContext, levelThreeCategory, emptyOptions());

      return present(userContext, levelThreeCategory, allTokens());
    }
  }

  public CandidateLevelTwoCategory requestCandidateParentCategory(
      RetailscmUserContext userContext, String ownerClass, String id, String filterKey, int pageNo)
      throws Exception {

    CandidateLevelTwoCategory result = new CandidateLevelTwoCategory();
    result.setOwnerClass(ownerClass);
    result.setOwnerId(id);
    result.setFilterKey(filterKey == null ? "" : filterKey.trim());
    result.setPageNo(pageNo);
    result.setValueFieldName("id");
    result.setDisplayFieldName("parentCategory");

    pageNo = Math.max(1, pageNo);
    int pageSize = 20;
    // requestCandidateProductForSkuAsOwner
    SmartList<LevelTwoCategory> candidateList =
        levelTwoCategoryDaoOf(userContext)
            .requestCandidateLevelTwoCategoryForLevelThreeCategory(
                userContext, ownerClass, id, filterKey, pageNo, pageSize);
    result.setCandidates(candidateList);
    int totalCount = candidateList.getTotalCount();
    result.setTotalPage(Math.max(1, (totalCount + pageSize - 1) / pageSize));
    return result;
  }

  // --------------------------------------------------------------

  protected LevelTwoCategory loadLevelTwoCategory(
      RetailscmUserContext userContext, String newParentCategoryId, Map<String, Object> options)
      throws Exception {

    return levelTwoCategoryDaoOf(userContext).load(newParentCategoryId, options);
  }

  // --------------------------------------------------------------

  public LevelThreeCategory forgetByAll(
      RetailscmUserContext userContext, String levelThreeCategoryId, int levelThreeCategoryVersion)
      throws Exception {
    return forgetByAllInternal(userContext, levelThreeCategoryId, levelThreeCategoryVersion);
  }

  protected LevelThreeCategory forgetByAllInternal(
      RetailscmUserContext userContext, String levelThreeCategoryId, int levelThreeCategoryVersion)
      throws Exception {

    return levelThreeCategoryDaoOf(userContext)
        .disconnectFromAll(levelThreeCategoryId, levelThreeCategoryVersion);
  }

  public int deleteAll(RetailscmUserContext userContext, String secureCode) throws Exception {
    /*
    if(!("dElEtEaLl".equals(secureCode))){
    	throw new LevelThreeCategoryManagerException("Your secure code is not right, please guess again");
    }
    return deleteAllInternal(userContext);
    */
    return 0;
  }

  protected void checkParamsForAddingProduct(
      RetailscmUserContext userContext,
      String levelThreeCategoryId,
      String name,
      String origin,
      String remark,
      String brand,
      String picture,
      String[] tokensExpr)
      throws Exception {

    checkerOf(userContext).checkIdOfLevelThreeCategory(levelThreeCategoryId);

    checkerOf(userContext).checkNameOfProduct(name);

    checkerOf(userContext).checkOriginOfProduct(origin);

    checkerOf(userContext).checkRemarkOfProduct(remark);

    checkerOf(userContext).checkBrandOfProduct(brand);

    checkerOf(userContext).checkPictureOfProduct(picture);

    checkerOf(userContext).throwExceptionIfHasErrors(LevelThreeCategoryManagerException.class);
  }

  public LevelThreeCategory addProduct(
      RetailscmUserContext userContext,
      String levelThreeCategoryId,
      String name,
      String origin,
      String remark,
      String brand,
      String picture,
      String[] tokensExpr)
      throws Exception {
    checkParamsForAddingProduct(
        userContext, levelThreeCategoryId, name, origin, remark, brand, picture, tokensExpr);

    Product product = createProduct(userContext, name, origin, remark, brand, picture);

    LevelThreeCategory levelThreeCategory =
        loadLevelThreeCategory(userContext, levelThreeCategoryId, emptyOptions());
    synchronized (levelThreeCategory) {
      // Will be good when the levelThreeCategory loaded from this JVM process cache.
      // Also good when there is a RAM based DAO implementation
      levelThreeCategory.addProduct(product);
      levelThreeCategory =
          saveLevelThreeCategory(
              userContext, levelThreeCategory, tokens().withProductList().done());

      productManagerOf(userContext).onNewInstanceCreated(userContext, product);
      return present(userContext, levelThreeCategory, mergedAllTokens(tokensExpr));
    }
  }

  protected void checkParamsForUpdatingProductProperties(
      RetailscmUserContext userContext,
      String levelThreeCategoryId,
      String id,
      String name,
      String origin,
      String remark,
      String brand,
      String picture,
      String[] tokensExpr)
      throws Exception {

    checkerOf(userContext).checkIdOfLevelThreeCategory(levelThreeCategoryId);
    checkerOf(userContext).checkIdOfProduct(id);

    checkerOf(userContext).checkNameOfProduct(name);
    checkerOf(userContext).checkOriginOfProduct(origin);
    checkerOf(userContext).checkRemarkOfProduct(remark);
    checkerOf(userContext).checkBrandOfProduct(brand);
    checkerOf(userContext).checkPictureOfProduct(picture);

    checkerOf(userContext).throwExceptionIfHasErrors(LevelThreeCategoryManagerException.class);
  }

  public LevelThreeCategory updateProductProperties(
      RetailscmUserContext userContext,
      String levelThreeCategoryId,
      String id,
      String name,
      String origin,
      String remark,
      String brand,
      String picture,
      String[] tokensExpr)
      throws Exception {
    checkParamsForUpdatingProductProperties(
        userContext, levelThreeCategoryId, id, name, origin, remark, brand, picture, tokensExpr);

    Map<String, Object> options =
        tokens()
            .allTokens()
            // .withProductListList()
            .searchProductListWith(Product.ID_PROPERTY, tokens().is(), id)
            .done();

    LevelThreeCategory levelThreeCategoryToUpdate =
        loadLevelThreeCategory(userContext, levelThreeCategoryId, options);

    if (levelThreeCategoryToUpdate.getProductList().isEmpty()) {
      throw new LevelThreeCategoryManagerException("Product is NOT FOUND with id: '" + id + "'");
    }

    Product item = levelThreeCategoryToUpdate.getProductList().first();
    beforeUpdateProductProperties(
        userContext,
        item,
        levelThreeCategoryId,
        id,
        name,
        origin,
        remark,
        brand,
        picture,
        tokensExpr);
    item.updateName(name);
    item.updateOrigin(origin);
    item.updateRemark(remark);
    item.updateBrand(brand);
    item.updatePicture(picture);

    // checkParamsForAddingProduct(userContext,levelThreeCategoryId,name, code, used,tokensExpr);
    LevelThreeCategory levelThreeCategory =
        saveLevelThreeCategory(
            userContext, levelThreeCategoryToUpdate, tokens().withProductList().done());
    synchronized (levelThreeCategory) {
      return present(userContext, levelThreeCategory, mergedAllTokens(tokensExpr));
    }
  }

  protected void beforeUpdateProductProperties(
      RetailscmUserContext userContext,
      Product item,
      String levelThreeCategoryId,
      String id,
      String name,
      String origin,
      String remark,
      String brand,
      String picture,
      String[] tokensExpr)
      throws Exception {
    // by default, nothing to do
  }

  protected Product createProduct(
      RetailscmUserContext userContext,
      String name,
      String origin,
      String remark,
      String brand,
      String picture)
      throws Exception {

    Product product = new Product();

    product.setName(name);
    product.setOrigin(origin);
    product.setRemark(remark);
    product.setBrand(brand);
    product.setPicture(picture);
    product.setLastUpdateTime(userContext.now());

    return product;
  }

  protected Product createIndexedProduct(String id, int version) {

    Product product = new Product();
    product.setId(id);
    product.setVersion(version);
    return product;
  }

  protected void checkParamsForRemovingProductList(
      RetailscmUserContext userContext,
      String levelThreeCategoryId,
      String productIds[],
      String[] tokensExpr)
      throws Exception {

    checkerOf(userContext).checkIdOfLevelThreeCategory(levelThreeCategoryId);
    for (String productIdItem : productIds) {
      checkerOf(userContext).checkIdOfProduct(productIdItem);
    }

    checkerOf(userContext).throwExceptionIfHasErrors(LevelThreeCategoryManagerException.class);
  }

  public LevelThreeCategory removeProductList(
      RetailscmUserContext userContext,
      String levelThreeCategoryId,
      String productIds[],
      String[] tokensExpr)
      throws Exception {

    checkParamsForRemovingProductList(userContext, levelThreeCategoryId, productIds, tokensExpr);

    LevelThreeCategory levelThreeCategory =
        loadLevelThreeCategory(userContext, levelThreeCategoryId, allTokens());
    synchronized (levelThreeCategory) {
      // Will be good when the levelThreeCategory loaded from this JVM process cache.
      // Also good when there is a RAM based DAO implementation
      levelThreeCategoryDaoOf(userContext)
          .planToRemoveProductList(levelThreeCategory, productIds, allTokens());
      levelThreeCategory =
          saveLevelThreeCategory(
              userContext, levelThreeCategory, tokens().withProductList().done());
      deleteRelationListInGraph(userContext, levelThreeCategory.getProductList());
      return present(userContext, levelThreeCategory, mergedAllTokens(tokensExpr));
    }
  }

  protected void checkParamsForRemovingProduct(
      RetailscmUserContext userContext,
      String levelThreeCategoryId,
      String productId,
      int productVersion,
      String[] tokensExpr)
      throws Exception {

    checkerOf(userContext).checkIdOfLevelThreeCategory(levelThreeCategoryId);
    checkerOf(userContext).checkIdOfProduct(productId);
    checkerOf(userContext).checkVersionOfProduct(productVersion);

    checkerOf(userContext).throwExceptionIfHasErrors(LevelThreeCategoryManagerException.class);
  }

  public LevelThreeCategory removeProduct(
      RetailscmUserContext userContext,
      String levelThreeCategoryId,
      String productId,
      int productVersion,
      String[] tokensExpr)
      throws Exception {

    checkParamsForRemovingProduct(
        userContext, levelThreeCategoryId, productId, productVersion, tokensExpr);

    Product product = createIndexedProduct(productId, productVersion);
    LevelThreeCategory levelThreeCategory =
        loadLevelThreeCategory(userContext, levelThreeCategoryId, allTokens());
    synchronized (levelThreeCategory) {
      // Will be good when the levelThreeCategory loaded from this JVM process cache.
      // Also good when there is a RAM based DAO implementation
      levelThreeCategory.removeProduct(product);
      levelThreeCategory =
          saveLevelThreeCategory(
              userContext, levelThreeCategory, tokens().withProductList().done());
      deleteRelationInGraph(userContext, product);
      return present(userContext, levelThreeCategory, mergedAllTokens(tokensExpr));
    }
  }

  protected void checkParamsForCopyingProduct(
      RetailscmUserContext userContext,
      String levelThreeCategoryId,
      String productId,
      int productVersion,
      String[] tokensExpr)
      throws Exception {

    checkerOf(userContext).checkIdOfLevelThreeCategory(levelThreeCategoryId);
    checkerOf(userContext).checkIdOfProduct(productId);
    checkerOf(userContext).checkVersionOfProduct(productVersion);

    checkerOf(userContext).throwExceptionIfHasErrors(LevelThreeCategoryManagerException.class);
  }

  public LevelThreeCategory copyProductFrom(
      RetailscmUserContext userContext,
      String levelThreeCategoryId,
      String productId,
      int productVersion,
      String[] tokensExpr)
      throws Exception {

    checkParamsForCopyingProduct(
        userContext, levelThreeCategoryId, productId, productVersion, tokensExpr);

    Product product = createIndexedProduct(productId, productVersion);
    LevelThreeCategory levelThreeCategory =
        loadLevelThreeCategory(userContext, levelThreeCategoryId, allTokens());
    synchronized (levelThreeCategory) {
      // Will be good when the levelThreeCategory loaded from this JVM process cache.
      // Also good when there is a RAM based DAO implementation

      product.updateLastUpdateTime(userContext.now());

      levelThreeCategory.copyProductFrom(product);
      levelThreeCategory =
          saveLevelThreeCategory(
              userContext, levelThreeCategory, tokens().withProductList().done());

      productManagerOf(userContext)
          .onNewInstanceCreated(
              userContext,
              (Product) levelThreeCategory.getFlexiableObjects().get(BaseEntity.COPIED_CHILD));
      return present(userContext, levelThreeCategory, mergedAllTokens(tokensExpr));
    }
  }

  protected void checkParamsForUpdatingProduct(
      RetailscmUserContext userContext,
      String levelThreeCategoryId,
      String productId,
      int productVersion,
      String property,
      String newValueExpr,
      String[] tokensExpr)
      throws Exception {

    checkerOf(userContext).checkIdOfLevelThreeCategory(levelThreeCategoryId);
    checkerOf(userContext).checkIdOfProduct(productId);
    checkerOf(userContext).checkVersionOfProduct(productVersion);

    if (Product.NAME_PROPERTY.equals(property)) {
      checkerOf(userContext).checkNameOfProduct(parseString(newValueExpr));
    }

    if (Product.ORIGIN_PROPERTY.equals(property)) {
      checkerOf(userContext).checkOriginOfProduct(parseString(newValueExpr));
    }

    if (Product.REMARK_PROPERTY.equals(property)) {
      checkerOf(userContext).checkRemarkOfProduct(parseString(newValueExpr));
    }

    if (Product.BRAND_PROPERTY.equals(property)) {
      checkerOf(userContext).checkBrandOfProduct(parseString(newValueExpr));
    }

    if (Product.PICTURE_PROPERTY.equals(property)) {
      checkerOf(userContext).checkPictureOfProduct(parseString(newValueExpr));
    }

    checkerOf(userContext).throwExceptionIfHasErrors(LevelThreeCategoryManagerException.class);
  }

  public LevelThreeCategory updateProduct(
      RetailscmUserContext userContext,
      String levelThreeCategoryId,
      String productId,
      int productVersion,
      String property,
      String newValueExpr,
      String[] tokensExpr)
      throws Exception {

    checkParamsForUpdatingProduct(
        userContext,
        levelThreeCategoryId,
        productId,
        productVersion,
        property,
        newValueExpr,
        tokensExpr);

    Map<String, Object> loadTokens =
        this.tokens()
            .withProductList()
            .searchProductListWith(Product.ID_PROPERTY, tokens().equals(), productId)
            .done();

    LevelThreeCategory levelThreeCategory =
        loadLevelThreeCategory(userContext, levelThreeCategoryId, loadTokens);

    synchronized (levelThreeCategory) {
      // Will be good when the levelThreeCategory loaded from this JVM process cache.
      // Also good when there is a RAM based DAO implementation
      // levelThreeCategory.removeProduct( product );
      // make changes to AcceleraterAccount.
      Product productIdVersionKey = createIndexedProduct(productId, productVersion);

      Product product = levelThreeCategory.findTheProduct(productIdVersionKey);
      if (product == null) {
        throw new LevelThreeCategoryManagerException(productId + " is NOT FOUND");
      }

      beforeUpdateProduct(
          userContext,
          product,
          levelThreeCategoryId,
          productId,
          productVersion,
          property,
          newValueExpr,
          tokensExpr);
      product.changeProperty(property, newValueExpr);
      product.updateLastUpdateTime(userContext.now());
      levelThreeCategory =
          saveLevelThreeCategory(
              userContext, levelThreeCategory, tokens().withProductList().done());
      productManagerOf(userContext).onUpdated(userContext, product, this, "updateProduct");
      return present(userContext, levelThreeCategory, mergedAllTokens(tokensExpr));
    }
  }

  /** if you has something need to do before update data from DB, override this */
  protected void beforeUpdateProduct(
      RetailscmUserContext userContext,
      Product existed,
      String levelThreeCategoryId,
      String productId,
      int productVersion,
      String property,
      String newValueExpr,
      String[] tokensExpr)
      throws Exception {}
  /*

  */

  public void onNewInstanceCreated(RetailscmUserContext userContext, LevelThreeCategory newCreated)
      throws Exception {
    ensureRelationInGraph(userContext, newCreated);
    sendCreationEvent(userContext, newCreated);
  }

  public void sendAllItems(RetailscmUserContext ctx) throws Exception {
    levelThreeCategoryDaoOf(ctx).loadAllAsStream().forEach(event -> sendInitEvent(ctx, event));
  }

  // -----------------------------------//  登录部分处理 \\-----------------------------------
  @Override
  protected BusinessHandler getLoginProcessBizHandler(RetailscmUserContextImpl userContext) {
    return this;
  }

  public void onAuthenticationFailed(
      RetailscmUserContext userContext,
      LoginContext loginContext,
      LoginResult loginResult,
      IdentificationHandler idHandler,
      BusinessHandler bizHandler)
      throws Exception {
    // by default, failed is failed, nothing can do
  }
  // when user authenticated success, but no sec_user related, this maybe a new user login from 3-rd
  // party service.
  public void onAuthenticateNewUserLogged(
      RetailscmUserContext userContext,
      LoginContext loginContext,
      LoginResult loginResult,
      IdentificationHandler idHandler,
      BusinessHandler bizHandler)
      throws Exception {
    // Generally speaking, when authenticated user logined, we will create a new account for
    // him/her.
    // you need do it like :
    // First, you should create new data such as:
    //   LevelThreeCategory newLevelThreeCategory = this.createLevelThreeCategory(userContext, ...
    // Next, create a sec-user in your business way:
    //   SecUser secUser = secUserManagerOf(userContext).createSecUser(userContext, login, mobile
    // ...
    // And set it into loginContext:
    //   loginContext.getLoginTarget().setSecUser(secUser);
    // Next, create an user-app to connect secUser and newLevelThreeCategory
    //   UserApp uerApp = userAppManagerOf(userContext).createUserApp(userContext, secUser.getId(),
    // ...
    // Also, set it into loginContext:
    //   loginContext.getLoginTarget().setUserApp(userApp);
    // and in most case, this should be considered as "login success"
    //   loginResult.setSuccess(true);
    //
    // Since many of detailed info were depending business requirement, So,
    throw new Exception("请重载函数onAuthenticateNewUserLogged()以处理新用户登录");
  }

  protected SmartList<UserApp> getRelatedUserAppList(
      RetailscmUserContext userContext, SecUser secUser) {
    MultipleAccessKey key = new MultipleAccessKey();
    key.put(UserApp.SEC_USER_PROPERTY, secUser.getId());
    key.put(UserApp.APP_TYPE_PROPERTY, LevelThreeCategory.INTERNAL_TYPE);
    SmartList<UserApp> userApps =
        userContext.getDAOGroup().getUserAppDAO().findUserAppWithKey(key, EO);
    return userApps;
  }
  // -----------------------------------\\  登录部分处理 //-----------------------------------

  // -----------------------------------// list-of-view 处理 \\-----------------------------------
  protected void enhanceForListOfView(
      RetailscmUserContext userContext, SmartList<LevelThreeCategory> list) throws Exception {
    if (list == null || list.isEmpty()) {
      return;
    }
    List<LevelTwoCategory> parentCategoryList =
        RetailscmBaseUtils.collectReferencedObjectWithType(
            userContext, list, LevelTwoCategory.class);
    userContext.getDAOGroup().enhanceList(parentCategoryList, LevelTwoCategory.class);
  }

  public Object listByParentCategory(RetailscmUserContext userContext, String parentCategoryId)
      throws Exception {
    return listPageByParentCategory(userContext, parentCategoryId, 0, 20);
  }

  @SuppressWarnings({"unchecked", "rawtypes"})
  public Object listPageByParentCategory(
      RetailscmUserContext userContext, String parentCategoryId, int start, int count)
      throws Exception {
    SmartList<LevelThreeCategory> list =
        levelThreeCategoryDaoOf(userContext)
            .findLevelThreeCategoryByParentCategory(
                parentCategoryId, start, count, new HashMap<>());
    enhanceForListOfView(userContext, list);
    RetailscmCommonListOfViewPage page = new RetailscmCommonListOfViewPage();
    page.setClassOfList(LevelThreeCategory.class);
    page.setContainerObject(LevelTwoCategory.withId(parentCategoryId));
    page.setRequestBeanName(this.getBeanName());
    page.setDataList((SmartList) list);
    page.setPageTitle("三级分类列表");
    page.setRequestName("listByParentCategory");
    page.setRequestOffset(start);
    page.setRequestLimit(count);
    page.setDisplayMode("auto");
    page.setLinkToUrl(
        TextUtil.encodeUrl(
            String.format("%s/listByParentCategory/%s/", getBeanName(), parentCategoryId)));

    page.assemblerContent(userContext, "listByParentCategory");
    return page.doRender(userContext);
  }

  // -----------------------------------\\ list-of-view 处理 //-----------------------------------v

  /**
   * miniprogram调用返回固定的detail class
   *
   * @return
   * @throws Exception
   */
  public Object wxappview(RetailscmUserContext userContext, String levelThreeCategoryId)
      throws Exception {
    SerializeScope vscope = SerializeScope.EXCLUDE().nothing();
    LevelThreeCategory merchantObj =
        (LevelThreeCategory) this.view(userContext, levelThreeCategoryId);
    String merchantObjId = levelThreeCategoryId;
    String linkToUrl = "levelThreeCategoryManager/wxappview/" + merchantObjId + "/";
    String pageTitle = "三级分类" + "详情";
    Map result = new HashMap();
    List propList = new ArrayList();
    List sections = new ArrayList();

    propList.add(
        MapUtil.put("id", "1-id")
            .put("fieldName", "id")
            .put("label", "ID")
            .put("type", "text")
            .put("linkToUrl", "")
            .put("displayMode", "{}")
            .into_map());
    result.put("id", merchantObj.getId());

    propList.add(
        MapUtil.put("id", "2-parentCategory")
            .put("fieldName", "parentCategory")
            .put("label", "父类")
            .put("type", "auto")
            .put("linkToUrl", "levelTwoCategoryManager/wxappview/:id/")
            .put(
                "displayMode",
                "{\"brief\":\"\",\"imageUrl\":\"\",\"name\":\"auto\",\"title\":\"name\",\"imageList\":\"\"}")
            .into_map());
    result.put("parentCategory", merchantObj.getParentCategory());

    propList.add(
        MapUtil.put("id", "3-name")
            .put("fieldName", "name")
            .put("label", "名称")
            .put("type", "text")
            .put("linkToUrl", "")
            .put("displayMode", "{}")
            .into_map());
    result.put("name", merchantObj.getName());

    // 处理 sectionList

    // 处理Section：productListSection
    Map productListSection =
        ListofUtils.buildSection(
            "productListSection",
            "产品列表",
            null,
            "",
            "__no_group",
            "productManager/listByParentCategory/" + merchantObjId + "/",
            "auto");
    sections.add(productListSection);

    result.put(
        "productListSection", ListofUtils.toShortList(merchantObj.getProductList(), "product"));

    result.put("propList", propList);
    result.put("sectionList", sections);
    result.put("pageTitle", pageTitle);
    result.put("linkToUrl", linkToUrl);

    vscope
        .field("propList", SerializeScope.EXCLUDE())
        .field("sectionList", SerializeScope.EXCLUDE())
        .field("pageTitle", SerializeScope.EXCLUDE())
        .field("linkToUrl", SerializeScope.EXCLUDE());
    userContext.forceResponseXClassHeader("com.terapico.appview.DetailPage");
    return BaseViewPage.serialize(result, vscope);
  }
}
