/02.TEMP-PROJECT-Web/src/main/java/com/temp/springboot/web/controller/NguoiDung/QtNguoiDungApiController.java

https://gitlab.com/truongnv9xbn/spring-boot-project · Java · 1226 lines · 665 code · 135 blank · 426 comment · 101 complexity · 14d8099bb05c6dcd100bb70562f7b04b MD5 · raw file

  1. package com.temp.springboot.web.controller.NguoiDung;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.lang.reflect.Field;
  7. import java.sql.Timestamp;
  8. import java.text.ParseException;
  9. import java.text.SimpleDateFormat;
  10. import java.util.ArrayList;
  11. import java.util.Arrays;
  12. import java.util.Date;
  13. import java.util.List;
  14. import javax.servlet.http.HttpServletRequest;
  15. import javax.validation.Valid;
  16. import javax.ws.rs.core.Context;
  17. import org.apache.commons.lang3.StringUtils;
  18. import org.apache.poi.ss.usermodel.Cell;
  19. import org.apache.poi.ss.usermodel.CellStyle;
  20. import org.apache.poi.ss.usermodel.CellType;
  21. import org.apache.poi.ss.usermodel.Row;
  22. import org.apache.poi.ss.usermodel.Sheet;
  23. import org.apache.poi.ss.usermodel.Workbook;
  24. import org.apache.poi.ss.util.CellRangeAddress;
  25. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  26. import org.slf4j.Logger;
  27. import org.slf4j.LoggerFactory;
  28. import org.springframework.beans.factory.annotation.Autowired;
  29. import org.springframework.beans.factory.annotation.Qualifier;
  30. import org.springframework.core.io.ByteArrayResource;
  31. import org.springframework.http.HttpStatus;
  32. import org.springframework.http.ResponseEntity;
  33. import org.springframework.security.crypto.password.PasswordEncoder;
  34. import org.springframework.web.bind.annotation.CrossOrigin;
  35. import org.springframework.web.bind.annotation.DeleteMapping;
  36. import org.springframework.web.bind.annotation.GetMapping;
  37. import org.springframework.web.bind.annotation.PathVariable;
  38. import org.springframework.web.bind.annotation.PostMapping;
  39. import org.springframework.web.bind.annotation.PutMapping;
  40. import org.springframework.web.bind.annotation.RequestBody;
  41. import org.springframework.web.bind.annotation.RequestMapping;
  42. import org.springframework.web.bind.annotation.RequestMethod;
  43. import org.springframework.web.bind.annotation.RequestParam;
  44. import org.springframework.web.bind.annotation.RestController;
  45. import com.temp.global.SendMail;
  46. import com.temp.global.UserInfoGlobal;
  47. import com.temp.global.WriteLogInDataBaseGlobal;
  48. import com.temp.global.exception.ApiRequestException;
  49. import com.temp.model.NguoiDung.NguoiDungJoinAllDTO;
  50. import com.temp.model.NguoiDung.QtLogHeThongDTO;
  51. import com.temp.model.NguoiDung.QtNguoiDungBDTO;
  52. import com.temp.model.NguoiDung.QtNguoiDungDTO;
  53. import com.temp.model.uploadfile.UploadFileDefaultDTO;
  54. import com.temp.service.NguoiDung.QtNguoiDungService;
  55. import com.temp.utils.CellConfig;
  56. import com.temp.utils.Constant;
  57. import com.temp.utils.Constants;
  58. import com.temp.utils.PasswordGenerator;
  59. import com.temp.utils.TimestampUtils;
  60. import com.temp.utils.Utils;
  61. @CrossOrigin(origins = Constants.BaseUrl)
  62. @RestController
  63. @RequestMapping("/api/v1")
  64. public class QtNguoiDungApiController {
  65. public static final Logger logger = LoggerFactory.getLogger(QtNguoiDungApiController.class);
  66. @Autowired
  67. private PasswordEncoder bcryptEncoder;
  68. @Autowired
  69. @Qualifier("QtNguoiDungServiceImpl")
  70. private QtNguoiDungService qtService;
  71. @RequestMapping(value = "/nguoidung/list", method = RequestMethod.GET)
  72. public ResponseEntity<QtNguoiDungBDTO> getList(
  73. @Valid @RequestParam(value = "strfilter", required = false) String strfilter,
  74. @RequestParam(name = "pageNo", defaultValue = "0") Integer pageNo,
  75. @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
  76. @RequestParam(name = "keySort", defaultValue = "id") String keySort,
  77. @RequestParam(name = "desc", defaultValue = "false") boolean desc,
  78. @RequestParam(value = "hoTen", required = false) String hoTen,
  79. @RequestParam(value = "taiKhoan", required = false) String taiKhoan,
  80. @RequestParam(value = "sEmail", required = false) String sEmail,
  81. @RequestParam(value = "sDiDong", required = false) String sDiDong,
  82. @RequestParam(value = "sMaNguoiDung", required = false) String sMaNguoiDung,
  83. @RequestParam(value = "trangThai", required = false) String trangThai,
  84. @RequestParam(value = "nhomNguoiDungId", required = false) Integer nhomNguoiDungId,
  85. @RequestParam(value = "loaiNguoiDung", required = false) Integer loaiNguoiDung) {
  86. if (!checkKeySort(keySort)) {
  87. QtNguoiDungBDTO bdto = new QtNguoiDungBDTO();
  88. bdto.setMessage("truyền sai keySort");
  89. return new ResponseEntity<QtNguoiDungBDTO>(bdto, HttpStatus.METHOD_NOT_ALLOWED);
  90. }
  91. QtNguoiDungBDTO lstQtNguoiDung = new QtNguoiDungBDTO();
  92. try {
  93. logger.info(Constants.Logs.LIST);
  94. if (pageNo > 0) {
  95. pageNo = pageNo - 1;
  96. }
  97. lstQtNguoiDung = qtService.listQtNguoiDungs(strfilter, hoTen, taiKhoan, sEmail, sDiDong, sMaNguoiDung,
  98. pageNo, pageSize, keySort, desc, trangThai, nhomNguoiDungId, loaiNguoiDung);
  99. return new ResponseEntity<QtNguoiDungBDTO>(lstQtNguoiDung, HttpStatus.OK);
  100. } catch (Exception e) {
  101. e.printStackTrace();
  102. logger.error(e.toString());
  103. return new ResponseEntity<QtNguoiDungBDTO>(lstQtNguoiDung, HttpStatus.INTERNAL_SERVER_ERROR);
  104. }
  105. }
  106. @RequestMapping(value = "/nguoiDung/exitsMa", method = RequestMethod.GET)
  107. public ResponseEntity<Boolean> checkExitsByMa(
  108. @RequestParam(value = "maNguoiDung", required = false) String maNguoiDung,
  109. @RequestParam(value = "id", required = false) Integer id) throws ApiRequestException {
  110. Boolean result=false;
  111. try {
  112. logger.info(Constants.Logs.EXITS);
  113. result = qtService.isExistByMa(maNguoiDung,id);
  114. return new ResponseEntity<Boolean>(result, HttpStatus.OK);
  115. } catch (Exception e) {
  116. logger.error(e.toString());
  117. throw new ApiRequestException(Constants.Messages.EXCEPTION_FAIL, HttpStatus.EXPECTATION_FAILED);
  118. }
  119. }
  120. @PostMapping("/nguoidung/add")
  121. public ResponseEntity<?> save(@Valid @RequestBody QtNguoiDungDTO dto, @Context HttpServletRequest request)
  122. throws ApiRequestException {
  123. logger.info(Constants.Logs.CREATE);
  124. if (!qtService.isExistQtNguoiDung(dto)) {
  125. try {
  126. this.ConvertData(dto);
  127. if (!StringUtils.isEmpty(dto.getAnhDaiDien())) {
  128. dto.setAnhDaiDien(Utils.CommonSavePathFile(dto.getAnhDaiDien(),
  129. Constants.FolderUpload.FOLDER_NGUOIDUNG, request));
  130. }
  131. qtService.saveQtNguoiDung(dto);
  132. // Send Mail
  133. // if (dto.getEmail() != null && !dto.getEmail().equals("")) {
  134. // String emailTo = dto.getEmail();
  135. // String tieuDe = "Dang ky tai khoan He thong quan ly cong ty chung khoan SCMS";
  136. // String noiDung = "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" style=\"padding-bottom:20px;max-width:516px;min-width:220px\"><tbody><tr><td width=\"8\" style=\"width:8px\"></td><td><div style=\"border-style:solid;border-width:thin;border-color:#dadce0;border-radius:8px;padding:40px 20px\" align=\"center\" class=\"m_205523402140519040mdv2rw\">"
  137. // + "<img src=\"http://scms.tinhvan.com/img/logo.png\" width=\"74\" height=\"74\" aria-hidden=\"true\" style=\"margin-bottom:16px\" alt=\"SCMS\" class=\"CToWUd\"><div style=\"font-family:'Google Sans',Roboto,RobotoDraft,Helvetica,Arial,sans-serif;border-bottom:thin solid #dadce0;color:rgba(0,0,0,0.87);line-height:32px;padding-bottom:24px;text-align:center;word-break:break-word\"><div style=\"font-size:24px\"><a style=\"text-decoration:none;color:rgba(0,0,0,0.87)\">Bạn đã đăng ký thành công tài khoản</a></div></div><div style=\"font-family:Roboto-Regular,Helvetica,Arial,sans-serif;font-size:14px;color:rgba(0,0,0,0.87);line-height:20px;padding-top:20px;text-align:left\"><h2><b>Thông tin tài khoản</b></h2><p><b>Tài khoản: </b><a style=\"text-decoration:none;color:rgba(0,0,0,0.87)\">"
  138. // + dto.getTaiKhoan()
  139. // + "</a></p><p><b>Mật khẩu: </b><a style=\"text-decoration:none;color:rgba(0,0,0,0.87)\">"
  140. // + dto.getMatKhauDefault() + "</a></p><p>Truy cập hệ thống "
  141. // + "<a href='http://scms.tinhvan.com/scms/login'><b>tại đây</b></a> và <b>ĐỔI MẬT KHẨU</b> ngay lập tức để chủ động bảo vệ tài khoản của bạn!.</p><p><i>Mọi thắc mắc liên quan đến tài khoản, vui lòng liên hệ với quản trị hệ thống.</i></p></div></div><div style=\"text-align:left\"><div style=\"font-family:Roboto-Regular,Helvetica,Arial,sans-serif;color:rgba(0,0,0,0.54);font-size:11px;line-height:18px;padding-top:12px;text-align:center\"><div>Vui lòng giữ bảo mật thông tin tài khoản đã được cấp</div><div style=\"direction:ltr\">© 2020 SCMS - Ủy ban chứng khoán nhà nước</div></div></div></td><td width=\"8\" style=\"width:8px\"></td></tr></tbody></table>";
  142. // SendMail.SendMail(emailTo, tieuDe, noiDung);
  143. // }
  144. } catch (Exception e) {
  145. throw new ApiRequestException(Constants.Messages.CREATE_FAIL, HttpStatus.EXPECTATION_FAILED);
  146. }
  147. return new ResponseEntity<QtNguoiDungDTO>(dto, HttpStatus.CREATED);
  148. } else {
  149. throw new ApiRequestException(Constants.Messages.RC_EXIST, HttpStatus.EXPECTATION_FAILED);
  150. }
  151. }
  152. /**
  153. * cập nhập người dùng
  154. *
  155. * @param qtBdto 1 đối tượng chứa id> 0
  156. * @return ResponseEntity
  157. * @throws ApiRequestException
  158. */
  159. @PutMapping("/nguoidung/update")
  160. public ResponseEntity<?> update(@Valid @RequestBody QtNguoiDungDTO dto, @Context HttpServletRequest request)
  161. throws ApiRequestException {
  162. try {
  163. logger.info(Constants.Logs.UPDATE);
  164. // check upload file
  165. String PathUpload_update = "";
  166. if (dto.getArrFileDinhKem() != null && !dto.getArrFileDinhKem().isEmpty()) {
  167. for (UploadFileDefaultDTO e : dto.getArrFileDinhKem()) {
  168. if (!StringUtils.isEmpty(e.getNameFile())) {
  169. PathUpload_update += e.getPath() + ",";
  170. }
  171. }
  172. if (PathUpload_update != "") {
  173. PathUpload_update = PathUpload_update.substring(0, PathUpload_update.length() - 1);
  174. }
  175. }
  176. if (!StringUtils.isEmpty(dto.getAnhDaiDien())) {
  177. dto.setAnhDaiDien(Utils.CommonSavePathFile(dto.getAnhDaiDien(), Constants.FolderUpload.FOLDER_NGUOIDUNG,
  178. request));
  179. } else {
  180. dto.setAnhDaiDien(dto.getAnhDaiDien().split("~")[0]);
  181. }
  182. if (qtService.isExistById(dto.getId())) {
  183. try {
  184. this.ConvertData(dto);
  185. qtService.updateQtNguoiDung(dto);
  186. // lấy dữ liệu cũ trươc skhi add
  187. QtNguoiDungDTO oldObject = this.qtService.findById(dto.getId());
  188. QtNguoiDungDTO newObject = qtService.saveQtNguoiDungGetObject(dto);
  189. if (!StringUtils.isEmpty(oldObject.getAnhDaiDien())) {
  190. // lấy các path của old và new
  191. String[] pathOld = oldObject.getAnhDaiDien().split(",");
  192. String[] pathNew = newObject.getAnhDaiDien().split(",");
  193. List<String> oldList = null;
  194. List<String> NewList = null;
  195. if (pathOld.length > 0) {
  196. oldList = Arrays.asList(pathOld);
  197. }
  198. if (pathNew.length > 0) {
  199. NewList = Arrays.asList(pathNew);
  200. }
  201. // biến chứa path cần remove
  202. List<String> pathRemove = new ArrayList<>();
  203. // check các trường hợp để add vào pathRemove
  204. if (oldList != null && NewList == null) {
  205. pathRemove.addAll(oldList);
  206. } else if (oldList != null && NewList != null) {
  207. for (String path : oldList) {
  208. if (!StringUtils.isEmpty(path) && !NewList.contains(path)) {
  209. pathRemove.add(path);
  210. }
  211. }
  212. }
  213. // kiểm tra nếu path cần xóa thì xóa
  214. if (!pathRemove.isEmpty()) {
  215. for (String pathx : pathRemove) {
  216. Utils.DeleteFile(Constant.UPLOADNOTTEMPFOLDER + pathx);
  217. }
  218. }
  219. }
  220. } catch (Exception e) {
  221. logger.error(e.getMessage());
  222. throw new ApiRequestException(Constants.Messages.UPDATE_FAIL, HttpStatus.EXPECTATION_FAILED);
  223. }
  224. return new ResponseEntity<QtNguoiDungDTO>(dto, HttpStatus.ACCEPTED);
  225. } else {
  226. throw new ApiRequestException(Constants.Messages.RC_NOT_EXIST, HttpStatus.NOT_FOUND);
  227. }
  228. } catch (Exception e) {
  229. logger.error(e.getMessage());
  230. throw new ApiRequestException(Constants.Messages.UPDATE_FAIL, HttpStatus.EXPECTATION_FAILED);
  231. }
  232. }
  233. @PutMapping("/phanquyen/")
  234. public ResponseEntity<?> phanQuyenNguoiDung(@RequestBody NguoiDungJoinAllDTO dto,
  235. @Context HttpServletRequest request) throws ApiRequestException {
  236. logger.info(Constants.Logs.UPDATE);
  237. if (qtService.isExistById(dto.getId())) {
  238. try {
  239. QtNguoiDungDTO userInfo = UserInfoGlobal.getUserInfoAuthor();
  240. Timestamp timeStamp = new Timestamp(new Date().getTime());
  241. qtService.phanQuyenChucNang(dto);
  242. QtLogHeThongDTO dtoLog = new QtLogHeThongDTO();
  243. // set IP thực hiện lấy IP remote client
  244. dtoLog.setIpThucHien(Utils.getClientIp(request));
  245. dtoLog.setLogType(Constants.LogSystem.LOG_SYS_ADD + "_NGUOI_DUNG");
  246. dtoLog.setNgayTao(timeStamp);
  247. dtoLog.setNguoiTaoId(userInfo.getId());
  248. dtoLog.setNoiDung("Tài khoản " + userInfo.getTaiKhoan() + " phân quyền cho người dùng " + dto.getHoTen()
  249. + " với tài khoản : " + dto.getTaiKhoan());
  250. // save db
  251. WriteLogInDataBaseGlobal.WriteLogDB(dtoLog);
  252. } catch (Exception e) {
  253. logger.error(e.getMessage());
  254. throw new ApiRequestException(Constants.Messages.UPDATE_FAIL, HttpStatus.EXPECTATION_FAILED);
  255. }
  256. return new ResponseEntity<NguoiDungJoinAllDTO>(dto, HttpStatus.ACCEPTED);
  257. } else {
  258. throw new ApiRequestException(Constants.Messages.RC_NOT_EXIST, HttpStatus.NOT_FOUND);
  259. }
  260. }
  261. @RequestMapping(value = "/nguoidung/changepassword", method = RequestMethod.GET)
  262. public ResponseEntity<?> changePass(@RequestParam(value = "id", defaultValue = "0") Integer id,
  263. @RequestParam(value = "matKhauCu", required = false) String matKhauCu,
  264. @RequestParam(value = "matKhauMoi", required = false) String matKhauMoi,
  265. @RequestParam(value = "matKhauMoiConfirm", required = false) String matKhauMoiConfirm,
  266. @Context HttpServletRequest request) throws ApiRequestException {
  267. try {
  268. logger.info(Constants.Logs.UPDATE);
  269. QtNguoiDungDTO userInfo = UserInfoGlobal.getUserInfoAuthor();
  270. id = userInfo.getId();
  271. if (qtService.isExistById(id)) {
  272. String validateStr = validateData(id, matKhauCu, matKhauMoi, matKhauMoiConfirm);
  273. if (validateStr.equals("")) {
  274. Timestamp timeStamp = new Timestamp(new Date().getTime());
  275. qtService.changePasswordQtNguoiDung(id, matKhauMoi);
  276. QtLogHeThongDTO dtoLog = new QtLogHeThongDTO();
  277. // set IP thực hiện lấy IP remote client
  278. dtoLog.setIpThucHien(Utils.getClientIp(request));
  279. dtoLog.setLogType(Constants.LogSystem.LOG_SYS_UPDATE + "_NGUOI_DUNG");
  280. dtoLog.setNgayTao(timeStamp);
  281. dtoLog.setNguoiTaoId(userInfo.getId());
  282. dtoLog.setNoiDung("Tài khoản " + userInfo.getTaiKhoan() + " đổi mật khẩu ");
  283. // save db
  284. WriteLogInDataBaseGlobal.WriteLogDB(dtoLog);
  285. return new ResponseEntity<String>("", HttpStatus.ACCEPTED);
  286. } else {
  287. return new ResponseEntity<String>(validateStr, HttpStatus.ACCEPTED);
  288. }
  289. }
  290. return new ResponseEntity<String>(Constants.Messages.RC_EXIST, HttpStatus.ACCEPTED);
  291. } catch (Exception e) {
  292. logger.error(e.getMessage());
  293. throw new ApiRequestException(Constants.Messages.UPDATE_FAIL, HttpStatus.EXPECTATION_FAILED);
  294. }
  295. }
  296. @DeleteMapping("/nguoidung/delete/{id}")
  297. public ResponseEntity<?> delete(@PathVariable("id") int id, @Context HttpServletRequest request)
  298. throws ApiRequestException {
  299. try {
  300. logger.info(Constants.Logs.DELETE);
  301. QtNguoiDungDTO userInfo = UserInfoGlobal.getUserInfoAuthor();
  302. Timestamp timeStamp = new Timestamp(new Date().getTime());
  303. if(id == 1) {
  304. return new ResponseEntity<>(new ApiRequestException("Bình tĩnh ! Người dùng này không thể xóa ! "),
  305. HttpStatus.LOCKED);
  306. }
  307. if (!qtService.isExistById(id)) {
  308. return new ResponseEntity<>(new ApiRequestException(Constants.Messages.RC_NOT_EXIST),
  309. HttpStatus.NOT_FOUND);
  310. }
  311. if (qtService.deleteById(id)) {
  312. QtLogHeThongDTO dtoLog = new QtLogHeThongDTO();
  313. // set IP thực hiện lấy IP remote client
  314. dtoLog.setIpThucHien(Utils.getClientIp(request));
  315. dtoLog.setLogType(Constants.LogSystem.LOG_SYS_DELETE + "_NGUOI_DUNG");
  316. dtoLog.setNgayTao(timeStamp);
  317. dtoLog.setNguoiTaoId(userInfo.getId());
  318. dtoLog.setNoiDung("Tài khoản " + userInfo.getTaiKhoan() + " xóa người dùng ID = " + id);
  319. // save db
  320. WriteLogInDataBaseGlobal.WriteLogDB(dtoLog);
  321. return new ResponseEntity<Object>(Constants.Messages.DELETE_SUCCESS, HttpStatus.NO_CONTENT);
  322. }
  323. return new ResponseEntity<Object>(Constants.Messages.DELETE_FAIL, HttpStatus.BAD_REQUEST);
  324. } catch (Exception e) {
  325. logger.error(e.getMessage());
  326. throw new ApiRequestException(Constants.Messages.DELETE_FAIL, HttpStatus.NOT_FOUND);
  327. }
  328. }
  329. @GetMapping("/nguoidung/detail/{id}")
  330. public ResponseEntity<?> detail(@PathVariable("id") int id) throws ApiRequestException {
  331. try {
  332. logger.info(Constants.Logs.GETBYID);
  333. NguoiDungJoinAllDTO qt = qtService.findByJoinAllId(id);
  334. if (qt == null) {
  335. return new ResponseEntity<>(new ApiRequestException(Constants.Messages.RC_NOT_EXIST),
  336. HttpStatus.BAD_REQUEST);
  337. }
  338. return ResponseEntity.ok().body(qt);
  339. } catch (Exception e) {
  340. logger.error(e.getMessage());
  341. throw new ApiRequestException(Constants.Messages.GETBYID_FAIL, HttpStatus.NOT_FOUND);
  342. }
  343. }
  344. @RequestMapping(value = "/nguoidung/export", method = RequestMethod.GET)
  345. public ResponseEntity<ByteArrayResource> ExportExcel(
  346. @Valid @RequestParam(value = "strfilter", required = false) String strfilter,
  347. @RequestParam(name = "pageNo", defaultValue = "0") Integer pageNo,
  348. @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
  349. @RequestParam(name = "keySort", defaultValue = "id") String keySort,
  350. @RequestParam(name = "desc", defaultValue = "false") boolean desc,
  351. @RequestParam(value = "hoTen", required = false) String hoTen,
  352. @RequestParam(value = "taiKhoan", required = false) String taiKhoan,
  353. @RequestParam(value = "sEmail", required = false) String sEmail,
  354. @RequestParam(value = "sDiDong", required = false) String sDiDong,
  355. @RequestParam(value = "sMaNguoiDung", required = false) String sMaNguoiDung,
  356. @RequestParam(value = "trangThai", required = false) String trangThai,
  357. @RequestParam(value = "nhomNguoiDungId", required = false) Integer nhomNguoiDungId,
  358. @RequestParam(value = "loaiNguoiDung", required = false) Integer loaiNguoiDung) throws ApiRequestException {
  359. QtNguoiDungBDTO lstQtNguoiDung = new QtNguoiDungBDTO();
  360. try {
  361. Workbook workbook = new XSSFWorkbook();
  362. lstQtNguoiDung = qtService.listQtNguoiDungs(strfilter, hoTen, taiKhoan, sEmail, sDiDong, sMaNguoiDung, 0,
  363. 999999999, keySort, desc, trangThai, nhomNguoiDungId, loaiNguoiDung);
  364. Sheet sheet = workbook.createSheet("Danh sách người dùng");
  365. CellStyle style = CellConfig.createStyleForTitleTBCN(sheet, workbook);
  366. CellStyle style1 = CellConfig.createCellStyle(sheet, workbook, true);
  367. Cell cell;
  368. Row row;
  369. Integer rownum = 0;
  370. row = sheet.createRow((rownum));
  371. cell = row.createCell(0, CellType.STRING);
  372. sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
  373. cell.setCellValue(("Danh sách người dùng".toUpperCase()));
  374. cell.setCellStyle(style1);
  375. // row bat dau
  376. rownum = 1;
  377. // Tạo tiêu đề
  378. row = sheet.createRow(rownum);
  379. cell = row.createCell(0, CellType.STRING);
  380. cell.setCellValue("Tài khoản");
  381. cell.setCellStyle(style);
  382. cell = row.createCell(1, CellType.STRING);
  383. cell.setCellValue("Họ tên");
  384. cell.setCellStyle(style);
  385. cell = row.createCell(2, CellType.STRING);
  386. cell.setCellValue("Mật khẩu mặc định");
  387. cell.setCellStyle(style);
  388. cell = row.createCell(3, CellType.STRING);
  389. cell.setCellValue("Nhóm người dùng");
  390. cell.setCellStyle(style);
  391. // cell = row.createCell(4, CellType.STRING);
  392. // cell.setCellValue("Công ty");
  393. // cell.setCellStyle(style);
  394. cell = row.createCell(4, CellType.STRING);
  395. cell.setCellValue("Trạng thái kích hoạt");
  396. cell.setCellStyle(style);
  397. cell = row.createCell(5, CellType.STRING);
  398. cell.setCellValue("Loại người dùng");
  399. cell.setCellStyle(style);
  400. // cell = row.createCell(6, CellType.STRING);
  401. // cell.setCellValue("Sử dụng chữ ký số");
  402. // cell.setCellStyle(style);
  403. if (lstQtNguoiDung != null && lstQtNguoiDung.getLstQtNguoiDung().size() > 0) {
  404. for (NguoiDungJoinAllDTO item : lstQtNguoiDung.getLstQtNguoiDung()) {
  405. rownum++;
  406. row = sheet.createRow(rownum);
  407. cell = row.createCell(0, CellType.STRING);
  408. cell.setCellValue(item.getTaiKhoan());
  409. CellConfig.BorderTable(cell, sheet);
  410. cell = row.createCell(1, CellType.STRING);
  411. cell.setCellValue(item.getHoTen());
  412. CellConfig.BorderTable(cell, sheet);
  413. cell = row.createCell(2, CellType.STRING);
  414. cell.setCellValue(item.getMatKhauDefault());
  415. CellConfig.BorderTable(cell, sheet);
  416. cell = row.createCell(3, CellType.STRING);
  417. cell.setCellValue(item.getTenNhomNguoiDung());
  418. CellConfig.BorderTable(cell, sheet);
  419. // String congTy = "";
  420. // List<CtckThongTinDTO> lstCongTys = item.getLis;
  421. // for (ChucVuDTO temp : lstChucVu) {
  422. // if (temp != null) {
  423. // chucVu += temp.getTenChucVu() == null ? "" : temp.getTenChucVu() + "/";
  424. // }
  425. // }
  426. String trangThaiKichHoat = "";
  427. if (item.getTrangThai() == true) {
  428. trangThaiKichHoat = "Đã kích hoạt";
  429. } else {
  430. trangThaiKichHoat = "Chưa kích hoạt";
  431. }
  432. cell = row.createCell(4, CellType.STRING);
  433. cell.setCellValue(trangThaiKichHoat);
  434. CellConfig.BorderTable(cell, sheet);
  435. String admin = "";
  436. if (item.getAdmin()) {
  437. admin = "Admin";
  438. } else {
  439. admin = "Không là Admin";
  440. }
  441. cell = row.createCell(5, CellType.STRING);
  442. cell.setCellValue(admin);
  443. CellConfig.BorderTable(cell, sheet);
  444. }
  445. for (int i = 0; i < 6; i++) {
  446. sheet.autoSizeColumn(i);
  447. }
  448. CellConfig.BorderTable(cell, sheet);
  449. }
  450. CellConfig.autoSizeColumns(workbook);
  451. String fileName = "danh_sach_nguoi_dung"
  452. + TimestampUtils.TimestampToString_ddMMyyyyHHmmFileName(Utils.getCurrentDate()) + ".xlsx";
  453. try {
  454. FileOutputStream out = new FileOutputStream(fileName);
  455. workbook.write(out);
  456. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  457. workbook.write(bos);
  458. byte[] bytes = bos.toByteArray();
  459. ByteArrayResource resource = new ByteArrayResource(bytes);
  460. out.close();
  461. workbook.close();
  462. return ResponseEntity.ok()
  463. .contentLength(bytes.length) //
  464. .body(resource);
  465. } catch (FileNotFoundException e) {
  466. // TODO Auto-generated catch block
  467. e.printStackTrace();
  468. } catch (IOException e) {
  469. // TODO Auto-generated catch block
  470. e.printStackTrace();
  471. }
  472. return null;
  473. } catch (Exception e) {
  474. logger.error(e.toString());
  475. return null;
  476. }
  477. }
  478. // ------------------------------------------------------------------------------------------------------------
  479. // @RequestMapping(value = "/qtnguoidung/filter", method = RequestMethod.GET)
  480. // public ResponseEntity<QtNguoiDungBDTO> listQtNguoiDungs(
  481. // @Valid @RequestParam(value = "strfilter", required = false) String strfilter,
  482. // @RequestParam(name = "pageNo", defaultValue = "0") Integer pageNo,
  483. // @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
  484. // @RequestParam(name = "keySort", defaultValue = "id") String keySort,
  485. // @RequestParam(name = "desc", defaultValue = "false") boolean desc,
  486. // @RequestParam(value = "dmChucVuId", required = false) Integer dmChucVuId,
  487. // @RequestParam(value = "hoTen", required = false) String hoTen,
  488. // @RequestParam(value = "taiKhoan", required = false) String taiKhoan,
  489. // @RequestParam(value = "sEmail", required = false) String sEmail,
  490. // @RequestParam(value = "sDiDong", required = false) String sDiDong,
  491. // @RequestParam(value = "sMaNguoiDung", required = false) String sMaNguoiDung,
  492. // @RequestParam(value = "trangThai", required = false) String trangThai,
  493. // @RequestParam(value = "tenCTCK", required = false) String tenCTCK,
  494. // @RequestParam(value = "nhomNguoiDungId", required = false) Integer nhomNguoiDungId,
  495. // @RequestParam(value = "loaiNguoiDung", required = false) Integer loaiNguoiDung,
  496. // @RequestParam(value = "trangThaiCTCK", required = false) Integer trangThaiCTCK) {
  497. //
  498. // if (!checkKeySort(keySort)) {
  499. //
  500. // QtNguoiDungBDTO bdto = new QtNguoiDungBDTO();
  501. // bdto.setMessage("truyền sai keySort");
  502. //
  503. // return new ResponseEntity<QtNguoiDungBDTO>(bdto, HttpStatus.METHOD_NOT_ALLOWED);
  504. // }
  505. //
  506. // QtNguoiDungBDTO lstQtNguoiDung = new QtNguoiDungBDTO();
  507. //
  508. // try {
  509. //
  510. // logger.info(Constants.Logs.LIST);
  511. // if (pageNo > 0) {
  512. // pageNo = pageNo - 1;
  513. // }
  514. //
  515. // lstQtNguoiDung = qtService.listQtNguoiDungs(strfilter, dmChucVuId, hoTen, taiKhoan, sEmail, sDiDong,
  516. // sMaNguoiDung, pageNo, pageSize, keySort, desc, trangThai, tenCTCK, trangThaiCTCK, nhomNguoiDungId,
  517. // loaiNguoiDung);
  518. // return new ResponseEntity<QtNguoiDungBDTO>(lstQtNguoiDung, HttpStatus.OK);
  519. //
  520. // } catch (Exception e) {
  521. //
  522. // e.printStackTrace();
  523. // logger.error(e.toString());
  524. // return new ResponseEntity<QtNguoiDungBDTO>(lstQtNguoiDung, HttpStatus.INTERNAL_SERVER_ERROR);
  525. // }
  526. // }
  527. // @RequestMapping(value = "/qtnguoidungtv/filter", method = RequestMethod.GET)
  528. // public ResponseEntity<QtNguoiDungBDTO> listQtNguoiDungsTV(
  529. // @Valid @RequestParam(value = "strfilter", required = false) String strfilter,
  530. // @RequestParam(name = "pageNo", defaultValue = "0") Integer pageNo,
  531. // @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
  532. // @RequestParam(name = "keySort", defaultValue = "id") String keySort,
  533. // @RequestParam(name = "desc", defaultValue = "false") boolean desc,
  534. // @RequestParam(value = "dmChucVuId", required = false) Integer dmChucVuId,
  535. // @RequestParam(value = "hoTen", required = false) String hoTen,
  536. // @RequestParam(value = "taiKhoan", required = false) String taiKhoan,
  537. // @RequestParam(value = "sEmail", required = false) String sEmail,
  538. // @RequestParam(value = "sDiDong", required = false) String sDiDong,
  539. // @RequestParam(value = "sMaNguoiDung", required = false) String sMaNguoiDung,
  540. // @RequestParam(value = "trangThai", required = false) String trangThai,
  541. // @RequestParam(value = "tenCTCK", required = false) String tenCTCK,
  542. // @RequestParam(value = "nhomNguoiDungId", required = false) Integer nhomNguoiDungId,
  543. // @RequestParam(value = "loaiNguoiDung", required = false) Integer loaiNguoiDung,
  544. // @RequestParam(value = "trangThaiCTCK", required = false) Integer trangThaiCTCK) {
  545. //
  546. // if (!checkKeySort(keySort)) {
  547. //
  548. // QtNguoiDungBDTO bdto = new QtNguoiDungBDTO();
  549. // bdto.setMessage("truyền sai keySort");
  550. //
  551. // return new ResponseEntity<QtNguoiDungBDTO>(bdto, HttpStatus.METHOD_NOT_ALLOWED);
  552. // }
  553. //
  554. // QtNguoiDungBDTO lstQtNguoiDung = new QtNguoiDungBDTO();
  555. //
  556. // try {
  557. //
  558. // logger.info(Constants.Logs.LIST);
  559. // if (pageNo > 0) {
  560. // pageNo = pageNo - 1;
  561. // }
  562. //
  563. // lstQtNguoiDung = qtService.listQtNguoiDungs(strfilter, dmChucVuId, hoTen, taiKhoan, sEmail, sDiDong,
  564. // sMaNguoiDung, pageNo, pageSize, keySort, desc, trangThai, tenCTCK, trangThaiCTCK, nhomNguoiDungId,
  565. // loaiNguoiDung);
  566. // return new ResponseEntity<QtNguoiDungBDTO>(lstQtNguoiDung, HttpStatus.OK);
  567. //
  568. // } catch (Exception e) {
  569. //
  570. // e.printStackTrace();
  571. // logger.error(e.toString());
  572. // return new ResponseEntity<QtNguoiDungBDTO>(lstQtNguoiDung, HttpStatus.INTERNAL_SERVER_ERROR);
  573. // }
  574. // }
  575. // @RequestMapping(value = "/qtnguoidung/export", method = RequestMethod.GET)
  576. // public ResponseEntity<ByteArrayResource> ExportExcel(
  577. // @Valid @RequestParam(value = "strfilter", required = false) String strfilter,
  578. // @RequestParam(name = "pageNo", defaultValue = "0") Integer pageNo,
  579. // @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
  580. // @RequestParam(name = "keySort", defaultValue = "id") String keySort,
  581. // @RequestParam(name = "desc", defaultValue = "false") boolean desc,
  582. // @RequestParam(value = "dmChucVuId", required = false) Integer dmChucVuId,
  583. // @RequestParam(value = "hoTen", required = false) String hoTen,
  584. // @RequestParam(value = "taiKhoan", required = false) String taiKhoan,
  585. // @RequestParam(value = "sEmail", required = false) String sEmail,
  586. // @RequestParam(value = "sDiDong", required = false) String sDiDong,
  587. // @RequestParam(value = "sMaNguoiDung", required = false) String sMaNguoiDung,
  588. // @RequestParam(value = "trangThai", required = false) String trangThai,
  589. // @RequestParam(value = "tenCTCK", required = false) String tenCTCK,
  590. // @RequestParam(value = "nhomNguoiDungId", required = false) Integer nhomNguoiDungId,
  591. // @RequestParam(value = "loaiNguoiDung", required = false) Integer loaiNguoiDung,
  592. // @RequestParam(value = "trangThaiCTCK", required = false) Integer trangThaiCTCK) throws ApiRequestException {
  593. //
  594. // QtNguoiDungBDTO lstQtNguoiDung = new QtNguoiDungBDTO();
  595. // try {
  596. // Workbook workbook = new XSSFWorkbook();
  597. //
  598. // lstQtNguoiDung = qtService.listQtNguoiDungs(strfilter, dmChucVuId, hoTen, taiKhoan, sEmail, sDiDong,
  599. // sMaNguoiDung, 0, 999999999, keySort, desc, trangThai, tenCTCK, trangThaiCTCK, nhomNguoiDungId,
  600. // loaiNguoiDung);
  601. //
  602. // Sheet sheet = workbook.createSheet("Danh sách người dùng");
  603. //
  604. // CellStyle style = CellConfig.createStyleForTitleTBCN(sheet, workbook);
  605. // CellStyle style1 = CellConfig.createCellStyle(sheet, workbook, true);
  606. //
  607. // Cell cell;
  608. // Row row;
  609. // Integer rownum = 0;
  610. // row = sheet.createRow((rownum));
  611. // cell = row.createCell(0, CellType.STRING);
  612. // sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
  613. // cell.setCellValue(("Danh sách người dùng".toUpperCase()));
  614. // cell.setCellStyle(style1);
  615. //
  616. // // row bat dau
  617. // rownum = 1;
  618. // // Tạo tiêu đề
  619. // row = sheet.createRow(rownum);
  620. //
  621. // cell = row.createCell(0, CellType.STRING);
  622. // cell.setCellValue("Tài khoản");
  623. // cell.setCellStyle(style);
  624. //
  625. // cell = row.createCell(1, CellType.STRING);
  626. // cell.setCellValue("Họ tên");
  627. // cell.setCellStyle(style);
  628. //
  629. // cell = row.createCell(2, CellType.STRING);
  630. // cell.setCellValue("Mật khẩu mặc định");
  631. // cell.setCellStyle(style);
  632. //
  633. // cell = row.createCell(3, CellType.STRING);
  634. // cell.setCellValue("Nhóm người dùng");
  635. // cell.setCellStyle(style);
  636. //
  637. //// cell = row.createCell(4, CellType.STRING);
  638. //// cell.setCellValue("Công ty");
  639. //// cell.setCellStyle(style);
  640. //
  641. // cell = row.createCell(4, CellType.STRING);
  642. // cell.setCellValue("Trạng thái kích hoạt");
  643. // cell.setCellStyle(style);
  644. //
  645. // cell = row.createCell(5, CellType.STRING);
  646. // cell.setCellValue("Loại người dùng");
  647. // cell.setCellStyle(style);
  648. //
  649. // cell = row.createCell(6, CellType.STRING);
  650. // cell.setCellValue("Sử dụng chữ ký số");
  651. // cell.setCellStyle(style);
  652. //
  653. // if (lstQtNguoiDung != null && lstQtNguoiDung.getLstQtNguoiDung().size() > 0) {
  654. //
  655. // for (NguoiDungJoinAllDTO item : lstQtNguoiDung.getLstQtNguoiDung()) {
  656. // rownum++;
  657. // row = sheet.createRow(rownum);
  658. //
  659. // cell = row.createCell(0, CellType.STRING);
  660. // cell.setCellValue(item.getTaiKhoan());
  661. // CellConfig.BorderTable(cell, sheet);
  662. //
  663. // cell = row.createCell(1, CellType.STRING);
  664. // cell.setCellValue(item.getHoTen());
  665. // CellConfig.BorderTable(cell, sheet);
  666. //
  667. // cell = row.createCell(2, CellType.STRING);
  668. // cell.setCellValue(item.getMatKhauDefault());
  669. // CellConfig.BorderTable(cell, sheet);
  670. //
  671. // cell = row.createCell(3, CellType.STRING);
  672. // cell.setCellValue(item.getTenNhomNguoiDung());
  673. // CellConfig.BorderTable(cell, sheet);
  674. //
  675. //// String congTy = "";
  676. //// List<CtckThongTinDTO> lstCongTys = item.getLis;
  677. //// for (ChucVuDTO temp : lstChucVu) {
  678. //// if (temp != null) {
  679. //// chucVu += temp.getTenChucVu() == null ? "" : temp.getTenChucVu() + "/";
  680. //// }
  681. //// }
  682. // String trangThaiKichHoat = "";
  683. // if (item.getTrangThai() == true) {
  684. // trangThaiKichHoat = "Đã kích hoạt";
  685. // } else {
  686. // trangThaiKichHoat = "Chưa kích hoạt";
  687. // }
  688. //
  689. // cell = row.createCell(4, CellType.STRING);
  690. // cell.setCellValue(trangThaiKichHoat);
  691. // CellConfig.BorderTable(cell, sheet);
  692. //
  693. // String thanhVien = "";
  694. // if (item.getThanhVien() == true) {
  695. // thanhVien = "Thành viên";
  696. // } else {
  697. // thanhVien = "UBCK";
  698. // }
  699. //
  700. // cell = row.createCell(5, CellType.STRING);
  701. // cell.setCellValue(thanhVien);
  702. // CellConfig.BorderTable(cell, sheet);
  703. //
  704. // String chuKySo = "";
  705. // if (item.getChuKySo() == true) {
  706. // chuKySo = "Có";
  707. // } else {
  708. // chuKySo = "Không";
  709. // }
  710. //
  711. // cell = row.createCell(6, CellType.STRING);
  712. // cell.setCellValue(chuKySo);
  713. // CellConfig.BorderTable(cell, sheet);
  714. //
  715. // }
  716. // for (int i = 0; i < 7; i++) {
  717. // sheet.autoSizeColumn(i);
  718. // }
  719. //
  720. // CellConfig.BorderTable(cell, sheet);
  721. // }
  722. // CellConfig.autoSizeColumns(workbook);
  723. //
  724. // String fileName = "danh_sach_nguoi_dung"
  725. // + TimestampUtils.TimestampToString_ddMMyyyyHHmmFileName(Utils.getCurrentDate()) + ".xlsx";
  726. //
  727. // try {
  728. // FileOutputStream out = new FileOutputStream(fileName);
  729. // workbook.write(out);
  730. // ByteArrayOutputStream bos = new ByteArrayOutputStream();
  731. // workbook.write(bos);
  732. //
  733. // byte[] bytes = bos.toByteArray();
  734. // ByteArrayResource resource = new ByteArrayResource(bytes);
  735. //
  736. // out.close();
  737. // workbook.close();
  738. // return ResponseEntity.ok()
  739. // // Content-Disposition
  740. // // Content-Type
  741. // // .contentType(mediaType) //
  742. // // Content-Lengh
  743. // .contentLength(bytes.length) //
  744. // .body(resource);
  745. // } catch (FileNotFoundException e) {
  746. // // TODO Auto-generated catch block
  747. // e.printStackTrace();
  748. // } catch (IOException e) {
  749. // // TODO Auto-generated catch block
  750. // e.printStackTrace();
  751. // }
  752. // return null;
  753. // } catch (Exception e) {
  754. // logger.error(e.toString());
  755. // return null;
  756. // }
  757. // }
  758. /**
  759. * Danh sach user online
  760. *
  761. * @param strfilter
  762. * @param pageNo
  763. * @param pageSize
  764. * @param keySort
  765. * @param desc
  766. * @param dmChucVuId
  767. * @param hoTen
  768. * @param taiKhoan
  769. * @param sEmail
  770. * @param sDiDong
  771. * @param sMaNguoiDung
  772. * @param trangThai
  773. * @return listQtNguoiDungLogins
  774. */
  775. @RequestMapping(value = "/qtnguoidung/filterlogin", method = RequestMethod.GET)
  776. public ResponseEntity<QtNguoiDungBDTO> listQtNguoiDungLogins(
  777. @Valid @RequestParam(value = "strfilter", required = false) String strfilter,
  778. @RequestParam(name = "pageNo", defaultValue = "0") Integer pageNo,
  779. @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
  780. @RequestParam(name = "keySort", defaultValue = "id") String keySort,
  781. @RequestParam(name = "desc", defaultValue = "false") boolean desc,
  782. @RequestParam(value = "dmChucVuId", required = false) Integer dmChucVuId,
  783. @RequestParam(value = "hoTen", required = false) String hoTen,
  784. @RequestParam(value = "taiKhoan", required = false) String taiKhoan,
  785. @RequestParam(value = "sEmail", required = false) String sEmail,
  786. @RequestParam(value = "sDiDong", required = false) String sDiDong,
  787. @RequestParam(value = "sMaNguoiDung", required = false) String sMaNguoiDung,
  788. @RequestParam(value = "trangThai", required = false) String trangThai) {
  789. if (!checkKeySort(keySort)) {
  790. QtNguoiDungBDTO bdto = new QtNguoiDungBDTO();
  791. bdto.setMessage("truyền sai keySort");
  792. return new ResponseEntity<QtNguoiDungBDTO>(bdto, HttpStatus.METHOD_NOT_ALLOWED);
  793. }
  794. QtNguoiDungBDTO lstQtNguoiDung = new QtNguoiDungBDTO();
  795. try {
  796. logger.info(Constants.Logs.LIST);
  797. if (pageNo > 0) {
  798. pageNo = pageNo - 1;
  799. }
  800. lstQtNguoiDung = qtService.listQtNguoiDungLogins(strfilter, dmChucVuId, hoTen, taiKhoan, sEmail, sDiDong,
  801. sMaNguoiDung, pageNo, pageSize, keySort, desc, trangThai);
  802. return new ResponseEntity<QtNguoiDungBDTO>(lstQtNguoiDung, HttpStatus.OK);
  803. } catch (Exception e) {
  804. logger.error(e.toString());
  805. return new ResponseEntity<QtNguoiDungBDTO>(lstQtNguoiDung, HttpStatus.INTERNAL_SERVER_ERROR);
  806. }
  807. }
  808. @RequestMapping(value = "/qtnguoidung/getall", method = RequestMethod.GET)
  809. public ResponseEntity<QtNguoiDungBDTO> listQtNguoiDungs(
  810. @RequestParam(name = "keySort", defaultValue = "id") String keySort,
  811. @RequestParam(name = "desc", defaultValue = "false") boolean desc,
  812. @RequestParam(value = "trangThai", required = false) String trangThai) {
  813. if (!checkKeySort(keySort)) {
  814. QtNguoiDungBDTO bdto = new QtNguoiDungBDTO();
  815. bdto.setMessage("truyền sai keySort");
  816. return new ResponseEntity<QtNguoiDungBDTO>(bdto, HttpStatus.METHOD_NOT_ALLOWED);
  817. }
  818. QtNguoiDungBDTO lstQtNguoiDung = new QtNguoiDungBDTO();
  819. try {
  820. logger.info(Constants.Logs.LIST);
  821. lstQtNguoiDung = qtService.listAllNguoiDung(keySort, desc, trangThai);
  822. return new ResponseEntity<QtNguoiDungBDTO>(lstQtNguoiDung, HttpStatus.OK);
  823. } catch (Exception e) {
  824. logger.error(e.toString());
  825. return new ResponseEntity<QtNguoiDungBDTO>(lstQtNguoiDung, HttpStatus.INTERNAL_SERVER_ERROR);
  826. }
  827. }
  828. /**
  829. * Create
  830. *
  831. * @param qt json object
  832. * @return ResponseEntity
  833. * @throws ApiRequestException
  834. * @throws Exception
  835. */
  836. @PostMapping("/qtnguoidung/themmoi")
  837. public ResponseEntity<?> createQtNguoiDung(@Valid @RequestBody QtNguoiDungDTO dto,
  838. @Context HttpServletRequest request) throws ApiRequestException {
  839. logger.info(Constants.Logs.CREATE);
  840. if(dto.getTaiKhoan() != null) {
  841. dto.setTaiKhoan(dto.getTaiKhoan().replaceAll(" ", ""));
  842. }
  843. if (!qtService.isExistQtNguoiDung(dto)) {
  844. try {
  845. this.ConvertData(dto);
  846. if (!StringUtils.isEmpty(dto.getAnhDaiDien())) {
  847. dto.setAnhDaiDien(Utils.CommonSavePathFile(dto.getAnhDaiDien(),
  848. Constants.FolderUpload.FOLDER_NGUOIDUNG, request));
  849. }
  850. qtService.saveQtNguoiDung(dto);
  851. // Send Mail
  852. if (dto.getEmail() != null && !dto.getEmail().equals("")) {
  853. String emailTo = dto.getEmail();
  854. String tieuDe = "Dang ky tai khoan He thong quan ly cong ty chung khoan SCMS";
  855. String noiDung = "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" style=\"padding-bottom:20px;max-width:516px;min-width:220px\"><tbody><tr><td width=\"8\" style=\"width:8px\"></td><td><div style=\"border-style:solid;border-width:thin;border-color:#dadce0;border-radius:8px;padding:40px 20px\" align=\"center\" class=\"m_205523402140519040mdv2rw\">"
  856. + "<img src=\"http://scms.tinhvan.com/img/logo.png\" width=\"74\" height=\"74\" aria-hidden=\"true\" style=\"margin-bottom:16px\" alt=\"SCMS\" class=\"CToWUd\"><div style=\"font-family:'Google Sans',Roboto,RobotoDraft,Helvetica,Arial,sans-serif;border-bottom:thin solid #dadce0;color:rgba(0,0,0,0.87);line-height:32px;padding-bottom:24px;text-align:center;word-break:break-word\"><div style=\"font-size:24px\"><a style=\"text-decoration:none;color:rgba(0,0,0,0.87)\">Bạn đã đăng ký thành công tài khoản</a></div></div><div style=\"font-family:Roboto-Regular,Helvetica,Arial,sans-serif;font-size:14px;color:rgba(0,0,0,0.87);line-height:20px;padding-top:20px;text-align:left\"><h2><b>Thông tin tài khoản</b></h2><p><b>Tài khoản: </b><a style=\"text-decoration:none;color:rgba(0,0,0,0.87)\">"
  857. + dto.getTaiKhoan()
  858. + "</a></p><p><b>Mật khẩu: </b><a style=\"text-decoration:none;color:rgba(0,0,0,0.87)\">"
  859. + dto.getMatKhauDefault() + "</a></p><p>Truy cập hệ thống "
  860. + "<a href='http://scms.tinhvan.com/scms/login'><b>tại đây</b></a> và <b>ĐỔI MẬT KHẨU</b> ngay lập tức để chủ động bảo vệ tài khoản của bạn!.</p><p><i>Mọi thắc mắc liên quan đến tài khoản, vui lòng liên hệ với quản trị hệ thống.</i></p></div></div><div style=\"text-align:left\"><div style=\"font-family:Roboto-Regular,Helvetica,Arial,sans-serif;color:rgba(0,0,0,0.54);font-size:11px;line-height:18px;padding-top:12px;text-align:center\"><div>Vui lòng giữ bảo mật thông tin tài khoản đã được cấp</div><div style=\"direction:ltr\">© 2020 SCMS - Ủy ban chứng khoán nhà nước</div></div></div></td><td width=\"8\" style=\"width:8px\"></td></tr></tbody></table>";
  861. SendMail.SendMail(emailTo, tieuDe, noiDung);
  862. }
  863. } catch (Exception e) {
  864. throw new ApiRequestException(Constants.Messages.CREATE_FAIL, HttpStatus.EXPECTATION_FAILED);
  865. }
  866. return new ResponseEntity<QtNguoiDungDTO>(dto, HttpStatus.CREATED);
  867. } else {
  868. throw new ApiRequestException(Constants.Messages.RC_EXIST, HttpStatus.EXPECTATION_FAILED);
  869. }
  870. // throw new ApiRequestException(Constants.Messages.RC_EXIST, HttpStatus.EXPECTATION_FAILED);
  871. }
  872. /**
  873. * @throws ParseException
  874. *
  875. */
  876. private void ConvertData(QtNguoiDungDTO dto) throws ParseException {
  877. if (dto.getNgayHetHanStr() != null && (!"".endsWith(dto.getNgayHetHanStr()))) {
  878. dto.setNgayHetHan(
  879. new java.sql.Timestamp(new SimpleDateFormat("dd/MM/yyyy").parse(dto.getNgayHetHanStr()).getTime()));
  880. }
  881. }
  882. /**
  883. * cập nhập người dùng
  884. *
  885. * @param qtBdto 1 đối tượng chứa id> 0
  886. * @return ResponseEntity
  887. * @throws ApiRequestException
  888. */
  889. @PutMapping("/qtnguoidung/capnhat")
  890. public ResponseEntity<?> updateQtNguoiDung(@Valid @RequestBody QtNguoiDungDTO dto,
  891. @Context HttpServletRequest request) throws ApiRequestException {
  892. try {
  893. logger.info(Constants.Logs.UPDATE);
  894. // check upload file
  895. String PathUpload_update = "";
  896. if (dto.getArrFileDinhKem() != null && !dto.getArrFileDinhKem().isEmpty()) {
  897. for (UploadFileDefaultDTO e : dto.getArrFileDinhKem()) {
  898. if (!StringUtils.isEmpty(e.getNameFile())) {
  899. PathUpload_update += e.getPath() + ",";
  900. }
  901. }
  902. if (PathUpload_update != "") {
  903. PathUpload_update = PathUpload_update.substring(0, PathUpload_update.length() - 1);
  904. }
  905. }
  906. // add file moi
  907. // if (dto.getArrFileDinhKem() != null) {
  908. if (!StringUtils.isEmpty(dto.getAnhDaiDien())) {
  909. dto.setAnhDaiDien(Utils.CommonSavePathFile(dto.getAnhDaiDien(), Constants.FolderUpload.FOLDER_NGUOIDUNG,
  910. request));
  911. // String UrlPathCDNLQ = Constants.FolderUpload.FOLDER_NGUOIDUNG;
  912. // String[] splipPath = dto.getAnhDaiDien().split("`");
  913. // String pathNew = "";
  914. // if (splipPath.length > 0) {
  915. // for (int i = 0; i < splipPath.length; i++) {
  916. // String path = splipPath[i];
  917. // String[] pathRelative = path.split("~");
  918. // if (pathRelative.length > 0 && pathRelative.length < 3) {
  919. // //System.out.println(pathRelative[1]);
  920. // //System.out.println(request.getServletContext().getRealPath("/") + pathRelative[0]);
  921. // File file = new File(pathRelative[0]);
  922. // Path tmpPath = Paths.get(UrlPathCDNLQ);
  923. // String tmp = request.getServletContext().getRealPath("/") + tmpPath.toFile();
  924. // try {
  925. // Utils.MoveFileToDirectory(file, tmp);
  926. // } catch (IOException e) {
  927. //
  928. // throw new ApiRequestException("Đăng tải tài liệu không thành công!",
  929. // HttpStatus.EXPECTATION_FAILED);
  930. // }
  931. // pathNew += UrlPathCDNLQ + pathRelative[1] + ",";
  932. //
  933. // }
  934. //
  935. // }
  936. //
  937. // }
  938. // if (pathNew != null) {
  939. // pathNew = pathNew.substring(0, pathNew.length() - 1);
  940. // if (PathUpload_update != "") {
  941. // PathUpload_update = PathUpload_update + "," + pathNew;
  942. // } else {
  943. // PathUpload_update = pathNew;
  944. // }
  945. //
  946. // }
  947. //
  948. // }
  949. // PathUpload_update = PathUpload_update.replace("\\", "/");
  950. // dto.setAnhDaiDien(PathUpload_update);
  951. } else {
  952. dto.setAnhDaiDien(dto.getAnhDaiDien().split("~")[0]);
  953. }
  954. if (qtService.isExistById(dto.getId())) {
  955. try {
  956. this.ConvertData(dto);
  957. qtService.updateQtNguoiDung(dto);
  958. // lấy dữ liệu cũ trươc skhi add
  959. QtNguoiDungDTO oldObject = this.qtService.findById(dto.getId());
  960. QtNguoiDungDTO newObject = qtService.saveQtNguoiDungGetObject(dto);
  961. if (!StringUtils.isEmpty(oldObject.getAnhDaiDien())) {
  962. // lấy các path của old và new
  963. String[] pathOld = oldObject.getAnhDaiDien().split(",");
  964. String[] pathNew = newObject.getAnhDaiDien().split(",");
  965. List<String> oldList = null;
  966. List<String> NewList = null;
  967. if (pathOld.length > 0) {
  968. oldList = Arrays.asList(pathOld);
  969. }
  970. if (pathNew.length > 0) {
  971. NewList = Arrays.asList(pathNew);
  972. }
  973. // biến chứa path cần remove
  974. List<String> pathRemove = new ArrayList<>();
  975. // check các trường hợp để add vào pathRemove
  976. if (oldList != null && NewList == null) {
  977. pathRemove.addAll(oldList);
  978. } else if (oldList != null && NewList != null) {
  979. for (String path : oldList) {
  980. if (!StringUtils.isEmpty(path) && !NewList.contains(path)) {
  981. pathRemove.add(path);
  982. }
  983. }
  984. }
  985. // kiểm tra nếu path cần xóa thì xóa
  986. if (!pathRemove.isEmpty()) {
  987. for (String pathx : pathRemove) {
  988. Utils.DeleteFile(Constant.UPLOADNOTTEMPFOLDER + pathx);
  989. }
  990. }
  991. }
  992. } catch (Exception e) {
  993. logger.error(e.getMessage());
  994. throw new ApiRequestException(Constants.Messages.UPDATE_FAIL, HttpStatus.EXPECTATION_FAILED);
  995. }
  996. return new ResponseEntity<QtNguoiDungDTO>(dto, HttpStatus.ACCEPTED);
  997. } else {
  998. throw new ApiRequestException(Constants.Messages.RC_NOT_EXIST, HttpStatus.NOT_FOUND);
  999. }
  1000. } catch (Exception e) {
  1001. logger.error(e.getMessage());
  1002. throw new ApiRequestException(Constants.Messages.UPDATE_FAIL, HttpStatus.EXPECTATION_FAILED);
  1003. }
  1004. }
  1005. /**
  1006. * cập nhập người dùng
  1007. *
  1008. * @param qtBdto 1 đối tượng chứa id> 0
  1009. * @return ResponseEntity
  1010. * @throws ApiRequestException
  1011. */
  1012. @PutMapping("/phanquyennguoidung/")
  1013. public ResponseEntity<?> phanQuyen(@RequestBody NguoiDungJoinAllDTO dto, @Context HttpServletRequest request)
  1014. throws ApiRequestException {
  1015. logger.info(Constants.Logs.UPDATE);
  1016. if (qtService.isExistById(dto.getId())) {
  1017. try {
  1018. QtNguoiDungDTO userInfo = UserInfoGlobal.getUserInfoAuthor();
  1019. Timestamp timeStamp = new Timestamp(new Date().getTime());
  1020. qtService.phanQuyenChucNang(dto);
  1021. QtLogHeThongDTO dtoLog = new QtLogHeThongDTO();
  1022. // set IP thực hiện lấy IP remote client
  1023. dtoLog.setIpThucHien(Utils.getClientIp(request));
  1024. dtoLog.setLogType(Constants.LogSystem.LOG_SYS_ADD + "_NGUOI_DUNG");
  1025. dtoLog.setNgayTao(timeStamp);
  1026. dtoLog.setNguoiTaoId(userInfo.getId());
  1027. dtoLog.setNoiDung("Tài khoản " + userInfo.getTaiKhoan() + " phân quyền cho người dùng " + dto.getHoTen()
  1028. + " với tài khoản : " + dto.getTaiKhoan());
  1029. // save db
  1030. WriteLogInDataBaseGlobal.WriteLogDB(dtoLog);
  1031. } catch (Exception e) {
  1032. logger.error(e.getMessage());
  1033. throw new ApiRequestException(Constants.Messages.UPDATE_FAIL, HttpStatus.EXPECTATION_FAILED);
  1034. }
  1035. return new ResponseEntity<NguoiDungJoinAllDTO>(dto, HttpStatus.ACCEPTED);
  1036. } else {
  1037. throw new ApiRequestException(Constants.Messages.RC_NOT_EXIST, HttpStatus.NOT_FOUND);
  1038. }
  1039. }
  1040. @RequestMapping(value = "/qtnguoidung/changestatus", method = RequestMethod.GET)
  1041. public ResponseEntity<?> changeStatus(@RequestParam(value = "id", defaultValue = "0") Integer id,
  1042. @RequestParam(name = "trangThai", defaultValue = "false") boolean trangThai,
  1043. @RequestParam(name = "thanhVien", defaultValue = "false") boolean thanhVien,
  1044. @RequestParam(name = "chuKySo", defaultValue = "false") boolean chuKySo,
  1045. @Context HttpServletRequest request) throws ApiRequestException {
  1046. try {
  1047. QtNguoiDungDTO userInfo = UserInfoGlobal.getUserInfoAuthor();
  1048. Timestamp timeStamp = new Timestamp(new Date().getTime());
  1049. logger.info(Constants.Logs.UPDATE);
  1050. if (qtService.isExistById(id)) {
  1051. qtService.changeStatusQtNguoiDung(id, trangThai, thanhVien, chuKySo);
  1052. QtLogHeThongDTO dtoLog = new QtLogHeThongDTO();
  1053. // set IP thực hiện lấy IP remote client
  1054. dtoLog.setIpThucHien(Utils.getClientIp(request));
  1055. dtoLog.setLogType(Constants.LogSystem.LOG_SYS_UPDATE + "_NGUOI_DUNG");
  1056. dtoLog.setNgayTao(timeStamp);
  1057. dtoLog.setNguoiTaoId(userInfo.getId());
  1058. dtoLog.setNoiDung(
  1059. "Tài khoản " + userInfo.getTaiKhoan() + " cập nhật trạng thái cho người dùng ID = " + id);
  1060. // save db
  1061. WriteLogInDataBaseGlobal.WriteLogDB(dtoLog);
  1062. return new ResponseEntity<String>("", HttpStatus.ACCEPTED);
  1063. }
  1064. return new ResponseEntity<String>(Constants.Messages.RC_EXIST, HttpStatus.ACCEPTED);
  1065. } catch (Exception e) {
  1066. logger.error(e.getMessage());
  1067. throw new ApiRequestException(Constants.Messages.UPDATE_FAIL, HttpStatus.EXPECTATION_FAILED);
  1068. }
  1069. }
  1070. @RequestMapping(value = "/qtnguoidung/changepassword", method = RequestMethod.GET)
  1071. public ResponseEntity<?> changePassword(@RequestParam(value = "id", defaultValue = "0") Integer id,
  1072. @RequestParam(value = "matKhauCu", required = false) String matKhauCu,
  1073. @RequestParam(value = "matKhauMoi", required = false) String matKhauMoi,
  1074. @RequestParam(value = "matKhauMoiConfirm", required = false) String matKhauMoiConfirm,
  1075. @Context HttpServletRequest request) throws ApiRequestException {
  1076. try {
  1077. logger.info(Constants.Logs.UPDATE);
  1078. QtNguoiDungDTO userInfo = UserInfoGlobal.getUserInfoAuthor();
  1079. id = userInfo.getId();
  1080. if (qtService.isExistById(id)) {
  1081. String validateStr = validateData(id, matKhauCu, matKhauMoi, matKhauMoiConfirm);
  1082. if (validateStr.equals("")) {
  1083. Timestamp timeStamp = new Timestamp(new Date().getTime());
  1084. qtService.changePasswordQtNguoiDung(id, matKhauMoi);
  1085. QtLogHeThongDTO dtoLog = new QtLogHeThongDTO();
  1086. // set IP thực hiện lấy IP remote client
  1087. dtoLog.setIpThucHien(Utils.getClientIp(request));
  1088. dtoLog.setLogType(Constants.LogSystem.LOG_SYS_UPDATE + "_NGUOI_DUNG");
  1089. dtoLog.setNgayTao(timeStamp);
  1090. dtoLog.setNguoiTaoId(userInfo.getId());
  1091. dto