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

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jeeplus.common.mapper.JsonMapper;
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.AreaDao;
import com.jeeplus.modules.sys.entity.Area;
import com.jeeplus.modules.sys.entity.Office;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.service.AreaService;
import com.jeeplus.modules.sys.service.OfficeService;
import com.jeeplus.modules.sys.utils.DictUtils;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.workbidingproject.dao.WorkBidingProjectDao;
import com.jeeplus.modules.workbidingproject.entity.WorkBidingProject;
import com.jeeplus.modules.workclientinfo.entity.WorkClientInfo;
import com.jeeplus.modules.workclientinfo.entity.WorkClientLinkman;
import com.jeeplus.modules.workclientinfo.service.WorkClientInfoService;
import com.jeeplus.modules.workcontractinfo.service.WorkContractInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 招标项目Service
 * @author 杨帆
 * @version 2017-12-25
 */
@Service
@Transactional(readOnly = true)
public class WorkBidingProjectService extends CrudService<WorkBidingProjectDao, WorkBidingProject> {
	@Autowired
	private WorkContractInfoService workContractInfoService;
	@Autowired
	private WorkClientInfoService workClientInfoService;
	@Autowired
	private OfficeService officeService;
	@Autowired
	private AreaService areaService;
	@Autowired
	private AreaDao areaDao;

	public WorkBidingProject get(String id) {
		WorkBidingProject entity = super.get(id);
		String projectMaster = entity.getProjectMaster();
		String projectMember = entity.getProjectMember();
		String inventorUser = entity.getInventorUser();
		Office demandOffice = entity.getDemandOffice();
		Area area = entity.getArea();
		if (area!=null && StringUtils.isNotBlank(entity.getArea().getId())){
			entity.setArea(areaService.get(area));
		}
		if (StringUtils.isNotBlank(demandOffice.getId())){
			entity.setDemandOffice(officeService.get(demandOffice));
		}
		String clientId = entity.getClient().getId();
		WorkClientInfo workClientInfo = workClientInfoService.get(clientId);
		if (workClientInfo!=null){
			entity.setClient(workClientInfo);
			List<WorkClientLinkman> linkmans = workClientInfo.getWorkClientLinkmanList();
			if (linkmans!=null && linkmans.size()!=0) {
				entity.setWorkClientLinkman(linkmans.get(0));
			}
		}
		if (StringUtils.isNotBlank(projectMaster)){
			String[] ids = projectMaster.split(",");
			String name = "";
			for (String str:ids) {
				User user = UserUtils.get(str);
				name += user.getName()+",";
			}
			if (StringUtils.isNotBlank(name)){
				name = name.substring(0,name.length() - 1);
			}
			entity.setProjectMasterName(name);
		}
		if (StringUtils.isNotBlank(projectMember)){
			String[] ids = projectMember.split(",");
			String name = "";
			for (String str:ids) {
				User user = UserUtils.get(str);
				name += user.getName()+",";
			}
			if (StringUtils.isNotBlank(name)){
				name = name.substring(0,name.length() - 1);
			}
			entity.setProjectMemberName(name);
		}
		if (StringUtils.isNotBlank(inventorUser)){
			String[] ids = inventorUser.split(",");
			String name = "";
			for (String str:ids) {
				User user = UserUtils.get(str);
				name += user.getName()+",";
			}
			if (StringUtils.isNotBlank(name)){
				name = name.substring(0,name.length() - 1);
			}
			entity.setInventorUserName(name);
		}
		return entity;
	}
	
	public List<WorkBidingProject> findList(WorkBidingProject workBidingProject) {
		workBidingProject.getSqlMap().put("dsf", dataScopeFilterOR(workBidingProject.getCurrentUser(), "o", "u","s", MenuStatusEnum.END.getValue()));
		return super.findList(workBidingProject);
	}
	
	public Page<WorkBidingProject> findPage(Page<WorkBidingProject> page, WorkBidingProject workBidingProject) {
		workBidingProject.getSqlMap().put("dsf", dataScopeFilterOR(workBidingProject.getCurrentUser(), "o", "u","s", MenuStatusEnum.END.getValue()));
		return super.findPage(page, workBidingProject);
	}
	
	@Transactional(readOnly = false)
	public void save(WorkBidingProject workBidingProject) {
		Area area = workBidingProject.getArea();
		area = areaDao.get(area.getId());
		workBidingProject.setArea(area);
		super.save(workBidingProject);
	}
	
	@Transactional(readOnly = false)
	public void delete(WorkBidingProject workBidingProject) {
		super.delete(workBidingProject);
	}


	public String parseList2Json(WorkClientInfo workClientInfo) {
		if(workClientInfo == null){
			return "";
		}
		List<WorkClientLinkman> list = workClientInfo.getWorkClientLinkmanList();
		List<Map<Object,Object>> resultList = Lists.newArrayList();
		String creditRank = DictUtils.getDictLabel(workClientInfo.getCreditRank(),"credit_rank","");
		if(list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				HashMap<Object, Object> map = Maps.newHashMap();
				map.put("linkmanName", list == null ? "" : list.get(i).getName());
				map.put("linkmanOffice", list == null ? "" : list.get(i).getOffice());
				map.put("linkmanPosition", list == null ? "" : list.get(i).getPosition());
				map.put("QQ", list == null ? "" : list.get(i).getQq());
				map.put("linkmanPhone", list == null ? "" : list.get(i).getLinkPhone());
				map.put("linkmanMobile", list == null ? "" : list.get(i).getLinkMobile());
				map.put("linkmanEmail", list == null ? "" : list.get(i).getEmail());
				map.put("clientName", workClientInfo.getName()==null ? "" :workClientInfo.getName());
				map.put("clientId", workClientInfo.getId() ==null?"":workClientInfo.getId());
				map.put("creditRank",creditRank);
				resultList.add(map);
			}
		}
		String resultJsonStr = JsonMapper.toJsonString(resultList);
		return resultJsonStr;
	}
}