UserChannelServiceImpl.java 13 KB

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