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

import com.railway.business.baseinfo.domain.BusFjxgmd;
import com.railway.business.baseinfo.domain.BusZzdzxx;
import com.railway.business.baseinfo.domain.vo.BusFjxgmdQueryVo;
import com.railway.business.baseinfo.domain.vo.BusSmartQueryVo;
import com.railway.business.baseinfo.mapper.BusZzdzxxMapper;
import com.railway.common.enums.DelFlagEnum;
import com.railway.business.baseinfo.mapper.BusFjxgmdMapper;
import com.railway.business.baseinfo.service.IBusFjxgmdService;
import com.railway.common.utils.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
/**
 * 附加悬挂锚段
 * @author author
 * @date 2021/10/26
 */
@Service
@Transactional(readOnly = true)
public class BusFjxgmdServiceImpl implements IBusFjxgmdService{

    private final BusFjxgmdMapper busFjxgmdMapper;
    private final BusZzdzxxMapper busZzdzxxMapper;

    public BusFjxgmdServiceImpl(BusFjxgmdMapper busFjxgmdMapper, BusZzdzxxMapper busZzdzxxMapper) {
        this.busFjxgmdMapper = busFjxgmdMapper;
        this.busZzdzxxMapper = busZzdzxxMapper;
    }

    /**
     * 新增
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int create(BusFjxgmd busFjxgmd) {
        busFjxgmd.setCreateTime(new Date());
        busFjxgmd.setCreateBy(SecurityUtils.getUsername());
        busFjxgmd.setDelFlag(DelFlagEnum.NOT_DELETE.getCode());
        return busFjxgmdMapper.insert(busFjxgmd);
    }

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

    /**
     * 更新
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(BusFjxgmd busFjxgmd) {
        busFjxgmd.setUpdateTime(new Date());
        busFjxgmd.setUpdateBy(SecurityUtils.getUsername());
        return busFjxgmdMapper.update(busFjxgmd);
    }

    /**
     * 获取单个
     */
    @Override
    public BusFjxgmd getInfo(String id) {
        return busFjxgmdMapper.getInfo(id);
    }

    /**
     * 查询列表
     */
    @Override
    public List<BusFjxgmd> getList(BusFjxgmd busFjxgmd) {
        return busFjxgmdMapper.getList(busFjxgmd);
    }

    /**
     * 通过支柱信息获取对锚列表
     * @param busFjxgmdQueryVo
     * @return
     */
    @Override
    public List<BusFjxgmd> getListByZz(BusFjxgmdQueryVo busFjxgmdQueryVo) {
        if (StringUtils.isBlank(busFjxgmdQueryVo.getPillarArea())) {
            busFjxgmdQueryVo.setPillarArea("500");
        }
        // 有站场区间，支柱号 查询支柱公里标
        if (StringUtils.isNoneBlank(busFjxgmdQueryVo.getPillarCode())
            && busFjxgmdQueryVo.getStationId() != null) {
            BusSmartQueryVo vo = new BusSmartQueryVo();
            vo.setPillarCode(busFjxgmdQueryVo.getPillarCode());
            vo.setStationId(busFjxgmdQueryVo.getStationId());
            vo.setLineId(busFjxgmdQueryVo.getLineId());
            vo.setXingbie(busFjxgmdQueryVo.getXingbie());
            List<BusZzdzxx> zzList = busZzdzxxMapper.smartQuery(vo);
            if (zzList != null && zzList.size() > 0) {
                BusZzdzxx zz = zzList.get(0);
                busFjxgmdQueryVo.setMarker(zz.getMarker());
            }
        }
        return busFjxgmdMapper.getListByZz(busFjxgmdQueryVo);
    }
}
