员工部门增加

master
LegnaYet 6 years ago
parent 01cd05faec
commit 95b336ef50

@ -0,0 +1,51 @@
package com.kiisoo.ic.config;
import com.google.common.collect.Maps;
import lombok.val;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
import me.chanjar.weixin.cp.constant.WxCpConsts;
import me.chanjar.weixin.cp.message.WxCpMessageRouter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author Binary Wang(https://github.com/binarywang)
*/
@Configuration
@EnableConfigurationProperties(WxCpProperties.class)
public class WxCpConfiguration {
private WxCpProperties properties;
private static Map<Integer, WxCpMessageRouter> routers = Maps.newHashMap();
private static Map<Integer, WxCpService> cpServices = Maps.newHashMap();
@Autowired
public WxCpConfiguration(WxCpProperties properties) {
this.properties = properties;
}
public static WxCpService getCpService(Integer agentId) {
return cpServices.get(agentId);
}
@PostConstruct
public void initServices() {
cpServices = this.properties.getAppConfigs().stream().map(a -> {
val configStorage = new WxCpDefaultConfigImpl();
configStorage.setCorpId(this.properties.getCorpId());
configStorage.setAgentId(a.getAgentId());
configStorage.setCorpSecret(a.getSecret());
val service = new WxCpServiceImpl();
service.setWxCpConfigStorage(configStorage);
return service;
}).collect(Collectors.toMap(service -> service.getWxCpConfigStorage().getAgentId(), a -> a));
}
}

@ -0,0 +1,53 @@
package com.kiisoo.ic.config;
import com.kiisoo.ic.utils.JsonUtils;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.List;
/**
* @author Binary Wang(https://github.com/binarywang)
*/
@Getter
@Setter
@ConfigurationProperties(prefix = "wechat.cp")
public class WxCpProperties {
/**
* corpId
*/
private String corpId;
private List<AppConfig> appConfigs;
@Getter
@Setter
public static class AppConfig {
/**
* AgentId
*/
private Integer agentId;
/**
* Secret
*/
private String secret;
/**
* token
*/
private String token;
/**
* EncodingAESKey
*/
private String aesKey;
}
@Override
public String toString() {
return JsonUtils.toJson(this);
}
}

@ -0,0 +1,39 @@
package com.kiisoo.ic.department.controller;
import com.kiisoo.ic.common.BaseController;
import com.kiisoo.ic.employee.entity.EmployeeDO;
import com.kiisoo.ic.employee.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.Map;
/**
* @Description: controller
* @Auther: yechenhao
* @Date: 2020/4/7 0002 10:06
* @Version: v1
*/
@Controller
@RequestMapping("/user")
@Slf4j
public class DepartmentConreoller extends BaseController {
@Autowired
private EmployeeService employeeService;
@RequestMapping(value = "add",method = RequestMethod.POST)
public Map<String,Object> addUser(@RequestBody EmployeeDO employee){
try {
Boolean hasAdd = employeeService.addUser(employee);
return data(hasAdd);
}catch (Exception e){
log.error("添加用户失败",e);
return fail();
}
}
}

@ -0,0 +1,21 @@
package com.kiisoo.ic.department.entity;
import lombok.Data;
import me.chanjar.weixin.cp.bean.Gender;
/**
* @Description:
* @Auther: yechenhao
* @Date: 2020/4/7 0002 10:06
* @Version: v1
*/
@Data
public class DepartmentDO {
/**
* idid
*/
private Long id;
private String name;
private Long parentId;
private Long order;
}

@ -0,0 +1,33 @@
package com.kiisoo.ic.department.service;
import com.kiisoo.ic.employee.entity.EmployeeDO;
import com.kiisoo.ic.wx.service.QWMailListManageService;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* @Description: service
* @Auther: yechenhao
* @Date: 2020/4/7 0002 10:06
* @Version: v1
*/
@Service
public class EmployeeService {
@Autowired
private QWMailListManageService qwMailListManageService;
@Transactional(rollbackFor = Exception.class)
public Boolean addUser(EmployeeDO employee) throws WxErrorException {
//数据库创建用户
//数据库创建登录account
//成功则添加用户到企业微信
qwMailListManageService.addUser(employee);
return true;
}
}

@ -0,0 +1,39 @@
package com.kiisoo.ic.employee.controller;
import com.kiisoo.ic.common.BaseController;
import com.kiisoo.ic.employee.entity.EmployeeDO;
import com.kiisoo.ic.employee.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.Map;
/**
* @Description: controller
* @Auther: yechenhao
* @Date: 2020/4/7 0002 10:06
* @Version: v1
*/
@Controller
@RequestMapping("/user")
@Slf4j
public class EmployeeConreoller extends BaseController {
@Autowired
private EmployeeService employeeService;
@RequestMapping(value = "add",method = RequestMethod.POST)
public Map<String,Object> addUser(@RequestBody EmployeeDO employee){
try {
Boolean hasAdd = employeeService.addUser(employee);
return data(hasAdd);
}catch (Exception e){
log.error("添加用户失败",e);
return fail();
}
}
}

@ -0,0 +1,89 @@
package com.kiisoo.ic.employee.entity;
import lombok.Data;
import me.chanjar.weixin.cp.bean.Gender;
/**
* @Description:
* @Auther: yechenhao
* @Date: 2020/4/7 0002 10:06
* @Version: v1
*/
@Data
public class EmployeeDO {
/**
* id
*/
private Long id;
/**
* id
* 1~64_-@.
*/
private String userId;
/**
*
*/
private String name;
/**
* id,20(idid)
*/
private Long[] departIds;
/**
* 0department[0, 2^32)
*/
private Integer[] orders;
/**
* 0~128
*/
private String position;
/**
* mobile/email
*/
private String mobile;
/**
* 12
*/
private Gender gender;
/**
* 6~64emailmobile/email
*/
private String email;
/**
* 128
*/
private String address;
/**
* mediaidmediaid
*/
private String avatarMediaId;
/**
*
*/
private Integer status;
/**
* /10
*/
private Integer enable;
/**
*
*/
private String alias;
/**
* is_leader_in_dept.
* department10
*/
private Integer[] isLeaderInDept;
/**
* 32-
*/
private String telephone;
/**
*
*/
private String inviteQrCode;
/**
* 使3true
*/
private Boolean toInvite;
}

@ -0,0 +1,33 @@
package com.kiisoo.ic.employee.service;
import com.kiisoo.ic.employee.entity.EmployeeDO;
import com.kiisoo.ic.wx.service.QWMailListManageService;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* @Description: service
* @Auther: yechenhao
* @Date: 2020/4/7 0002 10:06
* @Version: v1
*/
@Service
public class EmployeeService {
@Autowired
private QWMailListManageService qwMailListManageService;
@Transactional(rollbackFor = Exception.class)
public Boolean addUser(EmployeeDO employee) throws WxErrorException {
//数据库创建用户
//数据库创建登录account
//成功则添加用户到企业微信
qwMailListManageService.addUser(employee);
return true;
}
}

@ -0,0 +1,28 @@
package com.kiisoo.ic.utils;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
/**
* @author Binary Wang(https://github.com/binarywang)
*/
public class JsonUtils {
private static final ObjectMapper JSON = new ObjectMapper();
static {
JSON.setSerializationInclusion(Include.NON_NULL);
JSON.configure(SerializationFeature.INDENT_OUTPUT, Boolean.TRUE);
}
public static String toJson(Object obj) {
try {
return JSON.writeValueAsString(obj);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
}

@ -0,0 +1,53 @@
package com.kiisoo.ic.wx.service;
import com.kiisoo.ic.config.WxCpConfiguration;
import com.kiisoo.ic.department.entity.DepartmentDO;
import com.kiisoo.ic.employee.entity.EmployeeDO;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpDepartmentService;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.WxCpUserService;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.bean.WxCpDepart;
import me.chanjar.weixin.cp.bean.WxCpUser;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
/**
* @Description: service
* @Auther: yechenhao
* @Date: 2020/4/7 0002 10:06
* @Version: v1
*/
@Service
public class QWMailListManageService {
@Value("${qywx.applicationid}")
private Integer applicationid;
private WxCpService wxCpService;
/**
*
*/
public void addUser(EmployeeDO employeeDO) throws WxErrorException {
wxCpService = WxCpConfiguration.getCpService(applicationid);
WxCpUserService userService = wxCpService.getUserService();
WxCpUser wxCpUser = new WxCpUser();
BeanUtils.copyProperties(employeeDO,wxCpUser);
userService.create(wxCpUser);
}
/**
*
*/
public void addDepartment(DepartmentDO departmentDO) throws WxErrorException {
wxCpService = WxCpConfiguration.getCpService(applicationid);
WxCpDepartmentService departmentService = wxCpService.getDepartmentService();
WxCpDepart wxCpDepart = new WxCpDepart();
BeanUtils.copyProperties(departmentDO,wxCpDepart);
departmentService.create(wxCpDepart);
}
}
Loading…
Cancel
Save