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

import com.railway.business.baseinfo.domain.BaseDeptStation;
import com.railway.business.baseinfo.domain.BaseStation;
import com.railway.business.baseinfo.domain.vo.BaseStationQuery;
import com.railway.business.baseinfo.domain.vo.BaseStationVo;
import com.railway.business.baseinfo.mapper.BaseDeptStationMapper;
import com.railway.business.baseinfo.mapper.BaseStationMapper;
import com.railway.business.baseinfo.service.IBaseStationService;
import com.railway.common.enums.DelFlagEnum;
import com.railway.common.utils.SecurityUtils;
import com.railway.common.utils.bean.BeanUtils;
import java.util.Date;
import java.util.List;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * primary key (station_id) using
 *
 * @author author
 * @date 2021/10/16
 */
@Service
@Transactional(readOnly = true)
public class BaseStationServiceImpl implements IBaseStationService {

  private final BaseStationMapper baseStationMapper;
  private final BaseDeptStationMapper deptStationMapper;

  public BaseStationServiceImpl(BaseStationMapper baseStationMapper,
      BaseDeptStationMapper deptStationMapper) {
    this.baseStationMapper = baseStationMapper;
    this.deptStationMapper = deptStationMapper;
  }

  /**
   * 新增
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public int create(BaseStationVo baseStationVo) {
    BaseStation baseStation = new BaseStation();
    BeanUtils.copyBeanProp(baseStation, baseStationVo);
    baseStation.setCreateTime(new Date());
    baseStation.setCreateBy(SecurityUtils.getUsername());
    int rows = baseStationMapper.insert(baseStation);
    saveDeptStation(baseStationVo.getDeptStations(), baseStation.getStationId());
    return rows;
  }

  /**
   * 删除
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public int delete(Long[] stationIds) {
    int r = 0;
    for (Long stationId : stationIds) {
      deptStationMapper.delete(stationId);
      int j = baseStationMapper.delete(stationId);
      r = r + j;
    }
    return r;
  }

  /**
   * 更新
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public int update(BaseStationVo baseStationVo) {
    BaseStation baseStation = new BaseStation();
    BeanUtils.copyBeanProp(baseStation, baseStationVo);
    baseStation.setUpdateTime(new Date());
    baseStation.setUpdateBy(SecurityUtils.getUsername());
    int rows = baseStationMapper.update(baseStation);
    deptStationMapper.delete(baseStation.getStationId());
    saveDeptStation(baseStationVo.getDeptStations(), baseStation.getStationId());
    return rows;
  }

  private void saveDeptStation(List<BaseDeptStation> deptStations, Long stationId){
    if(CollectionUtils.isNotEmpty(deptStations)){
      for(BaseDeptStation baseDeptStation : deptStations){
        baseDeptStation.setDelFlag(DelFlagEnum.NOT_DELETE.getCode());
        baseDeptStation.setStationId(stationId);
        baseDeptStation.setCreateTime(new Date());
        baseDeptStation.setCreateBy(SecurityUtils.getUsername());
        deptStationMapper.insert(baseDeptStation);
      }
    }
  }

  /**
   * 获取单个
   */
  @Override
  public BaseStationVo getInfo(Long stationId) {
    BaseStationVo station = baseStationMapper.getInfo(stationId);
    if(null == station){
      return null;
    }
    setBaseStation(station);
    return station;
  }

  /**
   * 获取区间/站场与车间、线路关联ID
   *
   * @param deptId 车间ID
   * @param lineId 线路ID
   * @param stationId 区间ID
   * @return 关联记录ID
   */
  @Override
  public BaseDeptStation getInfo(Long deptId, Long lineId, Long stationId) {
    if(null == deptId){
      deptId = SecurityUtils.getDeptId();
    }
    BaseStationQuery query = new BaseStationQuery();
    query.setDeptId(deptId);
    query.setLineId(lineId);
    query.setStationId(stationId);
    return deptStationMapper.getInfo(query);
  }

  /**
   * 查询列表
   */
  @Override
  public List<BaseStationVo> getDetailList(BaseStationQuery query) {
    List<BaseStationVo> stations = baseStationMapper.getList(query);
    for(BaseStationVo station : stations){
      setBaseStation(station);
    }
    return stations;
  }

  private void setBaseStation(BaseStationVo station){
    List<BaseDeptStation> deptStations = deptStationMapper.getList(station.getStationId());
    station.setDeptStations(deptStations);
  }

  /**
   * 查询列表
   */
  @Override
  public List<BaseStation> getListByDeptId(BaseStationQuery query) {
    return baseStationMapper.getStationList(query);
  }

}
