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

import com.unis.base.app.service.EquipmentService;
import com.unis.base.client.EquipmentFeign;
import com.unis.base.common.vo.DSbxxsjyClassifyVO;
import com.unis.base.common.vo.DSbxxsjyVO;
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: EquipmentServiceImpl
 * @Description: 设备信息 service实现类
 * @author WangZL
 * @date 2019年9月6日 上午11:18:54  
 */
@Service
public class EquipmentServiceImpl implements EquipmentService {

	@Autowired
	private EquipmentFeign equipmentFeign;

	/**
	 * 新增设备列表信息
	 * @param dSbxxsjyVOList
	 * @return
	 */
	@Override
	public ResponseWrapper insertList(List<DSbxxsjyVO> dSbxxsjyVOList) {
		if (dSbxxsjyVOList != null && dSbxxsjyVOList.size() > 0 ){
			DSbxxsjyClassifyVO dSbxxsjyClassifyVO = new DSbxxsjyClassifyVO();
			dSbxxsjyClassifyVO.setKddm(dSbxxsjyVOList.get(0).getKddm());
			dSbxxsjyClassifyVO.setSsfl(dSbxxsjyVOList.get(0).getSsfl());
			ResponseWrapper responseWrapper = equipmentFeign.deleteList(dSbxxsjyClassifyVO);
			if (!responseWrapper.isSuccess()){
				return responseWrapper;
			}
			for (DSbxxsjyVO dSbxxsjyVO : dSbxxsjyVOList) {
				if (StringUtils.isEmpty(dSbxxsjyVO.getKddm())){
					ResponseWrapper.error("缺少必填字段，请联系管理员");
				}
				if (StringUtils.isEmpty(dSbxxsjyVO.getSbbh())){
					dSbxxsjyVO.setStatus(1);
					dSbxxsjyVO.setCreator(CurrentUtil.getUserName());
					dSbxxsjyVO.setCreateDate(new Date());
					ResponseWrapper responseWrapperInsert = equipmentFeign.insert(dSbxxsjyVO);
					if (!responseWrapperInsert.isSuccess()) {
						return responseWrapperInsert;
					}
				} else {
					dSbxxsjyVO.setStatus(1);
					dSbxxsjyVO.setRefresher(CurrentUtil.getUserName());
					dSbxxsjyVO.setRefreshTime(new Date());
					ResponseWrapper responseWrapperUpdate = equipmentFeign.equipmentUpdate(dSbxxsjyVO);
					if (!responseWrapperUpdate.isSuccess()) {
						return responseWrapperUpdate;
					}
				}
			}
			return ResponseWrapper.success();
		} else {
			return ResponseWrapper.success();
		}
	}
}
