com.taikang.dic.ltci.service.impl.AgencyStaffImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.taikang.dic.ltci.service.impl.AgencyStaffImpl.java

Source

package com.taikang.dic.ltci.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.taikang.dic.ltci.api.model.ResultDTO;
import com.taikang.dic.ltci.common.enumeration.IsValidEnum;
import com.taikang.dic.ltci.common.enumeration.StatusCodeEnum;
import com.taikang.dic.ltci.common.exception.InsertRepeatException;
import com.taikang.dic.ltci.common.exception.NotFoundByIdException;
import com.taikang.dic.ltci.common.util.DateFormatUtil;
import com.taikang.dic.ltci.common.util.JsonUtils;
import com.taikang.dic.ltci.dao.AgencyDAO;
import com.taikang.dic.ltci.dao.StaffDAO;
import com.taikang.dic.ltci.dao.StaffHistoryDAO;
import com.taikang.dic.ltci.dao.StaffSpecialDAO;
import com.taikang.dic.ltci.model.AgencyDO;
import com.taikang.dic.ltci.model.AgencyDOExample;
import com.taikang.dic.ltci.model.AgencyDOExample.Criteria;
import com.taikang.dic.ltci.model.PatrolTaskQueryDO;
import com.taikang.dic.ltci.model.StaffComparedDO;
import com.taikang.dic.ltci.model.StaffDO;
import com.taikang.dic.ltci.model.StaffDOExample;
import com.taikang.dic.ltci.model.StaffDetailDO;
import com.taikang.dic.ltci.model.StaffDiffDO;
import com.taikang.dic.ltci.model.StaffHistoryDO;
import com.taikang.dic.ltci.model.StaffHistoryDOExample;
import com.taikang.dic.ltci.model.StaffSpecialDO;
import com.taikang.dic.ltci.service.AgencyStaffService;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

@Service
public class AgencyStaffImpl implements AgencyStaffService {
    private Logger logger = LoggerFactory.getLogger(AgencyStaffImpl.class);
    @Autowired
    private AgencyDAO agencyDAO;
    @Autowired
    private StaffDAO staffDAO;
    @Autowired
    private StaffHistoryDAO staffHistoryDAO;
    @Autowired
    private StaffSpecialDAO staffSpecialDAO;

    private static char[] org = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

    @Override
    @Transactional
    public boolean insertAgency(String path) throws Exception {

        //?---
        File agencyfile = new File(path);
        InputStream agencyIn = new FileInputStream(agencyfile);
        Workbook angencyWorkbook = Workbook.getWorkbook(agencyIn);

        //?Sheet
        int agencySheetNum = angencyWorkbook.getNumberOfSheets();
        logger.info("========sheet?" + agencySheetNum + "=========");
        //sheet
        for (int i = 0; i < agencySheetNum; i++) {

            Sheet sheet = angencyWorkbook.getSheet(i);
            //?Sheet?????????0?
            int rowNum = sheet.getRows();

            //
            for (int j = 1; j < rowNum; j++) {
                int columnNum = sheet.getColumns();
                AgencyDO agencyDO = new AgencyDO();
                agencyDO.setAgencyid(UUID.randomUUID().toString().replaceAll("-", ""));
                agencyDO.setIsValid(IsValidEnum.NO_DELETE.getValue());
                agencyDO.setCreatedTime(new Date());
                agencyDO.setOperatedTime(new Date());
                if (null == sheet.getCell(0, j).getContents() || "".equals(sheet.getCell(0, j).getContents())) {
                    continue;
                }
                //
                for (int c = 0; c < columnNum; c++) {
                    Cell cell = sheet.getCell(c, j); //
                    String content = cell.getContents();

                    //??
                    if ("".equals(content) || content == null) {
                        continue;
                    }

                    switch (c) {
                    case 0:
                        agencyDO.setAgencyCode(content);
                        break;
                    case 1:
                        agencyDO.setOrgCode(content);
                        break;
                    case 2:
                        agencyDO.setAgencyName(content);
                        break;
                    case 3:
                        agencyDO.setProvinceCode(content);
                        break;
                    case 4:
                        agencyDO.setCityCode(content);
                        break;
                    case 5:
                        agencyDO.setAreaCode(content);
                        break;
                    case 6:
                        agencyDO.setAgencyAddressDetail(content);
                        break;
                    case 7:
                        agencyDO.setAgencyType(content);
                        break;
                    case 8:
                        agencyDO.setAgencyLevel(Integer.parseInt(content));
                        break;
                    case 9:
                        agencyDO.setAgencyState(Integer.parseInt(content));
                        break;
                    case 10:
                        agencyDO.setAgencyServiceType(content);
                        break;
                    case 11:
                        agencyDO.setOwnershipForm(Integer.parseInt(content));
                        break;
                    case 12:
                        agencyDO.setPracticeLicense(content);
                        break;
                    case 13:
                        agencyDO.setLegalRepresentative(content);
                        break;
                    case 14:
                        agencyDO.setLegalPapersType(content);
                        break;
                    case 15:
                        agencyDO.setLegalIdCard(content);
                        break;
                    case 16:
                        agencyDO.setLegalPhoneNo(content);
                        break;
                    case 17:
                        agencyDO.setAgencyServiceContact(content);
                        break;
                    case 18:
                        agencyDO.setAgencyContactPhone(content);
                        break;
                    case 19:
                        agencyDO.setAgencyContactEmail(content);
                        break;
                    case 20:
                        agencyDO.setAgencyBedNum(Integer.parseInt(content));
                        break;
                    case 21:
                        agencyDO.setConstructionArea(Double.parseDouble(content));
                        break;
                    case 22:
                        agencyDO.setRemark(content);
                        break;
                    case 23:
                        agencyDO.setAgencyCoordinates(content);
                        break;
                    case 24:
                        agencyDO.setCreatedBy(content);
                        break;
                    case 25:
                        agencyDO.setCreatedOrg(content);
                        break;
                    case 26:
                        agencyDO.setKeyBeginDate(DateFormatUtil.strToDate(content));
                        break;
                    case 27:
                        agencyDO.setKeyEndDate(DateFormatUtil.strToDate(content));
                        break;

                    default:
                        break;
                    }
                }
                String areaCode = "";
                //? ?
                if (StringUtils.isEmpty(agencyDO.getAreaCode())) {
                    areaCode = agencyDO.getCityCode();
                } else {
                    areaCode = agencyDO.getAreaCode();
                }
                String agencyCode = areaCode + agencyDO.getAgencyCode();
                //orgCode??,????
                //        while (true) {
                //          AgencyDOExample doExample = new AgencyDOExample();
                //          Criteria criteria = doExample.createCriteria();
                //          criteria.andAgencyCodeEqualTo(agencyCode);
                //          List<AgencyDO> selectByExample = agencyDAO.selectByExample(doExample);
                //
                //          if (selectByExample.size() <= 0) {
                //            //? ?code?? ????
                //            break;
                //          }
                //          agencyCode = this.getAgencyCode(areaCode);
                //        }

                agencyDO.setAgencyCode(agencyCode);
                agencyDO.setSecretKey(agencyCode);
                logger.info("====???" + JsonUtils.makeJsonForObject(agencyDO)
                        + "============");
                int insertLength = agencyDAO.insertSelective(agencyDO);
                if (insertLength < 0) {
                    throw new InsertRepeatException("?");
                }
            }
        }
        return true;
    }

    @Override
    @Transactional
    public boolean insertStaff(String path) throws Exception {

        //?---
        File agencyfile = new File(path);
        InputStream agencyIn = new FileInputStream(agencyfile);
        Workbook angencyWorkbook = Workbook.getWorkbook(agencyIn);

        //?Sheet
        int agencySheetNum = angencyWorkbook.getNumberOfSheets();
        logger.info("========sheet?" + agencySheetNum + "=========");
        //sheet
        for (int i = 0; i < agencySheetNum; i++) {
            Sheet sheet = angencyWorkbook.getSheet(i);
            //?Sheet?????????0?
            int rowNum = sheet.getRows();
            logger.info("===?" + rowNum + "========");
            //
            for (int j = 1; j < rowNum; j++) {
                int columnNum = sheet.getColumns();
                logger.info("===?" + columnNum + "========");
                StaffDO staffDO = new StaffDO();

                staffDO.setStaffid(UUID.randomUUID().toString().replaceAll("-", ""));
                staffDO.setIsValid(IsValidEnum.NO_DELETE.getValue());
                staffDO.setCreatedTime(new Date());
                staffDO.setOperatedTime(new Date());
                if (null == sheet.getCell(0, j).getContents() || "".equals(sheet.getCell(0, j).getContents())) {
                    continue;
                }
                //
                for (int c = 0; c < columnNum; c++) {
                    Cell cell = sheet.getCell(c, j); //
                    String content = cell.getContents();
                    //               logger.info("=====??" + content + "========");

                    //??
                    if ("".equals(content) || content == null) {
                        continue;
                    }

                    switch (c) {
                    case 0:
                        staffDO.setEmpCode(content);
                        break;
                    case 1:
                        staffDO.setStaffName(content);
                        break;
                    case 2:
                        staffDO.setAgencyCode(content);
                        break;
                    case 3:
                        staffDO.setAgencyName(content);
                        break;
                    case 4:
                        staffDO.setStaffPhone(content);
                        break;
                    case 5:
                        staffDO.setCredentialType(Integer.getInteger(content));
                        break;
                    case 6:
                        staffDO.setCredentialNumber(content);
                        break;
                    case 7:
                        staffDO.setCredentialValidityDate(DateFormatUtil.strToDateTime(content));
                        break;
                    case 8:
                        staffDO.setStaffGender(Integer.getInteger(content));
                        break;
                    case 9:
                        staffDO.setHighestEducation(content);
                        break;
                    case 10:
                        staffDO.setAcquireDegree(content);
                        break;
                    case 11:
                        staffDO.setDiplomaDate(DateFormatUtil.strToDateTime(content));
                        break;
                    case 12:
                        staffDO.setDegreeDate(DateFormatUtil.strToDateTime(content));
                        break;
                    case 13:
                        staffDO.setJobScope(content);
                        break;
                    case 14:
                        staffDO.setStaffLevel(content);
                        break;
                    case 15:
                        staffDO.setCategory(content);
                        break;
                    case 16:
                        staffDO.setRemark(content);
                        break;
                    case 17:
                        staffDO.setAuditState(Integer.parseInt(content));
                        break;
                    case 18:
                        staffDO.setCreatedBy(content);
                        break;
                    case 19:
                        staffDO.setCreatedOrg(content);
                        break;

                    default:
                        break;
                    }
                }
                String areaCode = staffDO.getAgencyCode();
                //? ?
                String staffCode = this.getStaffCode(areaCode);
                //orgCode??,????
                while (true) {
                    StaffDOExample doExample = new StaffDOExample();
                    com.taikang.dic.ltci.model.StaffDOExample.Criteria criteria = doExample.createCriteria();
                    criteria.andStaffCodeEqualTo(staffCode);
                    List<StaffDO> selectByExample = staffDAO.selectByExample(doExample);

                    if (selectByExample.size() <= 0) {
                        //? ?code?? ????
                        break;
                    }
                    staffCode = this.getStaffCode(staffCode);
                }

                staffDO.setStaffCode(staffCode);
                logger.info("====???" + JsonUtils.makeJsonForObject(staffDO)
                        + "============");
                int insertLength = staffDAO.insertSelective(staffDO);
                if (insertLength < 0) {
                    throw new InsertRepeatException("?");
                }
            }
        }
        return true;
    }

    public static String getAgencyCode(String areaCode) {
        StringBuffer sb = new StringBuffer(areaCode);
        SecureRandom random = new SecureRandom();
        for (int i = 0; i < 6; i++) {
            sb.append(org[random.nextInt(org.length)]);
        }

        return sb.toString();
    }

    public static String getStaffCode(String areaCode) {
        StringBuffer sb = new StringBuffer(areaCode);
        SecureRandom random = new SecureRandom();
        for (int i = 0; i < 6; i++) {
            sb.append(org[random.nextInt(org.length)]);
        }

        return sb.toString();
    }

    /** staffcode?staff? */
    @Override
    public ResultDTO getStaff(String staffCode) {
        StaffDOExample staffDOExample = new StaffDOExample();
        com.taikang.dic.ltci.model.StaffDOExample.Criteria criteria = staffDOExample.createCriteria();
        criteria.andStaffCodeEqualTo(staffCode);
        List<StaffDO> list = staffDAO.selectByExample(staffDOExample);
        ResultDTO resultDTO = new ResultDTO();

        if (!list.isEmpty()) {
            resultDTO.setMessage("?");
            resultDTO.setDatas(list.get(0));
            return resultDTO;
        } else {
            throw new NotFoundByIdException("");
        }
    }

    /** ?? ???staff? */
    @Override
    public ResultDTO staffDetail(String serialNo) {
        //??
        StaffHistoryDOExample example = new StaffHistoryDOExample();
        com.taikang.dic.ltci.model.StaffHistoryDOExample.Criteria criteria = example.createCriteria();
        criteria.andSerialNoEqualTo(serialNo);

        List<StaffHistoryDO> staffHistoryList = staffHistoryDAO.selectByExample(example);
        StaffHistoryDO staffHistoryDO = new StaffHistoryDO();
        if (!staffHistoryList.isEmpty()) {
            staffHistoryDO = staffHistoryList.get(0);
        } else {
            throw new NotFoundByIdException("?");
        }

        //??
        StaffDOExample staffDOExample = new StaffDOExample();
        com.taikang.dic.ltci.model.StaffDOExample.Criteria staffCriteria = staffDOExample.createCriteria();
        staffCriteria.andStaffCodeEqualTo(staffHistoryDO.getStaffCode());
        List<StaffDO> list = staffDAO.selectByExample(staffDOExample);
        StaffDO staffDO = new StaffDO();
        if (!list.isEmpty()) {
            staffDO = list.get(0);
        } else {
            throw new NotFoundByIdException("");
        }

        StaffComparedDO staffComparedBefore = new StaffComparedDO();
        StaffComparedDO staffComparedAfter = new StaffComparedDO();

        BeanUtils.copyProperties(staffHistoryDO, staffComparedAfter);
        BeanUtils.copyProperties(staffDO, staffComparedBefore);
        List<StaffDiffDO> staffDiffDOs = null;
        try {
            staffDiffDOs = convert(staffComparedBefore, staffComparedAfter);
        } catch (Exception e) {
            logger.error("?", e);
        }
        //??
        for (StaffDiffDO staffDiffDO : staffDiffDOs) {
            if (staffDiffDO.getKey().equals("credentialValidityDate") || staffDiffDO.getKey().equals("degreeDate")
                    || staffDiffDO.getKey().equals("diplomaDate")
                    || staffDiffDO.getKey().equals("qualificationCertificationDate")
                    || staffDiffDO.getKey().equals("practiceCertificationDate")) {
                staffDiffDO.setNewValue(DateFormatUtil.changeDateType((String) staffDiffDO.getNewValue()));
                staffDiffDO.setOldValue(DateFormatUtil.changeDateType((String) staffDiffDO.getOldValue()));
            }
        }
        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setDatas(staffDiffDOs);
        resultDTO.setMessage("?");
        resultDTO.setStatus(StatusCodeEnum.OK.getValue());
        return resultDTO;
    }

    private List<com.taikang.dic.ltci.model.StaffDiffDO> convert(StaffComparedDO staffComparedBefore,
            StaffComparedDO staffComparedAfter) throws IllegalArgumentException, IllegalAccessException {
        List<StaffDiffDO> staffDiffDOs = new ArrayList<StaffDiffDO>();
        Field[] fieldBefores = staffComparedBefore.getClass().getDeclaredFields();
        Field[] fieldAfters = staffComparedAfter.getClass().getDeclaredFields();
        for (Field fieldBefore : fieldBefores) {
            for (Field fieldAfter : fieldAfters) {
                if (fieldAfter.getName().equals(fieldBefore.getName())) {
                    StaffDiffDO staffDiffDO = new StaffDiffDO();
                    staffDiffDO.setKey(fieldAfter.getName());
                    fieldAfter.setAccessible(true);
                    fieldBefore.setAccessible(true);
                    if (fieldAfter.get(staffComparedBefore) != null) {
                        staffDiffDO.setOldValue(fieldAfter.get(staffComparedBefore).toString());
                    } else {
                        staffDiffDO.setOldValue("");
                    }
                    if (fieldBefore.get(staffComparedAfter) != null) {
                        staffDiffDO.setNewValue(fieldBefore.get(staffComparedAfter).toString());
                    } else {
                        staffDiffDO.setNewValue("");
                    }
                    staffDiffDOs.add(staffDiffDO);
                }
            }
        }
        return staffDiffDOs;
    }

    /** ? ??staffagency?? */
    @Override
    public ResultDTO queryStaff(String name) {
        List<StaffSpecialDO> staffs = staffSpecialDAO.getStaffs(name);
        List<PatrolTaskQueryDO> speciaLists = new ArrayList<PatrolTaskQueryDO>();

        for (StaffSpecialDO list : staffs) {
            PatrolTaskQueryDO patrolTaskQueryDO = new PatrolTaskQueryDO();
            patrolTaskQueryDO.setPatrolObjectAddr(list.getAgencyAddressDetail());
            patrolTaskQueryDO.setPatrolObjectId(list.getStaffCode());
            patrolTaskQueryDO.setPatrolObjectName(list.getStaffName());
            patrolTaskQueryDO.setPatrolObjectPhone(list.getStaffPhone());
            patrolTaskQueryDO.setIdCard(list.getCredentialNumber());
            speciaLists.add(patrolTaskQueryDO);
        }

        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setStatus(StatusCodeEnum.OK.getValue());
        resultDTO.setDatas(speciaLists);
        resultDTO.setMessage("??");
        return resultDTO;
    }

    /** ? ?? ? */
    @Override
    public ResultDTO queryAgency(String name) {
        AgencyDOExample agencyDOExample = new AgencyDOExample();
        Criteria criteria = agencyDOExample.createCriteria();
        if (name != null) {
            criteria.andAgencyNameLike("%" + name + "%");
        }
        criteria.andIsValidEqualTo(IsValidEnum.NO_DELETE.getValue());
        List<AgencyDO> lists = agencyDAO.selectByExample(agencyDOExample);
        List<PatrolTaskQueryDO> speciaLists = new ArrayList<PatrolTaskQueryDO>();

        for (AgencyDO list : lists) {
            PatrolTaskQueryDO patrolTaskQueryDO = new PatrolTaskQueryDO();
            patrolTaskQueryDO.setPatrolObjectAddr(list.getAgencyAddressDetail());
            patrolTaskQueryDO.setPatrolObjectId(list.getAgencyCode());
            patrolTaskQueryDO.setPatrolObjectName(list.getAgencyName());
            patrolTaskQueryDO.setPatrolObjectPhone(list.getAgencyContactPhone());
            speciaLists.add(patrolTaskQueryDO);
        }

        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setStatus(StatusCodeEnum.OK.getValue());
        resultDTO.setDatas(speciaLists);
        resultDTO.setMessage("??");
        return resultDTO;
    }

    @Override
    public ResultDTO getAgencyByStaff(String code) {
        StaffSpecialDO staffSpecialDO = staffSpecialDAO.getAgencyByStaff(code);
        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setStatus(StatusCodeEnum.OK.getValue());
        resultDTO.setDatas(staffSpecialDO);
        resultDTO.setMessage("??");
        return resultDTO;
    }

    /** ???? */
    @Override
    public ResultDTO getAgencyByAgencyCode(String code) {
        AgencyDOExample agencyDOExample = new AgencyDOExample();
        com.taikang.dic.ltci.model.AgencyDOExample.Criteria agencyCriteria = agencyDOExample.createCriteria();
        agencyCriteria.andAgencyCodeEqualTo(code);
        agencyCriteria.andIsValidEqualTo(IsValidEnum.NO_DELETE.getValue());

        List<AgencyDO> list = agencyDAO.selectByExample(agencyDOExample);
        if (list.isEmpty()) {
            throw new NotFoundByIdException("?" + code + " ??");
        }
        ResultDTO dto = new ResultDTO();
        dto.setStatus(StatusCodeEnum.OK.getValue());
        dto.setDatas(list.get(0));
        return dto;
    }

    /** ???? */
    @Override
    public ResultDTO getStaffByStaffCode(String code) {
        StaffSpecialDO staffSpecialDO = staffSpecialDAO.getAgencyByStaff(code);
        if (staffSpecialDO == null) {
            throw new NotFoundByIdException("?" + code + " ??");
        }
        ResultDTO dto = new ResultDTO();
        dto.setStatus(StatusCodeEnum.OK.getValue());
        dto.setDatas(staffSpecialDO);
        return dto;
    }
}