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

import com.railway.business.safetool.domain.BaseSafetyTool;
import com.railway.business.safetool.domain.SecScheduled;
import com.railway.business.safetool.domain.SecScheduledTool;
import com.railway.business.safetool.domain.vo.SecScheduledCountVo;
import com.railway.business.safetool.domain.vo.SecScheduledListParmVo;
import com.railway.business.safetool.domain.vo.SecScheduledToolListVo;
import com.railway.business.safetool.domain.vo.SecScheduledToolVo;
import com.railway.business.safetool.enums.TestStateEnum;
import com.railway.business.safetool.mapper.BaseSafetyToolMapper;
import com.railway.business.safetool.mapper.SecScheduledMapper;
import com.railway.business.safetool.mapper.SecScheduledToolMapper;
import com.railway.business.safetool.service.ISecScheduledService;
import com.railway.common.core.domain.entity.SysUser;
import com.railway.common.utils.SecurityUtils;
import com.railway.common.utils.bean.BeanUtils;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 安全工具计划表
 *
 * @author lijie
 * @date 2021/12/04
 */
@Slf4j
@Service
public class SecScheduledServiceImpl implements ISecScheduledService {

  private final SecScheduledMapper secScheduledMapper;
  private final SecScheduledToolMapper secScheduledToolMapper;
  private final BaseSafetyToolMapper safetyToolMapper;

  public SecScheduledServiceImpl(SecScheduledMapper secScheduledMapper,
      SecScheduledToolMapper secScheduledToolMapper,
      BaseSafetyToolMapper safetyToolMapper) {
    this.secScheduledMapper = secScheduledMapper;
    this.secScheduledToolMapper = secScheduledToolMapper;
    this.safetyToolMapper = safetyToolMapper;
  }

  /**
   * 新增
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public Long create(SecScheduledToolListVo scheduledToolVo) throws Exception {
    scheduledToolVo.setCreateTime(new Date());
    scheduledToolVo.setCreateBy(SecurityUtils.getUsername());
    List<SecScheduledToolVo> baseSafetyTools = scheduledToolVo.getBaseSafetyTools();

    if (CollectionUtils.isEmpty(baseSafetyTools)) {
      throw new Exception("安全工具不能为空");
    }
    SecScheduled scheduled = new SecScheduled();
    BeanUtils.copyBeanProp(scheduled, scheduledToolVo);
    scheduled.setState(TestStateEnum.WAITING.getCode());
    scheduled.setDelFlag("0");
    int r = secScheduledMapper.insert(scheduled);
    if (r < 1) {
      throw new Exception("计划插入失败");
    }
    for (SecScheduledToolVo t : baseSafetyTools) {
      // 插入计划-工具映射关系
      SecScheduledTool secScheduledTool = new SecScheduledTool();
      secScheduledTool.setScheduledId(scheduled.getId());
      secScheduledTool.setToolId(t.getToolId());
      r = secScheduledToolMapper.insert(secScheduledTool);
      if (r < 1) {
        throw new Exception("计划关系插入失败");
      }
      // 更新工具下次实验时间
      BaseSafetyTool tool = new BaseSafetyTool();
      tool.setToolId(t.getToolId());
      tool.setNextTestDate(scheduled.getScheduledTime());
      safetyToolMapper.update(tool);
    }
    return scheduled.getId();
  }

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

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

  /**
   * 获取单个
   */
  @Override
  public SecScheduledToolListVo getInfo(Long id) {
    return secScheduledMapper.getDetail(id);
  }

  /**
   * 查询列表
   */
  @Override
  public List<SecScheduledCountVo> getList(SecScheduledListParmVo secScheduled) {
    secScheduled.setDeptId(SecurityUtils.getLoginUser().getDeptId());
    List<SecScheduledCountVo> r = secScheduledMapper.getList(secScheduled);
    if (!CollectionUtils.isEmpty(r) && r.size() == 1) {
      if ("0件".equals(r.get(0).getC())) {
        return new ArrayList<>();
      }
    }
    return r;
  }


  @Override
  @Transactional(rollbackFor = Exception.class)
  public int delTools(SecScheduledToolVo secScheduled) {
    if (secScheduledToolMapper.checkdel(secScheduled) != 1) {
      return secScheduledToolMapper.delete(secScheduled);
    } else {
      return 0;
    }

  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public int addTools(SecScheduledToolVo toolsVo) throws Exception {
    BaseSafetyTool safetyTool = safetyToolMapper.getInfo(toolsVo.getToolId());
    SysUser sysUser = SecurityUtils.getLoginUser().getUser();
    if (!safetyTool.getDeptId().equals(sysUser.getDeptId())) {
      throw new Exception("该工具归属“" + safetyTool.getDeptName() + "”管理，请检查后重新操作");
    }
    // 查询计划详情
    SecScheduled scheduled = secScheduledMapper.getInfo(toolsVo.getScheduledId());
    // 创建计划-工具映射关系对象
    SecScheduledTool secScheduledTool = new SecScheduledTool();
    secScheduledTool.setScheduledId(toolsVo.getScheduledId());
    secScheduledTool.setToolId(toolsVo.getToolId());

    if (secScheduledToolMapper.check(secScheduledTool) == 0) {
      int ret = secScheduledToolMapper.insert(secScheduledTool);
      if(ret > 0){
        // 更新工具下次实验时间
        safetyTool.setNextTestDate(scheduled.getScheduledTime());
        safetyToolMapper.update(safetyTool);
      }
      return ret;
    } else {
      throw new Exception("该工具已经添加到其他计划中，请删除后重新操作");
    }

  }
}
