UserChannelServiceImpl.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. package com.goafanti.user.service.impl;
  2. import java.text.DecimalFormat;
  3. import java.text.SimpleDateFormat;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.Calendar;
  7. import java.util.Date;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11. import java.util.UUID;
  12. import javax.annotation.Resource;
  13. import org.apache.poi.hssf.usermodel.HSSFDateUtil;
  14. import org.apache.poi.ss.usermodel.Cell;
  15. import org.apache.poi.ss.usermodel.CellType;
  16. import org.apache.poi.ss.usermodel.Row;
  17. import org.apache.poi.ss.usermodel.Sheet;
  18. import org.apache.poi.ss.usermodel.Workbook;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.stereotype.Service;
  21. import org.springframework.web.multipart.MultipartFile;
  22. import com.goafanti.common.bo.CityBo;
  23. import com.goafanti.common.bo.Error;
  24. import com.goafanti.common.bo.ProvinceBo;
  25. import com.goafanti.common.constant.AFTConstants;
  26. import com.goafanti.common.dao.DistrictGlossoryMapper;
  27. import com.goafanti.common.dao.OrganizationContactBookMapper;
  28. import com.goafanti.common.dao.OrganizationIdentityMapper;
  29. import com.goafanti.common.dao.UserChannelMapper;
  30. import com.goafanti.common.dao.UserMapper;
  31. import com.goafanti.common.dao.UserTransferLogMapper;
  32. import com.goafanti.common.enums.ChannelStatus;
  33. import com.goafanti.common.enums.SocietyTag;
  34. import com.goafanti.common.error.BusinessException;
  35. import com.goafanti.common.model.User;
  36. import com.goafanti.common.utils.ExcelUtils;
  37. import com.goafanti.common.utils.LoggerUtils;
  38. import com.goafanti.common.utils.PasswordUtil;
  39. import com.goafanti.common.utils.StringUtils;
  40. import com.goafanti.core.mybatis.BaseMybatisDao;
  41. import com.goafanti.core.mybatis.page.Pagination;
  42. import com.goafanti.core.shiro.token.TokenManager;
  43. import com.goafanti.user.bo.InputUserChannel;
  44. import com.goafanti.user.bo.OutUserChannel;
  45. import com.goafanti.user.service.UserChannelService;
  46. @Service
  47. public class UserChannelServiceImpl extends BaseMybatisDao<UserChannelMapper> implements UserChannelService{
  48. @Autowired
  49. private UserMapper userMapper;
  50. @Autowired
  51. private OrganizationIdentityMapper organizationIdentityMapper;
  52. @Autowired
  53. private OrganizationContactBookMapper organizationContactBookMapper;
  54. @Autowired
  55. private DistrictGlossoryMapper districtGlossoryMapper;
  56. @Autowired
  57. private UserTransferLogMapper userTransferLogMapper;
  58. @Autowired
  59. private UserChannelMapper userChannelMapper;
  60. @Resource(name = "passwordUtil")
  61. private PasswordUtil passwordUtil;
  62. @Override
  63. public Object add(InputUserChannel in) {
  64. String uid=UUID.randomUUID().toString();
  65. Calendar now = Calendar.getInstance();
  66. now.set(Calendar.MILLISECOND, 0);
  67. in.setAid(TokenManager.getAdminId());
  68. in.setStatus(0);
  69. in.setUid(uid);
  70. User u=new User();
  71. u.setId(uid);
  72. u.setNickname(in.getUserName());
  73. u.setCreateTime(now.getTime());
  74. u.setPassword(passwordUtil.getEncryptPwd("123456", now.getTime()));
  75. userChannelMapper.insertSelective(in);
  76. userMapper.insertSelective(u);
  77. return 1;
  78. }
  79. @Override
  80. public Object batchListReceivables(MultipartFile file) {
  81. //获得Workbook工作薄对象
  82. Workbook workbook = ExcelUtils.getWorkBook(file);
  83. //创建一个对象,用来存储数据
  84. List<InputUserChannel> count=new ArrayList<>();
  85. List<InputUserChannel> list=new ArrayList<>();
  86. List<InputUserChannel> list2=new ArrayList<>();
  87. List<InputUserChannel> list3=new ArrayList<>();
  88. String aid=TokenManager.getAdminId();
  89. Calendar now = Calendar.getInstance();
  90. now.set(Calendar.MILLISECOND, 0);
  91. if(workbook != null){
  92. //获得当前sheet工作表
  93. Sheet sheet = workbook.getSheetAt(0);
  94. if(sheet == null){
  95. return null;
  96. }
  97. //获得当前sheet的开始行
  98. int firstRowNum = sheet.getFirstRowNum();
  99. //获得当前sheet的结束行
  100. int lastRowNum = sheet.getLastRowNum();
  101. //处理空白行
  102. for (int i = firstRowNum; i <= sheet.getLastRowNum();i++) {
  103. Row r = sheet.getRow(i);
  104. if(r == null){
  105. // 如果是空行(即没有任何数据、格式),直接把它以下的数据往上移动
  106. sheet.shiftRows(i+1, sheet.getLastRowNum(),-1);
  107. continue;
  108. }
  109. boolean flag = false;
  110. for(Cell c:r){
  111. if(c.getCellTypeEnum() != CellType.BLANK){
  112. flag = true;
  113. break;
  114. }
  115. }
  116. if(flag){
  117. continue;
  118. } else{//如果是空白行(即可能没有数据,但是有一定格式)
  119. if(i == sheet.getLastRowNum())//如果到了最后一行,直接将那一行remove掉
  120. sheet.removeRow(r);
  121. else//如果还没到最后一行,则数据往上移一行
  122. sheet.shiftRows(i+1, sheet.getLastRowNum(),-1);
  123. }
  124. }
  125. lastRowNum=sheet.getLastRowNum();
  126. if (lastRowNum<2) {
  127. throw new BusinessException(new Error("未找到正确的参数。"));
  128. }
  129. //循环第2行后的所有行
  130. for(int rowNum = firstRowNum+2;rowNum <= lastRowNum;rowNum++){
  131. //获得当前行
  132. Row row = sheet.getRow(rowNum);
  133. if(row == null){
  134. continue;
  135. }
  136. //获得当前行的开始列
  137. int firstCellNum = row.getFirstCellNum();
  138. //获得当前行的列数
  139. int lastCellNum = row.getLastCellNum();
  140. //循环当前行 cellNum = (firstCellNum+1) 则是除开第一例
  141. InputUserChannel in=new InputUserChannel();
  142. List<CityBo> cList=districtGlossoryMapper.getCity();
  143. List<ProvinceBo>pList=districtGlossoryMapper.getProvince();
  144. for(int cellNum = (firstCellNum); cellNum < lastCellNum;cellNum++){
  145. Cell cell = row.getCell(cellNum);
  146. try {
  147. switch (cellNum) {
  148. case 0:
  149. in.setUserName(getCellValue(cell));
  150. break;
  151. case 1:
  152. in.setProvince(getProvinceId(pList,getCellValue(cell)));
  153. in.setProvinces(getCellValue(cell));
  154. break;
  155. case 2:
  156. in.setCity(getCityId(cList,getCellValue(cell)));
  157. in.setCitys(getCellValue(cell));
  158. break;
  159. case 3:
  160. in.setSocietyTag(SocietyTag.getCodeByValue(getCellValue(cell)).toString());
  161. in.setSocietyTags(getCellValue(cell));
  162. break;
  163. case 4:
  164. in.setContacts(getCellValue(cell));
  165. break;
  166. case 5:
  167. in.setContactMobile(getCellValue(cell));
  168. break;
  169. case 6:
  170. in.setRemarks(getCellValue(cell));
  171. break;
  172. }
  173. } catch (Exception e) {
  174. throw new BusinessException(new Error("表格第"+(rowNum+1)+"行输入内容不完整。"));
  175. }
  176. }
  177. if (StringUtils.isBlank(in.getUserName())||
  178. StringUtils.isBlank(in.getContacts())||
  179. StringUtils.isBlank(in.getContactMobile())) {
  180. throw new BusinessException(new Error("表格第"+(rowNum+1)+"行输入内容不完整。"));
  181. }
  182. count.add(in);
  183. }
  184. }
  185. for (InputUserChannel in : count) {
  186. String uid=UUID.randomUUID().toString();
  187. in.setAid(aid);
  188. in.setCreateTime(now.getTime());
  189. in.setShareType(0);
  190. in.setPassword(passwordUtil.getEncryptPwd("123456", now.getTime()));
  191. User u=userMapper.selectByName(in.getUserName());
  192. if (checkUserName(in.getUserName())) {
  193. in.setUid(uid);
  194. in.setStatus(ChannelStatus.FFMC.getCode());
  195. in.setUserStatus(1);
  196. list3.add(in);
  197. }else if (u == null) {
  198. in.setUid(uid);
  199. in.setUserStatus(0);
  200. in.setStatus(ChannelStatus.WFP.getCode());
  201. list.add(in);
  202. }else {
  203. Integer i=0;
  204. if (u.getAid()!=null&&u.getAid().equals(TokenManager.getAdminId())) {
  205. i=1;
  206. }
  207. // 共享类型 0-私有 1-公共 2 签单
  208. if(u.getShareType()==0) {
  209. if(u.getChannel()==0) {
  210. if(i==1)in.setStatus(ChannelStatus.YCZWDSY.getCode());
  211. else in.setStatus(ChannelStatus.YCZBRSY.getCode());
  212. }
  213. else {
  214. if(i==1)in.setStatus(ChannelStatus.YCZWDQD.getCode());
  215. else in.setStatus(ChannelStatus.YCZBRQD.getCode());
  216. }
  217. }
  218. else if(u.getShareType()==1)in.setStatus(ChannelStatus.YCZGG.getCode());
  219. else if(u.getShareType()==2) {
  220. if(i==1)in.setStatus(ChannelStatus.YCZWDQIAND.getCode());
  221. else in.setStatus(ChannelStatus.YCZBRQIAND.getCode());
  222. }
  223. in.setUid(u.getId());
  224. list2.add(in);
  225. }
  226. }
  227. //检查重复名称
  228. checkListName(list);
  229. checkListName(list2);
  230. checkListName(list3);
  231. //list成功录入 list2是已存在 list3是非法名称 3个处理方式不一样,
  232. //已存在则是不用新增直接关联,非法名称则不用关联也不用新增
  233. if (!list.isEmpty()) {
  234. userMapper.insertBatch(list);
  235. organizationIdentityMapper.insertBatch(list);
  236. organizationContactBookMapper.insertBatch(list);
  237. userChannelMapper.insertBatch(list);
  238. userTransferLogMapper.insertBatch(list);
  239. }
  240. if(list2.isEmpty()&&list3.isEmpty()) {
  241. return 1;
  242. }else {
  243. if(!list3.isEmpty()){
  244. userMapper.insertBatch(list3);
  245. organizationIdentityMapper.insertBatch(list3);
  246. organizationContactBookMapper.insertBatch(list3);
  247. list2.addAll(list3);
  248. }
  249. userChannelMapper.insertBatch(list2);
  250. return getErorrList(1,10);
  251. }
  252. }
  253. private void checkListName(List<InputUserChannel> list) {
  254. for (InputUserChannel in : list) {
  255. int i=0;
  256. for (InputUserChannel in2 : list) {
  257. if (in.getUserName().equals(in2.getUserName())) {
  258. i++;
  259. if (i>1) {
  260. LoggerUtils.error(getClass(), String.format("客户名称:%s 重复录入", in.getUserName()));
  261. throw new BusinessException(String.format("客户名称:%s 重复录入", in.getUserName()));
  262. }
  263. }
  264. }
  265. }
  266. }
  267. /**
  268. * 获取Excel单元格的值
  269. *
  270. * @param cell
  271. * @return
  272. */
  273. public static String getCellValue(Cell cell) {
  274. String value = "";
  275. if (cell != null) {
  276. switch (cell.getCellTypeEnum()) {
  277. case NUMERIC: //数字
  278. value = cell.getNumericCellValue() + "";
  279. if (HSSFDateUtil.isCellDateFormatted(cell)) {
  280. Date date = cell.getDateCellValue();
  281. if (date != null) {
  282. value = new SimpleDateFormat("yyyy-MM-dd").format(date);//日期格式化
  283. } else {
  284. value = "";
  285. }
  286. } else {
  287. //在解析cell的时候,数字类型默认是double的,但是想要的的整数的类型,需要格式化,很重要
  288. value = new DecimalFormat("0").format(cell.getNumericCellValue());
  289. }
  290. break;
  291. case STRING://字符串
  292. value = cell.getStringCellValue();
  293. break;
  294. case BOOLEAN://boolean类型
  295. value = cell.getBooleanCellValue() + "";
  296. break;
  297. case BLANK://空值
  298. value = "";
  299. break;
  300. case ERROR://错误类型
  301. value = "非法字符";
  302. break;
  303. default:
  304. value = "未知类型";
  305. }
  306. }
  307. return value.trim();
  308. }
  309. private boolean checkUserName(String userName) {
  310. String str = AFTConstants.CHANNEL_SENSITIVE;
  311. String[] ss=str.split(",");
  312. for (String s : ss) {
  313. if (userName!=null&&userName.contains(s)) {
  314. return true;
  315. }
  316. }
  317. return false;
  318. }
  319. private String getCityId(List<CityBo> cList, String cellValue) {
  320. for (CityBo cityBo : cList) {
  321. if (cityBo.getName().contains(cellValue)) {
  322. return cityBo.getId().toString();
  323. }
  324. }
  325. return null;
  326. }
  327. private String getProvinceId(List<ProvinceBo> pList, String cellValue) {
  328. for (ProvinceBo provinceBo : pList) {
  329. if (provinceBo.getName().contains(cellValue)) {
  330. return provinceBo.getId().toString();
  331. }
  332. }
  333. return null;
  334. }
  335. @SuppressWarnings("unchecked")
  336. @Override
  337. public Pagination<OutUserChannel> getErorrList(Integer pageNo,Integer pageSize) {
  338. Map<String, Object> map =new HashMap<>();
  339. map.put("type", 1);
  340. map.put("aid", TokenManager.getAdminId());
  341. return (Pagination<OutUserChannel>) findPage("selectByAidAndstatusList", "selectByAidAndstatusCount", map, pageNo, pageSize);
  342. }
  343. @Override
  344. public Object delete(String ids) {
  345. String [] str=ids.split(",");
  346. List<String> list=Arrays.asList(str);
  347. userChannelMapper.deleteUser(list);
  348. return userChannelMapper.deleteByIds(list);
  349. }
  350. }