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.BusSmartQueryVo;
import com.railway.business.baseinfo.mapper.BusFjxgmdMapper;
import com.railway.business.baseinfo.mapper.BusZzdzxxMapper;
import com.railway.business.baseinfo.service.IBusFjxgmdService;
import com.railway.common.enums.DelFlagEnum;
import com.railway.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
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 busSmartQueryVo
     * @return
     */
    @Override
    public List<BusFjxgmd> getListByZz(BusSmartQueryVo busSmartQueryVo) {
        if (busSmartQueryVo.getPillarArea() == null) {
            busSmartQueryVo.setPillarArea("500");
        }
        List<BusZzdzxx> zzList = busZzdzxxMapper.smartQuery(busSmartQueryVo);
        if (zzList != null && zzList.size() == 1) {
            return busFjxgmdMapper.getListByZz(busSmartQueryVo);
        }
        else {
            return new ArrayList<BusFjxgmd>();
        }
    }
}
