You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

279 lines
13KB

  1. package vn.azteam.tpf.service;
  2. import java.time.Instant;
  3. import java.util.List;
  4. import java.util.Objects;
  5. import java.util.Optional;
  6. import javax.persistence.EntityManager;
  7. import javax.persistence.Query;
  8. import javax.persistence.criteria.JoinType;
  9. import com.google.common.collect.Lists;
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12. import org.springframework.data.domain.Page;
  13. import org.springframework.data.domain.PageImpl;
  14. import org.springframework.data.domain.Pageable;
  15. import org.springframework.data.jpa.domain.Specification;
  16. import org.springframework.stereotype.Service;
  17. import org.springframework.transaction.annotation.Transactional;
  18. import io.github.jhipster.service.QueryService;
  19. import io.github.jhipster.service.filter.LongFilter;
  20. import org.springframework.web.multipart.MultipartFile;
  21. import vn.azteam.tpf.domain.TBCustomer;
  22. import vn.azteam.tpf.domain.*; // for static metamodels
  23. import vn.azteam.tpf.repository.TBCustomerRepository;
  24. import vn.azteam.tpf.repository.search.TBCustomerSearchRepository;
  25. import vn.azteam.tpf.service.dto.TBAddressDTO;
  26. import vn.azteam.tpf.service.dto.TBCustomerCriteria;
  27. import vn.azteam.tpf.service.dto.TBCustomerDTO;
  28. import vn.azteam.tpf.service.dto.UserDTO;
  29. import vn.azteam.tpf.service.mapper.TBCustomerMapper;
  30. /**
  31. * Service for executing complex queries for TBCustomer entities in the database.
  32. * The main input is a {@link TBCustomerCriteria} which gets converted to {@link Specification},
  33. * in a way that all the filters must apply.
  34. * It returns a {@link List} of {@link TBCustomerDTO} or a {@link Page} of {@link TBCustomerDTO} which fulfills the criteria.
  35. */
  36. @Service
  37. @Transactional(readOnly = true)
  38. public class TBCustomerQueryService extends QueryService<TBCustomer> {
  39. private final Logger log = LoggerFactory.getLogger(TBCustomerQueryService.class);
  40. private final TBCustomerRepository tBCustomerRepository;
  41. private final TBCustomerMapper tBCustomerMapper;
  42. private final TBCustomerSearchRepository tBCustomerSearchRepository;
  43. private final FileStorageService fileStorageService;
  44. private final TBCustomerService tBCustomerService;
  45. private final TBAddressService tBAddressService;
  46. private final UserService userService;
  47. private final EntityManager em;
  48. public TBCustomerQueryService(TBCustomerRepository tBCustomerRepository, TBCustomerMapper tBCustomerMapper, TBCustomerSearchRepository tBCustomerSearchRepository, FileStorageService fileStorageService, TBCustomerService tBCustomerService, TBAddressService tBAddressService, UserService userService, EntityManager em) {
  49. this.tBCustomerRepository = tBCustomerRepository;
  50. this.tBCustomerMapper = tBCustomerMapper;
  51. this.tBCustomerSearchRepository = tBCustomerSearchRepository;
  52. this.fileStorageService = fileStorageService;
  53. this.tBCustomerService = tBCustomerService;
  54. this.tBAddressService = tBAddressService;
  55. this.userService = userService;
  56. this.em = em;
  57. }
  58. /**
  59. * Return a {@link List} of {@link TBCustomerDTO} which matches the criteria from the database
  60. * @param criteria The object which holds all the filters, which the entities should match.
  61. * @return the matching entities.
  62. */
  63. @Transactional(readOnly = true)
  64. public List<TBCustomerDTO> findByCriteria(TBCustomerCriteria criteria) {
  65. log.debug("find by criteria : {}", criteria);
  66. final Specification<TBCustomer> specification = createSpecification(criteria);
  67. return tBCustomerMapper.toDto(tBCustomerRepository.findAll(specification));
  68. }
  69. /**
  70. * Return a {@link Page} of {@link TBCustomerDTO} which matches the criteria from the database
  71. * @param criteria The object which holds all the filters, which the entities should match.
  72. * @param page The page, which should be returned.
  73. * @return the matching entities.
  74. */
  75. @Transactional(readOnly = true)
  76. public Page<TBCustomerDTO> findByCriteria(TBCustomerCriteria criteria, Pageable page) {
  77. log.debug("find by criteria : {}, page: {}", criteria, page);
  78. final Specification<TBCustomer> specification = createSpecification(criteria);
  79. return tBCustomerRepository.findAll(specification, page)
  80. .map(tBCustomerMapper::toDto);
  81. }
  82. /**
  83. * Return the number of matching entities in the database
  84. * @param criteria The object which holds all the filters, which the entities should match.
  85. * @return the number of matching entities.
  86. */
  87. @Transactional(readOnly = true)
  88. public long countByCriteria(TBCustomerCriteria criteria) {
  89. log.debug("count by criteria : {}", criteria);
  90. final Specification<TBCustomer> specification = createSpecification(criteria);
  91. return tBCustomerRepository.count(specification);
  92. }
  93. /**
  94. * Function to convert TBCustomerCriteria to a {@link Specification}
  95. */
  96. private Specification<TBCustomer> createSpecification(TBCustomerCriteria criteria) {
  97. Specification<TBCustomer> specification = Specification.where(null);
  98. if (criteria != null) {
  99. UserDTO currentUser = userService.getCurrentUserDTO().get();
  100. if (criteria.getId() != null) {
  101. specification = specification.and(buildSpecification(criteria.getId(), TBCustomer_.id));
  102. }
  103. if (criteria.getName() != null) {
  104. specification = specification.and(buildStringSpecification(criteria.getName(), TBCustomer_.name));
  105. }
  106. if (criteria.getPhone() != null) {
  107. specification = specification.and(buildStringSpecification(criteria.getPhone(), TBCustomer_.phone));
  108. }
  109. if (criteria.getEmail() != null) {
  110. specification = specification.and(buildStringSpecification(criteria.getEmail(), TBCustomer_.email));
  111. }
  112. if (criteria.getSlugName() != null) {
  113. specification = specification.and(buildStringSpecification(criteria.getSlugName(), TBCustomer_.slugName));
  114. }
  115. if (criteria.getAreaM2() != null) {
  116. specification = specification.and(buildRangeSpecification(criteria.getAreaM2(), TBCustomer_.areaM2));
  117. }
  118. if (criteria.getIsActivated() != null) {
  119. specification = specification.and(buildRangeSpecification(criteria.getIsActivated(), TBCustomer_.isActivated));
  120. }
  121. if (criteria.getCreatedDate() != null) {
  122. specification = specification.and(buildRangeSpecification(criteria.getCreatedDate(), TBCustomer_.createdDate));
  123. }
  124. if (criteria.getModifiedDate() != null) {
  125. specification = specification.and(buildRangeSpecification(criteria.getModifiedDate(), TBCustomer_.modifiedDate));
  126. }
  127. if (criteria.getDeletedDate() != null) {
  128. specification = specification.and(buildRangeSpecification(criteria.getDeletedDate(), TBCustomer_.deletedDate));
  129. }
  130. if (criteria.getTbAddressId() != null) {
  131. specification = specification.and(buildSpecification(criteria.getTbAddressId(),
  132. root -> root.join(TBCustomer_.tbAddress, JoinType.LEFT).get(TBAddress_.id)));
  133. }
  134. if (criteria.getTbDashboardTypeId() != null) {
  135. specification = specification.and(buildSpecification(criteria.getTbDashboardTypeId(),
  136. root -> root.join(TBCustomer_.tbDashboardType, JoinType.LEFT).get(TBDashboardType_.id)));
  137. }
  138. if (criteria.getCreatedById() != null) {
  139. specification = specification.and(buildSpecification(criteria.getCreatedById(),
  140. root -> root.join(TBCustomer_.createdBy, JoinType.LEFT).get(TBDetailUser_.id)));
  141. }
  142. if (criteria.getModifiedById() != null) {
  143. specification = specification.and(buildSpecification(criteria.getModifiedById(),
  144. root -> root.join(TBCustomer_.modifiedBy, JoinType.LEFT).get(TBDetailUser_.id)));
  145. }
  146. if (criteria.getDeletedById() != null) {
  147. specification = specification.and(buildSpecification(criteria.getDeletedById(),
  148. root -> root.join(TBCustomer_.deletedBy, JoinType.LEFT).get(TBDetailUser_.id)));
  149. }
  150. if(!userService.isAllCustomerRole(currentUser.getRoleId())) {
  151. LongFilter customerIdFilter = new LongFilter();
  152. customerIdFilter.setEquals(currentUser.getCustomerId());
  153. specification = specification.and(buildSpecification(customerIdFilter,
  154. root -> root.get("id")));
  155. }
  156. }
  157. return specification;
  158. }
  159. @Transactional()
  160. public TBCustomerDTO createCustomer(TBCustomerDTO tBCustomerDTO){
  161. Optional<UserDTO> currentUser = userService.getCurrentUserDTO();
  162. TBAddressDTO tbAddressDTO = new TBAddressDTO();
  163. tbAddressDTO.setId(tBCustomerDTO.getTbAddressId());
  164. tbAddressDTO.setTbCountryId(tBCustomerDTO.getCountryId());
  165. tbAddressDTO.setTbCityId(tBCustomerDTO.getCityId());
  166. tbAddressDTO.setTbDistrictId(tBCustomerDTO.getDistrictId());
  167. tbAddressDTO.setTbWardId(tBCustomerDTO.getWardId());
  168. tbAddressDTO.setAddress(tBCustomerDTO.getAddress());
  169. TBAddressDTO savedTBAddressDTO = this.tBAddressService.save(tbAddressDTO);
  170. tBCustomerDTO.setCreatedById(currentUser.get().getId());
  171. tBCustomerDTO.setCreatedDate(Instant.now());
  172. tBCustomerDTO.setTbAddressId(savedTBAddressDTO.getId());
  173. return tBCustomerService.save(tBCustomerDTO);
  174. }
  175. @Transactional()
  176. public TBCustomerDTO updateCustomerWithCertImage(TBCustomerDTO tBCustomerDTO,
  177. MultipartFile file, String path){
  178. Optional<UserDTO> currentUser = userService.getCurrentUserDTO();
  179. String imagePath;
  180. if (null != file) {
  181. if (Objects.equals(file.getOriginalFilename(), "")) {
  182. tBCustomerDTO.setCertificationImage(tBCustomerDTO.getCertificationImage());
  183. } else {
  184. imagePath = this.fileStorageService.storeSingleFile(path, file);
  185. if (tBCustomerDTO.getCertificationImage() != null &&
  186. !tBCustomerDTO.getCertificationImage().equals("")) {
  187. tBCustomerDTO.setCertificationImage(imagePath);
  188. } else {
  189. tBCustomerDTO.setCertificationImage(imagePath);
  190. }
  191. }
  192. } else {
  193. tBCustomerDTO.setCertificationImage(tBCustomerDTO.getCertificationImage());
  194. }
  195. tBCustomerDTO.setModifiedById(currentUser.get().getId());
  196. tBCustomerDTO.setModifiedDate(Instant.now());
  197. return tBCustomerService.save(tBCustomerDTO);
  198. }
  199. @Transactional()
  200. public TBCustomerDTO updateCustomer(TBCustomerDTO tBCustomerDTO){
  201. Optional<UserDTO> currentUser = userService.getCurrentUserDTO();
  202. TBAddressDTO tbAddressDTO = new TBAddressDTO();
  203. Optional<TBCustomerDTO> dbTBCustomer = tBCustomerService.findOne(tBCustomerDTO.getId());
  204. if(dbTBCustomer.isPresent() && dbTBCustomer.get().getTbAddressId() != null)
  205. {
  206. Optional<TBAddressDTO> optionalTBAddressDTO = tBAddressService.findOne(dbTBCustomer.get().getTbAddressId());
  207. if (optionalTBAddressDTO.isPresent()) {
  208. tbAddressDTO = optionalTBAddressDTO.get();
  209. }
  210. }
  211. tbAddressDTO.setTbCountryId(tBCustomerDTO.getCountryId());
  212. tbAddressDTO.setTbCityId(tBCustomerDTO.getCityId());
  213. tbAddressDTO.setTbDistrictId(tBCustomerDTO.getDistrictId());
  214. tbAddressDTO.setTbWardId(tBCustomerDTO.getWardId());
  215. tbAddressDTO.setAddress(tBCustomerDTO.getAddress());
  216. TBAddressDTO savedTBAddressDTO = this.tBAddressService.save(tbAddressDTO);
  217. tBCustomerDTO.setModifiedById(currentUser.get().getId());
  218. tBCustomerDTO.setModifiedDate(Instant.now());
  219. tBCustomerDTO.setTbAddressId(savedTBAddressDTO.getId());
  220. return tBCustomerService.save(tBCustomerDTO);
  221. }
  222. public Page<TBCustomerDTO> searchCustomer(String query, String status, Pageable pageable) {
  223. String sqlString = "select c.*\n" +
  224. " from tb_customer as c\n" +
  225. " join tb_address as a on c.tb_address_id = a.id\n" +
  226. " where 1 = 1";
  227. if(query != null && !query.isEmpty()) {
  228. sqlString = sqlString + " and ( c.name like '%" + query + "%'\n" +
  229. " or c.phone like '%" + query + "%'\n" +
  230. " or c.email like '%" + query + "%'\n" +
  231. " or a.address like '%" + query + "%')\n";
  232. }
  233. if(status != null && !status.isEmpty()) {
  234. int statusNumber = Integer.parseInt(status);
  235. sqlString = sqlString + " and (c.status = " + statusNumber + ")";
  236. }
  237. Query tbCustomerQuery = em.createNativeQuery(sqlString, TBCustomer.class);
  238. List<TBCustomerDTO> tbCustomerDTOs = tBCustomerMapper.toDto(tbCustomerQuery.getResultList());
  239. int start = Math.toIntExact(pageable.getOffset());
  240. int end = Math.toIntExact((start + pageable.getPageSize()) > tbCustomerDTOs.size() ? tbCustomerDTOs.size() : (start + pageable.getPageSize()));
  241. if(tbCustomerDTOs.size() > start) {
  242. return new PageImpl<>(tbCustomerDTOs.subList(start, end), pageable, tbCustomerDTOs.size());
  243. }
  244. return new PageImpl<>(Lists.newArrayList(), pageable, tbCustomerDTOs.size());
  245. }
  246. }