package com.railway.business.catenary.service.impl;

import com.railway.business.baseinfo.domain.BusZzdzxx;
import com.railway.business.baseinfo.domain.vo.BusSmartQueryVo;
import com.railway.business.baseinfo.mapper.BusZzdzxxMapper;
import com.railway.business.catenary.domain.BusJlgz;
import com.railway.business.catenary.domain.BusJlgzPic;
import com.railway.business.catenary.domain.vo.BusJlgzListByZzVo;
import com.railway.business.catenary.domain.vo.BusJlgzQueryVo;
import com.railway.common.enums.DelFlagEnum;
import com.railway.business.catenary.mapper.BusJlgzMapper;
import com.railway.business.catenary.mapper.BusJlgzPicMapper;
import com.railway.business.catenary.service.IBusJlgzService;
import com.railway.common.utils.SecurityUtils;
import com.railway.common.utils.StringUtils;
import com.railway.common.utils.sql.SqlUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
/**
* 接触网-记录故障
* @author author
* @date 2021/10/16
*/
@Service
@Transactional(readOnly = true)
public class BusJlgzServiceImpl implements IBusJlgzService {

	private final BusJlgzMapper busJlgzMapper;
	private final BusJlgzPicMapper busJlgzPicMapper;
	private final BusZzdzxxMapper busZzdzxxMapper;

	public BusJlgzServiceImpl(BusJlgzMapper busJlgzMapper, BusJlgzPicMapper busJlgzPicMapper, BusZzdzxxMapper busZzdzxxMapper) {
		this.busJlgzMapper = busJlgzMapper;
		this.busJlgzPicMapper = busJlgzPicMapper;
		this.busZzdzxxMapper = busZzdzxxMapper;
	}

	/**
	* 新增
	*/
	@Override
	@Transactional(rollbackFor = Exception.class)
	public int create(BusJlgz busJlgz) {
	    busJlgz.setCreateTime(new Date());
        busJlgz.setCreateBy(SecurityUtils.getUsername());
		busJlgz.setDelFlag(DelFlagEnum.NOT_DELETE.getCode());
		busJlgzMapper.insert(busJlgz);
		Long jlgzId = busJlgz.getId();

		List<BusJlgzPic> pics = busJlgz.getPics();
		if (CollectionUtils.isNotEmpty(pics)) {
			for (BusJlgzPic pic : pics) {
				pic.setJlgzId(jlgzId);
				busJlgzPicMapper.insert(pic);
			}
		}
		return 1;
	}

	/**
	* 删除
	*/
	@Override
	@Transactional(rollbackFor = Exception.class)
	public int delete(Long[] ids) {
		int r =0;
		for (Long id : ids) {
			int j= busJlgzMapper.delete(id);
			busJlgzPicMapper.deleteByJlgzId(id);
			r = r + j;
		}
		return	r;
	}

	/**
	* 更新
	*/
	@Override
	@Transactional(rollbackFor = Exception.class)
	public int update(BusJlgz busJlgz) {
		busJlgz.setUpdateTime(new Date());
    	busJlgz.setUpdateBy(SecurityUtils.getUsername());
		Long jlgzId = busJlgz.getId();

		List<BusJlgzPic> pics = busJlgz.getPics();
		if (CollectionUtils.isNotEmpty(pics)) {
			busJlgzPicMapper.deleteByJlgzId(jlgzId);
			for (BusJlgzPic pic : pics) {
				pic.setJlgzId(jlgzId);
				busJlgzPicMapper.insert(pic);
			}
		}
		return busJlgzMapper.update(busJlgz);
	}

	/**
	* 获取单个
	*/
	@Override
	public BusJlgz getInfo(Long id, Long lineId) {
		BusJlgz busJlgz = busJlgzMapper.getInfo(id, lineId);
		if (busJlgz != null) {
			BusJlgzPic busJlgzPic = new BusJlgzPic();
			busJlgzPic.setJlgzId(id);
			List<BusJlgzPic> pics = busJlgzPicMapper.getList(busJlgzPic);
			busJlgz.setPics(pics);
		}
		return busJlgz;
	}

	/**
	* 查询列表
	*/
	@Override
	public List<BusJlgz> getList(BusJlgz busJlgz) {
		if(StringUtils.isNotEmpty(busJlgz.getOrderBy())){
			busJlgz.setOrderBy(SqlUtil.humpToLine(busJlgz.getOrderBy()));
		} else {
			// 默认倒序排列
			busJlgz.setOrderBy("operation_date");
			busJlgz.setIsAsc("0");
		}
		if (busJlgz.getPillarArea() == null) {
			busJlgz.setPillarArea("500");
		}
		// 有站场区间，支柱号 查询支柱公里标
		if (StringUtils.isNoneBlank(busJlgz.getPillarCode())
				&& busJlgz.getStationId() != null) {
			BusSmartQueryVo vo = new BusSmartQueryVo();
			vo.setPillarCode(busJlgz.getPillarCode());
			vo.setStationId(busJlgz.getStationId());
			vo.setLineId(busJlgz.getLineId());
			vo.setXingbie(busJlgz.getXingbie());
			List<BusZzdzxx> zzList = busZzdzxxMapper.smartQuery(vo);
			if (zzList != null && zzList.size() > 0) {
				BusZzdzxx zz = zzList.get(0);
				busJlgz.setMarker(zz.getMarker());
			}
		}
		return busJlgzMapper.getList(busJlgz);
	}

	@Override
	public List<BusJlgzListByZzVo> getListByZz(BusJlgzQueryVo busJlgzQueryVo) {
		if (busJlgzQueryVo.getPillarArea() == null) {
			busJlgzQueryVo.setPillarArea("500");
		}
		// 有站场区间，支柱号 查询支柱公里标
		if (StringUtils.isNoneBlank(busJlgzQueryVo.getPillarCode())
				&& busJlgzQueryVo.getStationId() != null) {
			BusSmartQueryVo vo = new BusSmartQueryVo();
			vo.setPillarCode(busJlgzQueryVo.getPillarCode());
			vo.setStationId(busJlgzQueryVo.getStationId());
			vo.setLineId(busJlgzQueryVo.getLineId());
			vo.setXingbie(busJlgzQueryVo.getXingbie());
			List<BusZzdzxx> zzList = busZzdzxxMapper.smartQuery(vo);
			if (zzList != null && zzList.size() > 0) {
				BusZzdzxx zz = zzList.get(0);
				busJlgzQueryVo.setMarker(zz.getMarker());
			}
		}
		return busJlgzMapper.getListByZz(busJlgzQueryVo);
	}
}
