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

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.railway.business.safetool.mapper.BaseSafetyToolMapper;
import com.railway.business.safetool.domain.BaseSafetyTool;
import com.railway.business.safetool.service.IBaseSafetyToolService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.railway.common.utils.SecurityUtils;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Date;
/**
* 安全工具-基础数据
* @author wuhonghao
* @date 2021/12/01
*/
@Service
@Transactional(readOnly = true)
public class BaseSafetyToolServiceImpl implements IBaseSafetyToolService{
@Autowired
private BaseSafetyToolMapper baseSafetyToolMapper;

	/**
	* 新增
	*/
	@Transactional(rollbackFor = Exception.class)
	public int create(BaseSafetyTool baseSafetyTool) {
	    baseSafetyTool.setCreateTime(new Date());
        baseSafetyTool.setCreateBy(SecurityUtils.getUsername());
		baseSafetyTool.setState("1");
		return baseSafetyToolMapper.insert(baseSafetyTool);
	}

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

	/**
	* 更新
	*/
	@Transactional(rollbackFor = Exception.class)
	public int update(BaseSafetyTool baseSafetyTool) {
		baseSafetyTool.setUpdateTime(new Date());
    	baseSafetyTool.setUpdateBy(SecurityUtils.getUsername());
		return baseSafetyToolMapper.update(baseSafetyTool);
	}

	/**
	* 获取单个
	*/
	public BaseSafetyTool getInfo(String toolId) {
		return baseSafetyToolMapper.getInfo(toolId);
	}

	/**
	* 查询列表
	*/
	public List<BaseSafetyTool> getList(BaseSafetyTool baseSafetyTool) {
		return baseSafetyToolMapper.getList(baseSafetyTool);
	}

	/**
	 * 替换安全工具
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public int replaceTool(BaseSafetyTool baseSafetyTool) {
		Long toolId = baseSafetyTool.getToolId();
		//插入新的安全工具
		baseSafetyTool.setCreateTime(new Date());
		baseSafetyTool.setCreateBy(SecurityUtils.getUsername());
		baseSafetyTool.setState("1");
		baseSafetyToolMapper.insert(baseSafetyTool);

		//将原安全工具状态设置为报废并记录替换ID
		BaseSafetyTool temp = new BaseSafetyTool();
		temp.setToolId(toolId);
		temp.setScrapDate(new Date());
		temp.setState("0");
		temp.setScrapUser(SecurityUtils.getUsername());
		temp.setReplaceToolId(baseSafetyTool.getToolId());

		return baseSafetyToolMapper.update(temp);
	}
}
