package com.unis.base.server.domain;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import com.unis.base.common.util.IdWorkerUtils;
import com.unis.base.common.vo.*;

import com.unis.base.common.vo.echarts.simpleBar.SimpleBar;
import com.unis.base.common.vo.echarts.simpleBar.SimpleBarSeries;
import com.unis.base.common.vo.echarts.stackedline.StackedLine;
import com.unis.base.common.vo.echarts.stackedline.StackedLineSeries;
import com.unis.base.common.vo.echarts.stackedline.StackedLineXAxis;
import com.unis.base.server.dao.*;
import com.unis.base.server.entity.*;
import com.unis.common.utils.*;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author huangxing
 * @version 1.0
 * @Title TzSlqyDomain
 * @ProjectName 持久化
 * @Description
 * @date 2019/07/22 17点26分
 */
@Transactional
@Component
@Log4j2
public class CompanyDomain extends BaseDomain{

	@Autowired
	private TzSlqyDao tzSlqyMapper;

	@Autowired
	private TzQylxDao tzQylxMapper;

	@Autowired
	private TzYjqyDao tzYjqyMapper;

	@Autowired
	private CompanyRecordsDomain companyRecordsDomain;

	@Autowired
	private QybgsqDomain qybgsqDomain;

	@Autowired
	private IdWorkerUtils idWorkerUtils;

	@Autowired
	private FileDao fileMapper;

	@Autowired
	private DKdxxsjyDao dKdxxsjyDao;

	@Autowired
	private DKdxxsjyDomain dKdxxsjyDomain;

	@Autowired
    private TzSlqyUpdateLogDao tzSlqyUpdateLogDao;

	/**
	 * 获取企业信息列表
	 *
	 * @param companyQueryVO
	 * @return
	 */
	public List<CompanyVO> getCompanyList(CompanyQueryVO companyQueryVO) {
		List<CompanyVO> tzSlqyVOList = new ArrayList<>();
		if(companyQueryVO.getQylxbh() != null && companyQueryVO.getQylxbh().length < 1) {
			companyQueryVO.setQylxbh(null);
		}
		String orgid=CurrentUtil.getOrgId();
		//String orgid="LS001014001001";
		List<TzSlqyDO> slqyDOs = tzSlqyMapper.getCompanyList(companyQueryVO);
		for (TzSlqyDO slqyDO : slqyDOs) {
			CompanyVO companyVO = new CompanyVO();
			// 进行企业信息复制
			LsBeanUtils.copyAllProperties(slqyDO, companyVO);
			TzQylxDO qylxDO = new TzQylxDO();
			qylxDO.setQyid(companyVO.getId());
			List<TzQylxVO> tzQylxDOList = new ArrayList<>();
			List<TzQylxDO> tzQylxDOS = tzQylxMapper.select(qylxDO);
			if (tzQylxDOS != null && !tzQylxDOS.isEmpty()) {
				for (TzQylxDO tzQylxDO : tzQylxDOS) {
					TzQylxVO tzQylxVO = new TzQylxVO();
					LsBeanUtils.copyAllProperties(tzQylxDO, tzQylxVO);
					tzQylxDOList.add(tzQylxVO);
				}
			}
			// 属性赋值
			companyVO.setTzQylxVOList(tzQylxDOList);

			// 查询应急企业信息
			TzYjqyDO tzYjqyDO = tzYjqyMapper.getTzSlqyInfoBytQyid(companyVO.getId());
			if (tzYjqyDO != null) {
				TzYjqyVO tzYjqyVO = new TzYjqyVO();
				LsBeanUtils.copyAllProperties(tzYjqyDO, tzYjqyVO);
				companyVO.setTzYjqyVO(tzYjqyVO);
			}
			QybgsqVO qybgsqVO=new QybgsqVO();
			qybgsqVO.setSqqybm(orgid);
			qybgsqVO.setSfty("0");
			//List<QybgsqVO> sqList= qybgsqDomain.selectByEntity(qybgsqVO);
			tzSlqyVOList.add(companyVO);
		}
		return tzSlqyVOList;
	}

	/**
	 * 新增企业信息
	 *
	 * @param companyVO
	 * @return
	 */
	public int insert(CompanyVO companyVO) {
		TzSlqyDO vo = tzSlqyMapper.getCompanyBytyshxydm(companyVO.getTyshxydm());
		if (vo != null) {
			return -1;
		}

		TzSlqyDO tzSlqyDO = new TzSlqyDO();
		LsBeanUtils.copyAllProperties(companyVO, tzSlqyDO);

		// 设置获取到到的最大id
		Integer qyid = tzSlqyMapper.selectMaxId();
		if (qyid == null) {
			qyid = 0;
		}
		String[] qylxs = companyVO.getQylx();

		if(qylxs != null && qylxs.length > 0) {
			List<String> asList = Arrays.asList(qylxs);
			String qylx = asList.stream().collect(Collectors.joining(","));
			tzSlqyDO.setQylx(qylx);
		}

		tzSlqyDO.setId(qyid + 1);
		tzSlqyDO.setStatus(1);
		String qybm = getMaxCodeById(tzSlqyMapper.getMaxById("120000"),"120000",3);
        tzSlqyDO.setQybm(qybm);
		int x = tzSlqyMapper.insert(tzSlqyDO);
		if (x <= 0) {
			return x;
		}
		// 添加应急企业信息
		TzYjqyVO tzYjqyVO = companyVO.getTzYjqyVO();

		TzYjqyDO tzYjqyDO = new TzYjqyDO();
		if (tzYjqyVO != null) {
			LsBeanUtils.copyAllProperties(tzYjqyVO, tzYjqyDO);
		}
		// 获取应急企业id
		Integer id = tzYjqyMapper.selectMaxId();
		if (id == null) {
			id = 0;
		}
		tzYjqyDO.setQyid(qyid + 1);
		tzYjqyDO.setId(id + 1);
		tzYjqyMapper.insertTzYjqy(tzYjqyDO);
		// 添加企业类型信息
		List<TzQylxVO> tzQylxVOList = companyVO.getTzQylxVOList();
		if (tzQylxVOList != null && tzQylxVOList.size() > 0) {
			for (TzQylxVO record : tzQylxVOList) {
				TzQylxDO qylxDO = new TzQylxDO();
				LsBeanUtils.copyAllProperties(record, qylxDO);
				Integer lxid = tzQylxMapper.selectMaxId();
				if (lxid == null) {
					lxid = 0;
				}
				qylxDO.setQyid(qyid + 1);
				qylxDO.setId(lxid + 1);
				qylxDO.setStatus(1);
				tzQylxMapper.insert(qylxDO);
			}
		}
		// 添加库点信息
		DKdxxsjyVO dKdxxsjyVO = new DKdxxsjyVO();
		dKdxxsjyVO.setTyshxydm(companyVO.getTyshxydm());
		dKdxxsjyVO.setKdmc(companyVO.getQymc());
		dKdxxsjyVO.setQymc(companyVO.getQymc());
		dKdxxsjyVO.setSyzt(String.valueOf(companyVO.getQyzt()));
		dKdxxsjyDomain.insert(dKdxxsjyVO);
		return x;
	}

	/**
	 * 更新企业信息
	 *
	 * @param tzSlqyVO
	 * @return
	 */
	public int update(CompanyVO tzSlqyVO) {

		  TzSlqyDO vo = tzSlqyMapper.getCompanyBytyshxydm(tzSlqyVO.getTyshxydm());
		  if(vo==null) {
			  return -1;
		  }

		  TzSlqyDO tzSlqyDO=new TzSlqyDO();

		//更新企业信息
		LsBeanUtils.copyAllProperties(tzSlqyVO,tzSlqyDO);

		return tzSlqyMapper.update(tzSlqyDO);
	}

	//原方法    CompanyVO  qylx  是 数组
	public int update(CompanyNewVO tzSlqyVO) {

		  TzSlqyDO vo = tzSlqyMapper.getCompanyBytyshxydm(tzSlqyVO.getTyshxydm());
		  if(vo==null) {
			  return -1;
		  }

		  TzSlqyDO tzSlqyDO=new TzSlqyDO();

		//更新企业信息
		LsBeanUtils.copyAllProperties(tzSlqyVO,tzSlqyDO);

		return tzSlqyMapper.update(tzSlqyDO);
	}

	/**
	 * 根据统一社会信用代码获取企业信息
	 *
	 * @param tyshxydm
	 * @return
	 */
	public CompanyVO getCompanyBytyshxydm(String tyshxydm) {
		TzSlqyDO tzSlqyDO = tzSlqyMapper.getCompanyBytyshxydm(tyshxydm);

		if (tzSlqyDO == null) {
			return null;
		}
		String qylx = tzSlqyDO.getQylx();

		// 查询企业信息
		CompanyVO temp = new CompanyVO();
		LsBeanUtils.copyAllProperties(tzSlqyDO, temp);

		if (qylx != null) {
			String[] split = qylx.split(",");
			temp.setQylx(split);
		}
		// 查询企业类型
		TzQylxDO qylxDO = new TzQylxDO();
		qylxDO.setQyid(temp.getId());
		List<TzQylxVO> tzQylxDOList = new ArrayList<TzQylxVO>();
		for (TzQylxDO tzQylxDO : tzQylxMapper.select(qylxDO)) {
			TzQylxVO tzQylxVO = new TzQylxVO();
			LsBeanUtils.copyAllProperties(tzQylxDO, tzQylxVO);
			tzQylxDOList.add(tzQylxVO);
		}
		// 属性赋值
		temp.setTzQylxVOList(tzQylxDOList);
		// 查询应急企业信息
		TzYjqyDO tzYjqyDO = tzYjqyMapper.getTzSlqyInfoBytQyid(temp.getId());
		if (tzYjqyDO != null) {
			TzYjqyVO tzYjqyVO = new TzYjqyVO();
			LsBeanUtils.copyAllProperties(tzYjqyDO, tzYjqyVO);
			temp.setTzYjqyVO(tzYjqyVO);

		}

		// 库点信息
		List<DKdxxsjyVO> dKdxxsjyVOList = dKdxxsjyDao.getDKdxxsjyInfo(tyshxydm);
		if(!dKdxxsjyVOList.isEmpty()) {
			temp.setKqdm(dKdxxsjyVOList.get(0).getKqdm());
		}
        List<TzSlqyUpdateLogVO> updateLogList = tzSlqyUpdateLogDao.select(TzSlqyUpdateLogVO.builder().tyshxydm(temp.getTyshxydm()).status(1).build());
        temp.setUpdateLogList(updateLogList);

        return temp;
	}

	/**
	 * 获取企业信息
	 *
	 * @param qyjgdm
	 * @return
	 */
	public TzSlqyDO getSlqy(String qyjgdm) {
		return tzSlqyMapper.getSlqy(qyjgdm);
	}

	public List<TzSlqyDO> selectByEntity(CompanyQueryVO companyQueryVO){
		return tzSlqyMapper.selectByEntity(companyQueryVO);
	}

	/**
	 * <TZ_SLQY-企业信息表>根据实体的某些属性查询列表
	 * @param companyVO    <企业信息表>对象
	 * @author lipenghe with LAPTOP-DPSC399A
	 */
	public List<CompanyVO> selectByEntity(CompanyVO companyVO) {
		return tzSlqyMapper.selectByEntity(companyVO);
	}

	public TzSlqyDO selectById(CompanyQueryVO companyQueryVO){
		return tzSlqyMapper.selectById(companyQueryVO);
	}

	/**
	 * 判断是否存在 tyshxydm qyjgdm
	 *
	 * @param tyshxydm
	 */
	public Integer getQyjgDm(String tyshxydm) {
		return tzSlqyMapper.getQyjgDm(tyshxydm);
	}

	/**
	 * 新增企业结构信息
	 *
	 * @param tzQyjgDO
	 * @return
	 */
	public int insertQyjg(TzQyjgVO tzQyjgDO) {
		TzQyjgDO tzQDo = new TzQyjgDO();
		LsBeanUtils.copyAllProperties(tzQyjgDO, tzQDo);
		// 设置获取到到的最大id
		Integer id = tzSlqyMapper.selectMaxIdFormQyjg();
		if (id == null) {
			id = 0;
		}
		tzQyjgDO.setId(id + 1);
		int i = tzSlqyMapper.insertQyjg(tzQDo);
		return i;
	}

	public List<TzQyjgVO> selectQyjg(TzQyjgDO tzQyjgDO){
		return tzSlqyMapper.selectQyjg(tzQyjgDO);
	}

	public List<DKdxxsjyVO> selectKdxx(DKdxxsjyDO dKdxxsjyDO){
		return tzSlqyMapper.selectKdxx(dKdxxsjyDO);
	}

	public int updateSlqyInfo(CompanyVO tzSlqyTO) {
		TzSlqyDO slqyDO = tzSlqyMapper.getCompanyBytyshxydm(tzSlqyTO.getTyshxydm());
		if (slqyDO == null) {
			return -1;
		}
		TzSlqyDO newData = new TzSlqyDO();

		String[] qylxs = tzSlqyTO.getQylx();

		if(qylxs != null && qylxs.length > 0) {
			List<String> asList = Arrays.asList(qylxs);
			String qylx = asList.stream().collect(Collectors.joining(","));
			newData.setQylx(qylx);
		}

		// 更新企业信息
		LsBeanUtils.copyAllProperties(tzSlqyTO, newData);
        String refresher = newData.getRefresher();
        // 旧数据
        CompanyQueryVO queryParam = new CompanyQueryVO();
        String tyshxydm = newData.getTyshxydm();
        queryParam.setTyshxydm(tyshxydm);
        TzSlqyDO currentData = tzSlqyMapper.selectById(queryParam);
        Integer dataVer = currentData.getDataVer();
        Integer sfba = newData.getSfba();
        List<Integer> updateOperate = Arrays.asList(1, 2);
        List<TzSlqyUpdateLogVO> currentVerList = tzSlqyUpdateLogDao.listCurrentVerData(tyshxydm, dataVer);
        if (updateOperate.contains(sfba)) {
            if (currentVerList.size() == 0) {
                List<TzSlqyUpdateLogVO> updateDataList = currentData.checkInfo(newData);
                for (TzSlqyUpdateLogVO updateLog : updateDataList) {
                    updateLog.setDataVer(dataVer);
                    updateLog.setCreator(refresher);
                    updateLog.setRefresher(refresher);
                    tzSlqyUpdateLogDao.insertSelective(updateLog);
                }
            }else {
                if (sfba == 2) {
                    int updateCount = tzSlqyUpdateLogDao.updateRefresher(tyshxydm, dataVer, refresher);
                    log.info("{}  更新人 {}", refresher, updateCount);
                }
            }
        }else if(5 == sfba) {
            for (TzSlqyUpdateLogVO currentVerOldData : currentVerList) {
                if (TzSlqyUpdateLogVO.COL.QYMC.equName(currentVerOldData.getUpdateCol())) {
                    if (!currentVerOldData.getBeforeData().equals(newData.getQymc())) {
                        int r = tzSlqyUpdateLogDao.updateStatusByVer(tyshxydm, dataVer, TzSlqyUpdateLogVO.COL.QYMC.getName(), newData.getQymc());
                    }
                }else if (TzSlqyUpdateLogVO.COL.QYXZ.equName(currentVerOldData.getUpdateCol())) {
                    if (!currentVerOldData.getBeforeData().equals(newData.getQyxz())) {
                        int r = tzSlqyUpdateLogDao.updateStatusByVer(tyshxydm, dataVer, TzSlqyUpdateLogVO.COL.QYXZ.getName(), newData.getQyxz());
                    }
                }else if (TzSlqyUpdateLogVO.COL.FDDBR.equName(currentVerOldData.getUpdateCol())) {
                    if (!currentVerOldData.getBeforeData().equals(newData.getFddbr())) {
                        int r = tzSlqyUpdateLogDao.updateStatusByVer(tyshxydm, dataVer, TzSlqyUpdateLogVO.COL.FDDBR.getName(), newData.getFddbr());
                    }
                }
            }
            newData.setDataVer(dataVer + 1);
        }

        int x = tzSlqyMapper.update(newData);
		if (x <= 0) {
			return x;
		}
		// 更新应急企业信息

		TzYjqyVO tzYjqyVO = tzSlqyTO.getTzYjqyVO();

		TzYjqyDO tzYjqyDO = new TzYjqyDO();
		if (tzYjqyVO != null) {
			LsBeanUtils.copyAllProperties(tzYjqyVO, tzYjqyDO);
		}
		Integer yjid = tzYjqyDO.getId();

		tzYjqyDO.setQyid(tzYjqyDO.getQyid());

		tzYjqyDO.setStatus(1);

		if(yjid==null) {
			// 获取应急企业id
			Integer id = Optional.ofNullable(tzYjqyMapper.selectMaxId()).orElse(0);
            tzYjqyDO.setId(id);
			tzYjqyMapper.insertTzYjqy(tzYjqyDO); //新增应急企业
		}else {
			tzYjqyMapper.update(tzYjqyDO);// 更新应急企业信息
		}

		List<TzQylxVO> tzQylxVOList = tzSlqyTO.getTzQylxVOList();
		if (tzQylxVOList != null && tzQylxVOList.size() > 0) {
			// 删除企业类型信息
			TzQylxDO deleDO = new TzQylxDO();
			deleDO.setStatus(0);
			deleDO.setQyid(slqyDO.getId());
			tzQylxMapper.delete(deleDO);
			for (TzQylxVO record : tzQylxVOList) {
				TzQylxDO qylxDO = new TzQylxDO();
				LsBeanUtils.copyAllProperties(record, qylxDO);
				Integer id = tzQylxMapper.selectMaxId();
				if (id == null) {
					id = 0;
				}
				qylxDO.setId(id + 1);
				qylxDO.setQyid(slqyDO.getId());
				qylxDO.setStatus(1);
				tzQylxMapper.insert(qylxDO);
			}
		}
		DKdxxsjyDO dKdxxsjyDO = new DKdxxsjyDO();
		dKdxxsjyDO.setKddm(tzSlqyTO.getTyshxydm()+"001");
		dKdxxsjyDO.setKdmc(tzSlqyTO.getQymc());
		dKdxxsjyDO.setQymc(tzSlqyTO.getQymc());
		dKdxxsjyDO.setSyzt(String.valueOf(tzSlqyTO.getQyzt()));
		dKdxxsjyDao.update(dKdxxsjyDO);
		return x;
	}

	//传入规避条件,返回企业列表
	public List<CompanyVO> companyAll(RandomCompanyQuery randomCompanyQuery) {
		// 获取符合查询条件的企业列表
		List<TzSlqyDO> slqyDOs = tzSlqyMapper.randomCompany(randomCompanyQuery);
		List<CompanyVO> list=new ArrayList<>();
		LsBeanUtils.copyCollection(slqyDOs, list,CompanyVO.class);
		return list;
	}

	public Set<CompanyVO> randomCompany(RandomCompanyQuery randomCompanyQuery) {
		// 获取符合查询条件的企业列表
		List<TzSlqyDO> slqyDOs = tzSlqyMapper.randomCompany(randomCompanyQuery);

		// 创建返回企业信息
		List<CompanyVO> companyVOs = new ArrayList<>();
		for (TzSlqyDO slqyDO : slqyDOs) {// 进行类型转换
			CompanyVO companyVO = new CompanyVO();
			LsBeanUtils.copyAllProperties(slqyDO, companyVO);
			companyVOs.add(companyVO);
		}

		/*for (TzSlqyDO slqyDO : slqyDOs) {// 进行类型转换
			CompanyVO companyVO = new CompanyVO();
			boolean isFh = true;
			if (randomCompanyQuery.getBjqyxzfs() == 1) {// 组合方式需要进行循环处理筛选符合条件的企业
				TzQylxDO qylxDO = new TzQylxDO();
				qylxDO.setQyid(slqyDO.getId());
				qylxDO.setStatus(1);
				// 查询企业类型列表
				List<TzQylxDO> tzQylxDOList = tzQylxMapper.select(qylxDO);
				String qylxInfo = ",";// 进行企业类型拼接
				for (TzQylxDO tzQylxDO : tzQylxDOList) {
					qylxInfo += tzQylxDO.getQylxbh() + ",";
				}
				// 进行遍历查看企业类型是否符合
				if (randomCompanyQuery.getQylxList() != null && randomCompanyQuery.getQylxList().length > 0) {
					for (String qylx : randomCompanyQuery.getQylxList()) {
						if (!qylxInfo.contains(qylx)) {// 不符合直接退出循环，并删除该对象
							isFh = false;
							continue;
						}
					}
				}

			} // 随机出符合条件的企业列表
			if (isFh) {
				LsBeanUtils.copyAllProperties(slqyDO, companyVO);
				companyVOs.add(companyVO);
			}

		}*/

		Set<CompanyVO> set = new HashSet<>();
		if(randomCompanyQuery.getQysl()!=null){
			// 进行企业的随机数选取
			if (randomCompanyQuery.getQysl() > companyVOs.size()) {
				for (CompanyVO companyVO : companyVOs) {
					set.add(companyVO);
				}
				return set;
			}
			while (true) {// 进行循环获取用户信息
				if (set.size() == randomCompanyQuery.getQysl())
					break;// 获取到企业数量结束循环
				int x = (int) (Math.random() * companyVOs.size());// 获取随机数
				set.add(companyVOs.get(x));
			}
		}else{
			set.addAll(companyVOs);
		}
		return set;
	}

	/**
	 * 更新企业状态
	 *
	 * @param qyzt
	 * @param id
	 */
	public Integer updateQyzt(Integer qyzt, Integer id) {
		Map<String, Object> selectMap = new HashMap<>();
		selectMap.put("id", id);
		selectMap.put("qyzt", qyzt);
		return tzSlqyMapper.updateQyzt(selectMap);
	}

	public int updateQyjg(TzQyjgVO tzQyjgVO) {
		return tzSlqyMapper.updateQyjg(tzQyjgVO);
	}

	/**
	 * 更改备案状态
	 *
	 * @param companyRecordsVO
	 * @return
	 */
	public Integer updateFilingStatus(CompanyRecordsVO companyRecordsVO) {
		CompanyVO companyVO = new CompanyVO();
		companyVO.setId(companyRecordsVO.getId());
		companyVO.setSfba(companyRecordsVO.getSfba());
		return tzSlqyMapper.updateFilingStatus(companyVO);
	}

	public List<CompanyVO> getCompanyListByLaLo(CompanyQueryVO companyQueryVO) {
		// 进行传惨复制
		List<CompanyVO> tzSlqyVOList = new ArrayList<>();
		// 获取企业信息
		List<TzSlqyDO> slqyDOs = tzSlqyMapper.getCompanyListByLaLo(companyQueryVO);
		for (TzSlqyDO slqyDO : slqyDOs) {
			CompanyVO companyVO = new CompanyVO();
			// 进行企业信息复制
			LsBeanUtils.copyAllProperties(slqyDO, companyVO);
			TzQylxDO qylxDO = new TzQylxDO();
			qylxDO.setQyid(companyVO.getId());
			List<TzQylxVO> tzQylxDOList = new ArrayList<>();
			List<TzQylxDO> tzQylxDOS = tzQylxMapper.select(qylxDO);
			if (tzQylxDOS != null && !tzQylxDOS.isEmpty()) {
				for (TzQylxDO tzQylxDO : tzQylxDOS) {
					TzQylxVO tzQylxVO = new TzQylxVO();
					LsBeanUtils.copyAllProperties(tzQylxDO, tzQylxVO);
					tzQylxDOList.add(tzQylxVO);
				}
			}
			// 属性赋值
			companyVO.setTzQylxVOList(tzQylxDOList);

			// 查询应急企业信息
			TzYjqyDO tzYjqyDO = tzYjqyMapper.getTzSlqyInfoBytQyid(companyVO.getId());
			if (tzYjqyDO != null) {
				TzYjqyVO tzYjqyVO = new TzYjqyVO();
				LsBeanUtils.copyAllProperties(tzYjqyDO, tzYjqyVO);
				companyVO.setTzYjqyVO(tzYjqyVO);

			}
			tzSlqyVOList.add(companyVO);
		}

		return tzSlqyVOList;
	}

	public Integer auditCompany(Integer id, Integer qyzt, String shyj) throws UnsupportedEncodingException {

		TzSlqyDO tzSlqyDO = new TzSlqyDO();

		String decode = URLDecoder.decode(shyj, "utf-8");

		tzSlqyDO.setId(id);

		tzSlqyDO.setQyzt(qyzt);

		tzSlqyDO.setBz(decode);

		tzSlqyDO.setRefreshTime(new Date());

		// tzSlqyDO.setRefresherCode(CurrentUtil.getUserId());

		return tzSlqyMapper.updateById(tzSlqyDO);

	}

//	public static void main(String[] args) {
//		System.err.println("150000".substring(0,4));
//	}

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

		//组织机构代码 目前 根据代码长度 判断省级LS001 盟市LS001001 旗县LS001001001
		String zzjgdm = companyQueryVO.getYjOrgCode();
		String xzqhdm = companyQueryVO.getYjAreaCode();
		//用户行政区划 150000 省级 模糊15% 盟市1500% 旗县 150000%		qyszqybh
		if(zzjgdm.length() == 5) {
			xzqhdm = xzqhdm.substring(0,2);
		}else if(zzjgdm.length() == 8) {
			xzqhdm = xzqhdm.substring(0,4);
		}
		companyQueryVO.setYjAreaCode(xzqhdm);
		List<CompanyVO> tzSlqyVOList = new ArrayList<>();
		if(companyQueryVO.getQylxbh() != null && companyQueryVO.getQylxbh().length < 1) {
			companyQueryVO.setQylxbh(null);
		}

		List<TzSlqyDO> slqyDOs = tzSlqyMapper.getYjCompanyList(companyQueryVO);
		for (TzSlqyDO slqyDO : slqyDOs) {
			CompanyVO companyVO = new CompanyVO();
			// 进行企业信息复制
			LsBeanUtils.copyAllProperties(slqyDO, companyVO);
			TzQylxDO qylxDO = new TzQylxDO();
			qylxDO.setQyid(companyVO.getId());
			List<TzQylxVO> tzQylxDOList = new ArrayList<>();
			List<TzQylxDO> tzQylxDOS = tzQylxMapper.select(qylxDO);
			if (tzQylxDOS != null && !tzQylxDOS.isEmpty()) {
				for (TzQylxDO tzQylxDO : tzQylxDOS) {
					TzQylxVO tzQylxVO = new TzQylxVO();
					LsBeanUtils.copyAllProperties(tzQylxDO, tzQylxVO);
					tzQylxDOList.add(tzQylxVO);
				}
			}
			// 属性赋值
			companyVO.setTzQylxVOList(tzQylxDOList);
			// 查询应急企业信息
			TzYjqyDO tzYjqyDO = tzYjqyMapper.getTzSlqyInfoBytQyid(companyVO.getId());
			if (tzYjqyDO != null) {
				TzYjqyVO tzYjqyVO = new TzYjqyVO();
				LsBeanUtils.copyAllProperties(tzYjqyDO, tzYjqyVO);
				companyVO.setTzYjqyVO(tzYjqyVO);

			}
			tzSlqyVOList.add(companyVO);
		}

		return tzSlqyVOList;
	}

	public Integer updatesSfba(Integer id, Integer sfba, String shyj) {
		TzSlqyDO tzSlqyDO = new TzSlqyDO();

		tzSlqyDO.setId(id);

		tzSlqyDO.setShzt(sfba);

		if(shyj!=null) {
			tzSlqyDO.setShyj(shyj);
		}
        if(sfba.equals(1)){
        	tzSlqyDO.setQyzt(1);
			tzSlqyDO.setSfba(1);
		}
		tzSlqyDO.setRefreshTime(new Date());

		// tzSlqyDO.setRefresherCode(CurrentUtil.getUserId());

		return tzSlqyMapper.updateCompanySfba(tzSlqyDO);
	}

    public List<CompanyKdVO> getCompanyKdList() {
		return  tzSlqyMapper.getCompanyKdList();
    }

	public CompanyVO findByCompany(String qymc) {
		return tzSlqyMapper.getCompanyByQymc(qymc);
	}

	public List<String> companyType(String qyjgdm) {
		List<TzQylxDO> list = tzSlqyMapper.companyType(qyjgdm);
		String[] qylx = new String[11];
		qylx[0] = "13";
		qylx[1] = "14";
		qylx[2] = "15";
		qylx[3] = "16";
		qylx[4] = "25";
		qylx[5] = "17";
		qylx[6] = "18";
		qylx[7] = "19";
		qylx[8] = "21";
		qylx[9] = "22";
		qylx[10] = "23";
		HashSet<String> set = new HashSet<>();
		for(TzQylxDO tzQylxDO:list){
			if(tzQylxDO!=null){
				if("10".equals(tzQylxDO.getQylxbh())){
					set.add("0");
				}
				if("26".equals(tzQylxDO.getQylxbh())){
					set.add("1");
				}
				if(Arrays.asList(qylx).contains(tzQylxDO.getQylxbh())){
					set.add("2");
				}
			}
		}
		List<String> result = new ArrayList<>(set);
		return result;
	}

	public List<DKdxxsjyVO> selectKdxxLgd(DKdxxsjyDO dKdxxsjyDO) {
		return tzSlqyMapper.selectKdxxLgd(dKdxxsjyDO);
	}
	public List<DKdxxsjyVO> selectKdxxLgdForQc(DKdxxsjyDO dKdxxsjyDO) {
		return tzSlqyMapper.selectKdxxLgdForQc(dKdxxsjyDO);
	}

	public ResponseWrapper sgzgBar(SgzgBarVO sgzgBarVO){
		sgzgBarVO.setSfjysgzg(1);
		sgzgBarVO.setStatus(1);

		List<String> qyxzList =new ArrayList<>();
		qyxzList.add("国有及国有控股企业");
		qyxzList.add("私营企业");
		qyxzList.add("港澳台商及外商企业");
		qyxzList.add("个体工商户");
		sgzgBarVO.setQyxzList(qyxzList);

		List<SimpeCompanyBarVO> companyList = tzSlqyMapper.sgzgBar(sgzgBarVO);
		Map<String,SimpeCompanyBarVO> map=companyList.stream().collect(
				Collectors.toMap(k->k.getQyxz(), a ->a,(k1,k2)->k1));

		//柱状图
		SimpleBar simpleBar=new SimpleBar();
		List<String> xList = simpleBar.getXAxis().getData();
		xList.addAll(qyxzList);
		xList.add("总计");

		SimpleBarSeries simpleBarSeries=new SimpleBarSeries();
		simpleBar.getSeries().add(simpleBarSeries);
		List<Double> dataList= simpleBarSeries.getData();
		simpleBarSeries.setData(dataList);
		int total=0;
		for(String str : qyxzList){
			SimpeCompanyBarVO simpeCompanyBarVO= map.get(str);
			if(simpeCompanyBarVO!=null){
				dataList.add(Double.valueOf(simpeCompanyBarVO.getNum()));
				total+=simpeCompanyBarVO.getNum();
			}else{
				dataList.add(0d);
			}
		}
		dataList.add(Double.valueOf(total));
		return ResponseWrapper.success(simpleBar);
	}

	public ResponseWrapper sgzgLine(SgzgLineVO sgzgLineVO){
		sgzgLineVO.setSfjysgzg(1);
		sgzgLineVO.setStatus(1);

		List<String> qyxzList =new ArrayList<>();
		qyxzList.add("国有及国有控股企业");
		qyxzList.add("私营企业");
		qyxzList.add("港澳台商及外商企业");
		qyxzList.add("个体工商户");
		sgzgLineVO.setQyxzList(qyxzList);

		Date  startTime =sgzgLineVO.getStartTime();
		Date  endTime =sgzgLineVO.getEndTime();


		StackedLine stackedLine=new StackedLine();
		List<String> titleList = stackedLine.getLegend().getData();
		titleList.addAll(qyxzList);
		titleList.add("总计");

		StackedLineXAxis lineXAxis=stackedLine.getXAxis();
		List<String> xList= lineXAxis.getData();
		List<StackedLineSeries> seriesList= stackedLine.getSeries();
		for(String xz : qyxzList){
			StackedLineSeries stackedLineSeries=new StackedLineSeries();
			stackedLineSeries.setName(xz);
			seriesList.add(stackedLineSeries);
		}
		StackedLineSeries stackedLineSeries=new StackedLineSeries();
		stackedLineSeries.setName("总计");
		seriesList.add(stackedLineSeries);

		Calendar nowCa=Calendar.getInstance();

		boolean year=true;
		SimpleDateFormat sdf=null;
		if(sgzgLineVO.getMonthStart()!=null && sgzgLineVO.getMonthEnd()!=null){
			sdf= new SimpleDateFormat("yyyy年MM月");
			year=false;
		} else{
			sdf= new SimpleDateFormat("yyyy年");
		}
		SimpleDateFormat sdfYmd=new SimpleDateFormat("yyyyMMdd");;

		int i=0;
		Date yxDate=startTime;
		Calendar calendar=Calendar.getInstance();
		while(yxDate.compareTo(endTime)<0 && i<2 ){
			xList.add(sdf.format(yxDate));

			sgzgLineVO.setYxDateNum(Integer.valueOf(sdfYmd.format(yxDate)));
			List<CompanyLineVO > companyList = tzSlqyMapper.sgzgLine(sgzgLineVO);
			Map<String, Integer> map=new HashMap<>();
			for(CompanyLineVO companyLineVO : companyList){
				String qyxz= companyLineVO.getQyxz();
				if(map.containsKey(qyxz)){
					map.put(qyxz, map.get(qyxz) + 1);
				} else {
					map.put(qyxz, 1);
				}
			}
			map.put("总计", companyList.size());
			for(StackedLineSeries lineSeries : seriesList){
				String name=lineSeries.getName();
				Integer num=0;
				if(name!=null){
					num= map.get(name);
					if(num==null){
						num=0;
					}
				}
				lineSeries.getData().add(num);
			}

			calendar.setTime(yxDate);
			if(year){
				calendar.add(Calendar.YEAR, 1);
				yxDate= calendar.getTime();

				if(nowCa.get(Calendar.YEAR)==calendar.get(Calendar.YEAR)){
					yxDate= new Date();
					i++;
				}
			}else{
				calendar.set(Calendar.DAY_OF_MONTH, 1);
				calendar.add(Calendar.MONTH, 1);
				calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
				yxDate=calendar.getTime();
				if(nowCa.get(Calendar.YEAR)==calendar.get(Calendar.YEAR)
						&& nowCa.get(Calendar.MONTH)==calendar.get(Calendar.MONTH)){
					yxDate= new Date();
					i++;
				}
			}
		}
		//折线图
		return ResponseWrapper.success(stackedLine);
	}

	/**
	 * <TZ_SLQY-企业信息表>根据实体的唯一属性查询数量
	 * @param companyVO    <企业信息表>对象
	 * @author lipenghe with LAPTOP-DPSC399A
	 */
	public int selectCountById(CompanyVO companyVO) {
		int num= tzSlqyMapper.selectCountById(companyVO);
		return num;
	}

	/**
	 * <TZ_SLQY-企业信息表>根据实体不为空的属性,选择性更新字段
	 * @param companyVO    <企业信息表>对象
	 * @author lipenghe with LAPTOP-DPSC399A
	 */
	public int updateByEntitySelective(CompanyVO companyVO) {
		TzSlqyDO entity= JsonUtils.parse(JsonUtils.serialize(companyVO), TzSlqyDO.class);
		return tzSlqyMapper.updateByEntitySelective(entity);
	}

	public List<QyxxVO> qyxxList(QyxxVO qyxxVO) {
		List<QyxxVO> list = tzQylxMapper.qyxxList(qyxxVO);
		return  list;
	}
}
