ClientController.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. @ApiOperation("标准模板下载")
  51. @RequestMapping(value = "/downloadModel", method = RequestMethod.GET)
  52. public Response getModel(final HttpServletResponse response) throws Exception {
  53. byte[] data = null;
  54. OutputStream outputStream = null;
  55. try {
  56. data = clientService.getModel();
  57. response.setCharacterEncoding("UTF-8");
  58. response.setContentType("application/x-msdownload");
  59. response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("标准模版.xlsx", "UTF-8"));
  60. OutputStream outputSream = response.getOutputStream();
  61. outputSream.write(data);
  62. outputSream.flush();
  63. } catch (IOException e) {
  64. throw new BadRequestException("Fail to write stream", Constant.RET_DOCUMENT_ERROR, e);
  65. } finally {
  66. if (outputStream != null) {
  67. try {
  68. outputStream.close();
  69. } catch (IOException e) {
  70. }
  71. }
  72. }
  73. return Response.ok();
  74. }
  75. @OperationControllerLog(module = "CRM", operation = "客户管理-批量导入")
  76. @ApiOperation("批量导入")
  77. @RequestMapping(value = "/import/{organizationId}", method = RequestMethod.POST)
  78. public Response insert(@ApiParam(name = "uploadFile", required = true) MultipartFile uploadFile, @PathVariable long organizationId) {
  79. long userId = ContextUtils.getCurrentUser().getId();
  80. InputStream inputStream = null;
  81. try {
  82. inputStream = uploadFile.getInputStream();
  83. List<MgrClient> list = ExcelUtils.dispose(inputStream, 1, 1, 0, 1000, MgrClient.class);
  84. for (MgrClient mc : list) {
  85. mc.setUserId(userId);
  86. mc.setOrganizationId(organizationId);
  87. }
  88. clientService.batchInsert(list);
  89. return Response.ok();
  90. } catch (IOException e) {
  91. return Response.fail(Constant.CODE_BAD_REQUEST, Constant.RET_INPUT_ERROR);
  92. } finally {
  93. if (inputStream != null) {
  94. try {
  95. inputStream.close();
  96. } catch (IOException e) {
  97. }
  98. }
  99. }
  100. }
  101. @ApiOperation("更新")
  102. @RequestMapping(value = "/update", method = RequestMethod.PUT)
  103. public Response update(@RequestBody MgrClient mgrClient) {
  104. clientService.update(mgrClient);
  105. return Response.ok();
  106. }
  107. @ApiOperation("删除")
  108. @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
  109. public Response delete(@PathVariable long id) {
  110. clientService.delete(id);
  111. return Response.ok();
  112. }
  113. @ApiOperation("详情")
  114. @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  115. public Response get(@PathVariable long id) {
  116. return Response.ok(clientService.get(id));
  117. }
  118. @ApiOperation("按类型统计数量")
  119. @RequestMapping(value = "/typeCount", method = RequestMethod.POST)
  120. public Response typeCount(@RequestBody MgrClient mgrClient) {
  121. return Response.ok(clientService.typeCount(mgrClient));
  122. }
  123. }