package com.goafanti.organization.service.impl;

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

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

import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.organization.bo.OrganizationListOut;
import com.goafanti.organization.service.OrganizationService;
import com.goafanti.admin.bo.AdminListBo;
import com.goafanti.common.dao.OrganizationManagementMapper;
import com.goafanti.common.dao.TOrderMidMapper;
import com.goafanti.common.model.OrganizationManagement;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.core.mybatis.BaseMybatisDao;
@Service
public class OrganizationServiceImpl extends BaseMybatisDao<OrganizationManagementMapper> implements OrganizationService {
	@Autowired
	private OrganizationManagementMapper organizationManagementMapper;
	@Autowired
	private TOrderMidMapper	tOrderMidMapper;
	@Override
	public Pagination<OrganizationListOut> listOrganizationManagement(OrganizationListOut olo, Integer pageNo,
			Integer pageSize) {
		/*olo.setCreateId(TokenManager.getAdminId());*/
		Map<String,Object> params =disposeParams(olo);
		@SuppressWarnings("unchecked")
		Pagination<OrganizationListOut> p=(Pagination<OrganizationListOut>)findPage("selectOrganizationList","selectOrganizationCount",params,pageNo,pageSize);
		return p;
	}
	private Map<String,Object> disposeParams(OrganizationListOut olo){
		Map<String,Object> params = new HashMap<String, Object>();		
		if(StringUtils.isNotBlank(olo.getId())) params.put("id", olo.getId());
		if(StringUtils.isNotBlank(olo.getCreateId())) params.put("createId", olo.getCreateId());
		if(StringUtils.isNotBlank(olo.getCreateTime())) params.put("createTime", olo.getCreateTime());
		if(StringUtils.isNotBlank(olo.getUpdateTime())) params.put("updateTime", olo.getUpdateTime());
		if(StringUtils.isNotBlank(olo.getDeletedSign())) params.put("deletedSign", olo.getDeletedSign());
		if(StringUtils.isNotBlank(olo.getName())) params.put("name", olo.getName());
		if(StringUtils.isNotBlank(olo.getType())) params.put("type", olo.getType());
		if(StringUtils.isNotBlank(olo.getManagerId())) params.put("managerId", olo.getManagerId());
		if(StringUtils.isNotBlank(olo.getDepNo())) params.put("depNo", olo.getDepNo());
		if(StringUtils.isNotBlank(olo.getSuperId())) params.put("superId", olo.getSuperId());
		if(StringUtils.isNotBlank(olo.getRemarks())) params.put("remarks", olo.getRemarks());
		if(StringUtils.isNotBlank(olo.getStatus())) params.put("status", olo.getStatus());
		return params;
	}
	@Override
	public List<OrganizationListOut> selectSuperId(Integer i) {
		return organizationManagementMapper.selectSuperId(i);
	}
	/**
	 *   XXIn form 提交 ,bo包中
	 *   xxOut 输出 bo包中
	 *   domain 类 model中
	 *
	 */
	@Override
	public int addOrganization( String name, String managerId, String type, String superId, String remarks,String dep) {
		String id = UUID.randomUUID().toString();
		Date now = new Date();
		OrganizationManagement om=new OrganizationManagement(); 
		om.setId(id);
		String createId =organizationManagementMapper.selectAdminNameById(TokenManager.getAdminId());
		om.setCreateId(createId);
		om.setCreateTime(now);
		om.setUpdateTime(now);
		om.setDeletedSign("0");
		om.setName(name);
		om.setType(type);
		om.setManagerId(managerId);
		om.setDepNo(dep);
	//	superId=organizationManagementMapper.selectIdByName(superId);
		om.setSuperId(superId);
		om.setRemarks(remarks);
		om.setStatus("0");
		organizationManagementMapper.insert(om);
		return 1;
	}
	@Override
	public List<AdminListBo> selectName(String name) {
		List<AdminListBo> list=organizationManagementMapper.selectName(name);
		return list;
	}
	@Override
	public OrganizationListOut selectAllById(String id) {
		OrganizationListOut olo=organizationManagementMapper.selectAllById(id);
		olo.setSuperId(organizationManagementMapper.selectNameById(olo.getSuperId()));
		return olo;
	}
	@Override
	public int deleteById(String id) {
		organizationManagementMapper.deleteById(id);
		return 1;
	}
	@Override
	public int updateOrganization(String name, String type, String managerId, String superId, String status,String remarks,String id,String depn,String abbreviation,String financeId) {
		Date now=new Date();
		OrganizationManagement om=new OrganizationManagement();
		om.setName(name);
		om.setType(type);  
		om.setUpdateTime(now);
		om.setManagerId(managerId);
		om.setDepNo(depn);
		om.setSuperId(superId);
		om.setStatus(status);
		om.setRemarks(remarks); 
		om.setId(id);
		om.setAbbreviation(abbreviation);
		om.setFinanceId(financeId);
		tOrderMidMapper.updateFinanceId(id,financeId);
		int x=organizationManagementMapper.updateByPrimaryKeySelective(om);
		//*******下级编号修改
		int n=0;
		List<OrganizationListOut> Nos =organizationManagementMapper.selectIDNBySuperId(om.getId());
		for(OrganizationListOut i:Nos){			
			String sdepNo=organizationManagementMapper.selectDepNoById(i.getSuperId());
			if(n<10){
				String dep=sdepNo+"0"+n;			
				if(n>=10){
					dep=sdepNo+n;	
				}else{
					dep=sdepNo+"0"+n;
				}	
				i.setDepNo(dep);
			}
			if(n>=10){
				String dep=sdepNo+n;			
				dep=sdepNo+n;	
				i.setDepNo(dep);
			}
		organizationManagementMapper.updateByNumber(i);
			n++;
			
			//*******下下级编号修改
			int m=0;
			List<OrganizationListOut> Noss =organizationManagementMapper.selectIDNBySuperId(i.getId());
			//System.out.println("noss="+Noss);
			for(OrganizationListOut o:Noss){			
				sdepNo=organizationManagementMapper.selectDepNoById(o.getSuperId());
				if(m<10){
					String dep=sdepNo+"0"+m;			
					if(m>=10){
						dep=sdepNo+m;	
					}else{
						dep=sdepNo+"0"+m;
					}	
					o.setDepNo(dep);
				}
				if(m>=10){
					String dep=sdepNo+m;			
					dep=sdepNo+m;	
					o.setDepNo(dep);
				}
				organizationManagementMapper.updateByNumber(o);
				m++;
				
				//*******下下级编号修改
				int z=0;
				List<OrganizationListOut> Nosss =organizationManagementMapper.selectIDNBySuperId(o.getId());
				for(OrganizationListOut p:Nosss){			
					sdepNo=organizationManagementMapper.selectDepNoById(p.getSuperId());
					if(z<10){
						String dep=sdepNo+"0"+z;			
						if(z>=10){
							dep=sdepNo+z;	
						}else{
							dep=sdepNo+"0"+z;
						}	
						p.setDepNo(dep);
					}
					if(z>=10){
						String dep=sdepNo+z;			
						dep=sdepNo+z;	
						p.setDepNo(dep);
					}
					organizationManagementMapper.updateByNumber(p);
					z++;
				}
			}
		}
		return x;
	}

}
