ClientController.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package com.bosshand.virgo.controller;
  2. import com.bosshand.virgo.core.config.OperationControllerLog;
  3. import com.bosshand.virgo.core.model.MgrClient;
  4. import com.bosshand.virgo.core.response.Response;
  5. import com.bosshand.virgo.core.utils.ContextUtils;
  6. import com.bosshand.virgo.core.utils.ExcelUtils;
  7. import com.bosshand.virgo.exception.BadRequestException;
  8. import com.bosshand.virgo.exception.Constant;
  9. import com.bosshand.virgo.service.ClientService;
  10. import io.swagger.annotations.ApiOperation;
  11. import io.swagger.annotations.ApiParam;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.web.bind.annotation.*;
  14. import org.springframework.web.multipart.MultipartFile;
  15. import javax.servlet.http.HttpServletResponse;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.io.OutputStream;
  19. import java.net.URLEncoder;
  20. import java.util.HashMap;
  21. import java.util.List;
  22. import java.util.Map;
  23. @RestController
  24. @RequestMapping("client")
  25. public class ClientController {
  26. @Autowired
  27. ClientService clientService;
  28. @OperationControllerLog(module = "CRM", operation = "客户管理-客户列表查询")
  29. @ApiOperation("获取")
  30. @RequestMapping(value = "/{currPage}/{pageSize}", method = RequestMethod.POST)
  31. public Response list(@RequestBody MgrClient mgrClient, @PathVariable int currPage, @PathVariable int pageSize) {
  32. long id = ContextUtils.getCurrentUser().getId();
  33. mgrClient.setUserId(id);
  34. int totalCount = clientService.getTotalCount(mgrClient);
  35. List<MgrClient> dataList = clientService.getLimit(mgrClient, currPage, pageSize);
  36. Map<String, Object> result = new HashMap<>();
  37. result.put("dataList", dataList);
  38. result.put("totalCount", totalCount);
  39. return Response.ok(result);
  40. }
  41. @OperationControllerLog(module = "CRM", operation = "客户管理-新增客户")
  42. @ApiOperation("保存")
  43. @RequestMapping(value = "", method = RequestMethod.POST)
  44. public Response insert(@RequestBody MgrClient mgrClient) {
  45. long id = ContextUtils.getCurrentUser().getId();
  46. mgrClient.setUserId(id);
  47. clientService.insert(mgrClient);
  48. return Response.ok();
  49. }
  50. @OperationControllerLog(module = "CRM", operation = "客户管理-标准模板下载")
  51. @ApiOperation("标准模板下载")
  52. @RequestMapping(value = "/downloadModel", method = RequestMethod.GET)
  53. public Response getModel(final HttpServletResponse response) throws Exception {
  54. byte[] data = null;
  55. OutputStream outputStream = null;
  56. try {
  57. data = clientService.getModel();
  58. response.setCharacterEncoding("UTF-8");
  59. response.setContentType("application/x-msdownload");
  60. response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("标准模版.xlsx", "UTF-8"));
  61. OutputStream outputSream = response.getOutputStream();
  62. outputSream.write(data);
  63. outputSream.flush();
  64. } catch (IOException e) {
  65. throw new BadRequestException("Fail to write stream", Constant.RET_DOCUMENT_ERROR, e);
  66. } finally {
  67. if (outputStream != null) {
  68. try {
  69. outputStream.close();
  70. } catch (IOException e) {
  71. }
  72. }
  73. }
  74. return Response.ok();
  75. }
  76. @OperationControllerLog(module = "CRM", operation = "客户管理-批量导入")
  77. @ApiOperation("批量导入")
  78. @RequestMapping(value = "/import/{organizationId}", method = RequestMethod.POST)
  79. public Response insert(@ApiParam(name = "uploadFile", required = true) MultipartFile uploadFile, @PathVariable long organizationId) {
  80. long userId = ContextUtils.getCurrentUser().getId();
  81. InputStream inputStream = null;
  82. try {
  83. inputStream = uploadFile.getInputStream();
  84. List<MgrClient> list = ExcelUtils.dispose(inputStream, 1, 1, 0, 1000, MgrClient.class);
  85. for (MgrClient mc : list) {
  86. mc.setUserId(userId);
  87. mc.setOrganizationId(organizationId);
  88. }
  89. clientService.batchInsert(list);
  90. return Response.ok();
  91. } catch (IOException e) {
  92. return Response.fail(Constant.CODE_BAD_REQUEST, Constant.RET_INPUT_ERROR);
  93. } finally {
  94. if (inputStream != null) {
  95. try {
  96. inputStream.close();
  97. } catch (IOException e) {
  98. }
  99. }
  100. }
  101. }
  102. @ApiOperation("更新")
  103. @RequestMapping(value = "/update", method = RequestMethod.PUT)
  104. public Response update(@RequestBody MgrClient mgrClient) {
  105. clientService.update(mgrClient);
  106. return Response.ok();
  107. }
  108. @ApiOperation("删除")
  109. @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
  110. public Response delete(@PathVariable long id) {
  111. clientService.delete(id);
  112. return Response.ok();
  113. }
  114. @ApiOperation("详情")
  115. @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  116. public Response get(@PathVariable long id) {
  117. return Response.ok(clientService.get(id));
  118. }
  119. @ApiOperation("按类型统计数量")
  120. @RequestMapping(value = "/typeCount", method = RequestMethod.POST)
  121. public Response typeCount(@RequestBody MgrClient mgrClient) {
  122. return Response.ok(clientService.typeCount(mgrClient));
  123. }
  124. }