【Java项目】添加教职工

来源:互联网 发布:算法和程序的关系 编辑:程序博客网 时间:2024/06/02 01:10

简介

    项目中教职工管理的添加功能,由于是前辈留下的,总共用了5张表,前前后后这段代码改了n次,最后终于实现了,特此纪念一下。

代码

    @Override    @Transactional(rollbackFor = Exception.class)    public boolean insertTeacher(String schoolNo, StaffModel staffModel) {        //01、判断职工是否存在        if (!this.isExist(staffModel)) {            logger.error("Service-职工已经存在");            return false;        }        //构造机构实体        StaffEntity staffEntity = getStaffEntity(staffModel);        staffEntity.setId(UuidUtils.base58Uuid());//添加id        staffEntity.setCreateTime(new Date());//添加时间        staffEntity.setUpdateTime(new Date());        staffEntity.setIsDelete(0);        //02、更新staff表        int flag1 = staffDao.insert(staffEntity);        //03、更新教职工与机构信息。        int flag2 = insertInstitutions(staffModel.getInstitutionId(), staffEntity.getId());        //添加角色        List<UserRoleModel> roleList = new ArrayList<>();        UserRoleModel userRoleModel = new UserRoleModel();        userRoleModel.setId(staffEntity.getId());        List<String> roles = new ArrayList<>();        roles.add(staffModel.getRoleId());        userRoleModel.setRoleId(roles);        //userRoleModel的属性从UserEntity中获取        UserEntity userEntity = new UserEntity();        userEntity.setId(staffEntity.getId());        userEntity.setPassword(PasswordUtil.encryptPassword(staffModel.getCode()));//插入用户表的密码加密设置        userEntity.setEmail(staffModel.getEmail());        userEntity.setTelNum(staffModel.getTelNum());        userEntity.setUserCode(staffModel.getCode());        userEntity.setUserRealName(staffModel.getName());        userEntity.setSchoolNo(schoolNo);        userEntity.setCreateTime(new Date());        userEntity.setUpdateTime(new Date());        userRoleModel.setUserEntity(userEntity);        roleList.add(userRoleModel);        // 04、在user表添加user        int flag3 = userManagementFacade.insertUser(roleList);        if (flag1 > 0 && flag2 > 0 && flag3 > 0) {            return true;        } else {            return false;        }    }


 /**     * 判断教职工是否已经存在     *     * @param staffModel     * @return 存在返回false, 不存在返回true;     */    private boolean isExist(StaffModel staffModel) {        StaffExample staffExample = new StaffExample();        StaffCriteria staffCriteria = staffExample.createCriteria();        staffCriteria.andCodeEqualTo(staffModel.getCode());//判断教职工号是否相同        staffCriteria.andIdentityCardIdEqualTo(staffModel.getIdentityCardId()); //判断身份证是否相同        staffCriteria.andIsDeleteEqualTo((byte) 0);        List<StaffEntity> list = this.selectByExample(staffExample);        return list == null || list.isEmpty();    }


//插入教职工-机构表,关系为1:n

private int insertInstitutions(String institutionIds, String staffId) {        String[] ids = institutionIds.split(",");        int i = 0;        for (String id : ids) {            StaffInstitutionEntity s = new StaffInstitutionEntity();            s.setInstitutionId(id);            s.setStaffId(staffId);            s.setId(UuidUtils.base58Uuid());            s.setCreateTime(new Date());            s.setUpdateTime(new Date());            s.setIsDelete(0);            int flag = staffInstitutionDao.insert(s);            if (flag == 0) {                return 0;            } else {                i = i + 1;            }        }        return i;    }

//Model转换为entity实体

private StaffEntity getStaffEntity(StaffModel m) {        StaffEntity staffEntity = new StaffEntity();        staffEntity.setId(m.getId());        staffEntity.setName(m.getName());        staffEntity.setCode(m.getCode());        staffEntity.setAccountAddress(m.getAccountAddress());        staffEntity.setAchivement(m.getAchivement());        staffEntity.setBrief(m.getBrief());        staffEntity.setCheckOrNot(m.getCheckOrNot());        staffEntity.setDegree(m.getDegree());        staffEntity.setEducation(m.getEducation());        staffEntity.setEmail(m.getEmail());        staffEntity.setEntranceDate(m.getEntranceDate());        staffEntity.setGraduateSchool(m.getGraduateSchool());        staffEntity.setIdentityCardId(m.getIdentityCardId());        staffEntity.setIsExternal(m.getIsExternal());        staffEntity.setNation(m.getNation());        staffEntity.setPictrue(m.getPictrue());        staffEntity.setPoliticalStatus(m.getPoliticalStatus());        staffEntity.setSex(m.getSex());        staffEntity.setStatus(m.getStatus());        staffEntity.setTelNum(m.getTelNum());        staffEntity.setDegree(m.getDegree());        staffEntity.setIsTeacher(m.getIsTeacher());        staffEntity.setJobtitleId(m.getJobtitleId());        staffEntity.setNowAddress(m.getNowAddress());        staffEntity.setIsTutor(m.getIsTutor());        staffEntity.setDutyId(m.getDutyId());        return staffEntity;    }


错误原因

1.业务逻辑:插入五张表的顺序关系;

2.架构设计:不同服务,使用分布式事务问题;

3.数据库关联:教职工id与用户id一一对应,前期没有考虑到这一问题。

总结

认真的对待自己的代码,不断重复基础知识,积累实战经验。