PublicReleaseServiceImpl.java 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985
  1. package com.goafanti.weChat.service.impl;
  2. import cn.jiguang.common.utils.StringUtils;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.goafanti.admin.bo.AdminListBo;
  5. import com.goafanti.admin.service.DepartmentService;
  6. import com.goafanti.common.bo.EmailBo;
  7. import com.goafanti.common.constant.AFTConstants;
  8. import com.goafanti.common.dao.*;
  9. import com.goafanti.common.enums.NoticeStatus;
  10. import com.goafanti.common.error.BusinessException;
  11. import com.goafanti.common.model.*;
  12. import com.goafanti.common.utils.*;
  13. import com.goafanti.core.mybatis.BaseMybatisDao;
  14. import com.goafanti.core.mybatis.page.Pagination;
  15. import com.goafanti.core.shiro.token.TokenManager;
  16. import com.goafanti.core.websocket.SystemWebSocketHandler;
  17. import com.goafanti.customer.bo.FollowBusinessBo;
  18. import com.goafanti.customer.service.CustomerService;
  19. import com.goafanti.order.bo.TOrderNewBo;
  20. import com.goafanti.weChat.bo.*;
  21. import com.goafanti.weChat.service.PublicReleaseService;
  22. import org.springframework.beans.BeanUtils;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.cache.annotation.CacheEvict;
  25. import org.springframework.cache.annotation.Cacheable;
  26. import org.springframework.stereotype.Service;
  27. import org.springframework.transaction.annotation.Transactional;
  28. import org.springframework.web.socket.TextMessage;
  29. import java.math.BigDecimal;
  30. import java.text.ParseException;
  31. import java.util.*;
  32. import java.util.concurrent.atomic.AtomicReference;
  33. import java.util.stream.Collectors;
  34. @Service
  35. public class PublicReleaseServiceImpl extends BaseMybatisDao<PublicReleaseMapper> implements PublicReleaseService {
  36. @Autowired
  37. private PublicReleaseMapper publicReleaseMapper;
  38. @Autowired
  39. private PublicReleaseLogMapper publicReleaseLogMapper;
  40. @Autowired
  41. private CustomerService customerService;
  42. @Autowired
  43. private PublicReleaseDetailsMapper publicReleaseDetailsMapper;
  44. @Autowired
  45. private WeChatUtils weChatUtils;
  46. @Autowired
  47. private AdminMapper adminMapper;
  48. @Autowired
  49. private AsyncUtils asyncUtils;
  50. @Autowired
  51. private UserMapper userMapper;
  52. @Autowired
  53. private OrganizationContactBookMapper organizationContactBookMapper;
  54. @Autowired
  55. private SystemWebSocketHandler systemWebSocketHandler;
  56. @Autowired
  57. private TOrderNewMapper tOrderNewMapper;
  58. @Autowired
  59. private DepartmentService departmentService;
  60. @Autowired
  61. private PublicExamineMapper publicExamineMapper;
  62. @Autowired
  63. private AdminUserCountMapper adminUserCountMapper;
  64. @Autowired
  65. private PublicReleaseClockMapper publicReleaseClockMapper;
  66. @Autowired
  67. private PublicReleaseDateClockMapper publicReleaseDateClockMapper;
  68. @Autowired
  69. private TOrderPublicReleaseCountMapper tOrderPublicReleaseCountMapper;
  70. @Override
  71. @Transactional
  72. public Map<String, Object> addPublicRelease(InputPublicRelease in) {
  73. Date date =new Date();
  74. if (in.getPublicAgain()==null)in.setPublicAgain(0);
  75. in.setCreateTime(date);
  76. in.setPublicType(0);
  77. in.setTechStartProcess(0);
  78. in.setStatus(1);
  79. in.setAid(TokenManager.getAdminId());
  80. AdminListBo my = adminMapper.getDeptNameByAid(TokenManager.getAdminId());
  81. if (in.getAssist()==1){
  82. // in.setAssistAidName(getAssistAidName(in.getAssistAid()));
  83. in.setMainName(my.getName());
  84. }
  85. Map checkOrderNo =null;
  86. if (in.getType() == 1) {
  87. checkOrderNo = checkOrderNoDuration(in,my,1);
  88. boolean isValid = (boolean) checkOrderNo.get("code");
  89. if (isValid){
  90. in.setTechStartProcess(0);
  91. }else {
  92. in.setTechStartProcess(1);
  93. }
  94. }
  95. if (in.getValidDate()!=null){
  96. String validDate = in.getValidDate();
  97. List<Map> maps = JSONArray.parseArray(validDate, Map.class);
  98. }
  99. publicReleaseMapper.insertSelective(in);
  100. List<PublicReleaseDetails> prdList=new ArrayList<>();
  101. List<User> users=new ArrayList<>();
  102. StringBuilder str =new StringBuilder();
  103. if (in.getUids()!=null){
  104. List<String> split = Arrays.asList(in.getUids().split(","));
  105. int userCount=split.size();
  106. Double duration = in.getDuration();
  107. //计算平均
  108. double d = durationSum(duration, userCount);
  109. int index=1;
  110. //获取非已企业公出需要的审核人员
  111. for (String s : split) {
  112. PublicReleaseDetails prd=new PublicReleaseDetails();
  113. User u=userMapper.selectByPrimaryKey(s);
  114. users.add(u);
  115. str.append(u.getNickname()).append(",");
  116. if (u!=null&&u.getShareType()==2){
  117. prd.setNewUser(1);
  118. }else {
  119. prd.setNewUser(0);
  120. }
  121. if (u.getAid().equals(in.getAid())){
  122. prd.setMainStatus(1);
  123. }else {
  124. prd.setMainStatus(0);
  125. }
  126. prd.setPrid(in.getId());
  127. prd.setUid(s);
  128. prd.setSignSum(0);
  129. prd.setClockIn(0);
  130. prd.setDistrictName(in.getDistrictName());
  131. prd.setLatitude(in.getLatitude());
  132. prd.setLongitude(in.getLongitude());
  133. if (index<userCount){
  134. if (duration==0){
  135. prd.setDuration(duration);
  136. } else {
  137. prd.setDuration(d);
  138. duration=duration-d;
  139. }
  140. index++;
  141. }else {
  142. prd.setDuration(duration);
  143. }
  144. prdList.add(prd);
  145. }
  146. publicReleaseDetailsMapper.insertBatch(prdList);
  147. }
  148. addPublicReleaseDateClock(in);
  149. String userNames=str.substring(0,str.length()-1);
  150. in.setUserNames(userNames);
  151. PublicRelease updateName=new PublicRelease();
  152. updateName.setId(in.getId());
  153. updateName.setUserNames(userNames);
  154. publicReleaseMapper.updateByPrimaryKeySelective(updateName);
  155. addpublicLog(in.getId(), PublicReleaseLog.states.fq.getCode(), "发起外出申请");
  156. return (Map<String, Object>) addPublicReleaseLog(my, in,userNames,date,users,checkOrderNo);
  157. }
  158. private void addPublicExamine(Integer id, String aid) {
  159. Admin admin = adminMapper.selectByPrimaryKey(aid);
  160. addPublicExamine(id,admin);
  161. }
  162. private void addPublicExamine(Integer id, Admin a) {
  163. List<PublicExamine> publicExamines = publicExamineMapper.selectByPrid(id);
  164. boolean flag=false;
  165. for (PublicExamine pe : publicExamines) {
  166. if (pe.getAid().equals(a.getId())){
  167. flag=true;
  168. }
  169. }
  170. if (!flag){
  171. PublicExamine pe=new PublicExamine();
  172. pe.setPrid(id);
  173. pe.setAid(a.getId());
  174. pe.setAname(a.getName());
  175. pe.setStatus(0);
  176. publicExamineMapper.insertSelective(pe);
  177. }
  178. }
  179. private double durationSum(Double duration, Integer sum) {
  180. Double counts= Math.floor(duration/0.5);
  181. //计算当前时间
  182. Double my=Math.floor(counts/sum)*0.5;
  183. if (my<0.5)return 0.5;
  184. return my;
  185. }
  186. /**
  187. *
  188. * @param my 我的相关人员
  189. * @param in 公出信息
  190. * @param nickname 客户名称
  191. * @param date 时间
  192. */
  193. private Map<String, Object> addPublicReleaseLog(AdminListBo my,PublicRelease in,String nickname,Date date,List<User> users,
  194. Map<String,Object> checkOrderNo) {
  195. Map<String, Object>map=new HashMap<String, Object>();
  196. //无需订单负责人审核
  197. if (in.getTechStartProcess()==0){
  198. //获取公出审核人
  199. Admin a=adminMapper.selectByPrimaryKey(my.getReviewer());
  200. String str="["+my.getName()+"]发起["+nickname+"]的外出申请,请及时审核。";
  201. //默认上级审核
  202. AtomicReference<Integer> status= new AtomicReference<>(1);
  203. String formatDate = DateUtils.formatDate(in.getCreateTime(), AFTConstants.YYYYMMDDHHMMSS);
  204. //不是业务员
  205. if (in.getType()==0){
  206. if (my.getPublicPurview()==0){
  207. users.forEach(u->{
  208. if (!u.getAid().equals(my.getId())){
  209. StringBuffer str2=new StringBuffer();
  210. str2=str2.append(my.getName()).append("(需审核),准备公出您的企业");
  211. str2=str2.append("[").append(u.getNickname()).append("]!!!。");
  212. sendEmailAndNoticeAndSoucket(u.getAid(),3,str2.toString());
  213. Admin admin = adminMapper.selectByPrimaryKey(u.getAid());
  214. if (in.getType()==0){
  215. //不是自己的企业则需要审核并设置非自己客户待审核
  216. addPublicExamine(in.getId(), admin);
  217. if(in.getPublicType()==null|| in.getPublicType()==0)in.setPublicType(1);
  218. }
  219. pushNoticeAndSoucketAndEmail(my, in, date, admin, str, map);
  220. status.set(0);
  221. }
  222. });
  223. }else {
  224. users.forEach(u->{
  225. if (!u.getAid().equals(my.getId())){
  226. StringBuffer str2=new StringBuffer();
  227. str2=str2.append(my.getName()).append("计划公出您的企业").append(u.getNickname())
  228. .append(",").append(formatDate).append("【小程序端广播提示】");
  229. sendNoticeAndSoucket(u.getAid(),3,str2.toString());
  230. }
  231. });
  232. }
  233. }
  234. if (status.get()==1){
  235. pushNoticeAndSoucketAndEmail(my, in, date, a, str, map);
  236. }
  237. publicReleaseMapper.updateByPrimaryKeySelective(in);
  238. }else {
  239. TOrderNew tOrderNew = tOrderNewMapper.selectByPrimaryKey(in.getOrderNo());
  240. String salesmanId = tOrderNew.getSalesmanId();
  241. Admin a = adminMapper.selectByPrimaryKey(salesmanId);
  242. addPublicExamine(in.getId(),a);
  243. CheckOrderNoOut data = (CheckOrderNoOut) checkOrderNo.get("data");
  244. String format = getString(my, data);
  245. pushNoticeAndSoucketAndEmail(my, in, date, a, format, map);
  246. }
  247. map.put("data", publicReleaseMapper.selectDtails(in.getId()));
  248. return map;
  249. }
  250. private String getString(AdminListBo my, CheckOrderNoOut data) {
  251. String amountName="";
  252. String amountType="";
  253. if (data.getAmountType()==0){
  254. amountName="低于1万";
  255. amountType="原则上技术人员不可下户";
  256. }else if (data.getAmountType()==1){
  257. amountName="在1~3万";
  258. amountType="已超出合同可公出标准"+data.getDurationMax()+"小时";
  259. }else if (data.getAmountType()==2){
  260. amountName="在3~5万";
  261. amountType="已超出合同可公出标准"+data.getDurationMax()+"小时";
  262. }else if (data.getAmountType()==3){
  263. amountName="在5~10万";
  264. amountType="已超出合同可公出标准"+data.getDurationMax()+"小时";
  265. }
  266. String format = String.format("%s准备公出%s(%s)," +
  267. "总计公出:%s人,%s次,%s时。本合同%s,本次申请,%s。请确认是否同意本次公出?", my.getName(), data.getUserName(), data.getContractNo(),
  268. data.getPeopleCount(), data.getTimesCount(), data.getDurationCount().stripTrailingZeros().toPlainString(), amountName,
  269. amountType);
  270. return format;
  271. }
  272. /**
  273. *
  274. * @param my 发送人信息
  275. * @param in 公出信息
  276. * @param date 时间
  277. * @param a 接受者信息
  278. * @param str 站内信内容
  279. * @param map 返回实体
  280. */
  281. private void pushNoticeAndSoucketAndEmail(AdminListBo my, PublicRelease in, Date date, Admin a, String str, Map<String, Object> map) {
  282. sendNoticeAndSoucket(a.getId(),1, str);
  283. if (a.getOpenId() !=null) {
  284. Integer res= weChatUtils.addNotice(a.getOpenId(), in.getStatus(), in.getId(), date, my.getName(),"["+ my.getName()+"]发起外出申请");
  285. if (res!=0) {
  286. sendEmail(my, a,1);
  287. }
  288. map.put("code", res);
  289. }else {
  290. sendEmail(my, a,1);
  291. map.put("code", 2);
  292. }
  293. }
  294. private String getAssistAidName(String assistAid) {
  295. String [] ss=assistAid.split(",");
  296. StringBuffer sb=new StringBuffer();
  297. for (String s : ss) {
  298. Admin a=adminMapper.selectByPrimaryKey(s);
  299. if (a!=null&&a.getName()!=null)sb=sb.append(a.getName()).append(",");
  300. }
  301. if (sb.length()<1)return "";
  302. return sb.deleteCharAt(sb.length() - 1).toString();
  303. }
  304. private void sendEmailAndNoticeAndSoucket(String id,Integer type,String content){
  305. sendNoticeAndSoucket(id,type,content);
  306. Admin admin = adminMapper.selectByPrimaryKey(id);
  307. String title="暂无标题";
  308. if (type==3)title=NoticeStatus.PUBLIC_OTHERS_USER.getDesc();
  309. EmailBo emailBo=new EmailBo(title,admin.getEmail(),content);
  310. asyncUtils.send(emailBo);
  311. }
  312. /**
  313. *
  314. * @param id 接受者编号
  315. * @param type 状态 0驳回 1发起 2同意 3 公出他人企业
  316. * @param content 内容
  317. */
  318. private void sendNoticeAndSoucket(String id,Integer type,String content) {
  319. Integer noticeType=NoticeStatus.PUBLIC_RELEASE_START.getCode();
  320. if(type==0)noticeType=NoticeStatus.PUBLIC_RELEASE_NO.getCode();
  321. else if (type==2)noticeType=NoticeStatus.PUBLIC_RELEASE_YES.getCode();
  322. else if (type==3)noticeType=NoticeStatus.PUBLIC_OTHERS_USER.getCode();
  323. //触发通知有未读消息
  324. systemWebSocketHandler.sendMessageToUser(id, new TextMessage("unread"));
  325. Notice n =new Notice();
  326. n.setId(UUID.randomUUID().toString());
  327. n.setAid(id);
  328. n.setNoticeType(noticeType);
  329. n.setContent(content);
  330. n.setReaded(0);//未读
  331. asyncUtils.addNotice(n);
  332. }
  333. /**
  334. *
  335. * @param my 发送人
  336. * @param a 接受者
  337. * @param type 类型
  338. */
  339. private void sendEmail(AdminListBo my, Admin a,Integer type) {
  340. StringBuffer title=new StringBuffer();
  341. if (type==0) title=title.append("外出申请驳回提醒");
  342. else if (type==1) title=title.append("外出申请提醒");
  343. else if (type==2) title=title.append("外出申请同意提醒");
  344. if (SendEmailUtil.isEmail(a.getEmail())) {
  345. EmailBo email=new EmailBo(title.toString(),a.getEmail() , my.getDepartmentName(), my.getName(),"详情请登录微信打卡小程序查看,谢谢!") ;
  346. asyncUtils.send(email);
  347. }
  348. }
  349. @Override
  350. public int updatePublicRelease(InputPublicRelease in) {
  351. Date date = new Date();
  352. try {
  353. if(in.getReleaseStarts()!=null)in.setReleaseStart(DateUtils.parseDate(in.getReleaseStarts(), AFTConstants.YYYYMMDDHHMMSS));
  354. if(in.getReleaseEnds()!=null)in.setReleaseEnd(DateUtils.parseDate(in.getReleaseEnds(), AFTConstants.YYYYMMDDHHMMSS));
  355. } catch (ParseException e) {
  356. e.printStackTrace();
  357. throw new BusinessException("转换异常");
  358. }
  359. Map checkOrderNo =null;
  360. List<User> uses=null;
  361. PublicRelease use = publicReleaseMapper.selectByPrimaryKey(in.getId());
  362. in.setCreateTime(use.getCreateTime());
  363. String userNames=publicReleaseDetailsMapper.getNamesByPrid(in.getId());
  364. PublicReleaseLog log=new PublicReleaseLog(in.getId(),TokenManager.getAdminId(),null,in.getReason(),date);
  365. in.setUserNames(userNames);
  366. if(in.getStatus()==null||in.getStatus()!=3) {
  367. if (in.getType() == 1) {
  368. checkOrderNo = checkOrderNoDuration(in,null,2);
  369. boolean isValid = (boolean) checkOrderNo.get("code");
  370. if (isValid){
  371. in.setTechStartProcess(0);
  372. }else {
  373. in.setTechStartProcess(1);
  374. }
  375. }else {
  376. in.setTechStartProcess(0);
  377. }
  378. if (in.getUids()!=null){
  379. List<String> split = Arrays.asList(in.getUids().split(","));
  380. List<OutPublicReleaseDetails> prList = publicReleaseDetailsMapper.selectByPrid(in.getId());
  381. uses=pushPublicReleaseDetails(split,prList,use,in);
  382. }
  383. in.setStatus(1);
  384. log.setStatus(PublicReleaseLog.states.xg.getCode());
  385. in.setUpdateStatus(1);
  386. //如果是业务切公出则撤销营销审核与审核人
  387. pushUpdateType(in,use);
  388. //如果是业务公出,切审核状态为其他,则重置审核
  389. if(in.getType()==0&&(use.getPublicType()==2||use.getPublicType()==3)){
  390. in.setPublicType(1);
  391. PublicExamine pe=new PublicExamine();
  392. pe.setPrid(in.getId());
  393. pe.setStatus(0);
  394. publicExamineMapper.updateByPridSelective(pe);
  395. }
  396. AdminListBo my = adminMapper.getDeptNameByAid(TokenManager.getAdminId());
  397. //重新发起
  398. addPublicReleaseLog(my,in,in.getUserNames(),date,uses,checkOrderNo);
  399. }else {
  400. List<PublicRelease> list = publicReleaseMapper.selectByMainId(in.getId(),3,null);
  401. List<PublicRelease> list2 = publicReleaseMapper.selectByMainId(in.getId(),4,null);
  402. log.setStatus(PublicReleaseLog.states.cx.getCode());
  403. for (PublicRelease out : list) {
  404. addpublicLog(out.getId(), PublicReleaseLog.states.sc.getCode(), "修改撤销协单");
  405. out.setStatus(3);
  406. publicReleaseMapper.updateByPrimaryKeySelective(out);
  407. }
  408. for (PublicRelease out : list2) {
  409. addpublicLog(out.getId(), PublicReleaseLog.states.sc.getCode(), "修改撤销协单助手");
  410. out.setStatus(3);
  411. publicReleaseMapper.updateByPrimaryKeySelective(out);
  412. }
  413. }
  414. addPublicReleaseDateClock(in);
  415. publicReleaseLogMapper.insertSelective(log);
  416. publicReleaseMapper.updateByPrimaryKeySelective(in);
  417. return 1;
  418. }
  419. private void pushUpdateType(InputPublicRelease in, PublicRelease use) {
  420. //营销切换成技术
  421. if(use.getType()==0&&in.getType()==1){
  422. in.setPublicType(0);
  423. }
  424. if(use.getType()==1&&in.getType()==0){
  425. in.setTechStartProcess(0);
  426. }
  427. publicExamineMapper.deletePrid(in.getId());
  428. }
  429. private List<User> pushPublicReleaseDetails(List<String> split, List<OutPublicReleaseDetails> prList, PublicRelease use, InputPublicRelease in) {
  430. //先删除,再修改个,再新增,有利于减少计算
  431. deletePRD(split,prList);
  432. publicReleaseDetailsMapper.updateDistrictByPrid(in);
  433. List<User> users = addPRD(split, prList, use,in);
  434. return users;
  435. }
  436. private void deletePRD(List<String> split, List<OutPublicReleaseDetails> prdList) {
  437. for (OutPublicReleaseDetails prd : prdList) {
  438. boolean flag=true;
  439. for (String s : split) {
  440. if (prd.getUid().equals(s)){
  441. flag=false;
  442. }
  443. }
  444. if (flag){
  445. publicReleaseDetailsMapper.deleteByPrimaryKey(prd.getId());
  446. }
  447. }
  448. }
  449. /**
  450. * @param split 修改后客户集
  451. * @param prdList 修改前客户集
  452. * @param use 原订单信息
  453. */
  454. private List<User> addPRD(List<String> split, List<OutPublicReleaseDetails> prdList, PublicRelease use, InputPublicRelease in) {
  455. List<PublicReleaseDetails> addPrdList=new ArrayList<>();
  456. List<User> users=new ArrayList<>();
  457. Integer publicType = use.getPublicType();
  458. //计算平均
  459. Integer userCount=split.size();
  460. Integer index=0;
  461. Double duration= in.getDuration();
  462. double d = durationSum(duration,userCount );
  463. for (String s : split) {
  464. User u=userMapper.selectByPrimaryKey(s);
  465. users.add(u);
  466. boolean flag=true;
  467. Integer id=null;
  468. String districtName=null,latitude=null,longitude=null;
  469. for (OutPublicReleaseDetails prd : prdList) {
  470. if (id==null)id=prd.getPrid();
  471. if (districtName==null)districtName=prd.getDistrictName();
  472. if (latitude==null)latitude=prd.getLatitude();
  473. if (longitude==null)longitude=prd.getLongitude();
  474. if (s.equals(prd.getUid())){
  475. flag=false;
  476. }
  477. }
  478. //计算下
  479. if (flag){
  480. PublicReleaseDetails prd=new PublicReleaseDetails();
  481. if (u!=null&&u.getShareType()==2){
  482. prd.setNewUser(1);
  483. }else {
  484. prd.setNewUser(0);
  485. }
  486. if (u.getAid().equals(use.getAid())){
  487. prd.setMainStatus(1);
  488. }else {
  489. prd.setMainStatus(0);
  490. }
  491. prd.setPrid(id);
  492. prd.setUid(s);
  493. prd.setSignSum(0);
  494. prd.setDistrictName(districtName);
  495. prd.setLatitude(latitude);
  496. prd.setLongitude(longitude);
  497. prd.setClockIn(0);
  498. addPrdList.add(prd);
  499. //设置时长
  500. if (index<userCount){
  501. if (in.getDuration()==0){
  502. prd.setDuration(in.getDuration());
  503. } else {
  504. prd.setDuration(d);
  505. duration=duration-d;
  506. }
  507. }else {
  508. prd.setDuration(duration);
  509. }
  510. index++;
  511. if (!use.getAid().equals(u.getAid())){
  512. addPublicExamine(use.getId(), u.getAid());
  513. if (publicType==1)publicType=1;
  514. }
  515. }else {
  516. InputPublicRelease inp=new InputPublicRelease();
  517. inp.setId(in.getId());
  518. inp.setUid(s);
  519. PublicReleaseDetails publicReleaseDetails = publicReleaseDetailsMapper.selectBypridAndUid(inp);
  520. PublicReleaseDetails newprd =new PublicReleaseDetails();
  521. newprd.setId(publicReleaseDetails.getId());
  522. //设置时长
  523. if (index<userCount){
  524. if (in.getDuration()==0){
  525. newprd.setDuration(in.getDuration());
  526. } else {
  527. newprd.setDuration(d);
  528. duration=duration-d;
  529. }
  530. }else {
  531. newprd.setDuration(duration);
  532. }
  533. index++;
  534. publicReleaseDetailsMapper.updateByPrimaryKeySelective(newprd);
  535. }
  536. }
  537. StringBuffer str=new StringBuffer();
  538. for (User u : users) {
  539. str=str.append(use.getUserNames()).append(",");
  540. }
  541. if (publicType!=use.getPublicType()){
  542. PublicRelease newP=new PublicRelease();
  543. newP.setId(use.getId());
  544. if (str.length()>1)newP.setUserNames(str.substring(0,str.length()-1));
  545. newP.setPublicType(publicType);
  546. publicReleaseMapper.updateByPrimaryKeySelective(newP);
  547. }
  548. if (!addPrdList.isEmpty())publicReleaseDetailsMapper.insertBatch(addPrdList);
  549. return users;
  550. }
  551. @SuppressWarnings("unchecked")
  552. @Override
  553. public Pagination<OutPublicReleaseList> listPublicRelease(InputPublicReleaseList in) {
  554. Map<String,Object> map =new HashMap<String, Object>();
  555. map.put("type", in.getType()==null?0:in.getType());
  556. //先修改他人公出做测试
  557. // if(in.getType()==4)map.put("type", 5);
  558. if (in.getType()==3) {
  559. in.setAssist(2);
  560. if (!TokenManager.hasRole(AFTConstants.TECH_FINANCE_DIRECTOR))map.put("aid",TokenManager.getAdminId());
  561. }else{
  562. map.put("aid",TokenManager.getAdminId());
  563. }
  564. if (StringUtils.isNotEmpty(in.getClockTime())) map.put("clockTime", in.getClockTime());
  565. if(StringUtils.isNotEmpty(in.getReleaseStarts())) map.put("releaseStarts", in.getReleaseStarts());
  566. if(StringUtils.isNotEmpty(in.getReleaseEnds())) map.put("publicEnd", in.getReleaseEnds()+" 23:59:59");
  567. if(StringUtils.isNotEmpty(in.getUserName())) map.put("userName", in.getUserName());
  568. if(in.getStatus()!=null) map.put("status", in.getStatus());
  569. if(in.getClockIn()!=null) map.put("clockIn", in.getClockIn());
  570. map.put("assist",in.getAssist()==null?0:in.getAssist());
  571. Pagination<OutPublicReleaseList> p=(Pagination<OutPublicReleaseList>) findPage("listPublicRelease", "countPublicRelease", map, in.getPageNo(), in.getPageSize());
  572. if(in.getType()==0||in.getType()==4){
  573. pushExamines(p);
  574. }
  575. return p;
  576. }
  577. private void pushExamines(Pagination<OutPublicReleaseList> p) {
  578. List<OutPublicReleaseList> list = (List<OutPublicReleaseList>) p.getList();
  579. if (!list.isEmpty()){
  580. List<Integer> ids=new ArrayList<>();
  581. list.forEach(e ->{ ids.add(e.getId()); });
  582. List<PublicExamine> publicExamines = publicExamineMapper.selectByIds(ids);
  583. for (OutPublicReleaseList e : list) {
  584. if (e.getStatus()==1&&
  585. ((e.getPublicType()==0||e.getPublicType()==2)&&(e.getTechStartProcess()==0||e.getTechStartProcess()==2))
  586. ){
  587. e.setAdminExamine(e.getReviewerName()+" 未审核");
  588. }else if (e.getStatus()==0) {
  589. e.setAdminExamine("");
  590. }else {
  591. if (!publicExamines.isEmpty()){
  592. StringBuffer str =new StringBuffer();
  593. for (PublicExamine pe : publicExamines) {
  594. if (e.getId().equals(pe.getPrid())){
  595. str=str.append(pe.getAname());
  596. if (pe.getStatus()==0)str=str.append(" 未审核,");
  597. if (pe.getStatus()==1)str=str.append(" 已审核,");
  598. }
  599. if (str.length()>1) e.setAdminExamine(str.substring(0,str.length()-1));
  600. }
  601. }
  602. }
  603. }
  604. }
  605. }
  606. @Override
  607. @Transactional
  608. public int pushExaminePublicRelease(Integer id, Integer status, String remarks,Integer examineType) {
  609. PublicRelease p=new PublicRelease();
  610. Date date =new Date();
  611. boolean flag=false;
  612. String aid=TokenManager.getAdminId();
  613. p.setId(id);
  614. PublicRelease use=publicReleaseMapper.selectByPrimaryKey(id);
  615. if (use.getStatus()!=1) {
  616. throw new BusinessException("只能修改发起的状态公出");
  617. }
  618. if((use.getTechStartProcess()==1||use.getTechStartProcess()==3)&&(examineType==0||examineType==1)){
  619. if(use.getTechStartProcess()!=0) throw new BusinessException("技术公出先需营销审核。");
  620. }
  621. //当前审核人员
  622. Admin ad=adminMapper.selectByPrimaryKey(aid);
  623. //公出负责人
  624. Admin a;
  625. Admin publicAdmin =adminMapper.selectByPrimaryKey(use.getAid());
  626. a=publicAdmin;
  627. StringBuffer str=new StringBuffer();
  628. str=str.append("(").append(ad.getName()).append(")");
  629. StringBuffer content=new StringBuffer().append("[").append(use.getUserNames()).append("]公司的外出申请,");
  630. //上级审核
  631. if(examineType==0){
  632. p.setStatus(status);
  633. if (status==0){
  634. str=str.append("驳回,");
  635. if (use.getAssist()==2){
  636. content=content.append("技术协单被[").append(ad.getName()).append("]拒绝!");
  637. }else {
  638. content=content.append("未通过公出审核!");
  639. }
  640. if(use.getPublicType()>1){
  641. p.setPublicType(1);
  642. PublicExamine inpe=new PublicExamine();
  643. inpe.setStatus(0);
  644. inpe.setPrid(use.getId());
  645. publicExamineMapper.updateStatusByPrid(inpe);
  646. }
  647. }else if(status==2){
  648. str=str.append("同意,");
  649. //如果是协单公出主公出,在审核通过
  650. if (use.getAssist()==1){
  651. pushPublicReleaseAssist(use,status,date,a);
  652. }else if (use.getAssist()==0){
  653. publicReleaseMapper.updateStatusByMainId(3,use.getId(),null,null);
  654. }
  655. content=content.append("已通过审核!");
  656. }
  657. str=str.append(remarks);
  658. p.setAuditInfo(str.toString());
  659. p.setAuditTime(date);
  660. p.setRejectName(ad.getName());
  661. addpublicLog(id, status, remarks);
  662. flag=true;
  663. //营销员审核
  664. }else if (examineType==1){
  665. //通知公出人切换成公出人审核人
  666. a=adminMapper.selectByPrimaryKey(a.getReviewer());
  667. if (status==2){//审核通过
  668. str=str.append("同意公出我的企业,").append(remarks);
  669. content=content.append("已通过审核,请尽快审核。");
  670. List<PublicExamine> publicExamines = publicExamineMapper.selectByPrid(use.getId());
  671. int x=0;
  672. for (PublicExamine pe : publicExamines) {
  673. if (pe.getAid().equals(aid)){
  674. pe.setStatus(1);
  675. PublicExamine newp=new PublicExamine();
  676. newp.setId(pe.getId());
  677. newp.setStatus(1);
  678. publicExamineMapper.updateByPrimaryKeySelective(newp);
  679. x++;
  680. }else {
  681. if (pe.getStatus()==1){
  682. x++;
  683. }
  684. }
  685. }
  686. if (x==publicExamines.size()){
  687. p.setPublicType(2);
  688. flag=true;
  689. }
  690. addpublicLog(id, status, str.toString());
  691. }else if (status==0){//审核驳回
  692. //切换通知人公出负责人
  693. a=publicAdmin;
  694. str=str.append("不同意公出我的企业,").append(remarks);;
  695. content=content.append("未通过审核,请修改后重新发起。");
  696. p.setPublicType(3);
  697. p.setStatus(0);
  698. p.setAuditInfo(str.toString());
  699. p.setRejectName(ad.getName());
  700. addpublicLog(id, 0, str.toString());
  701. flag=true;
  702. }
  703. }else if (examineType==2){
  704. //通知公出人切换成公出人审核人
  705. a=adminMapper.selectByPrimaryKey(a.getReviewer());
  706. if (status==2){//审核通过
  707. str=str.append("同意公出我的企业,").append(remarks);
  708. content=content.append("已通过审核,请尽快审核。");
  709. List<PublicExamine> publicExamines = publicExamineMapper.selectByPrid(use.getId());
  710. int x=0;
  711. for (PublicExamine pe : publicExamines) {
  712. if (pe.getAid().equals(aid)){
  713. pe.setStatus(1);
  714. PublicExamine newp=new PublicExamine();
  715. newp.setId(pe.getId());
  716. newp.setStatus(1);
  717. publicExamineMapper.updateByPrimaryKeySelective(newp);
  718. x++;
  719. }else {
  720. if (pe.getStatus()==1){
  721. x++;
  722. }
  723. }
  724. }
  725. if (x==publicExamines.size()){
  726. p.setTechStartProcess(2);
  727. flag=true;
  728. }
  729. addpublicLog(id, status, str.toString());
  730. }else if (status==0){//审核驳回
  731. //切换通知人公出负责人
  732. a=publicAdmin;
  733. str=str.append("不同意公出我的企业,").append(remarks);;
  734. content=content.append("未通过审核,请修改后重新发起。");
  735. p.setTechStartProcess(3);
  736. p.setStatus(0);
  737. p.setAuditInfo(str.toString());
  738. p.setRejectName(ad.getName());
  739. addpublicLog(id, 0, str.toString());
  740. flag=true;
  741. }
  742. }
  743. AdminListBo my = adminMapper.getDeptNameByAid(TokenManager.getAdminId());
  744. if (flag){
  745. sendNoticeAndSoucket(a.getId(),status,content.toString());
  746. }
  747. if (p.getStatus()!=null||p.getPublicType()!=null||p.getTechStartProcess()!=null){
  748. publicReleaseMapper.updateByPrimaryKeySelective(p);
  749. updateAdminUserCountPublicRelease(use.getAid(),use.getReleaseStart());
  750. }
  751. if (a.getOpenId()!=null) {
  752. if (remarks.length()>19) {
  753. remarks=remarks.substring(0, 15)+"...";
  754. }
  755. Integer res=weChatUtils.addNotice(a.getOpenId(),status,p.getId(),date,my.getName(),remarks);
  756. if (res!=0) {
  757. sendEmail( my, a, status);
  758. }
  759. return res;
  760. }
  761. return 1;
  762. }
  763. private void updateAdminUserCountPublicRelease(String aid, Date transferTime) {
  764. String startTime= DateUtils.formatDate(transferTime,AFTConstants.YYYYMMDD);
  765. String endTime=startTime+" 23:59:59";
  766. AdminUserCount adminUserCount = customerService.getAdminUserCount(aid,startTime);
  767. Integer count = publicReleaseMapper.selectCountByaidAndDate(aid, startTime, endTime);
  768. if (count==null){
  769. count=0;
  770. }
  771. if (adminUserCount==null){
  772. AdminUserCount selectAUC=userMapper.selectByaidAndDate(aid,startTime,endTime);
  773. if (selectAUC==null)selectAUC=new AdminUserCount();
  774. selectAUC.setPublicReleaseCount(count);
  775. selectAUC.setAid(aid);
  776. selectAUC.setDateTime(transferTime);
  777. adminUserCountMapper.insertSelective(selectAUC);
  778. }else {
  779. AdminUserCount selectAUC=userMapper.selectByaidAndDate(aid,startTime,endTime);
  780. AdminUserCount newAUC=new AdminUserCount();
  781. newAUC.setId(adminUserCount.getId());
  782. newAUC.setPublicReleaseCount(count);
  783. if (selectAUC==null){
  784. newAUC.setPrivateCount(0);
  785. newAUC.setChannelCount(0);
  786. newAUC.setSignCount(0);
  787. }else {
  788. if (selectAUC.getPrivateCount()==null)selectAUC.setPrivateCount(0);
  789. if (selectAUC.getChannelCount()==null)selectAUC.setChannelCount(0);
  790. if (selectAUC.getSignCount()==null)selectAUC.setSignCount(0);
  791. newAUC.setPrivateCount(selectAUC.getPrivateCount());
  792. newAUC.setChannelCount(selectAUC.getChannelCount());
  793. newAUC.setSignCount(selectAUC.getSignCount());
  794. }
  795. adminUserCountMapper.updateByPrimaryKeySelective(newAUC);
  796. }
  797. }
  798. private void addpublicLog(Integer id, Integer status, String remarks) {
  799. PublicReleaseLog log = new PublicReleaseLog(id, TokenManager.getAdminId(), status, remarks, new Date());
  800. publicReleaseLogMapper.insertSelective(log);
  801. }
  802. @Override
  803. public int pushTechReject(Integer id, String remarks) {
  804. Integer status=0;
  805. PublicRelease use=publicReleaseMapper.selectByPrimaryKey(id);
  806. PublicRelease mainPublic= publicReleaseMapper.selectByPrimaryKey(use.getMainId());
  807. Admin ad=adminMapper.selectByPrimaryKey(TokenManager.getAdminId());
  808. StringBuffer str=new StringBuffer();
  809. if (use.getAssist()!=2){
  810. throw new BusinessException("只有技术公出能发起技术驳回");
  811. }
  812. str=str.append("拒绝技术协单,").append(remarks);
  813. PublicRelease newMain=new PublicRelease();
  814. newMain.setId(mainPublic.getId());
  815. newMain.setStatus(4);
  816. newMain.setRejectName(ad.getName());
  817. newMain.setAuditInfo(str.toString());
  818. addWeChatNotice(mainPublic.getAid(),0,str.toString(),mainPublic.getId());
  819. publicReleaseMapper.updateByPrimaryKeySelective(newMain);
  820. PublicRelease up=new PublicRelease();
  821. up.setId(id);
  822. up.setAuditInfo(str.toString());
  823. up.setStatus(3);
  824. addpublicLog(id, status, remarks);
  825. publicReleaseMapper.updateByPrimaryKeySelective(up);
  826. deleteAssistantPulicRelease(use,null);
  827. return addNoticeAndWeChat(mainPublic.getId(), mainPublic.getAid(), str.toString(), status);
  828. }
  829. /**
  830. * 新增日志、站内信、微信提醒
  831. * @param id 日志公出编号
  832. * @param aid 接受者
  833. * @param remarks 日志内容
  834. * @param status 0驳回
  835. * @return
  836. */
  837. private Integer addNoticeAndWeChat(Integer id,String aid, String remarks, Integer status) {
  838. AdminListBo my = adminMapper.getDeptNameByAid(aid);
  839. Admin a = adminMapper.selectByPrimaryKey(TokenManager.getAdminId());
  840. if (my.getOpenId()!=null) {
  841. if (remarks.length()>19) {
  842. remarks=remarks.substring(0, 15)+"...";
  843. }
  844. return weChatUtils.addNotice(my.getOpenId(),status,id,new Date(),my.getName(), remarks);
  845. }else {
  846. sendEmail( my, a, status);
  847. return 1;
  848. }
  849. }
  850. /**
  851. *
  852. * @param use 原始公出信息
  853. * @param status 状态
  854. * @param date 时间
  855. * @param a
  856. */
  857. private void pushPublicReleaseAssist(PublicRelease use,Integer status,Date date,Admin a) {
  858. if (use.getAssistAid()!=null){
  859. updatePushassist(use, status, date,a);
  860. if(use.getAssistantAid()!=null){
  861. updatepushAssistant(use,a);
  862. }
  863. }
  864. }
  865. /**
  866. * 处理技术协单
  867. * @param use
  868. * @param status
  869. * @param date
  870. */
  871. private void updatePushassist(PublicRelease use, Integer status, Date date, Admin a) {
  872. //协单
  873. List<PublicRelease> list = publicReleaseMapper.selectByMainId(use.getId(),3,null);
  874. String []ss= use.getAssistAid().split(",");
  875. //数据库与传入比对,如果不存在就标记撤销 ,如果存在就修改成
  876. for (PublicRelease out : list) {
  877. boolean flag=false;
  878. for (String s : ss) {
  879. if (out.getAid().equals(s)){
  880. flag=true;
  881. break;
  882. }
  883. }
  884. if (!flag){
  885. PublicRelease in = new PublicRelease();
  886. in.setId(out.getId());
  887. in.setStatus(3);
  888. publicReleaseMapper.updateByPrimaryKeySelective(in);
  889. StringBuffer str = new StringBuffer("[").append(a.getName()).append("]取消公出").append("[").append(use.getUserNames()).append("]。");
  890. PublicReleaseLog log = new PublicReleaseLog(in.getId(), TokenManager.getAdminId(), PublicReleaseLog.states.cx.getCode(), str.toString(), date);
  891. publicReleaseLogMapper.insertSelective(log);
  892. sendNoticeAndSoucket(out.getAid(),in.getStatus(),str.toString());
  893. }else {
  894. PublicRelease np = new PublicRelease();
  895. BeanUtils.copyProperties(use,np);
  896. np.setId(out.getId());
  897. np.setMainId(null);
  898. np.setType(null);
  899. np.setAssist(2);
  900. np.setStatus(status);
  901. np.setAid(null);
  902. publicReleaseMapper.updateByPrimaryKeySelective(np);
  903. updatePublicReleaseDtails(use,np);
  904. StringBuffer str = new StringBuffer("[").append(a.getName()).append("]修改了公出").append("[").append(use.getUserNames()).append("]。");
  905. PublicReleaseLog log = new PublicReleaseLog(np.getId(), TokenManager.getAdminId(), PublicReleaseLog.states.xg.getCode(),str.toString(), date);
  906. publicReleaseLogMapper.insertSelective(log);
  907. sendNoticeAndSoucket(out.getAid(),np.getStatus(),str.toString());
  908. }
  909. }
  910. for (String s : ss) {
  911. boolean flag2 = false;
  912. for (PublicRelease o : list) {
  913. if (s.equals(o.getAid())) {
  914. flag2 = true;
  915. }
  916. }
  917. if (!flag2) {
  918. PublicRelease np = new PublicRelease();
  919. BeanUtils.copyProperties(use,np);
  920. np.setAssist(2);
  921. np.setAid(s);
  922. np.setStatus(2);
  923. np.setType(3);
  924. np.setMainId(use.getId());
  925. np.setId(null);
  926. publicReleaseMapper.insertSelective(np);
  927. //补充客户表
  928. addpublicReleaseDtails(use,np);
  929. StringBuffer str = new StringBuffer("[").append(a.getName()).append("]邀请你技术协单").append("[").append(use.getUserNames()).append("]。");
  930. PublicReleaseLog log = new PublicReleaseLog(np.getId(), TokenManager.getAdminId(), PublicReleaseLog.states.fq.getCode(), str.toString(), date);
  931. sendNoticeAndSoucket(np.getAid(),np.getStatus(),str.toString());
  932. publicReleaseLogMapper.insertSelective(log);
  933. }
  934. }
  935. }
  936. private void updatePublicReleaseDtails(PublicRelease use, PublicRelease in) {
  937. List<OutPublicReleaseDetails> list = publicReleaseDetailsMapper.selectByPrid(use.getId());
  938. List<OutPublicReleaseDetails> list2 = publicReleaseDetailsMapper.selectByPrid(in.getId());
  939. //打卡信息重新梳理
  940. //查找新增的
  941. for (OutPublicReleaseDetails out : list) {
  942. boolean flag=true;
  943. for (OutPublicReleaseDetails out2 : list2) {
  944. if (out.getUid().equals(out2.getUid())){
  945. flag=false;
  946. }
  947. }
  948. if (flag){
  949. OutPublicReleaseDetails prd=new OutPublicReleaseDetails();
  950. BeanUtils.copyProperties(out,prd);
  951. prd.setPrid(in.getId());
  952. prd.setClockIn(0);
  953. prd.setClockInTime(null);
  954. prd.setClockInRemarks(null);
  955. prd.setUfid(null);
  956. publicReleaseDetailsMapper.insertSelective(prd);
  957. }
  958. }
  959. for (OutPublicReleaseDetails out2 : list2) {
  960. boolean flag=true;
  961. for (OutPublicReleaseDetails out : list) {
  962. if (out2.getUid().equals(out.getUid())){
  963. flag=false;
  964. }
  965. }
  966. if (flag){
  967. publicReleaseDetailsMapper.deleteByPrimaryKey(out2.getId());
  968. }
  969. }
  970. //信息补充,修改时没有打卡信息
  971. if(in.getValidDate()==null)in.setValidDate(use.getValidDate());
  972. //最后再来处理打卡信息
  973. addPublicReleaseDateClock(in);
  974. }
  975. private void addpublicReleaseDtails(PublicRelease use, PublicRelease in) {
  976. List<OutPublicReleaseDetails> list = publicReleaseDetailsMapper.selectByPrid(use.getId());
  977. for (OutPublicReleaseDetails out : list) {
  978. OutPublicReleaseDetails prd=new OutPublicReleaseDetails();
  979. BeanUtils.copyProperties(out,prd);
  980. prd.setPrid(in.getId());
  981. prd.setClockIn(0);
  982. prd.setClockInTime(null);
  983. prd.setMainStatus(0);
  984. publicReleaseDetailsMapper.insertSelective(prd);
  985. }
  986. addPublicReleaseDateClock(in);
  987. }
  988. /**
  989. * 处理技术助手
  990. * @param use
  991. */
  992. private void updatepushAssistant(PublicRelease use,Admin a) {
  993. List<PublicRelease> uselist = publicReleaseMapper.selectByMainId(use.getId(),4,null);
  994. for (PublicRelease p : uselist) {
  995. if(!use.getReleaseStart().equals(p.getReleaseStart()))p.setReleaseStart(use.getReleaseStart());
  996. if (!use.getReleaseEnd().equals(p.getReleaseEnd()))p.setReleaseEnd(use.getReleaseEnd());
  997. p.setStatus(2);
  998. p.setAssistantAid(use.getAssistantAid());
  999. p.setAssistantName(use.getAssistantName());
  1000. p.setUserNames(use.getUserNames());
  1001. publicReleaseMapper.updateByPrimaryKeySelective(p);
  1002. StringBuffer str = new StringBuffer("[").append(a.getName()).append("]修改了").append("[").append(use.getUserNames()).append("]。");
  1003. PublicReleaseLog log = new PublicReleaseLog(p.getId(), TokenManager.getAdminId(), PublicReleaseLog.states.ty.getCode(), str.toString(), new Date());
  1004. sendNoticeAndSoucket(p.getAid(),p.getStatus(),str.toString());
  1005. publicReleaseLogMapper.insertSelective(log);
  1006. }
  1007. }
  1008. @Override
  1009. public List<outPublicReleaseLog> listPublicReleaseLog(Integer id,String ufid) {
  1010. List<outPublicReleaseLog> list=publicReleaseLogMapper.listPublicReleaseLog(id,ufid);
  1011. return list;
  1012. }
  1013. @Override
  1014. public int pushPublicReleaseClockIn(Integer id,String photoUrl,Integer clockIn ,String clockInRemarks ,String uid) {
  1015. if (clockIn == null) clockIn = 1;
  1016. PublicRelease use = publicReleaseMapper.selectByPrimaryKey(id);
  1017. if (use.getPublicType()==1||use.getPublicType()==3) {
  1018. return -1;
  1019. }
  1020. // 取消打卡限制
  1021. // if (use.getTechStartProcess()==1||use.getTechStartProcess()==3){
  1022. // return -2;
  1023. // }
  1024. List<OutPublicReleaseDetails> prdList = publicReleaseDetailsMapper.selectByPrid(id);
  1025. List<PublicReleaseDateClock> prdcList = publicReleaseDateClockMapper.selectByPrid(id);
  1026. pushDateClockDetails(prdList,prdcList);
  1027. String str = "";
  1028. Date date = new Date();
  1029. boolean flag=false;
  1030. Integer clockCount=0;
  1031. PublicReleaseClock pc=new PublicReleaseClock();
  1032. pc.setPrid(id);
  1033. pc.setPhotoUrl(photoUrl);
  1034. pc.setClockInTime(date);
  1035. for (OutPublicReleaseDetails prd : prdList) {
  1036. if (prd.getUid().equals(uid)){
  1037. String aid = TokenManager.getAdminId();
  1038. pc.setPrdid(prd.getId());
  1039. prd.setClockInRemarks(clockInRemarks);
  1040. prd.setPhotoUrl(photoUrl);
  1041. prd.setClockInTime(date);
  1042. int count=0;
  1043. for (PublicReleaseDateClock ep : prd.getPrdcList()) {
  1044. if (DateUtils.isSameDay(prd.getClockInTime(), ep.getClockInTime())){
  1045. if (ep.getStatus()==0){
  1046. ep.setStatus(1);
  1047. FollowBusinessBo fbb = new FollowBusinessBo();
  1048. fbb.setUid(prd.getUid());
  1049. fbb.setContactType("5");
  1050. fbb.setResult(use.getRemarks());
  1051. OrganizationContactBook ub = organizationContactBookMapper.getMajor(prd.getUid(), aid);
  1052. if (ub != null) {
  1053. fbb.setOcbId(ub.getId());
  1054. }
  1055. fbb.setFollowTime(DateUtils.formatDate(prd.getClockInTime(), AFTConstants.YYYYMMDDHHMMSS));
  1056. String ufid = UUID.randomUUID().toString();
  1057. prd.setUfid(ufid);
  1058. customerService.addFollow(fbb, ufid, prd.getMainStatus());
  1059. if (clockIn==1){
  1060. str = "公出人员打卡";
  1061. }else if (clockIn==2) {
  1062. str = "公出人员异常打卡";
  1063. }
  1064. count++;
  1065. publicReleaseDateClockMapper.updateByPrimaryKeySelective(ep);
  1066. }else {
  1067. if (clockIn==1){
  1068. str = "刷新打卡";
  1069. }else if (clockIn==2) {
  1070. str = "刷新打卡,公出人员异常打卡";
  1071. }
  1072. count++;
  1073. }
  1074. }else {
  1075. if (ep.getStatus()==1){
  1076. count++;
  1077. }
  1078. }
  1079. }
  1080. if(prd.getPrdcList().size() == count){
  1081. prd.setClockIn(1);
  1082. }
  1083. PublicReleaseLog log = new PublicReleaseLog(id, aid, PublicReleaseLog.states.dk.getCode(), str, new Date());
  1084. log.setPhotoUrl(photoUrl);
  1085. publicReleaseLogMapper.insertSelective(log);
  1086. publicReleaseDetailsMapper.updateByPrimaryKeySelective(prd);
  1087. flag=true;
  1088. clockCount++;
  1089. }else if (prd.getClockIn()==1){
  1090. clockCount++;
  1091. }
  1092. }
  1093. if (prdList.size()==clockCount){
  1094. PublicRelease newp=new PublicRelease();
  1095. newp.setId(id);
  1096. newp.setClockIn(1);
  1097. publicReleaseMapper.updateByPrimaryKeySelective(newp);
  1098. }
  1099. publicReleaseClockMapper.insertSelective(pc);
  1100. if (!flag){
  1101. return 0;
  1102. }
  1103. return 1;
  1104. }
  1105. @Override
  1106. public OutPublicRelease dtails(Integer id) {
  1107. String aid=TokenManager.getAdminId();
  1108. //如果不是本人和审核人员则不返回信息
  1109. OutPublicRelease out=publicReleaseMapper.selectDtails(id);
  1110. List<PublicExamine> ps = publicExamineMapper.selectByPrid(out.getId());
  1111. out.setPeList(ps);
  1112. if(out!=null){
  1113. boolean flag=false;
  1114. //自己、审核人员与部门负责人可以查看
  1115. AdminListBo ab=adminMapper.getDeptNameByAid(out.getAid());
  1116. if (!aid.equals( out.getAid())&&
  1117. !aid.equals(out.getReviewer())&&
  1118. !aid.equals(ab.getDepManager())&&
  1119. !TokenManager.hasRole(AFTConstants.TECH_FINANCE_DIRECTOR)){
  1120. flag=true;
  1121. }
  1122. //是否可以审核
  1123. if (!ps.isEmpty()){
  1124. for (PublicExamine p : ps) {
  1125. if (p.getAid().equals(aid)){
  1126. if (p.getStatus()==1)out.setMyExamine(1);
  1127. else out.setMyExamine(0);
  1128. flag=false;
  1129. break;
  1130. }
  1131. }
  1132. }
  1133. //处理返回
  1134. if(flag){
  1135. out=null;
  1136. return out;
  1137. }else {
  1138. if (out.getPublicType()==1){
  1139. if (aid.equals(out.getAid())){
  1140. out.setPermission(0);
  1141. }else {
  1142. out.setPermission(1);
  1143. }
  1144. }else {
  1145. if(aid.equals(out.getAid())) {
  1146. out.setPermission(0);
  1147. }else {
  1148. out.setPermission(1);
  1149. }
  1150. }
  1151. }
  1152. }
  1153. if (out.getStatus()==1&&
  1154. ((out.getPublicType()==0||out.getPublicType()==2)&&(out.getTechStartProcess()==0||out.getTechStartProcess()==2))
  1155. ){
  1156. out.setAdminExamine(out.getReviewerName()+" 未审核");
  1157. }else if (out.getStatus()==0) {
  1158. out.setAdminExamine("");
  1159. }else {
  1160. if (!ps.isEmpty()){
  1161. StringBuffer str =new StringBuffer();
  1162. for (PublicExamine e : ps) {
  1163. if (out.getId().equals(e.getPrid())){
  1164. str.append(e.getAname());
  1165. if (e.getStatus()==0) str.append(" 未审核,");
  1166. if (e.getStatus()==1) str.append(" 已审核,");
  1167. }
  1168. if (str.length()>1) out.setAdminExamine(str.substring(0,str.length()-1));
  1169. }
  1170. }
  1171. }
  1172. List<OutPublicReleaseDetails> prdList = publicReleaseDetailsMapper.selectByPrid(id);
  1173. List<PublicReleaseDateClock> prdcList = publicReleaseDateClockMapper.selectByPrid(id);
  1174. pushDateClockDetailsToday(prdList,prdcList);
  1175. out.setPrdList(prdList);
  1176. return out;
  1177. }
  1178. @Override
  1179. public OutPublicReleaseAndDetails followDtails(String id) {
  1180. OutPublicReleaseAndDetails out = publicReleaseMapper.selectDtailsByFid( id);
  1181. List<OutPublicReleaseDetails> prdList = publicReleaseDetailsMapper.selectByPrid(out.getId());
  1182. List<PublicReleaseDateClock> prdcList = publicReleaseDateClockMapper.selectByPrid(out.getId());
  1183. pushDateClockDetails(prdList,prdcList);
  1184. out.setPrdList(prdList);
  1185. return out;
  1186. }
  1187. @SuppressWarnings("unchecked")
  1188. @Override
  1189. // @Cacheable(value = "publicReleaseStatistics#300", key = "'publicReleaseStatistics:" +
  1190. // "aid:'+#in.aid+'depId:'+#in.depId+'clockStart:'+#in.clockStart+" +
  1191. // "'clockEnd:'+#in.clockEnd+'createStart:'+#in.createStart+'createEnd'+#in.createEnd+" +
  1192. // "'pageNo:'+#in.pageNo+'pageSzie:'+#in.pageSize")
  1193. public Object publicReleaseStatistics(InputPublicStatistics in) {
  1194. Map<String,Object> map =new HashMap<String, Object>();
  1195. if(in.getAid()!=null)map.put("aid", in.getAid());
  1196. if(in.getDepId()!=null)map.put("depId", in.getDepId());
  1197. if(in.getClockStart()!=null)map.put("clockStart", in.getClockStart());
  1198. if(in.getClockEnd()!=null)map.put("clockEnd", in.getClockEnd()+" 23:59:59");
  1199. if(in.getCreateStart()!=null)map.put("createStart", in.getCreateStart());
  1200. if(in.getCreateEnd()!=null)map.put("createEnd", in.getCreateEnd()+" 23:59:59");
  1201. return (Pagination<OutPublicStatistics>) findPage("listPublicStatistics", "countPublicStatistics", map, in.getPageNo(), in.getPageSize());
  1202. }
  1203. @SuppressWarnings("unchecked")
  1204. @Override
  1205. public List<OutPublicStatistics> publicReleaseStatisticsList(InputPublicStatistics in) {
  1206. Map<String,Object> map =new HashMap<String, Object>();
  1207. if(in.getAid()!=null)map.put("aid", in.getAid());
  1208. if (TokenManager.hasRole(AFTConstants.SUPERADMIN)|| TokenManager.hasRole(AFTConstants.APPROVAL_DECISION)||
  1209. TokenManager.hasRole(AFTConstants.CED)|| TokenManager.hasRole(AFTConstants.CED_ASSISTANT)){
  1210. if(in.getDepId()!=null)map.put("depId", in.getDepId());
  1211. }else if (TokenManager.hasRole(AFTConstants.COMPANY_MANAGER)||TokenManager.hasRole(AFTConstants.PERSONNEL_AUDITOR)){
  1212. }
  1213. if(in.getClockStart()!=null)map.put("clockStart", in.getClockStart());
  1214. if(in.getClockEnd()!=null)map.put("clockEnd", in.getClockEnd()+" 23:59:59");
  1215. if(in.getCreateStart()!=null)map.put("createStart", in.getCreateStart());
  1216. if(in.getCreateEnd()!=null)map.put("createEnd", in.getCreateEnd()+" 23:59:59");
  1217. return (List<OutPublicStatistics>) findList("listPublicStatistics",map, in.getPageNo(), 99999);
  1218. }
  1219. @SuppressWarnings("unchecked")
  1220. @Override
  1221. public Object publicReleaseListDtails(InputPublicDtails in) {
  1222. Map<String,Object> map =new HashMap<String, Object>();
  1223. addParams(in, map);
  1224. Pagination<OutPublicDtails> page = (Pagination<OutPublicDtails>) findPage("listPublicDtails", "countPublicDtails", map, in.getPageNo(), in.getPageSize());
  1225. List<OutPublicDtails> list = (List<OutPublicDtails>)page.getList();
  1226. List<Integer> collect = list.stream().map(OutPublicDtails::getId).collect(Collectors.toList());
  1227. if (!collect.isEmpty()){
  1228. List<PublicReleaseClock> publicReleaseClocks = publicReleaseClockMapper.selectList(collect);
  1229. pushiClockInTime(list, publicReleaseClocks);
  1230. }
  1231. return page;
  1232. }
  1233. private void addParams(InputPublicDtails in, Map<String, Object> map) {
  1234. if(in.getAid()!=null) map.put("aid", in.getAid());
  1235. if(in.getSid()!=null) map.put("sid", in.getSid());
  1236. if(in.getReleaseStart()!=null) map.put("releaseStart", in.getReleaseStart());
  1237. if(in.getReleaseEnd()!=null) map.put("releaseEnd", in.getReleaseEnd()+" 23:59:59");
  1238. if(in.getCreateStart()!=null) map.put("createStart", in.getCreateStart());
  1239. if(in.getCreateEnd()!=null) map.put("createEnd", in.getCreateEnd()+" 23:59:59");
  1240. if(in.getUid()!=null) map.put("uid", in.getUid());
  1241. if(in.getContractNo()!=null) map.put("contractNo", in.getContractNo());
  1242. if(in.getStatus()!=null) map.put("status", in.getStatus());
  1243. if(in.getClockIn()!=null) map.put("clockIn", in.getClockIn());
  1244. if(in.getPublicType()!=null)map.put("publicType",in.getPublicType());
  1245. //type 0私有 1管理权限 2所有权限
  1246. if(TokenManager.hasRole(AFTConstants.SUPERADMIN)||TokenManager.hasRole(AFTConstants.APPROVAL_DECISION)
  1247. ||TokenManager.hasRole(AFTConstants.CED_ASSISTANT )||TokenManager.hasRole(AFTConstants.PERSONNEL_AUDITOR)
  1248. ||TokenManager.hasRole(AFTConstants.FINANCE_ADMIN)
  1249. ||TokenManager.hasRole(AFTConstants.FINANCE)||TokenManager.hasRole(AFTConstants.FINANCE_MANAGER)){
  1250. //添加财务和财务经理。查看客户所有公出
  1251. in.setType(2);
  1252. if(in.getDeps()!=null){
  1253. map.put("deps", departmentService.parseArray(in.getDeps()));
  1254. }
  1255. }else if(TokenManager.hasRole(AFTConstants.COMPANY_MANAGER)||TokenManager.hasRole(AFTConstants.SALESMAN_ADMIN)
  1256. ||TokenManager.hasRole(AFTConstants.OPERATION_MANAGER)){
  1257. in.setType(1);
  1258. if (in.getDeps()!=null){
  1259. List<String> list = departmentService.selectSubDeps(in.getDeps());
  1260. if (list.isEmpty()) throw new BusinessException("无权查看此部门信息");
  1261. map.put("deps", list);
  1262. }else {
  1263. map.put("deps",departmentService.selectMyDeps());
  1264. }
  1265. }else{
  1266. in.setType(0);
  1267. if (in.getAid()==null) map.put("aid",TokenManager.getAdminId());
  1268. if(in.getDeps()!=null){
  1269. map.put("deps", departmentService.parseArray(in.getDeps()));
  1270. }
  1271. }
  1272. map.put("type", in.getType());
  1273. if(in.getStatusType()!=null) map.put("statusType", in.getStatusType());
  1274. }
  1275. @SuppressWarnings("unchecked")
  1276. @Override
  1277. public List<OutPublicDtails> publicReleaseListDtailsList(InputPublicDtails in) {
  1278. Map<String,Object> map =new HashMap<String, Object>();
  1279. addParams(in, map);
  1280. return (List<OutPublicDtails>) findList("listPublicDtails", map, in.getPageNo(), 99999);
  1281. }
  1282. @Override
  1283. public int addSupplement(Integer id, String supplement, String nextPlan) {
  1284. PublicRelease pr=new PublicRelease();
  1285. pr.setId(id);
  1286. pr.setSupplement(supplement);
  1287. pr.setNextPlan(nextPlan);
  1288. return publicReleaseMapper.updateByPrimaryKeySelective(pr);
  1289. }
  1290. @Override
  1291. public boolean checkTime(InputPublicRelease in) {
  1292. try {
  1293. in.setReleaseStart(DateUtils.parseDate(in.getReleaseStarts(), AFTConstants.YYYYMMDDHHMMSS));
  1294. in.setReleaseEnd(DateUtils.parseDate(in.getReleaseEnds(), AFTConstants.YYYYMMDDHHMMSS));
  1295. } catch (ParseException e) {
  1296. e.printStackTrace();
  1297. throw new BusinessException("转换异常");
  1298. }
  1299. List<Integer> is=publicReleaseMapper.checkTime(TokenManager.getAdminId(),in.getReleaseStart(),in.getReleaseEnd());
  1300. if (is.size()>1) {
  1301. return true;
  1302. }else if(is.size()==1){
  1303. //如果id为新增那么直接不允许时间重复
  1304. if (in.getId()==null){
  1305. return true;
  1306. }else {
  1307. //如果有id,与获取id一直,则为当前修改可以
  1308. if (in.getId().equals(is.get(0))){
  1309. return false;
  1310. }else {
  1311. return true;
  1312. }
  1313. }
  1314. }
  1315. return false;
  1316. }
  1317. @Override
  1318. public List<Map<String, Object>> selectOrderByUid(String uid) {
  1319. return tOrderNewMapper.selectOrderByuid(uid);
  1320. }
  1321. @Override
  1322. public List<OutPublicRelease> publicByOrder(String orderNo) {
  1323. return publicReleaseMapper.publicByOrder(orderNo);
  1324. }
  1325. @Override
  1326. public Object getMyNewPublic() {
  1327. List<PublicRelease> myPublic=publicReleaseMapper.selectMyNewPublic(TokenManager.getAdminId());
  1328. if(myPublic.isEmpty())return null;
  1329. Integer id=null;
  1330. long count=999999;
  1331. for (PublicRelease e : myPublic) {
  1332. long nowTime=System.currentTimeMillis();
  1333. long time = Math.abs(nowTime-e.getReleaseStart().getTime())/(1000*60);
  1334. long time2 = Math.abs(e.getReleaseEnd().getTime()-nowTime)/(1000*60);
  1335. //距离当前时间近的开始时间,不大于当前时间
  1336. if (time<count&&e.getReleaseStart().getTime()<nowTime){
  1337. count=time;
  1338. id=e.getId();
  1339. }
  1340. //距离当前时间近的结束时间,不小于当前时间
  1341. if (time2<count&&e.getReleaseEnd().getTime()>nowTime){
  1342. count=time2;
  1343. id=e.getId();
  1344. }
  1345. }
  1346. if (id==null)id=myPublic.get(0).getId();
  1347. OutPublicReleaseList out = publicReleaseMapper.selectOutListBoById(id);
  1348. List<OutPublicReleaseDetails> prdList = publicReleaseDetailsMapper.selectByPrid(id);
  1349. List<PublicReleaseDateClock> prdcList = publicReleaseDateClockMapper.selectByPrid(id);
  1350. pushDateClockDetails(prdList,prdcList);
  1351. pushDateClockDetailsToday(prdList,prdcList);
  1352. out.setPrdList(prdList);
  1353. return out;
  1354. // List<OutPublicReleaseList> myPublic = getMyPublic(0,1);
  1355. // if (myPublic.isEmpty()){
  1356. // List<OutPublicReleaseList> myPublic2 = getMyPublic(0,0);
  1357. // if (!myPublic2.isEmpty()){
  1358. // return myPublic2.get(0);
  1359. // }
  1360. // }else {
  1361. // return myPublic.get(0);
  1362. // }
  1363. }
  1364. private void pushDateClockDetailsToday(List<OutPublicReleaseDetails> prdList, List<PublicReleaseDateClock> prdcList) {
  1365. for (OutPublicReleaseDetails out : prdList) {
  1366. List<PublicReleaseDateClock> elist=new ArrayList<>();;
  1367. for (PublicReleaseDateClock prdc : prdcList) {
  1368. if (prdc.getPrdid().equals(out.getId())&&DateUtils.isSameDay(new Date(), prdc.getClockInTime())){
  1369. elist.add(prdc);
  1370. }
  1371. }
  1372. out.setPrdcList(elist);
  1373. }
  1374. }
  1375. private void pushDateClockDetails(List<OutPublicReleaseDetails> prdList, List<PublicReleaseDateClock> prdcList) {
  1376. for (OutPublicReleaseDetails out : prdList) {
  1377. List<PublicReleaseDateClock> elist=new ArrayList<>();;
  1378. for (PublicReleaseDateClock prdc : prdcList) {
  1379. if (prdc.getPrdid().equals(out.getId())){
  1380. elist.add(prdc);
  1381. }
  1382. }
  1383. out.setPrdcList(elist);
  1384. }
  1385. }
  1386. /**
  1387. *
  1388. * @param type 查看类型 0我的公出 1公出审核 2管理员 3协单(技术财税总监看全部)
  1389. * @param dateType 是否含打卡时间 0否 1是
  1390. * @return
  1391. */
  1392. public List<OutPublicReleaseList>getMyPublic(Integer type ,Integer dateType){
  1393. Map<String, Object> map = new HashMap<>();
  1394. if (type==null)type=0;
  1395. map.put("type",type);
  1396. //statusType=1情况下
  1397. map.put("statusType",1);
  1398. if (dateType==1){
  1399. Date date = new Date();
  1400. String clockTime=DateUtils.formatDate(date,AFTConstants.YYYYMMDDHHMMSS);
  1401. map.put("clockTime",clockTime);
  1402. }
  1403. map.put("aid",TokenManager.getAdminId());
  1404. List<OutPublicReleaseList> list = (List<OutPublicReleaseList>) findList("listPublicRelease",map,1,1);
  1405. for (OutPublicReleaseList out : list) {
  1406. out.setPrdList(publicReleaseDetailsMapper.selectByPrid(out.getId()));
  1407. }
  1408. return list;
  1409. }
  1410. @Override
  1411. public int addAssistant(Integer id,String aid) {
  1412. PublicRelease p = publicReleaseMapper.selectByPrimaryKey(id);
  1413. List<String> list=new ArrayList<>();
  1414. if (StringUtils.isNotEmpty(p.getAssistantAid())){
  1415. list =new ArrayList<>(Arrays.asList(p.getAssistantAid().split(",")));
  1416. }
  1417. String sname = pushAssistant(aid, list,0);
  1418. String said =String.join(",",list);
  1419. p.setAssistantAid(said);
  1420. p.setAssistantName(sname);
  1421. addAssistantPublicRelease(p,aid);
  1422. return updateAssistant(p, said, sname);
  1423. }
  1424. private void addAssistantPublicRelease(PublicRelease p, String aid) {
  1425. List<PublicRelease> list = publicReleaseMapper.selectByMainId(p.getMainId(), 4, null);
  1426. PublicRelease newP= new PublicRelease();
  1427. if(list.isEmpty()){
  1428. BeanUtils.copyProperties(p,newP);
  1429. newP.setId(null);
  1430. newP.setAid(aid);
  1431. newP.setAssist(3);
  1432. newP.setType(4);
  1433. publicReleaseMapper.insertSelective(newP);
  1434. addpublicReleaseDtails(p,newP);
  1435. StringBuffer str=new StringBuffer();
  1436. str=str.append("公出").append("[").append(p.getUserNames()).append("]").append("邀请你协助!");
  1437. addWeChatNotice( aid,1,str.toString(),newP.getId());
  1438. }else {
  1439. for (PublicRelease pr : list) {
  1440. newP.setId(pr.getId());
  1441. newP.setAssistantAid(p.getAssistantAid());
  1442. newP.setAssistantName(p.getAssistantName());
  1443. publicReleaseMapper.updateByPrimaryKeySelective(newP);
  1444. updatePublicReleaseDtails(p,newP);
  1445. }
  1446. }
  1447. }
  1448. /**
  1449. * 添加微信提示和站内消息
  1450. * @param aid 接受者
  1451. * @param type 状态 0驳回 1发起 2同意 3撤销
  1452. * @param str 备注
  1453. * @param prid 公出编号
  1454. */
  1455. private void addWeChatNotice(String aid,Integer type,String str,Integer prid){
  1456. Admin recipient = adminMapper.selectByPrimaryKey(aid);
  1457. addWeChatNotice( recipient, type, str, prid);
  1458. }
  1459. /**
  1460. * 添加微信提示和站内消息
  1461. * @param recipient 接受者
  1462. * @param type 状态 0驳回 1发起 2同意 3撤销
  1463. * @param str
  1464. */
  1465. private Integer addWeChatNotice(Admin recipient,Integer type,String str,Integer prid){
  1466. AdminListBo my = adminMapper.getDeptNameByAid(TokenManager.getAdminId());
  1467. String openid=recipient.getOpenId();
  1468. str=new StringBuffer("[").append(my.getName()).append("]").append(str).toString();
  1469. if(type==3){
  1470. addpublicLog(prid,4,str);
  1471. }else {
  1472. addpublicLog(prid,type,str);
  1473. }
  1474. sendNoticeAndSoucket(recipient.getId(),type,str);
  1475. if (type==0||type==2){
  1476. if (openid!=null) {
  1477. Integer res= weChatUtils.addNotice(openid, type,prid,new Date(),my.getName(),str);
  1478. return res;
  1479. }else {
  1480. sendEmail(my, recipient,type);
  1481. }
  1482. }
  1483. return 0;
  1484. }
  1485. private int updateAssistant(PublicRelease p, String said, String sname) {
  1486. PublicRelease newPr=new PublicRelease();
  1487. newPr.setId(p.getId());
  1488. newPr.setAssistantAid(said);
  1489. newPr.setAssistantName(sname);
  1490. if (p.getMainId()!=null){
  1491. PublicRelease mianPr=new PublicRelease();
  1492. mianPr.setId(p.getMainId());
  1493. mianPr.setAid(said);
  1494. mianPr.setAssistantAid(said);
  1495. mianPr.setAssistantName(sname);
  1496. publicReleaseMapper.updateByPrimaryKeySelective(mianPr);
  1497. }
  1498. return publicReleaseMapper.updateByPrimaryKeySelective(newPr);
  1499. }
  1500. /**
  1501. *
  1502. * @param aid 元素
  1503. * @param list 集合
  1504. * @param type 0新增 1删除
  1505. * @return
  1506. */
  1507. private String pushAssistant(String aid, List<String> list,Integer type) {
  1508. StringBuffer sb=new StringBuffer();
  1509. if(type==0){
  1510. list.add(aid);
  1511. }else if (type==1){
  1512. Iterator<String > it =list.iterator();
  1513. while (it.hasNext()){
  1514. if (it.next().equals(aid))
  1515. it.remove();
  1516. }
  1517. }
  1518. for (String s : list) {
  1519. sb.append(adminMapper.selectByPrimaryKey(s).getName()).append(",");
  1520. }
  1521. return sb.length()>0?sb.substring(0,sb.length()-1):sb.toString();
  1522. }
  1523. @Override
  1524. public int deleteAssistant(Integer id, String aid) {
  1525. PublicRelease p = publicReleaseMapper.selectByPrimaryKey(id);
  1526. if (p.getAssistantAid()==null){
  1527. throw new BusinessException("人员不存在");
  1528. }
  1529. List<String> list=new ArrayList<>(Arrays.asList(p.getAssistantAid().split(",")));
  1530. if (!list.contains(aid)){
  1531. throw new BusinessException("人员不存在");
  1532. }
  1533. String sname = pushAssistant(aid, list,1);
  1534. String said =String.join(",",list);
  1535. p.setAssistantAid(said);
  1536. p.setAssistantName(sname);
  1537. deleteAssistantPulicRelease(p,aid);
  1538. return updateAssistant(p, said, sname);
  1539. }
  1540. private void deleteAssistantPulicRelease(PublicRelease p, String aid) {
  1541. publicReleaseMapper.updateStatusByMainId(3,4,p.getMainId(),aid);
  1542. List<PublicRelease> list = publicReleaseMapper.selectByMainId(p.getMainId(), 4, null);
  1543. StringBuffer str=new StringBuffer();
  1544. str=str.append("公出").append("[").append(p.getUserNames()).append("]").append("撤销了你的协助!");
  1545. for (PublicRelease pr : list) {
  1546. if (pr.getAid().equals(aid)){
  1547. addWeChatNotice( pr.getAid(),3,str.toString(),pr.getId());
  1548. }else {
  1549. PublicRelease newPr=new PublicRelease();
  1550. newPr.setId(pr.getId());
  1551. newPr.setAssistantAid(p.getAssistantAid());
  1552. newPr.setAssistantName(p.getAssistantName());
  1553. publicReleaseMapper.updateByPrimaryKeySelective(newPr);
  1554. }
  1555. }
  1556. }
  1557. @Override
  1558. public boolean checkdeleteAssistant(Integer id, String aid) {
  1559. PublicRelease p = publicReleaseMapper.selectByPrimaryKey(id);
  1560. List<PublicRelease> list = publicReleaseMapper.selectByMainId(p.getMainId(), 4, aid);
  1561. for (PublicRelease use : list) {
  1562. // if (use.getClockIn()==1){
  1563. // return true;
  1564. // }
  1565. }
  1566. return false;
  1567. }
  1568. @Override
  1569. public boolean checkaddAssistant(Integer id, String aid) {
  1570. PublicRelease p = publicReleaseMapper.selectByPrimaryKey(id);
  1571. List<PublicRelease> list = publicReleaseMapper.selectByMainId(p.getMainId(), 4, aid);
  1572. for (PublicRelease use : list) {
  1573. if (use.getAid().equals(aid)&&use.getStatus()!=3){
  1574. return true;
  1575. }
  1576. }
  1577. return false;
  1578. }
  1579. @Override
  1580. public int updateLocation(InputPublicRelease in) {
  1581. PublicReleaseDetails prd=publicReleaseDetailsMapper.selectBypridAndUid(in);
  1582. if (prd==null){
  1583. throw new BusinessException("公出企业不存在");
  1584. }
  1585. PublicReleaseDetails updateprd=new PublicReleaseDetails();
  1586. updateprd.setId(prd.getId());
  1587. updateprd.setLatitude(in.getLatitude());
  1588. updateprd.setLongitude(in.getLongitude());
  1589. updateprd.setDistrictName(in.getDistrictName());
  1590. updateprd.setClockIn(0);
  1591. addpublicLog(in.getId(),5,"发起公出地点修改");
  1592. return publicReleaseDetailsMapper.updateByPrimaryKeySelective(updateprd);
  1593. }
  1594. @Override
  1595. public Object publicReleaseAndCount(InputPublicDtails in) {
  1596. List<OutPublicDtails> list = publicReleaseListDtailsList(in);
  1597. Map<String,Object> map=new HashMap<String, Object>();
  1598. if (list.isEmpty()){
  1599. map.put("list",list);
  1600. map.put("count",0);
  1601. map.put("durationCount",0);
  1602. map.put("peopleCount",0);
  1603. }else {
  1604. //公出次数
  1605. int count=0;
  1606. BigDecimal durationCount=BigDecimal.ZERO;
  1607. List<String> aList =new ArrayList<>();
  1608. for (OutPublicDtails e : list) {
  1609. if (e.getType()==0||e.getType()==1||e.getType()==2){
  1610. count++;
  1611. if (e.getDuration()!=null){
  1612. durationCount=durationCount.add(new BigDecimal(e.getDuration()));
  1613. }
  1614. if (!aList.contains(e.getAname())){
  1615. aList.add(e.getAname());
  1616. }
  1617. if (StringUtils.isNotEmpty(e.getAssistAidName())){
  1618. String[] split = e.getAssistAidName().split(",");
  1619. for (String s : split) {
  1620. if (!aList.contains(s)){
  1621. aList.add(s);
  1622. }
  1623. }
  1624. }
  1625. if (StringUtils.isNotEmpty(e.getAssistantName())){
  1626. String[] split = e.getAssistantName().split(",");
  1627. for (String s : split) {
  1628. if (!aList.contains(s)){
  1629. aList.add(s);
  1630. }
  1631. }
  1632. }
  1633. }
  1634. }
  1635. map.put("list",list);
  1636. map.put("count",count);
  1637. map.put("durationCount",durationCount);
  1638. map.put("peopleCount",aList.size());
  1639. }
  1640. return map;
  1641. }
  1642. /**
  1643. *
  1644. * @param in 公出参数
  1645. * @param a 新增时需哟额外知道当前任务
  1646. * @param type 0查询 1新增 2修改
  1647. * @return
  1648. */
  1649. public Map checkOrderNoDuration( InputPublicRelease in,AdminListBo a,int type) {
  1650. List<PublicRelease> list = publicReleaseMapper.selectByOrderNo(in.getOrderNo());
  1651. TOrderNewBo t = tOrderNewMapper.getOrderNewDetail(in.getOrderNo());
  1652. OutPublicRelease outPublicRelease=null;
  1653. BigDecimal count = BigDecimal.ZERO;
  1654. int timesCount=0;
  1655. List<String> peopleList = new ArrayList<>();
  1656. if (in.getId()!=null){
  1657. outPublicRelease= publicReleaseMapper.selectDtails(in.getId());
  1658. }else {
  1659. peopleList.add(TokenManager.getAdminId());
  1660. count=count.add(BigDecimal.valueOf(in.getDuration()));
  1661. timesCount++;
  1662. }
  1663. for (PublicRelease e : list) {
  1664. //新增直接计算
  1665. if (in.getId()==null){
  1666. if (e.getDuration()!=null){
  1667. count=count.add(BigDecimal.valueOf(e.getDuration()));
  1668. if (!peopleList.contains(e.getAid())){
  1669. peopleList.add(e.getAid());
  1670. }
  1671. timesCount++;
  1672. }
  1673. }else {
  1674. //修改当前数据要从修改后获取
  1675. if (!e.getId().equals(in.getId())){
  1676. if (e.getDuration()!=null){
  1677. count=count.add(BigDecimal.valueOf(e.getDuration()));
  1678. if (!peopleList.contains(e.getAid())){
  1679. peopleList.add(e.getAid());
  1680. }
  1681. timesCount++;
  1682. }
  1683. }else {
  1684. if (outPublicRelease != null && outPublicRelease.getDuration() != null) {
  1685. count = count.add(BigDecimal.valueOf(outPublicRelease.getDuration()));
  1686. if (!peopleList.contains(outPublicRelease.getAid())) {
  1687. peopleList.add(outPublicRelease.getAid());
  1688. }
  1689. timesCount++;
  1690. }
  1691. }
  1692. }
  1693. }
  1694. Map map=new HashMap();
  1695. CheckOrderNoOut data= new CheckOrderNoOut();
  1696. if (outPublicRelease!=null){
  1697. data.setPublicReleaseName(outPublicRelease.getAname());
  1698. }
  1699. //新增与修改,需要重新获取实际公出与人数与次数
  1700. if (type==1||type==2){
  1701. TOrderPublicReleaseCount update=new TOrderPublicReleaseCount();
  1702. update.setOrderNo(in.getOrderNo());
  1703. update.setFrequency(timesCount);
  1704. update.setActualDuration(count.doubleValue());
  1705. update.setPeopleCount(peopleList.size());
  1706. tOrderPublicReleaseCountMapper.updateByOrderNo(update);
  1707. }
  1708. data.setTimesCount(timesCount);
  1709. data.setPeopleCount(peopleList.size());
  1710. data.setUserName(t.getUserName());
  1711. data.setContractNo(t.getContractNo());
  1712. data.setAdminName(t.getSalesmanName());
  1713. data.setDurationCount(count);
  1714. // 0=0-1 1=1-3 2=3-5 3=5-10 4=10以上
  1715. if (t.getTotalAmount().compareTo(BigDecimal.valueOf(1)) < 0) {
  1716. data.setAmountType(0);
  1717. data.setDurationMax(BigDecimal.ZERO);
  1718. map.put("code",false);
  1719. map.put("data",data);
  1720. } else if (t.getTotalAmount().compareTo(BigDecimal.valueOf(3))<0) {
  1721. data.setAmountType(1);
  1722. data.setDurationMax(BigDecimal.valueOf(15));
  1723. if (count.compareTo(data.getDurationMax())<1){
  1724. map.put("code",true);
  1725. map.put("data",null);
  1726. }else {
  1727. map.put("code",false);
  1728. map.put("data",data);
  1729. }
  1730. } else if (t.getTotalAmount().compareTo(BigDecimal.valueOf(5))<0) {
  1731. data.setAmountType(2);
  1732. data.setDurationMax(BigDecimal.valueOf(45));
  1733. if (count.compareTo(data.getDurationMax())<1){
  1734. map.put("code",true);
  1735. map.put("data",null);
  1736. }else {
  1737. map.put("code",false);
  1738. map.put("data",data);
  1739. }
  1740. } else if (t.getTotalAmount().compareTo(BigDecimal.valueOf(10))<0) {
  1741. data.setAmountType(3);
  1742. data.setDurationMax(BigDecimal.valueOf(120));
  1743. if (count.compareTo(data.getDurationMax())<1){
  1744. map.put("code",true);
  1745. map.put("data",null);
  1746. }else {
  1747. map.put("code",false);
  1748. map.put("data",data);
  1749. }
  1750. } else if (t.getTotalAmount().compareTo(BigDecimal.valueOf(10))>=0) {
  1751. data.setAmountType(4);
  1752. map.put("code",true);
  1753. map.put("data",null);
  1754. }
  1755. return map;
  1756. }
  1757. @Override
  1758. public Map checkOrderNoDuration( InputPublicRelease in){
  1759. return checkOrderNoDuration(in,null,0);
  1760. }
  1761. @Override
  1762. public void pushOutPublicDtails(List<OutPublicDtails> list) {
  1763. List<PublicReleaseClock> publicReleaseClocks = publicReleaseClockMapper.selectList(list.stream().map(OutPublicDtails::getId).collect(Collectors.toList()));
  1764. pushiClockInTime(list, publicReleaseClocks);
  1765. }
  1766. private void pushiClockInTime(List<OutPublicDtails> list, List<PublicReleaseClock> publicReleaseClocks) {
  1767. for (OutPublicDtails outPublicDtails : list) {
  1768. List<PublicReleaseClock> colokList =new ArrayList<>();
  1769. StringBuilder sb = new StringBuilder();
  1770. for (PublicReleaseClock e : publicReleaseClocks) {
  1771. if (outPublicDtails.getPrdId().equals(e.getPrdid())){
  1772. sb.append(DateUtils.formatDateYYYYMMddHHmm(e.getClockInTime())).append("\n");
  1773. colokList.add(e);
  1774. }
  1775. colokList.sort(Comparator.comparing(PublicReleaseClock::getClockInTime));
  1776. Collections.reverse(colokList);
  1777. outPublicDtails.setList(colokList);
  1778. }
  1779. outPublicDtails.setClockInTimes(sb.toString());
  1780. }
  1781. }
  1782. @Override
  1783. public Object pushDateClock() {
  1784. List<PublicRelease> publicReleases = publicReleaseMapper.selectAll();
  1785. for (PublicRelease e : publicReleases) {
  1786. addPublicReleaseDateClock(e);
  1787. }
  1788. return 1;
  1789. }
  1790. private void addPublicReleaseDateClock(PublicRelease e) {
  1791. publicReleaseDateClockMapper.deleteByPrid(e.getId());
  1792. List<PublicReleaseDateClock> list= new ArrayList<>();
  1793. if (e.getValidDate()!=null){
  1794. List<Map> maps = JSONArray.parseArray(e.getValidDate(), Map.class);
  1795. for (Map map : maps) {
  1796. String dates = (String) map.get("value");
  1797. if (dates!=null){
  1798. Date date = DateUtils.parseDate(dates);
  1799. List<OutPublicReleaseDetails> outPublicReleaseDetails = publicReleaseDetailsMapper.selectByPrid(e.getId());
  1800. for (OutPublicReleaseDetails det : outPublicReleaseDetails) {
  1801. PublicReleaseDateClock newPRDC = new PublicReleaseDateClock();
  1802. newPRDC.setClockInTime(date);
  1803. newPRDC.setPrid(e.getId());
  1804. newPRDC.setPrdid(det.getId());
  1805. newPRDC.setStatus(det.getClockIn());
  1806. list.add(newPRDC);
  1807. }
  1808. }
  1809. }
  1810. }
  1811. if(!list.isEmpty()){
  1812. publicReleaseDateClockMapper.insertBatch(list);
  1813. list.clear();
  1814. }
  1815. }
  1816. @Override
  1817. @Cacheable(value = "releaseAndExpenseCount#300", key = "'getCounts:key='+#in.buyerName+#in.contractNo+#in.aid+#in.depId+#in.allStatus")
  1818. public List<releaseAndExpenseCountOut> releaseAndExpenseCount(InputreleaseAndExpenseCount in) {
  1819. List<releaseAndExpenseCountOut> expenseCountsOrders = publicReleaseMapper.releaseAndExpenseCount(in);
  1820. expenseCountsOrders.forEach(e->{
  1821. if (e.getExceedDuration()!=null){
  1822. double exceedDuration = Double.parseDouble(e.getExceedDuration());
  1823. if (exceedDuration>0){
  1824. e.setExceedType(1);
  1825. }
  1826. }
  1827. if (e.getMaxDuration()!=null){
  1828. if(e.getMaxDuration().equals("-1")){
  1829. e.setMaxDuration("不限制");
  1830. }
  1831. }
  1832. });
  1833. return expenseCountsOrders;
  1834. }
  1835. @Override
  1836. @CacheEvict(value = "releaseAndExpenseCount#300",allEntries = true)
  1837. public Object releaseAndExpenseCountClear() {
  1838. LoggerUtils.debug(getClass(),"公出&报销缓存清除。");
  1839. return 1;
  1840. }
  1841. }