/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.workbidingevaluation.service;

import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.common.utils.MenuStatusEnum;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.sys.dao.JudgeDao;
import com.jeeplus.modules.sys.entity.Judge;
import com.jeeplus.modules.sys.service.JudgeService;
import com.jeeplus.modules.workbidingevaluation.dao.WorkBidingEvaluationDao;
import com.jeeplus.modules.workbidingevaluation.entity.WorkBidingEvaluation;
import com.jeeplus.modules.workbidingproject.entity.WorkBidingProject;
import com.jeeplus.modules.workbidingproject.service.WorkBidingProjectService;
import com.jeeplus.modules.workprojectdeposit.entity.WorkProjectDeposit;
import com.jeeplus.modules.workprojectdeposit.service.WorkProjectDepositService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 评标管理Service
 * @author fgy
 * @version 2017-12-28
 */
@SuppressWarnings("all")
@Service
@Transactional(readOnly = true)
public class WorkBidingEvaluationService extends CrudService<WorkBidingEvaluationDao, WorkBidingEvaluation> {

	@Autowired
	private JudgeService judgeService;
	@Autowired
	private JudgeDao judgeDao;
	@Autowired
	private WorkBidingEvaluationDao workBidingEvaluationDao;
	@Autowired
	private WorkBidingProjectService workBidingProjectService;
	@Autowired
	private WorkProjectDepositService workProjectDepositService;

	public WorkBidingEvaluation get(String id) {
		WorkBidingEvaluation workBidingEvaluation = super.get(id);
		if(workBidingEvaluation!=null){
			String judgeMembers = workBidingEvaluation.getJudgeMembers();
			List<Judge> judgeMembersList = new ArrayList<>();
			if(StringUtils.isNotBlank(judgeMembers)){
				String[] judgeMembersArr = judgeMembers.split(",");
				if(judgeMembersArr!=null && judgeMembersArr.length>0){
					for (String judgeId : judgeMembersArr){
						if(StringUtils.isNotBlank(judgeId)){
							Judge judge = judgeService.get(judgeId);
							judgeMembersList.add(judge);
						}
					}
				}
			}
			workBidingEvaluation.setJudgeMembersList(judgeMembersList);

			String judgeMembersBak = workBidingEvaluation.getJudgeMembersBak();
			List<Judge> judgeMembersBakList = new ArrayList<>();
			if(StringUtils.isNotBlank(judgeMembersBak)){
				String[] judgeMembersBakArr = judgeMembersBak.split(",");
				if(judgeMembersBakArr!=null && judgeMembersBakArr.length>0){
					for (String judgeId : judgeMembersBakArr){
						if(StringUtils.isNotBlank(judgeId)){
							Judge judge = judgeService.get(judgeId);
							judgeMembersBakList.add(judge);
						}
					}
				}
			}
			workBidingEvaluation.setJudgeMembersBakList(judgeMembersBakList);

			if(StringUtils.isNotBlank(workBidingEvaluation.getJudgeLeader())){
				String judgeId = workBidingEvaluation.getJudgeLeader().replaceAll(",","");
				String judgeName = judgeDao.get(judgeId)==null?"":judgeDao.get(judgeId).getName();
				workBidingEvaluation.setJudgeLeaderName(judgeName);
			}
		}

		if(workBidingEvaluation!=null && StringUtils.isNotBlank(workBidingEvaluation.getWorkBidingProjectId())){
			List<WorkProjectDeposit> workProjectDepositList = workProjectDepositService.getByBidingProjectId(workBidingEvaluation.getWorkBidingProjectId());
			workBidingEvaluation.setWorkProjectDepositList(workProjectDepositList);
		}
		return workBidingEvaluation;
	}
	
	public List<WorkBidingEvaluation> findList(WorkBidingEvaluation workBidingEvaluation) {
		return super.findList(workBidingEvaluation);
	}
	
	public Page<WorkBidingEvaluation> findPage(Page<WorkBidingEvaluation> page, WorkBidingEvaluation workBidingEvaluation) {
		workBidingEvaluation.getSqlMap().put("dsf", dataScopeFilterOR(workBidingEvaluation.getCurrentUser(), "o", "u","s", MenuStatusEnum.END.getValue()));
		workBidingEvaluation.setPage(page);
		page.setList(findWorkBidingProjectList(workBidingEvaluation));
		return page;
	}

	private List<WorkBidingEvaluation> findWorkBidingProjectList(WorkBidingEvaluation workBidingEvaluation) {
		List<WorkBidingEvaluation> bidList =workBidingEvaluationDao.findlistWithWorkBidingProject(workBidingEvaluation);
		if(bidList!=null && bidList.size()>0){
			for (WorkBidingEvaluation w : bidList){
				WorkBidingProject workBidingProject = w.getWorkBidingProject();
				if(workBidingProject!=null && StringUtils.isNotBlank(workBidingProject.getId())){
					workBidingProject = workBidingProjectService.get(workBidingProject.getId());
					w.setWorkBidingProject(workBidingProject);
				}
			}
		}



		return bidList;
	}

	@Transactional(readOnly = false)
	public void save(WorkBidingEvaluation workBidingEvaluation) {
		super.save(workBidingEvaluation);
	}
	
	@Transactional(readOnly = false)
	public void delete(WorkBidingEvaluation workBidingEvaluation) {
		super.delete(workBidingEvaluation);
	}


    public List<Judge> selectJudgeList(String judge, String consultType,String judgeCount) {
		try {
			return  judgeService.selectJudgeMembers(judge,consultType,judgeCount);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return  new ArrayList<>();
	}
	public List<Judge> selectjudgeBakList(String judge, String consultType, String judgeCount,List<Judge>membersList, String workBidingEvaluationId){
		try {
			String judgeMembers = "";
			StringBuilder builder = new StringBuilder(",");
			if(StringUtils.isNotBlank(workBidingEvaluationId)){
                WorkBidingEvaluation workBidingEvaluation = get(workBidingEvaluationId);
                judgeMembers = workBidingEvaluation.getJudgeMembers();
            }else {
                if(membersList!=null && membersList.size()>0){
                    for (int i = 0; i < membersList.size(); i++) {
                        builder.append(membersList.get(i));
                    }
                    judgeMembers = builder.toString();
                }
            }
			return judgeService.selectJudgeMembersBak(judge,consultType,judgeCount,judgeMembers);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ArrayList<>();
	}

    @Transactional(readOnly = false)
    public void saveJudgeMembers(String workBidingEvaluationId, List<Judge> judgeList) {
		if(judgeList!=null && judgeList.size()>0){
			StringBuilder builder = new StringBuilder().append(",");
			int num = 0;
			for (Judge judge : judgeList){
				if(StringUtils.isNotBlank(judge.getId())){
					builder.append(judge.getId()).append(",");
					num++;
				}
			}
			Map<String,Object> map = new HashMap<>();
			map.put("id",workBidingEvaluationId);
			map.put("judgeMembers",builder.toString());
			map.put("judgeCount",num);
			workBidingEvaluationDao.saveJudgeMembers(map);
		}
    }

    @Transactional(readOnly = false)
	public void saveJudgeLeader(String workBidingEvaluationId, String judgeId) {
		if(StringUtils.isBlank(judgeId)){judgeId="";}
		if(StringUtils.isNotBlank(workBidingEvaluationId)){
			Map<String,Object> map = new HashMap<>();
			map.put("id",workBidingEvaluationId);
			map.put("judgeLeader",judgeId);
			workBidingEvaluationDao.saveJudgeLeader(map);

		}
	}

	@Transactional(readOnly = false)
	public void deleteJudgeMember(String workBidingEvaluationId, String judgeId,String judgeType) {
		if(StringUtils.isNotBlank(judgeType)&&judgeType.equals("0")){
			Map<String,Object> map = new HashMap<>();
			StringBuilder builder = new StringBuilder();
			WorkBidingEvaluation workBidingEvaluation = get(workBidingEvaluationId);
			String judgeMembers = workBidingEvaluation.getJudgeMembers();
			if(StringUtils.isNotBlank(judgeMembers)){
				String[] split = judgeMembers.split(",");
				if(split!=null && split.length>0){
					for (String str : split){
						if(str!=null && !str.equals(judgeId)){
							builder.append(str);
						}
					}
				}
			}

			map.put("id",workBidingEvaluationId);
			map.put("judgeMembers",builder.toString());
			workBidingEvaluationDao.saveJudgeMembers(map);

			if(StringUtils.isNotBlank(judgeId)&&judgeId.equals(workBidingEvaluation.getJudgeLeader())){
				map.put("judgeLeader","");
				workBidingEvaluationDao.saveJudgeLeader(map);
			}
		}
		if(StringUtils.isNotBlank(judgeType)&&judgeType.equals("1")){
			Map<String,Object> map = new HashMap<>();
			StringBuilder builder = new StringBuilder();
			WorkBidingEvaluation workBidingEvaluation = get(workBidingEvaluationId);
			String judgeMembersBak = workBidingEvaluation.getJudgeMembersBak();
			if(StringUtils.isNotBlank(judgeMembersBak)){
				String[] split = judgeMembersBak.split(",");
				if(split!=null && split.length>0){
					for (String str : split){
						if(str!=null && !str.equals(judgeId)){
							builder.append(str);
						}
					}
				}
			}
			map.put("id",workBidingEvaluationId);
			map.put("judgeMembersBak",builder.toString());
			workBidingEvaluationDao.saveJudgeMembersBak(map);
		}

	}

	@Transactional(readOnly = false)
	public void saveJudgeMembersBak(String workBidingEvaluationId, List<Judge> judgeList) {
		WorkBidingEvaluation workBidingEvaluation = get(workBidingEvaluationId);
		if(judgeList!=null && judgeList.size()>0){
			StringBuilder builder = new StringBuilder().append(",");
			for (Judge judge : judgeList){
				if(StringUtils.isNotBlank(judge.getId())){
					builder.append(judge.getId()).append(",");
				}
			}
			Map<String,Object> map = new HashMap<>();
			map.put("id",workBidingEvaluationId);
			map.put("judgeMembersBak",builder.toString());
			workBidingEvaluationDao.saveJudgeMembersBak(map);
		}
	}

	@Transactional(readOnly = false)
	public void confirmDeposit(String num, String statusValue, String workProjectDepositId) {
		Map<String,String> map = new HashMap<>();
		map.put("num",num);
		map.put("statusValue",statusValue);
		map.put("workProjectDepositId",workProjectDepositId);
		workBidingEvaluationDao.confirmDeposit(map);
	}

	public String getUserIdsFromList(List sessionList) {
		StringBuilder builder = new StringBuilder("");
		try {
			if(sessionList!=null && sessionList.size()>0){
                for (int i = 0; i < sessionList.size(); i++) {
                    if(sessionList.get(i)!=null){
                        if(sessionList.get(i) instanceof Judge){
                            Judge judge = (Judge) sessionList.get(i);
                            if(StringUtils.isNotBlank(judge.getId())){
                            	builder.append(judge.getId()).append(",");
							}
                        }
                    }
                }
            }
		} catch (Exception e) {
			e.printStackTrace();
		}
		return builder.toString();
	}

	@Transactional(readOnly = false)
	public List modifyJudgeList(List judgeList, String judgeId) {
		try {
			if(StringUtils.isNotBlank(judgeId)){
				Iterator iterator = judgeList.iterator();
				while (iterator.hasNext()){
					Judge j = (Judge) iterator.next();
					if(j.getId().equals(judgeId)){
						iterator.remove();
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return judgeList;
	}

	@Transactional(readOnly = false)
	public WorkBidingEvaluation joinInJudgeMembers(String workBidingEvaluationId, String judgeId) {
		try {
			if(StringUtils.isNotBlank(workBidingEvaluationId)&&StringUtils.isNotBlank(judgeId)){
                WorkBidingEvaluation w = super.get(workBidingEvaluationId);
                String judgeMembers = w.getJudgeMembers();
                String judgeMembersBak = w.getJudgeMembersBak();
                judgeMembersBak = judgeMembersBak.replace(judgeId,"").replace(",,",",");
                judgeMembers = judgeMembers+judgeId;
                Map<String,String> map = new HashMap<>();
                map.put("id",workBidingEvaluationId);
                map.put("judgeMembers",judgeMembers);
                map.put("judgeMembersBak",judgeMembersBak);
                workBidingEvaluationDao.updateMembersAndBak(map);
            }
		} catch (Exception e) {
			e.printStackTrace();
		}
		return get(workBidingEvaluationId);
	}
}