/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.community.staff.service.impl;

import org.springblade.common.Constants;
import org.springblade.community.enterprise.entity.Enterprise;
import org.springblade.community.enterprise.service.IEnterpriseService;
import org.springblade.community.residential.entity.Residential;
import org.springblade.community.residential.service.IResidentialService;
import org.springblade.community.staff.dto.EnterpriseStaffDTO;
import org.springblade.community.staff.entity.EnterpriseStaff;
import org.springblade.community.staff.vo.EnterpriseStaffVO;
import org.springblade.community.staff.mapper.EnterpriseStaffMapper;
import org.springblade.community.staff.service.IEnterpriseStaffService;
import org.springblade.community.staff.wrapper.EnterpriseStaffWrapper;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.utils.AESPlus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.List;

/**
 *  服务实现类
 *
 * @author BladeX
 * @since 2020-12-11
 */
@Service
public class EnterpriseStaffServiceImpl extends BaseServiceImpl<EnterpriseStaffMapper, EnterpriseStaff> implements IEnterpriseStaffService {

	@Autowired
	private IResidentialService residentialService;
	@Autowired
	private IEnterpriseService enterpriseService;

	@Override
	public IPage<EnterpriseStaffVO> selectEnterpriseStaffPage(IPage<EnterpriseStaffVO> page, EnterpriseStaffVO enterpriseStaff) {
		return page.setRecords(baseMapper.selectEnterpriseStaffPage(page, enterpriseStaff));
	}

	@Override
	public List<String> getListByEnterpriseCreditCode(String enterpriseVreditCode) {
		return this.baseMapper.getListByEnterpriseCreditCode(enterpriseVreditCode);
	}

	@Override
	public R staffSubmit(EnterpriseStaff enterpriseStaff){
		Enterprise enterprise = this.enterpriseService.getById(enterpriseStaff.getEnterpriseId());
		Residential residential = residentialService.getById(enterprise.getResidentialId());
		enterpriseStaff.setEnterpriseName(enterprise.getEnterpriseName());
		enterpriseStaff.setAgencyId(residential.getAgencyId());
		enterpriseStaff.setResidentialId(residential.getId());
//		try {
//			enterpriseStaff.setRealName(AESPlus.encrypt(Constants.encryptKey, enterpriseStaff.getRealName()));
//			enterpriseStaff.setPhone(AESPlus.encrypt(Constants.encryptKey, enterpriseStaff.getPhone()));
//			enterpriseStaff.setEmail(AESPlus.encrypt(Constants.encryptKey, enterpriseStaff.getEmail()));
//			enterpriseStaff.setIdcard(AESPlus.encrypt(Constants.encryptKey, enterpriseStaff.getIdcard()));
//		} catch (Exception e) {
//			e.printStackTrace();
//			log.error(e.getMessage());
//		}
		if (enterpriseStaff.getId() == null) {
			this.save(enterpriseStaff);
		}
		enterpriseStaff.setOrgPosition(residential.getOrgPosition() + "/" + enterpriseStaff.getId());
		return R.status(this.updateById(enterpriseStaff));
	}

}
