Commit 8f48260b authored by Phạm Duy Phi's avatar Phạm Duy Phi

phi pd commit: chinh sua tim kiem

parent 6b55d2d4
......@@ -6,9 +6,27 @@ import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.List;
public interface CustomerListMappingRepository extends JpaRepository<CustomerListMapping, Long> {
// ----------- customer ------------ //
@Modifying
@Query("delete from CustomerListMapping c where c.customerId=:p_customer_id and c.customerListId=:p_customer_list_id")
int deleteMappingByCustomerId(@Param("p_customer_id") Long p_customer_id, @Param("p_customer_list_id") Long p_customer_list_id);
@Modifying
@Query("delete from CustomerListMapping c where c.customerId in (:p_id) and c.customerListId =:p_customer_list_id")
int deleteMappingByCustomerIds(@Param("p_id") List<Long> p_id, @Param("p_customer_list_id") Long p_customer_list_id);
// ----------- customer list --------------- //
@Modifying
@Query("delete from CustomerListMapping c where c.customerListId=:p_customerListId")
int deleteMappingByCustomerListId(@Param("p_customerListId") Long p_customerListId);
@Modifying
@Query("delete from CustomerListMapping c where c.customerListId=:customerListId")
int deleteMappingByCustomerListId(@Param("customerListId") Long customerListId);
@Query("delete from CustomerListMapping c where c.customerListId in (:p_ids)")
int deleteMappingByCustomerListIds(@Param("p_ids") List<Long> p_ids);
}
......@@ -15,6 +15,10 @@ public interface CustomerListRepository extends JpaRepository<CustomerList, Long
CustomerList findByCustomerListCode(String customerListCode);
@Modifying
@Query("delete from CustomerList c where c.customerListId in (:ids)")
int deleteCustomerListIds(@Param("ids") List<Long> ids);
@Query("update CustomerList c set c.status = 0 where c.customerListId=:p_customerListId")
int deleteCustomerList(@Param("p_customerListId") Long p_customerListId);
@Modifying
@Query("update CustomerList c set c.status = 0 where c.customerListId in (:p_ids)")
int deleteCustomerListIds(@Param("p_ids") List<Long> p_ids);
}
......@@ -3,6 +3,8 @@ package com.viettel.campaign.service;
import com.viettel.campaign.web.dto.CustomerDTO;
import com.viettel.campaign.web.dto.CustomerListDTO;
import com.viettel.campaign.web.dto.ResultDTO;
import com.viettel.campaign.web.dto.request_dto.CampaignCustomerRequestDTO;
import com.viettel.campaign.web.dto.request_dto.DeleteCustomerRequestDTO;
import java.util.Date;
import java.util.List;
......@@ -16,9 +18,9 @@ public interface CustomerService {
ResultDTO createCustomer(CustomerDTO customerDTO);
ResultDTO deleteCustomer(CustomerDTO customerDTO);
ResultDTO deleteCustomer(DeleteCustomerRequestDTO deleteCustomerRequestDTO);
ResultDTO deleteIds(List<Long> ids);
ResultDTO deleteIds(DeleteCustomerRequestDTO deleteCustomerRequestDTO);
// ------------ customer list ------------ //
......@@ -32,5 +34,5 @@ public interface CustomerService {
ResultDTO deleteCustomerListIds(List<Long> ids);
ResultDTO searchCustomerList(String customerListCode, String customerListName, Date dateFrom, Date dateTo, int page, int pageSize, String sort);
ResultDTO searchCustomerList(CampaignCustomerRequestDTO campaignCustomerRequestDTO);
}
package com.viettel.campaign.service;
package com.viettel.campaign.service.impl;
import com.viettel.campaign.service.CampaignCompleteCodeService;
import com.viettel.campaign.utils.Constants;
import com.viettel.campaign.web.dto.CampaignCompleteCodeDTO;
import com.viettel.campaign.web.dto.ResultDTO;
......
......@@ -16,10 +16,16 @@ import com.viettel.campaign.mapper.CustomerMapper;
import com.viettel.campaign.model.Customer;
import com.viettel.campaign.repository.CustomerRepository;
import com.viettel.campaign.utils.DataUtil;
import com.viettel.campaign.web.dto.request_dto.CampaignCustomerRequestDTO;
import com.viettel.campaign.web.dto.request_dto.DeleteCustomerRequestDTO;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.hibernate.type.DateType;
import org.hibernate.type.LongType;
import org.hibernate.type.ShortType;
import org.hibernate.type.StringType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
......@@ -111,12 +117,13 @@ public class CustomerServiceImpl implements CustomerService {
}
@Override
public ResultDTO deleteCustomer(CustomerDTO customerDTO) {
public ResultDTO deleteCustomer(DeleteCustomerRequestDTO deleteCustomerRequestDTO) {
ResultDTO resultDTO = new ResultDTO();
try {
if (customerDTO != null) {
if (deleteCustomerRequestDTO != null) {
// delete
customerRepository.deleteById(customerDTO.getCustomerId());
// customerRepository.deleteById(customerDTO.getCustomerId());
customerListMappingRepository.deleteMappingByCustomerId(deleteCustomerRequestDTO.getCustomerId(), deleteCustomerRequestDTO.getCustomerListId());
resultDTO.setErrorCode(Constants.ApiErrorCode.SUCCESS);
resultDTO.setDescription(Constants.ApiErrorDesc.SUCCESS);
......@@ -133,11 +140,12 @@ public class CustomerServiceImpl implements CustomerService {
@Transactional
@Override
public ResultDTO deleteIds(List<Long> ids) {
public ResultDTO deleteIds(DeleteCustomerRequestDTO deleteCustomerRequestDTO) {
ResultDTO resultDTO = new ResultDTO();
try {
if (ids != null) {
customerRepository.deleteIds(ids);
if (deleteCustomerRequestDTO != null) {
// customerRepository.deleteIds(ids);
customerListMappingRepository.deleteMappingByCustomerIds(deleteCustomerRequestDTO.getCustomerIds(), deleteCustomerRequestDTO.getCustomerListId());
resultDTO.setErrorCode(Constants.ApiErrorCode.SUCCESS);
resultDTO.setDescription(Constants.ApiErrorDesc.SUCCESS);
} else {
......@@ -220,7 +228,7 @@ public class CustomerServiceImpl implements CustomerService {
if (customerListDTO != null) {
// update
customerList = customerListMapper.toPersistenceBean(customerListDTO);
customerList = customerListRepository.save(customerList);
customerListRepository.save(customerList);
resultDTO.setErrorCode(Constants.ApiErrorCode.SUCCESS);
resultDTO.setDescription(Constants.ApiErrorDesc.SUCCESS);
......@@ -239,14 +247,12 @@ public class CustomerServiceImpl implements CustomerService {
@Override
public ResultDTO deleteCustomerList(CustomerListDTO customerListDTO) {
ResultDTO resultDTO = new ResultDTO();
CustomerListMapper customerListMapper = new CustomerListMapper();
CustomerList customerList = customerListRepository.findCustomerListByCustomerListId(customerListDTO.getCustomerListId());
try {
// delete
if (DataUtil.isNullOrZero(campaignCustomerListRepository.campaignCount(customerListDTO.getCustomerListId()))) {
customerList = customerListMapper.toPersistenceBean(customerListDTO);
customerList = customerListRepository.save(customerList);
customerListRepository.deleteCustomerList(customerListDTO.getCustomerListId());
customerListMappingRepository.deleteMappingByCustomerListId(customerListDTO.getCustomerListId());
......@@ -272,6 +278,9 @@ public class CustomerServiceImpl implements CustomerService {
if (ids != null) {
if (DataUtil.isNullOrZero(campaignCustomerListRepository.campaignIdsCount(ids))) {
customerListRepository.deleteCustomerListIds(ids);
customerListMappingRepository.deleteMappingByCustomerListIds(ids);
resultDTO.setErrorCode(Constants.ApiErrorCode.SUCCESS);
resultDTO.setDescription(Constants.ApiErrorDesc.SUCCESS);
} else {
......@@ -289,70 +298,87 @@ public class CustomerServiceImpl implements CustomerService {
}
@Override
public ResultDTO searchCustomerList(String customerListCode, String customerListName, Date dateFrom, Date dateTo, int page, int pageSize, String sort) {
public ResultDTO searchCustomerList(CampaignCustomerRequestDTO campaignCustomerRequestDTO) {
ResultDTO resultDTO = new ResultDTO();
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
if (DataUtil.isNullOrEmpty(campaignCustomerRequestDTO.getCompanySiteId())) {
resultDTO.setErrorCode(Constants.ApiErrorCode.ERROR);
resultDTO.setDescription(Constants.ApiErrorDesc.ERROR);
return resultDTO;
}
try {
StringBuilder sqlStrBuilder = new StringBuilder();
sqlStrBuilder.append(SQLBuilder.getSqlQueryById(SQLBuilder.SQL_MODULE_CAMPAIGN_MNG, "search-campaign-customer-by-params"));
if (!customerListCode.equals("undefined")) {
if (DataUtil.isNullOrEmpty(campaignCustomerRequestDTO.getCustomerListCode())) {
sqlStrBuilder.append(" AND CUSTOMER_LIST_CODE LIKE :p_list_code");
}
if (!customerListName.equals("undefined")) {
if (DataUtil.isNullOrEmpty(campaignCustomerRequestDTO.getCustomerListName())) {
sqlStrBuilder.append(" AND CUSTOMER_LIST_NAME LIKE :p_list_name");
}
SQLQuery query = session.createSQLQuery(sqlStrBuilder.toString());
query.setParameter("p_date_from", dateFrom);
query.setParameter("p_date_to", dateTo);
query.setParameter("p_date_from", campaignCustomerRequestDTO.getConvertedDateFrom());
query.setParameter("p_date_to", campaignCustomerRequestDTO.getConvertedDateTo());
if (!customerListCode.equals("undefined")) {
if (!campaignCustomerRequestDTO.getCustomerListCode().equals("undefined")) {
query.setParameter("p_list_code", "%" +
customerListCode
campaignCustomerRequestDTO.getCustomerListCode()
.replace("\\", "\\\\")
.replaceAll("%", "\\\\%")
.replaceAll("_", "\\\\_")
+ "%");
}
if (!customerListName.equals("undefined")) {
if (!campaignCustomerRequestDTO.getCustomerListName().equals("undefined")) {
query.setParameter("p_list_name", "%" +
customerListName
campaignCustomerRequestDTO.getCustomerListName()
.replace("\\", "\\\\")
.replaceAll("%", "\\\\%")
.replaceAll("_", "\\\\_")
+ "%");
}
// Pageable pageable = PageRequest.of(page, pageSize, Sort.by(sort));
// Page<CustomerList> pc = customerListRepository.search();
//
// result.put("totalItem", pc.getTotalElements());
// result.put("searchList", pc.iterator());
query.addScalar("customerListId", new LongType());
query.addScalar("companySiteId", new LongType());
query.addScalar("customerListCode", new StringType());
query.addScalar("customerListName", new StringType());
query.addScalar("status", new ShortType());
query.addScalar("createBy", new StringType());
query.addScalar("createAt", new DateType());
query.addScalar("updateBy", new StringType());
query.addScalar("updateAt", new DateType());
query.addScalar("source", new StringType());
query.addScalar("deptCreate", new StringType());
query.setResultTransformer(Transformers.aliasToBean(CustomerListDTO.class));
int count = 0;
List<CustomerListDTO> dtoList = query.list();
if (dtoList.size() > 0) {
count = query.list().size();
}
// Pageable pageable = buildPageable();
// if (pageable != null) {
// query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
// query.setMaxResults(pageable.getPageSize());
// }
Pageable pageable = PageRequest.of(page, pageSize, Sort.by(sort));
Pageable pageable = SQLBuilder.buildPageable(campaignCustomerRequestDTO);
if (pageable != null) {
query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
query.setMaxResults(pageable.getPageSize());
}
List<CustomerListDTO> data = query.list();
Page<CustomerListDTO> dataPage = new PageImpl<>(data, pageable, count);
resultDTO.setData(dataPage);
resultDTO.setErrorCode(Constants.ApiErrorCode.SUCCESS);
resultDTO.setDescription(Constants.ApiErrorDesc.SUCCESS);
} catch (Exception e) {
resultDTO.setErrorCode(Constants.ApiErrorCode.ERROR);
resultDTO.setDescription(Constants.ApiErrorDesc.ERROR);
} finally {
session.close();
}
return resultDTO;
}
......
......@@ -17,7 +17,6 @@ import org.springframework.data.domain.Sort;
public class SQLBuilder {
public static final String SQL_MODULE_CAMPAIGN_MNG = "campaign-mng";
public static final String SQL_MODULE_CAMPAIGN_STATUS_MNG = "campaign-status-mng";
public static final String SQL_MODULE_CAMPAIGN_CUSTOMER = "campaign-customer";
public static String getSqlQueryById(String module,
String queryId) {
......
package com.viettel.campaign.web.dto.request_dto;
import com.viettel.campaign.web.dto.BaseDTO;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class CampaignCustomerRequestDTO extends BaseDTO {
String customerListCode;
String customerListName;
String convertedDateFrom;
String convertedDateTo;
String companySiteId;
}
package com.viettel.campaign.web.dto.request_dto;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
@Getter
@Setter
public class DeleteCustomerRequestDTO {
Long customerId;
Long customerListId;
List<Long> customerIds;
}
......@@ -4,6 +4,8 @@ import com.viettel.campaign.web.dto.CustomerDTO;
import com.viettel.campaign.web.dto.CustomerListDTO;
import com.viettel.campaign.web.dto.ResultDTO;
import com.viettel.campaign.service.CustomerService;
import com.viettel.campaign.web.dto.request_dto.CampaignCustomerRequestDTO;
import com.viettel.campaign.web.dto.request_dto.DeleteCustomerRequestDTO;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
......@@ -12,7 +14,6 @@ import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;
......@@ -61,17 +62,17 @@ public class CustomerController {
@PostMapping("/delete")
@ResponseBody
public ResultDTO deleteCustomer(@RequestBody @Valid CustomerDTO customerDTO) {
public ResultDTO deleteCustomer(@RequestBody @Valid DeleteCustomerRequestDTO deleteCustomerRequestDTO) {
ResultDTO result = new ResultDTO();
result = customerService.deleteCustomer(customerDTO);
result = customerService.deleteCustomer(deleteCustomerRequestDTO);
return result;
}
@PostMapping("/deleteIds")
@ResponseBody
public ResultDTO deleteIds(@RequestBody @Valid List<Long> ids) {
public ResultDTO deleteIds(@RequestBody @Valid DeleteCustomerRequestDTO deleteCustomerRequestDTO) {
ResultDTO result = new ResultDTO();
result = customerService.deleteIds(ids);
result = customerService.deleteIds(deleteCustomerRequestDTO);
return result;
}
......@@ -140,10 +141,9 @@ public class CustomerController {
return result;
}
@GetMapping("/searchCustomerList")
@ResponseBody
public ResponseEntity searchCustomerList(@RequestParam("customerListCode") String customerListCode, @RequestParam("customerListName") String customerListName, @RequestParam("dateFrom") Date dateFrom, @RequestParam("dateTo") Date dateTo, @RequestParam("page") int page, @RequestParam("pageSize") int pageSize, @RequestParam("sort") String sort) {
ResultDTO result = customerService.searchCustomerList(customerListCode, customerListName, dateFrom, dateTo, page, pageSize, sort);
@RequestMapping(value = "/searchCustomerList", method = RequestMethod.POST)
public ResponseEntity searchCustomerList(@RequestBody CampaignCustomerRequestDTO campaignCustomerRequestDTO) {
ResultDTO result = customerService.searchCustomerList(campaignCustomerRequestDTO);
return new ResponseEntity<>(result, HttpStatus.OK);
}
}
SELECT
CUSTOMER_LIST_ID,
COMPANY_SITE_ID,
CUSTOMER_LIST_CODE,
CUSTOMER_LIST_NAME,
STATUS,
CREATE_BY,
CREATE_AT,
UPDATE_BY,
UPDATE_AT,
SOURCE,
DEPT_CREATE
CUSTOMER_LIST_ID customerListId,
COMPANY_SITE_ID companySiteId,
CUSTOMER_LIST_CODE customerListCode,
CUSTOMER_LIST_NAME customerListName,
STATUS status,
CREATE_BY createBy,
CREATE_AT createAt,
UPDATE_BY updateBy,
UPDATE_AT updateAt,
SOURCE source,
DEPT_CREATE deptCreate
FROM CUSTOMER_LIST
WHERE 1 = 1
AND CREATE_AT BETWEEN :p_date_from AND :p_date_to
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment