package cn.com.countrygarden.bi.main.controller.marketing.backend;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageRowBounds;

import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.dao.common.CommInFoMapper;
import cn.com.countrygarden.bi.main.dao.common.CommMapper;
import cn.com.countrygarden.bi.main.dao.dup.TbHsprContractMapper;
import cn.com.countrygarden.bi.main.dao.sys.RoleMapper;
import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.model.PageData;
import cn.com.countrygarden.bi.main.model.project.CommInfoVO;
import cn.com.countrygarden.bi.main.model.project.CommVO;
import cn.com.countrygarden.bi.main.model.project.ProjectClass;
import cn.com.countrygarden.bi.main.model.project.ProjectPlanOne;
import cn.com.countrygarden.bi.main.model.sys.Role;
import cn.com.countrygarden.bi.main.service.AccessTokenService;
import cn.com.countrygarden.bi.main.service.common.CommService;
import cn.com.countrygarden.bi.main.service.project.impl.vo.IndicatorLight;
import cn.com.countrygarden.bi.main.util.DateUtil;
@Controller
@RequestMapping("/api/biapp/marketing/backend/commnode")
public class ProjectCommController {
	 	@Autowired
	    private CommMapper commMapper;
	 	@Autowired
	 	CommInFoMapper commInFoMapper;
	 	@Autowired
	    private AccessTokenService accessTokenService;
	    @Autowired
	    private  RoleMapper roleMapper;
	    @Autowired
	    private CommService commService;
	    @Autowired
	    TbHsprContractMapper tbHsprContractMapper;

	 @ResponseBody
	 @GetMapping("/projectnode")
	 public Object getProject(@RequestParam(value = "organId", required = false) String organId,
			 @RequestParam(value = "commCode", required = false) String commCode,
			 @RequestParam(value = "erpOrganCode", required = false) String erpOrganCode,
			 @RequestParam(value = "colour", required = false) String colour,
			 @RequestParam(value = "pageIndex", required = false, defaultValue = "0") int page,
            @RequestParam(value = "pageSize", required = false, defaultValue = "0") int size,
            @RequestParam(value = "asort", required = false) String asort,
            @RequestParam(value = "bsort", required = false) String bsort,
            @RequestParam(value = "csort", required = false) String csort,
            @RequestParam(value = "dsort", required = false) String dsort,
            @RequestParam(value = "ysort", required = false) String ysort,
            @RequestParam(value = "node", required = false) String node,
            HttpServletRequest request) {
		 String accessToken = request.getParameter("AccessToken");
	        AccessToken token = accessTokenService.findByToken(accessToken);
	        String userCode = token.getUserCode();
	        List<Map<String, String>> findOrganByUserCode = tbHsprContractMapper.findOrganByUserCode(userCode);
	        List<Role> rList=  roleMapper.findByUserCode(userCode);
	        for (Role role : rList) {
				if(role.getCode().equals("marketing_manager")) {
					userCode = null;
					break;
				}
			}
	        
		 PageRowBounds pageRowBounds = new PageRowBounds(page,size);
		 Map<String, Object> map = new HashMap<>();
		 map.put("organId", organId);
		 map.put("colour", colour);
		 map.put("erpOrganCode", erpOrganCode);
		 map.put("commCode", commCode);
		 map.put("contractorCode", userCode);
		 map.put("node", node);
		 if(ysort!=null||asort!=null||bsort!=null||csort!=null||dsort!=null) {
			 map.put("ysort", ysort);
			 map.put("asort", asort);
			 map.put("bsort", bsort);
			 map.put("csort", csort);
			 map.put("dsort", dsort);
			 
		 }else {
			 map.put("sort", "1");
		 }
		
		 if(findOrganByUserCode.size()>0) {
		 map.put("areaCode", findOrganByUserCode.get(0).get("OrganCode"));
		 }
		 List<CommVO> project = commMapper.getProject(map, pageRowBounds);

		 List<Map<String, Object>> jointPlan = commMapper.getJointPlan();
		 Map<String, String> jointPlanmap = new HashMap<>();
		 for (Map<String, Object> map2 : jointPlan) {
			 Integer id = (Integer) map2.get("id");
			 Object is_suspicious = map2.get("is_suspicious");
			 jointPlanmap.put(id.toString(), is_suspicious.toString());
		}
		 
		 for (CommVO comm : project) {	
			 comm.setIsLampTimeMenu("false"); 
			 if(userCode==null) {
				 comm.setIsLampTimeMenu("true");
			 }else if(userCode.equals(comm.getRuserCode())) {
				 comm.setIsLampTimeMenu("true"); 
			 }
			 if(!comm.getProjectPlanCode().equals(ProjectPlanOne.CODE_OVER)) {
				 ProjectPlan(comm,jointPlanmap,comm.getProjectPlanCode(),false);
			 }	
			//项目结束
			if(comm.getProjectPlanCode().equals(ProjectPlanOne.CODE_OVER)) {
				String upperProjectPlanCode = comm.getUpperProjectPlanCode();
				ProjectPlan(comm,jointPlanmap,upperProjectPlanCode,true);
				/**String projectClassCode = comm.getProjectClassCode();
				if(ProjectClass.CODE_CLASSBYA.equals(projectClassCode)) {
					if(comm.getProjectClassD()==null) {
						comm.setProjectClassD(CommUtil.GREEN);
					}
					if(comm.getProjectClassC()==null) {
						comm.setProjectClassC(CommUtil.GREEN);
					}
					if(comm.getProjectClassB()==null) {
						comm.setProjectClassB(CommUtil.GREEN);
					}
					comm.setProjectClassA("end");
				}
				if(ProjectClass.CODE_CLASSBYB.equals(projectClassCode)) {
					if(comm.getProjectClassD()==null) {
						comm.setProjectClassD(CommUtil.GREEN);
					}
					if(comm.getProjectClassC()==null) {
						comm.setProjectClassC(CommUtil.GREEN);
					}
					comm.setProjectClassB("end");
				}
				if(ProjectClass.CODE_CLASSBYC.equals(projectClassCode)) {
					if(comm.getProjectClassD()==null) {
						comm.setProjectClassD(CommUtil.GREEN);
					}
					comm.setProjectClassC("end");
				}
				if(ProjectClass.CODE_CLASSBYD.equals(projectClassCode)) {
					comm.setProjectClassD("end");
				}
				//不是结束状态  计算当前节点时间灯
			}else if(!ProjectClass.CODE_CLASSBYO.equals(comm.getProjectClassCode())){
//				   //当前节点时间
//				   Date endDateStr = DateUtil.parseDate(comm.getThisStage());
//				   //下一节点时间
//				   Date beginDateStr = new Date();
//				 //计算  当前节点时间 - 下一节点时间 天数
//				   long day = CommUtil.getDaySub(beginDateStr, endDateStr);
//				   //查询项目时间配置 组装时间配置 优先级 项目==》公共==》默认
//			       CommInfoVO ci = new CommInfoVO();
//			       ci.setCommId(comm.getId()+"");
//			       List<CommInfoVO> select = commInFoMapper.select(ci);
//			       IndicatorLight findLight = commService.findLight();
//				   //组装项目过期时间
//			       Map<String, CommInfoVO> colourDay = CommUtil.colourDay(select,findLight);
				   String stage = comm.getStage();
				   if(stage!=null) {
					   String[] split = stage.split(",");
				   if("A".equals(split[0])) {
					   if(comm.getProjectClassD()==null) {
							comm.setProjectClassD(CommUtil.GREEN);
						}
						if(comm.getProjectClassC()==null) {
							comm.setProjectClassC(CommUtil.GREEN);
						}
						if(comm.getProjectClassB()==null) {
							comm.setProjectClassB(CommUtil.GREEN);
						}
						comm.setProjectClassA(split[1]);
					}
					if("B".equals(split[0])) {
						if(comm.getProjectClassD()==null) {
							comm.setProjectClassD(CommUtil.GREEN);
						}
						if(comm.getProjectClassC()==null) {
							comm.setProjectClassC(CommUtil.GREEN);
						}
						comm.setProjectClassB(split[1]);
					}
					if("C".equals(split[0])) {
						if(comm.getProjectClassD()==null) {
							comm.setProjectClassD(CommUtil.GREEN);
						}
						comm.setProjectClassC(split[1]);
					}
					if("D".equals(split[0])) {
						comm.setProjectClassD(split[1]);
					} 
				   }
				   */
			}
		}
		 PageData pageData = new PageData(project,pageRowBounds);
		 return Response.response(pageData);
	 }
	 public void ProjectPlan(CommVO comm,Map<String, String> jointPlan,String code,boolean isend) {
		 if(code==null) {
			 System.out.println("项目结束，上个业务节点丢失。。。。。。。。。。。。。。。。。。");
			 return;
		 }
		 //关键人
			if(code.equals(ProjectPlanOne.CODE_PROJECTKEY)	
					||code.equals(ProjectPlanOne.CODE_COOPERATION)	
					||code.equals(ProjectPlanOne.CODE_PRICE)	
					||code.equals(ProjectPlanOne.CODE_OWNER)	
					||code.equals(ProjectPlanOne.CODE_SIGN)						
					) {
				if(isend && code.equals(ProjectPlanOne.CODE_PROJECTKEY)) {
					comm.setPlankeyPerson("end");
				}else {
					//对接人权利
					 String jointRight = jointPlan.get(comm.getJointRight()); 
					 if("1".equals(jointRight)) {
					comm.setPlankeyPerson("false");	
					 }else {
						 comm.setPlankeyPerson("true"); 
					 }
				}
			}
			//合同意向
			if(code.equals(ProjectPlanOne.CODE_COOPERATION)
					||code.equals(ProjectPlanOne.CODE_PRICE)	
					||code.equals(ProjectPlanOne.CODE_OWNER)	
					||code.equals(ProjectPlanOne.CODE_SIGN)) {
				if(isend && code.equals(ProjectPlanOne.CODE_COOPERATION)) {
					comm.setPlanCooperation("end");
				}else {
					comm.setPlanCooperation("true");
				}
			}
			
			//价格细节
			if(code.equals(ProjectPlanOne.CODE_PRICE)
					||code.equals(ProjectPlanOne.CODE_OWNER)	
					||code.equals(ProjectPlanOne.CODE_SIGN)) {
				if(isend && code.equals(ProjectPlanOne.CODE_PRICE)) {
					comm.setPlanPriceDetail("end");
				}else {
					//信息费预算
					 String messageFee = jointPlan.get(comm.getMessageFee());
					//物业费定价测算 
					 String propertyFee = jointPlan.get(comm.getPropertyFee());
					if("1".equals(messageFee) && "1".equals(propertyFee)) {
					comm.setPlanPriceDetail("false");
					}else {	
					comm.setPlanPriceDetail("true");
					}
				
				}
			}
			//决策层
			if(code.equals(ProjectPlanOne.CODE_OWNER)
					||code.equals(ProjectPlanOne.CODE_SIGN)) {
				if(isend && code.equals(ProjectPlanOne.CODE_OWNER)&&!ProjectClass.CODE_CLASSBYY.equals(comm.getProjectClassCode())) {
					comm.setPlanDecision("end");
				}else {
					//决策层意见
					 String opinion = jointPlan.get(comm.getOpinion());
					 if("1".equals(opinion)) {
						 comm.setPlanDecision("false");
					 }else {
						 comm.setPlanDecision("true");
					 }
					
				}
			}
			//项目中标率，合规性操盘
			if(code.equals(ProjectPlanOne.CODE_SIGN)) {
				//招标文件
				String callFile = jointPlan.get(comm.getCallFile());
				if("1".equals(callFile)) {
					 comm.setPlanTrader("false");
				 }else {
					 comm.setPlanTrader("true");
				 }
			}
		 }
	
	 @ResponseBody
	 @RequestMapping(value = "/saveproject", produces = "application/json;charset=utf-8",  method = RequestMethod.POST)
	 public Object addCommInFo(@RequestBody List<CommInfoVO> commInfoVO) {
		 for (CommInfoVO commInfoVO2 : commInfoVO) {
			 if(commInfoVO2.getId()==null || commInfoVO2.getId().equals("")) {
			 commInFoMapper.insert(commInfoVO2);
			 }else {
				 commInFoMapper.updateByPrimaryKey(commInfoVO2); 
			 }			 
		}	
		 return Response.response("成功");
	 }

	 @ResponseBody
	 @GetMapping("/findcomminfo")
	 public Object findCommInFo(@RequestParam(value = "commId", required = true) String commId) {
		 CommInfoVO ci = new CommInfoVO();
		 ci.setCommId(commId);
		 List<CommInfoVO> selectOne = commInFoMapper.select(ci);
		 return Response.response(selectOne);
	 }
	 


}
