package com.fuzamei.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.fuzamei.constant.HintMSG;
import com.fuzamei.constant.Role;
import com.fuzamei.constant.Roles;
import com.fuzamei.entity.PlannerSupplier;
import com.fuzamei.entity.User;
import com.fuzamei.service.PlannerService;
import com.fuzamei.service.UserAuthoricationService;
import com.fuzamei.util.JSONUtil;
import com.fuzamei.util.ValidationUtil;

@RestController
@RequestMapping(path="/planner")
public class PlannerAction {
	@Autowired
	private PlannerService plannerService;
	
	@Autowired
	private UserAuthoricationService userAuthoricationService;
	@Autowired
	private HttpServletRequest req;
	
	/**
	 * 
	* @Title: queryPlanner
	* @Description: TODO(查询计划员用户信息列表)
	{
		"user_id":"xxx",
		"username":""
	}
	* @param @return    设定文件
	* @return Map<String,Object>    返回类型
	* @author ylx
	* @date 2018年1月29日 下午8:22:50
	* @throws
	 */
	@PostMapping(value="queryPlanner")
	public Map<String, Object> queryPlanner(@RequestBody String data){
		try {
			@SuppressWarnings("unchecked")
			Map<String, Object> map = JSON.parseObject(data,Map.class);
			//Integer user_id = ValidationUtil.checkAndAssignInt(map.get("user_id"));
			//String tokenId= (String) map.get("tokenId");
			Integer user_id = ValidationUtil.checkAndAssignInt(req.getHeader("Authorization").split("&")[1]);
			userAuthoricationService.queryUserAuthority(user_id, Roles.ADMIN);
			String username=ValidationUtil.checkBlankStringAndAssignEmptyIfIsBlank(map.get("username"));
			Map<String, Object> mapToService = new LinkedHashMap<String, Object>();
			mapToService.put("user_id",user_id );
			mapToService.put("username", username);
			List<User> list=plannerService.queryPlanner(mapToService);
			return JSONUtil.getJsonMap(200, true, HintMSG.QUERY_SUCCESS, list);
		} catch (Exception e) {
			return JSONUtil.getJsonMap(500, false, HintMSG.QUERY_FAIL+":"+e.getMessage(), null);
		}
		
	 }
	/**
	   {
	   	"user_id":"xxx",
       	"plannerId":"1002"
       }
	 * 配置供应商
	 * 通过计划员id  查询已经添加的供应商 和  未添加的供应商
	 * @param data
	 * @return
	 */
	@PostMapping(value="queryYesOrNoSupplierByPlannerId")
	public Map<String, Object> queryYesOrNoSupplierByPlannerId(@RequestBody String data){
		try {
			List<User> SupplierList=null;
			List<User> SupplierList2=null;
			@SuppressWarnings("unchecked")
			Map<String, Object> map = JSON.parseObject(data,Map.class);
			//Integer user_id = ValidationUtil.checkAndAssignInt(map.get("user_id"));
			//String tokenId= (String) map.get("tokenId");
			Integer user_id = ValidationUtil.checkAndAssignInt(req.getHeader("Authorization").split("&")[1]);
			userAuthoricationService.queryUserAuthority(user_id, Roles.ADMIN);
			int plannerId = ValidationUtil.checkAndAssignInt(map.get("plannerId"));// 管理员根据传计划员id  来配置供应商
			userAuthoricationService.queryUserAuthority(plannerId);//？？？？？？由计划员来配置？？---TODO
		    SupplierList=plannerService.queryAllSupplierByPlannerId(plannerId);
		    SupplierList2=plannerService.queryAllNoSupplierByPlannerId(plannerId);
		    Map<String, Object>  twolist =new HashMap<String, Object>();
		    twolist.put("SupplierList", SupplierList);
		    twolist.put("SupplierList2", SupplierList2);
		    return JSONUtil.getJsonMap(200, true, HintMSG.QUERY_SUCCESS, twolist);
		} catch (Exception e) {
			return JSONUtil.getJsonMap(500, false, HintMSG.QUERY_FAIL+":"+e.getMessage(), null);
		}
	
	}
	/**
	 * 配置供应商后 点击提交完  再次插入供应商到中间表
	 * @param data
	 * @return
	 */
	@PostMapping(value="inserAginSupplierList")
	public Map<String, Object> inserAginSupplierList(@RequestBody PlannerSupplier  pp){
		try {
			@SuppressWarnings("unchecked")
			Integer user_id = ValidationUtil.checkAndAssignInt(req.getHeader("Authorization").split("&")[1]);
			userAuthoricationService.queryUserAuthority(user_id, Roles.ADMIN);
			Integer plannerId = ValidationUtil.checkAndAssignInt(pp.getPlannerId());//得到前端传来的计划员id（校验）
			ValidationUtil.checkBlankAndAssignString(pp.getSign());//校验前端传来的签名
		    if(pp.getSupplierId().equals("")) {//如果传过来得到的供应商是空的 （点击配置后，什么操作都没做） 就报异常    不让删除原先得供应商
			 throw  new  RuntimeException("不能操作");
			   
		    }else {//否则就做先删除原先得  在添加新得到id做插入操作
		    	plannerService.deleteBeforeSupplierByUserId(plannerId);//(前端传来的计划员id)根据计划员id先删除之前所对应的供应商
		    	plannerService.insertAgainSupplier(pp);
		    }
			 
			return  JSONUtil.getJsonMap(200, true, HintMSG.OPERATION_SUCCESS,pp);//待
		} catch (Exception e) {
			e.printStackTrace();
			return JSONUtil.getJsonMap(500, false, HintMSG.OPERATION_FAIL+":"+e.getMessage(), null);
		}
		
	}
	
	
}
