package com.unis.base.app.service.impl;

import com.unis.base.app.service.PersonnelService;
import com.unis.base.client.PersonnelFegin;
import com.unis.base.common.vo.DRyxxsjyVO;
import com.unis.common.utils.CurrentUtil;
import com.unis.common.utils.ResponseWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/** 
 * @ClassName: PersonnelServiceImpl
 * @Description: 人员信息 service实现类
 * @author WangZL
 * @date 2019年9月6日 上午11:18:54  
 */
@Service
public class PersonnelServiceImpl implements PersonnelService {

	@Autowired
	private PersonnelFegin personnelFegin;

	/**
	 * 新增人员列表信息
	 * @param dRyxxsjyVOList
	 * @return
	 */
	@Override
	public ResponseWrapper insertList(List<DRyxxsjyVO> dRyxxsjyVOList) {
		if (dRyxxsjyVOList != null && dRyxxsjyVOList.size() > 0 ){
			ResponseWrapper responseWrapper = personnelFegin.deleteList(dRyxxsjyVOList.get(0).getKddm());
			if (!responseWrapper.isSuccess()){
				return responseWrapper;
			}
			for (DRyxxsjyVO dRyxxsjyVO : dRyxxsjyVOList) {
				if (StringUtils.isEmpty(dRyxxsjyVO.getKddm())){
					ResponseWrapper.error("缺少必填字段，请联系管理员");
				}
				if (StringUtils.isEmpty(dRyxxsjyVO.getRybh())){
					dRyxxsjyVO.setStatus(1);
					dRyxxsjyVO.setCreator(CurrentUtil.getUserName());
					dRyxxsjyVO.setCreateDate(new Date());
					ResponseWrapper responseWrapperInsert = personnelFegin.insert(dRyxxsjyVO);
					if (!responseWrapperInsert.isSuccess()) {
						return responseWrapperInsert;
					}
				} else {
					dRyxxsjyVO.setStatus(1);
					dRyxxsjyVO.setRefresher(CurrentUtil.getUserName());
					dRyxxsjyVO.setRefreshTime(new Date());
					ResponseWrapper responseWrapperUpdate = personnelFegin.personnelUpdate(dRyxxsjyVO);
					if (!responseWrapperUpdate.isSuccess()) {
						return responseWrapperUpdate;
					}
				}
			}
			return ResponseWrapper.success();
		} else {
			return ResponseWrapper.success();
		}
	}
}
