package com.goafanti.admin.service.impl;

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

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

import com.goafanti.admin.bo.AdminDetail;
import com.goafanti.admin.bo.AdminListBo;
import com.goafanti.admin.service.AdminService;
import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.dao.AdminLocationMapper;
import com.goafanti.common.dao.AdminMapper;
import com.goafanti.common.dao.UserRoleMapper;
import com.goafanti.common.model.Admin;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.core.shiro.token.TokenManager;

@Service
public class AdminServiceImpl extends BaseMybatisDao<AdminMapper> implements AdminService {
	@Autowired
	private AdminMapper			adminMapper;
	@Autowired
	private UserRoleMapper		userRoleMapper;
	@Autowired
	private AdminLocationMapper	adminLocationMapper;
	@Override
	public List<Admin> selectAllAdmin() {
		return adminMapper.selectAllAdmin();
	}
	
	@Override
	public Admin selectByMobile(String mobile,String roleName) {
		return adminMapper.selectByMobile(mobile,roleName);
	}

	@Override
	public Admin selectByPrimaryKey(String key) {
		return adminMapper.selectByPrimaryKey(key);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pagination<AdminListBo> listAdmin(Integer province, Integer number, String mobile, String name,
			Integer pageNo, Integer pageSize) {
		Map<String, Object> params = new HashMap<>();

		if (StringUtils.isNotBlank(mobile)) {
			params.put("mobile", mobile);
		}

		if (StringUtils.isNotBlank(name)) {
			params.put("name", name);
		}

		if (null != number) {
			params.put("number", number);
		}

		if (pageNo == null || pageNo < 0) {
			pageNo = 1;
		}

		if (pageSize == null || pageSize < 0 || pageSize > 10) {
			pageSize = 10;
		}
		if (province != null) {
			params.put("province", province);
		}
		return (Pagination<AdminListBo>) findPage("findAdminListByPage", "findAdminCount", params, pageNo,
					pageSize);
	}

	@Override
	public int insert(Admin ad) {
		return adminMapper.insert(ad);
	}

	@Override
	public int updateByPrimaryKeySelective(Admin ad, List<String> roleIds) {
		Map<String, Object> params = new HashMap<>();
		params.put("uid", ad.getId());
		params.put("roles", roleIds);

		if (ad.getId() != "1") {
			userRoleMapper.deleteByUserId(ad.getId());
			if (!roleIds.isEmpty()) {
				userRoleMapper.insertBatch(params);
			}
			TokenManager.clearUserAuthByUserId(ad.getId());
		}
		return adminMapper.updateByPrimaryKeySelective(ad);
	}

	@Override
	public int updateByPrimaryKey(Admin a) {
		return adminMapper.updateByPrimaryKeySelective(a);
	}

	@Override
	public AdminDetail selectAdminDetail(String id) {
		Admin a = adminMapper.selectByPrimaryKey(id);
		if (null == a) {
			return null;
		}

		AdminDetail ad = new AdminDetail();
		BeanUtils.copyProperties(a, ad);
		ad.setRoles(adminMapper.selectRolesByPrimaryKey(id));
		return null;
	}

	@Override
	public List<Admin> selectCognizanceConsultant() {
		return adminMapper.selectCognizanceConsultant();
	}

	@Override
	public List<Admin> selectPatentAuthor() {
		return adminMapper.selectPatentAuthor();
	}

	@Override
	public List<Admin> selectPatentPrincipal() {
		return adminMapper.selectPatentPrincipal();
	}

	@Override
	public List<Admin> selectCopyrightConsultant() {
		return adminMapper.selectCopyrightConsultant();
	}

	@Override
	public List<Admin> selectCognizancePrincipal() {
		return adminMapper.selectCognizancePrincipal();
	}

	@Override
	public List<Admin> selectCopyrightPrincipal() {
		return adminMapper.selectCopyrightPrincipal();
	}

	@Override
	public List<Admin> selectTechprojectConsultant() {
		return adminMapper.selectTechprojectConsultant();
	}

	@Override
	public List<Admin> selectTechprojectPrincipal() {
		return adminMapper.selectTechprojectPrincipal();
	}

	@Override
	public List<String> selectRoleByPrimaryKey(String uid) {
		return adminMapper.selectRoleByPrimaryKey(uid);
	}

	@Override
	public List<String> listAuditor() {
		return userRoleMapper.listAuditor();
	}

	@Override
	public Map<String, String> selectAccoutManager() {
		return disposeAdminList(adminMapper.selectAccoutManager());
	}

	@Override
	public Map<String, String> selectTechnician() {
		return disposeAdminList(adminMapper.selectTechnician());
	}

	@Override
	public Map<String, String> selectSalesman() {
		return disposeAdminList(adminMapper.selectSalesman());
	}

	@Override
	public Map<String, String> selectContractManager() {
		return disposeAdminList(adminMapper.selectContractManager());
	}

	@Override
	public Map<String, String> selectTechBroder() {
		return disposeAdminList(adminMapper.selectTechBroder());
	}

	@Override
	public int insertNewAdmin(Admin ad) {
		return adminMapper.insert(ad);
	}

	@Override
	public Map<String, String> listAdminSelect() {
		if (TokenManager.hasRole(AFTConstants.SUPERADMIN)) {
			return disposeAdminList(adminMapper.listAdminSelectBySuperAdmin());
		} else if (TokenManager.hasRole(AFTConstants.AREAADMIN)) {
			List<Integer> provinceList = adminLocationMapper
					.selectProvinceWhereCityIsNullByAdminId(TokenManager.getAdminId());
			List<Integer> cityList = adminLocationMapper.selectCityByAdminId(TokenManager.getAdminId());
			provinceList.addAll(cityList);
			return disposeAdminList(adminMapper.listAdminSelectByAreaAdmin(provinceList, cityList));
		}
		return null;
	}

	private Map<String, String> disposeAdminList(List<Admin> list) {
		Map<String, String> map = new TreeMap<String, String>();
		for (Admin o : list) {
			map.put(o.getId(), o.getName());
		}
		return map;
	}

	@Override
	public List<Admin> listAdminByName(String name) {
		
		return adminMapper.listAdminByName(name);
	}

	@Override
	public List<AdminListBo> selectDepartmentStaff(String departmentId, String name) {
		
		return adminMapper.selectDepartmentStaff( departmentId,  name);
	}	

}
