PublicReleaseServiceImpl.java 69 KB

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