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

import com.unis.base.app.service.CompanyService;
import com.unis.base.client.*;
import com.unis.base.common.constant.FilingStatusConstant;
import com.unis.base.common.vo.*;
import com.unis.common.utils.CurrentUtil;
import com.unis.common.utils.DateUtil;
import com.unis.common.utils.JsonUtils;
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.ArrayList;
import java.util.Date;
import java.util.List;

/** 
 * @ClassName: CompanyServiceImpl
 * @Description: 企业信息 service实现类
 * @author WangZL
 * @date 2019年9月6日 上午11:18:54  
 */
@Service
public class CompanyServiceImpl implements CompanyService {

	@Autowired
	private CompanyFeign companyFeign;

	@Autowired
	private SinkpointFeign sinkpointFeign;

	@Autowired
	private EquipmentFeign equipmentFeign;

	@Autowired
	private WarehouseFeign warehouseFeign;

	@Autowired
	private PersonnelFegin personnelFegin;

	@Autowired
	private CompanyRecordsFeign companyRecordsFeign;

	/**
	 * 新增企业信息
	 * @param companyVO
	 * @return
	 */
	@Override
	public ResponseWrapper insert(CompanyVO companyVO) {
		companyVO.setStatus(1);
		companyVO.setCreator(CurrentUtil.getUserName());
		companyVO.setCreateDate(new Date());
		//新增设备/设施/仪器列表信息
		List<DSbxxsjyVO> dSbxxsjyVOList = companyVO.getDSbxxsjyVOList();
		if (dSbxxsjyVOList != null && dSbxxsjyVOList.size() > 0) {
			for (DSbxxsjyVO dSbxxsjyVO : dSbxxsjyVOList) {
				dSbxxsjyVO.setStatus(1);
				dSbxxsjyVO.setCreator(CurrentUtil.getUserName());
				dSbxxsjyVO.setCreateDate(new Date());
				ResponseWrapper responseWrapper = equipmentFeign.insert(dSbxxsjyVO);
				if (!responseWrapper.isSuccess()){
					return responseWrapper;
				}
			}
		}
		//新增库点列表信息
		List<DKdxxsjyVO> dKdxxsjyVOList  = companyVO.getDKdxxsjyVOList();
		if (dKdxxsjyVOList != null && dKdxxsjyVOList.size() > 0) {
			for (DKdxxsjyVO dKdxxsjyVO : dKdxxsjyVOList) {
				dKdxxsjyVO.setStatus(1);
				dKdxxsjyVO.setCreator(CurrentUtil.getUserName());
				dKdxxsjyVO.setCreateDate(new Date());
				dKdxxsjyVO.setTyshxydm(companyVO.getTyshxydm());
				ResponseWrapper responseWrapper = sinkpointFeign.insert(dKdxxsjyVO);
				if (!responseWrapper.isSuccess()){
					return responseWrapper;
				}
			}
		}
		ResponseWrapper responseWrapper = companyFeign.insert(companyVO);
		return responseWrapper;
	}

	/**
	 * 根据统一社会行政代码获取企业信息
	 * @param  tyshxydm
	 * @return
	 */
	@Override
	public ResponseWrapper getCompanyBytyshxydm(String tyshxydm) {
		ResponseWrapper responseWrapper = companyFeign.getCompanyBytyshxydm(tyshxydm);
		if (responseWrapper.isSuccess()) {
			CompanyVO companyVO = JsonUtils.parse(JsonUtils.serialize(responseWrapper.getData()), CompanyVO.class);
			if (companyVO != null) {
				//获取设备列表信息
				List<DSbxxsjyVO> dSbxxsjyVOList = new ArrayList<>();
				ResponseWrapper responseWrapperEquipment = equipmentFeign.getEquipmentListByKddm(tyshxydm);
				if (responseWrapperEquipment.isSuccess()) {
					List<DSbxxsjyVO> sbxxsjyVOList = (List<DSbxxsjyVO>) JsonUtils.parseList(JsonUtils.serialize(responseWrapperEquipment.getData())
							, DSbxxsjyVO.class);
					if (sbxxsjyVOList != null && sbxxsjyVOList.size() > 0) {
						for (DSbxxsjyVO dSbxxsjyVO : sbxxsjyVOList) {
							dSbxxsjyVOList.add(dSbxxsjyVO);
						}
						companyVO.setDSbxxsjyVOList(dSbxxsjyVOList);
					}
				} else {
					return responseWrapperEquipment;
				}
				//获取库点列表信息
				List<DKdxxsjyVO> dKdxxsjyVOList = new ArrayList<>();
				ResponseWrapper responseWrapperSinkpoint = sinkpointFeign.getDKdxxsjyInfoBytyshxydm(tyshxydm);
				if (responseWrapperSinkpoint.isSuccess()) {
					List<DKdxxsjyVO> kdxxsjyVOList = (List<DKdxxsjyVO>) JsonUtils.parseList(JsonUtils.serialize(responseWrapperSinkpoint.getData())
							, DKdxxsjyVO.class);
					if (kdxxsjyVOList != null && kdxxsjyVOList.size() > 0) {
						for (DKdxxsjyVO dKdxxsjyVO : kdxxsjyVOList) {
							dKdxxsjyVOList.add(dKdxxsjyVO);
						}
						companyVO.setDKdxxsjyVOList(dKdxxsjyVOList);
					}
				} else {
					return responseWrapperSinkpoint;
				}
			}
			return ResponseWrapper.success(companyVO);
		} else {
			return responseWrapper;
		}
	}

	/**
	 * 根据统一社会行政代码删除企业信息
	 * @param tyshxydm
	 * @return
	 */
	@Override
	public ResponseWrapper delete(String tyshxydm) {
		//删除设备列表信息
		DSbxxsjyClassifyVO dSbxxsjyClassifyVO = new DSbxxsjyClassifyVO();
		dSbxxsjyClassifyVO.setKddm(tyshxydm);
		ResponseWrapper responseWrapperEquipment = equipmentFeign.deleteList(dSbxxsjyClassifyVO);
		if (!responseWrapperEquipment.isSuccess()){
			return responseWrapperEquipment;
		}
		//删除库点列表信息
		ResponseWrapper responseWrapperSinkpoint = sinkpointFeign.deleteList(tyshxydm);
		if (!responseWrapperSinkpoint.isSuccess()){
			return responseWrapperSinkpoint;
		}
		ResponseWrapper responseWrapper = companyFeign.delete(tyshxydm);
		return responseWrapper;
	}

	/**
	 * 更新企业信息
	 * @param companyVO
	 * @return
	 */
	@Override
	public ResponseWrapper update(CompanyVO companyVO) {
		companyVO.setRefresher(CurrentUtil.getUserName());
		companyVO.setRefreshTime(new Date());
		//更新设备/设施/仪器列表信息
		List<DSbxxsjyVO> dSbxxsjyVOList = companyVO.getDSbxxsjyVOList();
		if (dSbxxsjyVOList != null && dSbxxsjyVOList.size() > 0) {
			for (DSbxxsjyVO dSbxxsjyVO : dSbxxsjyVOList) {
				if (StringUtils.isEmpty(dSbxxsjyVO.getSbbh())){
					dSbxxsjyVO.setCreator(CurrentUtil.getUserName());
					dSbxxsjyVO.setCreateDate(new Date());
					dSbxxsjyVO.setStatus(1);
					ResponseWrapper responseWrapper = equipmentFeign.insert(dSbxxsjyVO);
					if (!responseWrapper.isSuccess()) {
						return responseWrapper;
					}
				} else {
					dSbxxsjyVO.setRefresher(CurrentUtil.getUserName());
					dSbxxsjyVO.setRefreshTime(new Date());
					ResponseWrapper responseWrapper = equipmentFeign.update(dSbxxsjyVO);
					if (!responseWrapper.isSuccess()) {
						return responseWrapper;
					}
				}
			}
		}
		//更新库点列表信息
		List<DKdxxsjyVO> dKdxxsjyVOList  = companyVO.getDKdxxsjyVOList();
		if (dKdxxsjyVOList != null && dKdxxsjyVOList.size() > 0) {
			for (DKdxxsjyVO dKdxxsjyVO : dKdxxsjyVOList) {
				if (StringUtils.isEmpty(dKdxxsjyVO.getKddm())){
					dKdxxsjyVO.setCreator(CurrentUtil.getUserName());
					dKdxxsjyVO.setCreateDate(new Date());
					dKdxxsjyVO.setStatus(1);
					ResponseWrapper responseWrapper = sinkpointFeign.insert(dKdxxsjyVO);
					if (!responseWrapper.isSuccess()){
						return responseWrapper;
					}
				} else {
					dKdxxsjyVO.setRefresher(CurrentUtil.getUserName());
					dKdxxsjyVO.setRefreshTime(new Date());
					dKdxxsjyVO.setTyshxydm(companyVO.getTyshxydm());
					ResponseWrapper responseWrapper = sinkpointFeign.update(dKdxxsjyVO);
					if (!responseWrapper.isSuccess()){
						return responseWrapper;
					}
				}
			}
		}else {//当前端不传库点信息，查看库里是否存在企业对应库点信息，当为空提醒新增库点信息
            ResponseWrapper responseWrapper = sinkpointFeign.getDKdxxsjyInfoBytyshxydm(companyVO.getTyshxydm());
            if (!responseWrapper.isSuccess()){
                return responseWrapper;
            } else {
                List<DKdxxsjyVO> kdxxsjyVOList = (List<DKdxxsjyVO>) JsonUtils.parseList(JsonUtils.serialize(responseWrapper.getData())
                        , DKdxxsjyVO.class);
                if (kdxxsjyVOList == null){
                    ResponseWrapper.error("此企业无库点，请联系管理员");
                }
            }
        }
		ResponseWrapper responseWrapper = companyFeign.update(companyVO);
		return responseWrapper;
	}

	/**
	 * 企业备案
	 * @param companyRecordsVO
	 * @return
	 */
	@Override
	public ResponseWrapper updateFilingStatus(CompanyRecordsVO companyRecordsVO) {
		String sfba = companyRecordsVO.getSfba().toString();
		if (StringUtils.isEmpty(sfba)){
			return ResponseWrapper.error("缺少备案必须条件");
		}
		//只有已备案、已驳回和待审核状态，新增历史表
		if (FilingStatusConstant.FILING.getValue().equals(sfba)
				|| FilingStatusConstant.REJECTED.getValue().equals(sfba)
				|| FilingStatusConstant.TO_BE_REVIEW.getValue().equals(sfba)) {
			//驳回的审批意见必填
			if (FilingStatusConstant.REJECTED.getValue().equals(sfba)) {
				if (StringUtils.isEmpty(companyRecordsVO.getSpyj()))
					return ResponseWrapper.error("驳回，需填写审批意见");
			}
			//当是已备案状态，人员、设备、仓房表同时更新主表备案状态
			if (FilingStatusConstant.FILING.getValue().equals(sfba)) {
				String tyshxydm = companyRecordsVO.getTyshxydm();
				//判断今年是否备案过，已存在已备案的历史记录无法重复备案
				CompanyRecordsQueryVO companyRecordsQueryVO = new CompanyRecordsQueryVO();
				companyRecordsQueryVO.setSfba(companyRecordsVO.getSfba());
				companyRecordsQueryVO.setTyshxydm(tyshxydm);
				companyRecordsQueryVO.setYear(DateUtil.getYearString().toString());
				ResponseWrapper responseWrapperCompanyRecords = companyRecordsFeign.getCompanyRecordsList(companyRecordsQueryVO);
				if (responseWrapperCompanyRecords.isSuccess()){
					List<CompanyRecordsVO> companyRecordsVOList = (List<CompanyRecordsVO>) JsonUtils.parseList(JsonUtils.serialize(responseWrapperCompanyRecords.getData())
							, CompanyRecordsVO.class);
					if (companyRecordsVOList != null && companyRecordsVOList.size() > 0){
						return ResponseWrapper.error("当前年度已备案过，请勿重复备案");
					}
				}
				/*//设备主表备案
				ResponseWrapper responseWrapperEquipment = equipmentFeign.getEquipmentListByKddm(tyshxydm);
				if (responseWrapperEquipment.isSuccess()) {
					List<DSbxxsjyVO> sbxxsjyVOList = (List<DSbxxsjyVO>) JsonUtils.parseList(JsonUtils.serialize(responseWrapperEquipment.getData())
							, DSbxxsjyVO.class);
					for (DSbxxsjyVO dSbxxsjyVO : sbxxsjyVOList) {
						ResponseWrapper responseWrapper = equipmentFeign.updateFiling(dSbxxsjyVO);
						if (!responseWrapper.isSuccess()){
							return responseWrapper;
						}
					}
				}else {
					return responseWrapperEquipment;
				}
				//仓房主表备案
				ResponseWrapper responseWrapperWarehouse = warehouseFeign.getWarehouseListByKddm(tyshxydm);
				if (responseWrapperWarehouse.isSuccess()) {
					List<DCfxxsjyVO> dCfxxsjyVOList = (List<DCfxxsjyVO>) JsonUtils.parseList(JsonUtils.serialize(responseWrapperWarehouse.getData())
							, DCfxxsjyVO.class);
					for (DCfxxsjyVO dCfxxsjyVO : dCfxxsjyVOList) {
						ResponseWrapper responseWrapper = warehouseFeign.updateFiling(dCfxxsjyVO);
						if (!responseWrapper.isSuccess()){
							return responseWrapper;
						}
					}
				}else {
					return responseWrapperWarehouse;
				}
				//人员主表备案
				ResponseWrapper responseWrapperPersonnel = personnelFegin.getPersonnelListByKddm(tyshxydm);
				if (responseWrapperPersonnel.isSuccess()) {
					List<DRyxxsjyVO> dRyxxsjyVOList = (List<DRyxxsjyVO>) JsonUtils.parseList(JsonUtils.serialize(responseWrapperPersonnel.getData())
							, DRyxxsjyVO.class);
					for (DRyxxsjyVO dRyxxsjyVO : dRyxxsjyVOList) {
						ResponseWrapper responseWrapper = personnelFegin.updateFiling(dRyxxsjyVO);
						if (!responseWrapper.isSuccess()){
							return responseWrapper;
						}
					}
				}else {
					return responseWrapperPersonnel;
				}*/
			}
			ResponseWrapper responseWrapper = companyFeign.updateFilingStatus(companyRecordsVO);
			return responseWrapper;
		}
		return ResponseWrapper.error("备案失败，请联系管理员");
	}
}
