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

import com.railway.business.baseinfo.domain.BaseDeptLine;
import com.railway.business.baseinfo.domain.BaseLine;
import com.railway.business.baseinfo.domain.vo.BaseLineQuery;
import com.railway.business.baseinfo.domain.vo.BaseLineVo;
import com.railway.business.baseinfo.mapper.BaseDeptLineMapper;
import com.railway.business.baseinfo.mapper.BaseLineMapper;
import com.railway.business.baseinfo.service.IBaseLineService;
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 (line_id) using
 *
 * @author author
 * @date 2021/10/16
 */
@Service
@Transactional(readOnly = true)
public class BaseLineServiceImpl implements IBaseLineService {

  private final BaseLineMapper baseLineMapper;
  private final BaseDeptLineMapper deptLineMapper;

  public BaseLineServiceImpl(BaseLineMapper baseLineMapper, BaseDeptLineMapper deptLineMapper) {
    this.baseLineMapper = baseLineMapper;
    this.deptLineMapper = deptLineMapper;
  }

  /**
   *  新增
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public int create(BaseLineVo baseLineVo) {
    BaseLine baseLine = new BaseLine();
    BeanUtils.copyBeanProp(baseLine, baseLineVo);
    baseLine.setCreateTime(new Date());
    baseLine.setCreateBy(SecurityUtils.getUsername());
    int rows = baseLineMapper.insert(baseLine);
    saveDeptLine(baseLineVo.getDeptLines(), baseLine.getLineId());
    return rows;
  }

  /**
   *  删除
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public int delete(Long[] lineIds) {
    int r = 0;
    for (Long lineId : lineIds) {
      deptLineMapper.delete(lineId);
      int j = baseLineMapper.delete(lineId);
      r = r + j;
    }
    return r;
  }

  /**
   *  更新
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public int update(BaseLineVo baseLineVo) {
    BaseLine baseLine = new BaseLine();
    BeanUtils.copyBeanProp(baseLine, baseLineVo);
    baseLine.setUpdateTime(new Date());
    baseLine.setUpdateBy(SecurityUtils.getUsername());
    int rows = baseLineMapper.update(baseLine);
    deptLineMapper.delete(baseLine.getLineId());
    saveDeptLine(baseLineVo.getDeptLines(), baseLine.getLineId());
    return rows;
  }

  private void saveDeptLine(List<BaseDeptLine> deptLines, Long lineId){
    if(CollectionUtils.isNotEmpty(deptLines)){
      for(BaseDeptLine baseDeptLine : deptLines){
        baseDeptLine.setDeptId(baseDeptLine.getDeptId());
        baseDeptLine.setDelFlag(DelFlagEnum.NOT_DELETE.getCode());
        baseDeptLine.setLineId(lineId);
        baseDeptLine.setCreateTime(new Date());
        baseDeptLine.setCreateBy(SecurityUtils.getUsername());
        deptLineMapper.insert(baseDeptLine);
      }
    }
  }

  /**
   *  获取单个
   */
  @Override
  public BaseLineVo getInfo(Long lineId) {
    BaseLineVo line = baseLineMapper.getInfo(lineId);
    if(null == line){
      return null;
    }
    setBaseLine(line);
    return line;
  }

  /**
   *  查询列表
   */
  @Override
  public List<BaseLineVo> getDetailList(BaseLineQuery query) {
    List<BaseLineVo> lines = baseLineMapper.getList(query);
    for(BaseLineVo line : lines){
      setBaseLine(line);
    }
    return lines;
  }

  private void setBaseLine(BaseLineVo line){
    List<BaseDeptLine> deptLines = deptLineMapper.getList(line.getLineId());
    line.setDeptLines(deptLines);
  }

  /**
   *  查询列表
   */
  @Override
  public List<BaseLine> getListByDeptId(Long deptId) {
    return baseLineMapper.getListByDeptId(deptId);
  }

  /**
   * 查询列表
   */
  @Override
  public List<BaseDeptLine> getDeptByLineId(Long lineId) {
    return deptLineMapper.getDeptByLineId(lineId);
  }

}
