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

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.railway.business.safetool.domain.BaseSafetyTool;
import com.railway.business.safetool.domain.SecScheduledTool;
import com.railway.business.safetool.domain.vo.*;
import com.railway.business.safetool.mapper.SecScheduledMapper;
import com.railway.business.safetool.domain.SecScheduled;
import com.railway.business.safetool.mapper.SecScheduledToolMapper;
import com.railway.business.safetool.service.ISecScheduledService;
import com.railway.common.core.page.TableDataInfo;
import com.railway.common.utils.bean.BeanUtils;
import io.swagger.annotations.ApiOperation;
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 org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Date;
/**
* 安全工具计划表
* @author lijie
* @date 2021/12/04
*/
@Service
@Transactional(readOnly = true)
public class SecScheduledServiceImpl implements ISecScheduledService{
@Autowired
private SecScheduledMapper secScheduledMapper;
	@Autowired
	private SecScheduledToolMapper secScheduledToolMapper;
	/**
	* 新增
	*/
	@Override
	@Transactional(rollbackFor = Exception.class)
	public int create(SecScheduledListVo secScheduled) throws Exception {
	    secScheduled.setCreateTime(new Date());
        secScheduled.setCreateBy(SecurityUtils.getUsername());
		List<SecScheduledToolsVo> baseSafetyTools = secScheduled.getBaseSafetyTools() ;

		if (CollectionUtils.isEmpty(baseSafetyTools))
		{
			throw new Exception("安全工具不能为空");
		}
		SecScheduled s = new SecScheduled();
		BeanUtils.copyBeanProp(s,secScheduled);
		int r = secScheduledMapper.insert(s);
		if (r<1){
			throw new Exception("计划插入失败");
		}
		for (SecScheduledToolsVo t:baseSafetyTools) {
			SecScheduledTool secScheduledTool = new SecScheduledTool();
			secScheduledTool.setScheduledId(s.getId());
			secScheduledTool.setToolId(t.getToolId());
			r = secScheduledToolMapper.insert(secScheduledTool);
			if (r<1){
				throw new Exception("计划关系插入失败");
			}
		}
		return  1;
	}

	/**
	* 删除
	*/
	@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 List<SecScheduledDetailVo> getInfo(String id) {
		List<SecScheduledDetailVo> r = secScheduledMapper.getInfo(id);
		r.forEach( vo->
				vo.setC(vo.getBaseSafetyTools().size()+"")
				);
		return r;
	}

	/**
	* 查询列表
	*/
	@Override
	public List<SecScheduledListVo2> getList(SecScheduledListParmVo secScheduled) {
		secScheduled.setDeptId(SecurityUtils.getLoginUser().getDeptId());
		List<SecScheduledListVo2> 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(SecScheduledToolsAddVo secScheduled) throws Exception {

		if (secScheduledToolMapper.checkdel(secScheduled)!=1){
			return secScheduledToolMapper.delete(secScheduled);
		}else {
			return 0;
		}

	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int addTools(SecScheduledToolsAddVo toolsVo) {


		SecScheduledTool secScheduledTool = new SecScheduledTool();
		secScheduledTool.setScheduledId(toolsVo.getScheduledId());
		secScheduledTool.setToolId(toolsVo.getToolId());


		if (secScheduledToolMapper.check(secScheduledTool)==0){
			return secScheduledToolMapper.insert(secScheduledTool);
		}else {
			return 0;
		}

	}
}
