package com.fuzamei.service.serviceImpl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.fuzamei.constant.OperationType;
import com.fuzamei.constant.Roles;
import com.fuzamei.entity.PlannerSupplier;
import com.fuzamei.entity.User;
import com.fuzamei.mapperInterface.PlannerMapper;
import com.fuzamei.mapperInterface.ReceivingClerkMapper;
import com.fuzamei.mapperInterface.UserMapper;
import com.fuzamei.service.PlannerService;
@Service
public class PlannerServiceImpl implements  PlannerService{
  @Autowired
  private PlannerMapper  plannerMapper;
  @Autowired
  private UserMapper userMapper;
	@Override
	public List<User> queryPlanner(Map<String, Object> map) {
		 return plannerMapper.queryPlanner(map);
    }

	@Override
	public List<User> queryAllSupplierByPlannerId(int plannerId) {//根据计划员id查询已经有的供应商
		return plannerMapper.queryAllSupplierByPlannerId(plannerId);
	}

	@Override
	public List<User> queryAllNoSupplierByPlannerId(int plannerId) {////根据计划员id查询未添加的供应商
		return plannerMapper.queryAllNoSupplierByPlannerId(plannerId);
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public void deleteBeforeSupplierByUserId(int plannerId) {//根据计划员id删除之前所有的供应商
	
		plannerMapper.deleteBeforeSupplierByUserId(plannerId);
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public void insertAgainSupplier(PlannerSupplier  pp) {//得到前端传来所有得供应商ID 做插入操作
		String[] str=pp.getSupplierId().split(",");
		for (int i = 0; i < str.length; i++) {
			pp.setSupplierId(str[i]);
			plannerMapper.insertAgainSupplier(pp);
		}
		
		
		
		//下面待改动   XXX
		//下面往【操作记录】表插入数据
		Long currentTime = System.currentTimeMillis();//得到当前系统时间
		User user=new  User();
		String sign = user.getSign();
		@SuppressWarnings("unchecked")
		Map<String,Object> map = JSON.parseObject(sign, Map.class);
		String hash = (String) map.get("sid");
		user.setOperatorId(user.getUserId());//操作人id
		user.setOperationTypeId(OperationType.EDIT);//操作类型 (编辑修改)
		user.setBeOperatedId(user.getUserId());//被操作人账号id  TODO？？？？？？？？？？？？？？待
		user.setOperationTime(currentTime);//操作时间
		user.setHash(hash);//操作hash
		userMapper.insertOperationHistory(user);//插入到操作记录表去
		
		/*boolean flag = blockChainUtil.sendBlockChain(user.getSign());//发送签名直接转发到区块链
		if(!flag) {
			 throw new RuntimeException("区块链操作失败2");	
		}*/

	}

	
	
}
