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

import com.railway.business.safetool.domain.BaseSafetyTool;
import com.railway.business.safetool.domain.SecExperiment;
import com.railway.business.safetool.domain.SecExperimentTool;
import com.railway.business.safetool.domain.vo.SecExpAddParmVo;
import com.railway.business.safetool.domain.vo.SecExpEndParmVo;
import com.railway.business.safetool.domain.vo.SecExpInfoVo;
import com.railway.business.safetool.domain.vo.SecExpListParmVo;
import com.railway.business.safetool.domain.vo.SecExpListVo;
import com.railway.business.safetool.domain.vo.SecExpNQParmVo;
import com.railway.business.safetool.domain.vo.SecExpToolsVo;
import com.railway.business.safetool.domain.vo.SecScheduledToolVo;
import com.railway.business.safetool.enums.TestStateEnum;
import com.railway.business.safetool.enums.ToolStateEnum;
import com.railway.business.safetool.mapper.BaseSafetyToolMapper;
import com.railway.business.safetool.mapper.SecExperimentMapper;
import com.railway.business.safetool.mapper.SecExperimentToolMapper;
import com.railway.business.safetool.mapper.SecScheduledMapper;
import com.railway.business.safetool.mapper.SecScheduledToolMapper;
import com.railway.business.safetool.service.ISecExperimentService;
import com.railway.common.utils.SecurityUtils;
import java.util.Date;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 送检记录表
 *
 * @author lijie
 * @date 2021/12/25
 */
@Service
public class SecExperimentServiceImpl implements ISecExperimentService {

  private final SecScheduledMapper scheduledMapper;
  private final SecExperimentMapper secExperimentMapper;
  private final SecExperimentToolMapper secExperimentToolMapper;
  private final SecScheduledToolMapper scheduledToolMapper;
  private final BaseSafetyToolMapper toolMapper;

  public SecExperimentServiceImpl(
      SecScheduledMapper scheduledMapper,
      SecExperimentMapper secExperimentMapper,
      SecExperimentToolMapper secExperimentToolMapper,
      SecScheduledToolMapper scheduledToolMapper,
      BaseSafetyToolMapper toolMapper) {
    this.scheduledMapper = scheduledMapper;
    this.secExperimentMapper = secExperimentMapper;
    this.secExperimentToolMapper = secExperimentToolMapper;
    this.scheduledToolMapper = scheduledToolMapper;
    this.toolMapper = toolMapper;
  }

  /**
   * 新增
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public int create(SecExpAddParmVo secExpAddParmVo) {
    secExpAddParmVo.setCreateby(SecurityUtils.getUsername());
    int r = secExperimentMapper.insertExp(secExpAddParmVo);

    if (r > 0) {
      String secid = secExpAddParmVo.getSecid();
      String expid = secExpAddParmVo.getId();
      String createby = SecurityUtils.getUsername();
      secExperimentMapper.insertTool(secid, expid, createby);
    }
    //设置已送检状态
    if (r > 0) {
      r = scheduledMapper.updateScheduled(secExpAddParmVo.getSecid(), TestStateEnum.ALREADY.getCode());
    }
    return r;
  }

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

  @Override
  public int setExperimentState(SecExpEndParmVo param) {
    SecExperiment secExperiment = new SecExperiment();
    BeanUtils.copyProperties(param, secExperiment);
    secExperiment.setState(TestStateEnum.ALREADY.getCode());
    secExperiment.setExpDate(new Date());
    int ret = update(secExperiment);
    if (ret > 0) {
      //设置待送检状态
      ret = secExperimentMapper.updateScheduled(secExperiment.getId(), TestStateEnum.WAITING.getCode());
      // 更新工具最后实验时间
      List<BaseSafetyTool> toolList =  toolMapper.listByExperimentId(secExperiment.getId());
      for(BaseSafetyTool tool : toolList){
        tool.setLastTestDate(secExperiment.getExpDate());
        toolMapper.update(tool);
        SecScheduledToolVo scheduledToolVo = new SecScheduledToolVo();
        scheduledToolVo.setScheduledId(secExperiment.getId());
        scheduledToolVo.setToolId(tool.getToolId());
        scheduledToolMapper.delete(scheduledToolVo);
      }
    }
    return ret;
  }

  /**
   * 设置安全工具
   *
   * @param param 设置参数
   * @return 设置结果
   */
  @Override
  @Transactional(rollbackFor = Exception.class)
  public int setToolTestState(SecExpNQParmVo param) {
    SecExperimentTool experimentTool = secExperimentToolMapper.getInfo(param.getId());
    experimentTool.setIsok(param.getIsok());
    experimentTool.setUpdateTime(new Date());
    experimentTool.setUpdateBy(SecurityUtils.getUsername());
    int ret = secExperimentToolMapper.update(experimentTool);
    // 如果工具不合格，则同时更新安全工具记录状态为报废
    if(ret > 0 && param.getIsok().equals(ToolStateEnum.SCRAP.getCode())){
      BaseSafetyTool tool = toolMapper.getInfo(experimentTool.getToolId());
      tool.setState(ToolStateEnum.SCRAP.getCode());
      toolMapper.update(tool);
    }
    return ret;
  }

  /**
   * 获取单个
   */
  @Override
  public SecExpInfoVo getInfo(String id) {

    SecExpInfoVo r = secExperimentMapper.getInfo(id);
    List<SecExpToolsVo> baseSafetyTools = secExperimentMapper.listSafetyTools(id);
    r.setBaseSafetyTools(baseSafetyTools);
    return r;

  }

  /**
   * 查询列表
   */
  @Override
  public List<SecExpListVo> getList(SecExpListParmVo parm) {
    parm.setDeptId(SecurityUtils.getLoginUser().getDeptId());
    return secExperimentMapper.getList(parm);
  }
}
