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

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.*;

import com.unis.base.common.vo.*;
import com.unis.common.utils.FieldUtil;
import com.unis.common.utils.JsonUtils;
import com.unis.common.utils.PoiModelExcelUtil;
import com.unis.common.utils.ResponseWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.unis.base.server.domain.CompanyDomain;
import com.unis.base.server.entity.TzQyjgDO;
import com.unis.base.server.entity.TzSlqyDO;
import com.unis.base.server.service.CompanyService;

import net.bytebuddy.asm.Advice.Return;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author huangxing
 * @version 1.0
 * @Title TzSlqyServiceImpl
 * @ProjectName 企业服务实现层
 * @Description
 * @date 2019/07/22 17点26分
 */
@Service
public class CompanyServiceImpl implements CompanyService {

	@Autowired
	private CompanyDomain companyDomain;

	/**
	 * 获取企业信息列表
	 * @return
	 */
	@Override
	public List<CompanyVO> getCompanyList(CompanyQueryVO companyQueryVO) {
		return companyDomain.getCompanyList(companyQueryVO);
	}

	/**
	 * 分页获取企业信息列表
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@Override
	public List<CompanyVO> getCompanyPage(CompanyQueryVO companyQueryVO, Integer pageNum, Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		PageHelper.orderBy("CAST(ID AS INT) desc");
		return companyDomain.getCompanyList(companyQueryVO);
	}

	/**
	 * 添加企业信息
	 * @return
	 */
	@Override
	public int insert(CompanyVO companyVO) {
		//根据社会统一信用代码查询是否已注册
		return companyDomain.insert(companyVO);
	}

	/**
	 * 更新企业信息
	 * @return
	 */
	@Override
	public int update(CompanyVO companyVO) {
		//根据社会统一信用代码查询是否已存在
		return companyDomain.update(companyVO);
	}

	/**
	 * 根据统一社会信用代码获取企业信息
	 * @return
	 */
	@Override
	public CompanyVO getCompanyBytyshxydm(String tyshxydm) {
		return companyDomain.getCompanyBytyshxydm(tyshxydm);
	}

	/**
	 * 根据统一社会信用代码删除企业信息
	 * @return
	 */
	@Override
	public int delete(String tyshxydm) {
		CompanyVO tzSlqyVO = new CompanyVO();
		tzSlqyVO.setTyshxydm(tyshxydm);
		tzSlqyVO.setStatus(0);
		return companyDomain.update(tzSlqyVO);
	}


	/**
	 * 根据企业机构代码 查找企业信息
	 * @return
	 */
	@Override
	public TzSlqyDO getSlqy(String qyjgdm) {
		return companyDomain.getSlqy(qyjgdm);
	}

	/**
	 * 根据企业机构代码 查找企业信息
	 * @param companyQueryVO
	 * @return
	 */
	public List<TzSlqyDO> selectByEntity(CompanyQueryVO companyQueryVO){
		return companyDomain.selectByEntity(companyQueryVO);
	}

	/**
	 * 根据企业机构代码 查找企业信息
	 * @param companyQueryVO
	 * @return
	 */
	public TzSlqyDO selectById(CompanyQueryVO companyQueryVO){
		return companyDomain.selectById(companyQueryVO);
	}

	/**
	 * 企业机构关联添加
	 * @return
	 */
	@Override
	public int insertQyjg(TzQyjgVO tzQyjgDO) {
		Integer id = companyDomain.getQyjgDm(tzQyjgDO.getTyshxydm());
		if( id != null ) {
			int i = companyDomain.updateQyjg(tzQyjgDO);
			return i;
		}else {
			//该企业不存在，新增一条企业信息
			CompanyVO companyVO = new CompanyVO();
			companyVO.setTyshxydm(tzQyjgDO.getTyshxydm());
			companyVO.setQyjgdm(tzQyjgDO.getQyjgdm());
			companyDomain.insert(companyVO);

		}
		return 0;
	}

	@Override
	public int updateSlqyInfo(CompanyVO tzSlqyTO) {
		return companyDomain.updateSlqyInfo(tzSlqyTO);
	}

	//传入规避条件,返回企业列表
	public ResponseWrapper companyAll(RandomCompanyQuery randomCompanyQuery) {
		return ResponseWrapper.success(companyDomain.companyAll(randomCompanyQuery));
	}

	@Override
	public Set<CompanyVO> randomCompany(RandomCompanyQuery randomCompanyQuery) {
		return companyDomain.randomCompany(randomCompanyQuery);
	}

	@Override
	public Integer updateQyzt(Integer qyzt, Integer id) {
		return companyDomain.updateQyzt(qyzt,id);
	}

	/**
	 * 更改备案状态
	 * @param companyRecordsVO
	 * @return
	 */
	@Override
	public Integer updateFilingStatus(CompanyRecordsVO companyRecordsVO) {
		return companyDomain.updateFilingStatus(companyRecordsVO);
	}

	@Override
	public List<CompanyVO> getCompanyListByLaLo(CompanyQueryVO companyQueryVO, Integer pageNum, Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		return companyDomain.getCompanyListByLaLo(companyQueryVO);
	}

	@Override
	public Integer auditCompany(Integer id, Integer qyzt, String shyj) throws UnsupportedEncodingException {
		return companyDomain.auditCompany(id, qyzt, shyj);
	}

	/**
	 * 分页获取企业信息列表-应急
	 * @param companyQueryVO
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@Override
	public List<CompanyVO> getYjCompanyPage(CompanyQueryVO companyQueryVO, Integer pageNum, Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		PageHelper.orderBy("id desc");
		return companyDomain.getYjCompanyPage(companyQueryVO);
	}

	/**
	 * 获取企业信息列表-应急
	 * @param companyQueryVO
	 * @return
	 */
	@Override
	public List<CompanyVO> getYjCompanyList(CompanyQueryVO companyQueryVO) {
		return companyDomain.getYjCompanyPage(companyQueryVO);
	}

	@Override
	public Integer updatesSfba(Integer id, Integer sfba, String shyj) {
		return companyDomain.updatesSfba(id, sfba, shyj);
	}


	/**
	 * 获取承储库企业(固定)
	 * @return
	 */
	@Override
	public List<CompanyKdVO> getCompanyKdList() {
		return companyDomain.getCompanyKdList();
	}

	/**
	 * 获取承储库企业(固定)
	 * @return
	 */
	@Override
	public CompanyVO findByCompany(String qymc) {
		return companyDomain.findByCompany(qymc);
	}

	@Override
	public List<String> companyType(String qyjgdm) {
		return companyDomain.companyType(qyjgdm);
	}

	@Override
	public ResponseWrapper sgzgBar(SgzgBarVO sgzgBarVO){
		return companyDomain.sgzgBar(sgzgBarVO);
	}

	@Override
	public ResponseWrapper sgzgLine(SgzgLineVO sgzgLineVO){
		return companyDomain.sgzgLine(sgzgLineVO);
	}

	/**
	 * <TZ_SLQY-企业信息表>
	 * 根据条件导出数据
	 * @param companyVO    <企业信息表>对象
	 * @author lipenghe with LAPTOP-DPSC399A
	 */
	@Override
	public List<CompanyVO> exportByEntityResult(CompanyQueryVO companyVO) {
		List<CompanyVO> companyVOList=  companyDomain.getCompanyList(companyVO);;
		return companyVOList;
	}

	/**
	 * <TZ_SLQY-企业信息表>
	 * 根据条件导入数据
	 * @param companyVO    <企业信息表>对象
	 * @author lipenghe with LAPTOP-DPSC399A
	 */
	@Override
	public ResponseWrapper importByEntityResult(CompanyVO companyVO, List<MultipartFile> fileList, ImportConfigVO importConfigVO) throws IOException {
		List<CompanyVO> companyVOList= null;
		//数据库字段集合
		LinkedHashMap<String, String> entityHeadMap= new LinkedHashMap();
		FieldUtil.getPoweredOrderedFields(entityHeadMap, new CompanyVO(), new BaseEntity());

		List<Map<String, Object>> allSheetData= new ArrayList<>();
		ResponseWrapper responseWrapper= PoiModelExcelUtil.resloveExcel(fileList, entityHeadMap, allSheetData);
		if(responseWrapper.isSuccess()){
			companyVOList = JsonUtils.parseList(
					JsonUtils.serialize(allSheetData), CompanyVO.class);
			if(companyVOList!= null && companyVOList.size()> 0){
				int insertNum= 0;
				int updateNum= 0;
				int skipNum= 0;
				int failNum= 0;
				int size= companyVOList.size();
				for(CompanyVO companyVOTemp : companyVOList){
					int num= companyDomain.selectCountById(companyVOTemp);
					if(num== 0){
						int res= companyDomain.insert(companyVOTemp);
						if(res>0){
							insertNum+= 1;
						} else{
							failNum+= 1;
						}
					} else if(num> 0 && importConfigVO!= null && !importConfigVO.isIgnoreExists()){
						int res= companyDomain.update(companyVOTemp);
						if(res>0){
							updateNum+= 1;
						} else{
							failNum+= 1;
						}
					} else{
						skipNum+= 1;
					}
				}
				return ResponseWrapper.success("共"+size+",跳过"+skipNum+",失败"+failNum+",成功新增"+insertNum+",成功更新"+updateNum);
			} else{
				return ResponseWrapper.fail("未读取到有效数据或存在无法解析的属性");
			}
		}else{
			return responseWrapper;
		}
	}

	@Override
	public List<QyxxVO> qyxxList(QyxxVO qyxxVO) {
		return companyDomain.qyxxList(qyxxVO);
	}

	@Override
	public List<QyxxVO> getQyxxListPage(QyxxVO qyxxVO, Integer pageNum, Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize);

		return companyDomain.qyxxList(qyxxVO);
	}

	@Override
	public ResponseWrapper saveCompany(CompanyVO companyVO) {
		try {
			int num= companyDomain.selectCountById(companyVO);
			if(num== 0) {
				companyVO.setSfba(2);
				companyVO.setZzjgdm(companyVO.getTyshxydm());
				int res = companyDomain.insert(companyVO);
			} else {
				int res= companyDomain.update(companyVO);
			}
			return ResponseWrapper.success();
		} catch (Exception e) {
			return ResponseWrapper.fail(e.getMessage());
		}
	}

	@Override
	public int updateByEntitySelective(CompanyVO companyVO) {
		return companyDomain.updateByEntitySelective(companyVO);
	}
}

