Evra70

Untitled

May 22nd, 2020
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.18 KB | None | 0 0
  1. package org.jleaf.erp.sls.bo.tasksales;
  2.  
  3. import java.text.SimpleDateFormat;
  4. import java.util.Date;
  5.  
  6. import javax.persistence.Query;
  7.  
  8. import org.jleaf.common.CommonExceptionConstants;
  9. import org.jleaf.common.entity.User;
  10. import org.jleaf.core.BusinessFunction;
  11. import org.jleaf.core.BusinessTransaction;
  12. import org.jleaf.core.CoreException;
  13. import org.jleaf.core.DefaultBusinessTransaction;
  14. import org.jleaf.core.Dto;
  15. import org.jleaf.core.GeneralConstants;
  16. import org.jleaf.core.annotation.ErrorList;
  17. import org.jleaf.core.annotation.Info;
  18. import org.jleaf.core.annotation.InfoIn;
  19. import org.jleaf.core.annotation.InfoOut;
  20. import org.jleaf.core.dao.QueryBuilder;
  21. import org.jleaf.erp.master.MasterExceptionConstants;
  22. import org.jleaf.erp.sls.SalesConstants;
  23. import org.jleaf.erp.sls.SalesExceptionConstants;
  24. import org.jleaf.erp.sls.bo.tasksales.utils.DecodedImageFile;
  25. import org.jleaf.erp.sls.dao.SalesmanVisitPlanDao;
  26. import org.jleaf.erp.sls.dao.SalesmanVisitRealizationDao;
  27. import org.jleaf.erp.sls.entity.SalesmanVisitPlan;
  28. import org.jleaf.erp.sls.entity.SalesmanVisitRealization;
  29. import org.jleaf.erp.sls.entity.UserPartner;
  30. import org.jleaf.util.DateUtil;
  31. import org.jleaf.util.GsonUtil;
  32. import org.jleaf.util.ValidationUtil;
  33. import org.slf4j.Logger;
  34. import org.slf4j.LoggerFactory;
  35. import org.springframework.beans.factory.annotation.Autowired;
  36. import org.springframework.beans.factory.annotation.Qualifier;
  37. import org.springframework.core.env.Environment;
  38. import org.springframework.stereotype.Service;
  39.  
  40.  
  41. /**
  42.  *
  43.  * @author WTC, Jul 4, 2013
  44.  *
  45.  */
  46. //@formatter:off
  47. @Service
  48. @InfoIn(value = {
  49.         @Info(name = "userLoginId", description = "user Login Id", type = Long.class),
  50.         @Info(name = "roleLoginId", description = "role Login Id", type = Long.class),
  51.         @Info(name = "tenantLoginId", description = "tenant Login Id", type = Long.class),
  52.         @Info(name = "datetime", description = "datetime", type = String.class),
  53.  
  54.         @Info(name = "visitId", description = "visit Id", type = Long.class),
  55.         @Info(name = "customerId", description = "customer Id", type = Long.class),
  56.         @Info(name = "customerCode", description = "customer Code", type = String.class),
  57.         @Info(name = "image", description = "image", type = String.class),
  58.         @Info(name = "latitude", description = "latitude", type = String.class),
  59.         @Info(name = "longtitude", description = "longtitude", type = String.class),
  60.         @Info(name = "checkInTime", description = "check In Time", type = String.class),
  61.         @Info(name = "strategy", description = "strategy", type = String.class),
  62. })
  63. @InfoOut(value = {
  64.         @Info(name = "tenantId", description = "tenant Id", type = Long.class),
  65.         @Info(name = "salesmanId", description = "salesman Id", type = Long.class),
  66.         @Info(name = "customerId", description = "customer Id", type = Long.class),
  67.         @Info(name = "createUserId", description = "create User Id", type = Long.class),
  68.         @Info(name = "updateUserId", description = "update User Id", type = Long.class),
  69.         @Info(name = "version", description = "version", type = Long.class),
  70.         @Info(name = "salesmanVisitPlanId", description = "salesman Visit PlanId", type = Long.class),
  71.         @Info(name = "checkInTimeLocal", description = "check In Time Local", type = String.class),
  72.         @Info(name = "checkInTimeServer", description = "check In Time Server", type = String.class),
  73.         @Info(name = "checkInLongitude", description = "check In Longitude", type = String.class),
  74.         @Info(name = "checkInLatitude", description = "check In Latitude", type = String.class),
  75.         @Info(name = "visitDate", description = "visit Date", type = String.class),
  76.         @Info(name = "statusVisit", description = "status Visit", type = String.class),
  77.         @Info(name = "checkInPhotoFileName", description = "check In Photo File Name", type = String.class),
  78.         @Info(name = "createDatetime", description = "create Datetime", type = String.class),
  79.         @Info(name = "updateDatetime", description = "update Datetime", type = String.class),
  80. })
  81. @ErrorList(errorKeys = {
  82.         CommonExceptionConstants.TENANT_LOGIN_CANT_USED,
  83.         CommonExceptionConstants.OU_ID_NOT_FOUND,
  84.         CommonExceptionConstants.OU_NOT_LEGAL_ENTITY,
  85.         CommonExceptionConstants.OULEGAL_ID_NOT_FOUND,
  86.         MasterExceptionConstants.PARTNER_ID_NOT_FOUND,
  87.         MasterExceptionConstants.POLICY_PARTNER_AUTHORIZATION_FAILED,
  88. })
  89. //@formatter:on
  90. public class AddSalesmanVisitCheckIn extends DefaultBusinessTransaction implements BusinessTransaction {
  91.  
  92.     private static final Logger log = LoggerFactory.getLogger(AddSalesmanVisitCheckIn.class);
  93.    
  94.     @Autowired
  95.     private SalesmanVisitPlanDao salesmanVisitPlanDao;
  96.    
  97.     @Autowired
  98.     private SalesmanVisitRealizationDao salesmanVisitRalizationDao;
  99.    
  100.     @Autowired
  101.     Environment env;
  102.    
  103.     @Autowired
  104.     @Qualifier("findSalesmanVisitPlanById")
  105.     private BusinessFunction findSalesmanVisitPlanById;
  106.    
  107.     @Autowired
  108.     @Qualifier("findPartnerById")
  109.     private BusinessFunction findPartnerById;
  110.  
  111.     @Override
  112.     public String getDescription() {
  113.         return "Add Salesman Visit Check In";
  114.     }
  115.  
  116.     @SuppressWarnings("unchecked")
  117.     @Override
  118.     public Dto prepare(Dto inputDto, Dto originalDto) throws Exception {
  119. //        log.info("INPUT DTO prepare {} --> {}", inputDto);
  120.  
  121.         ValidationUtil.valDtoContainsKey(inputDto, "userLoginId");
  122.         ValidationUtil.valDtoContainsKey(inputDto, "tenantLoginId");
  123.         ValidationUtil.valDtoContainsKey(inputDto, "roleLoginId");
  124.         ValidationUtil.valDtoContainsKey(inputDto, "datetime");
  125.        
  126.         Long userId = inputDto.getLong("userLoginId");
  127.         Long tenantId = inputDto.getLong("tenantLoginId");
  128.         Long roleLoginId = inputDto.getLong("roleLoginId");    
  129.         String datetime = inputDto.getString("datetime");
  130.        
  131.         Long visitId  = inputDto.getLong("visitId");
  132.         String visitStrategy = inputDto.getString("strategy");
  133.        
  134.         Long customerId  = inputDto.getLong("customerId");
  135.         String customerCode = inputDto.getString("customerCode");
  136.         String image = inputDto.getString("image");
  137.         Double latitude = inputDto.getDouble("latitude");
  138.         Double longitude = inputDto.getDouble("longitude");
  139.         String checkInTime = inputDto.getString("checkInTime");
  140.         String checkInTimeServer = DateUtil.dateTimeNow();
  141.        
  142.         QueryBuilder builderUserPartner = new QueryBuilder();
  143.         builderUserPartner.add("SELECT A.partner_id ")
  144.             .add(" FROM ").add(UserPartner.TABLE_NAME).add(" A ")
  145.             .add(" INNER JOIN ").add(User.TABLE_NAME).add(" B ON A.user_id = B.user_id AND A.tenant_id =B.tenant_id")
  146.             .add(" WHERE B.user_id = :userId AND B.tenant_id = :tenantId");
  147.                
  148.         Query qUserPartner = salesmanVisitPlanDao.createNativeQuery(builderUserPartner.toString());
  149.         qUserPartner.setParameter("tenantId", tenantId);
  150.         qUserPartner.setParameter("userId", userId);
  151.        
  152.         String salesmanIdString = qUserPartner.getSingleResult().toString();
  153.         Long salesmanId = Long.valueOf(salesmanIdString) != null ? Long.valueOf(salesmanIdString) : -99l;
  154.        
  155.         Dto inputFindSalesmanVistPlanById = new Dto();
  156.         inputFindSalesmanVistPlanById.put("id", visitId);
  157.  
  158.         String now = DateUtil.dateTimeNow();
  159.         if( Long.valueOf(now) < Long.valueOf(checkInTime)) {
  160.             throw new CoreException(SalesExceptionConstants.CHECK_IN_TIME_INVALID);
  161.         }
  162.        
  163.         Dto inputFindSalesmanByIdDto = new Dto();
  164.         inputFindSalesmanByIdDto.put("id", salesmanId);
  165.         Dto salesmanDto = findPartnerById.execute(inputFindSalesmanByIdDto);
  166.         String salesmanCode = salesmanDto.getString("code");
  167.        
  168.         if (!GeneralConstants.YES.equals(salesmanDto.getString("active"))) {
  169.             throw new CoreException(SalesExceptionConstants.DATA_NOT_ACTIVE,"Salesman : ".concat(salesmanCode));
  170.         }
  171.        
  172.         Dto inputFindCustomerByIdDto = new Dto();
  173.         inputFindCustomerByIdDto.put("id", customerId);
  174.         Dto customerDto = findPartnerById.execute(inputFindCustomerByIdDto);
  175.        
  176.         if (!GeneralConstants.YES.equals(customerDto.getString("active"))) {
  177.             throw new CoreException(SalesExceptionConstants.DATA_NOT_ACTIVE,"Customer : ".concat(customerCode));
  178.         }
  179.        
  180.         Dto salesmanVisitPlanDto = findSalesmanVisitPlanById.execute(inputFindSalesmanVistPlanById);
  181.         String statusVisit = salesmanVisitPlanDto.getString("statusVisit");
  182.         if(!GeneralConstants.NO.equals(statusVisit) && !SalesConstants.VISIT_PENDING.equals(statusVisit)) {
  183.             throw new CoreException(SalesExceptionConstants.CANT_VISIT_TO_CUSTOMER,customerCode);
  184.         }
  185.        
  186.         salesmanVisitPlanDto.put("visitStrategy",visitStrategy);
  187.         salesmanVisitPlanDto.put("statusVisit",SalesConstants.IN_PROGRESS_TRANSACTION);
  188.         this.prepareUpdateAudit(salesmanVisitPlanDto, userId, datetime);
  189.         inputDto.put("salesmanVisitPlanDto", salesmanVisitPlanDto);
  190.        
  191.         //image
  192.         String[] verificationImage = image.split(",");
  193.         String imageEncoded = verificationImage[1];
  194.         String imageHome =  env.getProperty("jleaf.taskhub.imageHome");
  195.         String imageFullDir = imageHome.concat(SalesConstants.TASKSALES).concat("/");
  196.        
  197.         String imageName = DateUtil.dateNow().concat("/").concat(salesmanId.toString()) .concat("-")
  198.                 .concat(String.valueOf(System.currentTimeMillis())
  199.                 .concat(".jpeg"));
  200.        
  201.         image = DecodedImageFile.writeFile(imageEncoded, imageFullDir, imageName);
  202. //        log.info("IMAGE ENCODE {} --> {}", imageEncoded);
  203.            
  204.        
  205.         Dto salesmanVisitRealizationDto = new Dto();
  206.         salesmanVisitRealizationDto.put("salesmanVisitPlanId", visitId);
  207.         salesmanVisitRealizationDto.put("tenantId", tenantId);
  208.         salesmanVisitRealizationDto.put("salesmanId", salesmanId);
  209.         salesmanVisitRealizationDto.put("visitDate", salesmanVisitPlanDto.getString("visitDate"));
  210.         salesmanVisitRealizationDto.put("customerId", customerId);
  211.         salesmanVisitRealizationDto.put("checkInTimeLocal", checkInTime);
  212.         salesmanVisitRealizationDto.put("checkInTimeServer", checkInTimeServer);
  213.         salesmanVisitRealizationDto.put("checkInLatitude", latitude);
  214.         salesmanVisitRealizationDto.put("checkInLongitude", longitude);
  215.         salesmanVisitRealizationDto.put("checkInPhotoFileName", image);
  216.         salesmanVisitRealizationDto.put("statusVisit", SalesConstants.IN_PROGRESS_TRANSACTION);
  217.         this.prepareInsertAudit(salesmanVisitRealizationDto, userId, datetime);
  218.         this.prepareUpdateAudit(salesmanVisitRealizationDto, userId, datetime);
  219. //        log.info("SALESMAN VISIT REALIZATION DTO prepare {} --> {}", salesmanVisitRealizationDto);
  220.         inputDto.put("salesmanVisitRealizationDto", salesmanVisitRealizationDto);
  221.        
  222.         return null;
  223.     }
  224.  
  225.     @SuppressWarnings("unchecked")
  226.     @Override
  227.     public Dto process(Dto inputDto, Dto originalDto) throws Exception {
  228. //        log.info("INPUT DTO proses {} --> {}", inputDto);
  229.        
  230.         Dto outputDto = new Dto();
  231.         Dto salesmanVisitPlanDto = inputDto.getDto("salesmanVisitPlanDto");
  232.         log.info("SALESMAN VISIT PLAN DTO proses {} --> {}", salesmanVisitPlanDto);
  233.         Dto salesmanVisitRealizationDto = inputDto.getDto("salesmanVisitRealizationDto");
  234.         log.info("SALESMAN VISIT REALIZATION DTO proses {} --> {}", salesmanVisitRealizationDto);
  235.        
  236.         SalesmanVisitPlan salesmanVisitPlan = GsonUtil.fromDto(salesmanVisitPlanDto, SalesmanVisitPlan.class);
  237.         salesmanVisitPlan = salesmanVisitPlanDao.merge(salesmanVisitPlan.getId(), salesmanVisitPlan);
  238.        
  239.         SalesmanVisitRealization salesmanVisitRealization = GsonUtil.fromDto(salesmanVisitRealizationDto, SalesmanVisitRealization.class);
  240.         salesmanVisitRalizationDao.persist(salesmanVisitRealization);
  241.                
  242.         outputDto = new Dto(salesmanVisitRealization);
  243.         return outputDto;
  244.     }
  245. }
Add Comment
Please, Sign In to add comment