package com.hfkj.service.impl;

import com.hfkj.dao.BsCompanyMapper;
import com.hfkj.dao.BsOrganizationMapper;
import com.hfkj.dao.SecUserMapper;
import com.hfkj.dao.SecUserRoleRelMapper;
import com.hfkj.entity.*;
import com.hfkj.service.BsCompanyService;
import com.hfkj.service.CommonService;
import com.hfkj.sysenum.SecUserRoleIdEnum;
import com.hfkj.sysenum.SecUserTypeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service("bsCompanyService")
public class BsCompanyServiceImpl implements BsCompanyService {

	@Resource
	private BsCompanyMapper bsCompanyMapper;

	@Resource
	private BsOrganizationMapper bsOrganizationMapper;

	@Resource
	private SecUserMapper secUserMapper;

	@Resource
	private SecUserRoleRelMapper secUserRoleRelMapper;

	@Resource
	private CommonService commonService;

	@Override
	public List<BsCompany> findPage(Map<String, String> map) {
		BsCompanyExample example = new BsCompanyExample();
		BsCompanyExample.Criteria criteria = example.createCriteria();
		criteria.andIdNotEqualTo(1L);
		if(StringUtils.isNotBlank(map.get("name"))){
			criteria.andNameLike("%" + map.get("name") + "%");
		}
		example.setOrderByClause("create_time desc");
		List<BsCompany> companyList = bsCompanyMapper.selectByExample(example);
		if (companyList != null && companyList.size() != 0) {
			for (BsCompany company : companyList) {
				if (StringUtils.isNotBlank(company.getRegionId())) {
					SecRegion region = commonService.getRegionsById(Long.valueOf(company.getRegionId()));
					company.setRegionName(region!=null?region.getRegionName():null);
				}
			}
		}
		return companyList;
	}

	@Override
	@Transactional
	public void addCompanyInfo(BsCompany secCompany, SecUser secUser, BsOrganization secOrganization) throws Exception {
		// 保存公司
		bsCompanyMapper.insert(secCompany);
		// 保存公司对应顶级部门
		secOrganization.setCompanyId(secCompany.getId());
		bsOrganizationMapper.insert(secOrganization);
		// 保存公司主角色
		secUser.setCompanyId(secCompany.getId());
		secUser.setObjectType(SecUserTypeEnum.type1.getNumber());
		secUser.setObjectId(secCompany.getId());
		secUser.setOrganizationId(secOrganization.getId());
		secUser.setRoleId(SecUserRoleIdEnum.roleId7.getRoleId());
		secUserMapper.insert(secUser);
		// 修改部门管理员为公司主角色
		secOrganization.setManagerId(secUser.getId());
		secOrganization.setManagerName(secUser.getUserName());
		secOrganization.setManagerPhone(secUser.getTelephone());
		bsOrganizationMapper.updateByPrimaryKey(secOrganization);
		// 保存用户角色
		SecUserRoleRel userRole = new SecUserRoleRel();
		userRole.setUserId(secUser.getId());
		userRole.setRoleId(7L);
		secUserRoleRelMapper.insert(userRole);
	}

	@Override
	public int updateCompany(BsCompany secCompany) {
		return bsCompanyMapper.updateByPrimaryKey(secCompany);
	}

	@Override
	public BsCompany getCompanyById(Long id) {
		BsCompany company =bsCompanyMapper.selectByPrimaryKey(id);
		if (company.getRegionId() != null) {
			// company.setRegionName(commonService.getRegionName(Long.valueOf(company.getRegionId())));
		}
		return company;
	}

	@Override
	public List<BsCompany> getCompany(Map<String, String> map) {
		BsCompanyExample example = new BsCompanyExample();
		example.createCriteria().andIdNotEqualTo(1L).andIdNotEqualTo(2L).andStatusNotEqualTo(0);
		return bsCompanyMapper.selectByExample(example);
	}

	@Override
	@Transactional
	public void updateCompanyAndDept(BsCompany secCompany, SecUser secUser, BsOrganization secOrganization) {
		bsCompanyMapper.updateByPrimaryKey(secCompany);
		bsOrganizationMapper.updateByPrimaryKey(secOrganization);
		secUserMapper.updateByPrimaryKey(secUser);
	}

	@Override
	public BsCompany selectCompanyByName(String name) {
		BsCompanyExample example = new BsCompanyExample();
		BsCompanyExample.Criteria criteria = example.createCriteria();
		criteria.andNameEqualTo(name);
		criteria.andStatusEqualTo(1);
		List<BsCompany> list = bsCompanyMapper.selectByExample(example);
		if (list != null && list.size() != 0) {
			return list.get(0);
		}else {
			return null;
		}
	}

	@Override
	public BsCompany selectCompanyByRegion(String regionId) {
		BsCompanyExample example = new BsCompanyExample();
		example.createCriteria().andStatusEqualTo(1).andRegionIdEqualTo(regionId);
		List<BsCompany> companies = bsCompanyMapper.selectByExample(example);
		if (companies != null && companies.size() != 0) {
			return companies.get(0);
		}
		return null;
	}


}
