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

import com.google.common.collect.Lists;
import com.jeeplus.common.oss.OSSClientUtil;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.hr.dao.UserInfoDao;
import com.jeeplus.modules.hr.entity.UserInfo;
import com.jeeplus.modules.sys.dao.UserDao;
import com.jeeplus.modules.sys.entity.Certificate;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.entity.Workattachment;
import com.jeeplus.modules.sys.service.CertificateService;
import com.jeeplus.modules.sys.service.OfficeService;
import com.jeeplus.modules.sys.service.UserService;
import com.jeeplus.modules.sys.service.WorkattachmentService;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.sysuseroffice.dao.UserofficeDao;
import com.jeeplus.modules.sysuseroffice.entity.Useroffice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 人员基本信息Service
 * @author fgy
 * @version 2017-12-06
 */
@SuppressWarnings("all")
@Service
@Transactional(readOnly = true)
public class UserInfoService extends CrudService<UserInfoDao, UserInfo> {

	@Autowired
	private UserInfoDao userInfoDao;
	@Autowired
	private UserofficeDao userofficeDao;
	@Autowired
	private UserDao userDao;
	@Autowired
	private UserService userService;
	@Autowired
	private OfficeService officeService;
	@Autowired
	private WorkattachmentService workattachmentService;
	@Autowired
	private CertificateService certificateService;

	public UserInfo get(String id) {
		UserInfo userInfo = super.get(id);
		if(userInfo!=null){
			if(StringUtils.isNotBlank(userInfo.getUserId())){
				User user = UserUtils.get(userInfo.getUserId());
				Workattachment jobResume = workattachmentService.getByAttachmentIdAndCompanyIdAndFlag(userInfo.getUserId(), UserUtils.getSelectCompany().getId(), "62");
				Workattachment userEvaluation = workattachmentService.getByAttachmentIdAndCompanyIdAndFlag(userInfo.getUserId(), UserUtils.getSelectCompany().getId(), "0");
				Workattachment satisfaction = workattachmentService.getByAttachmentIdAndCompanyIdAndFlag(userInfo.getUserId(), UserUtils.getSelectCompany().getId(), "63");
				Workattachment certificate = workattachmentService.getByAttachmentIdAndCompanyIdAndFlag(userInfo.getUserId(),UserUtils.getSelectCompany().getId(),"64");
				if(jobResume!=null){
					user.setJobResume(jobResume);
					if(StringUtils.isNotBlank(jobResume.getAttachmentUser())){
						jobResume.setAttachmentUser(UserUtils.get(jobResume.getAttachmentUser()).getName());
					}
				}
				if(userEvaluation!=null){
					user.setWorkattachment(userEvaluation);
					if(StringUtils.isNotBlank(userEvaluation.getAttachmentUser())){
						userEvaluation.setAttachmentUser(UserUtils.get(userEvaluation.getAttachmentUser()).getName());
					}
				}
				if(satisfaction!=null){
					user.setSatisfaction(satisfaction);
					if(StringUtils.isNotBlank(satisfaction.getAttachmentUser())){
						satisfaction.setAttachmentUser(UserUtils.get(satisfaction.getAttachmentUser()).getName());
					}
				}
				if(StringUtils.isBlank(userInfo.getGender())){
					userInfo.setGender(user.getSex());
				}
				if(StringUtils.isBlank(userInfo.getName())){
					userInfo.setName(user.getName());
				}
				if(StringUtils.isBlank(userInfo.getMobilePhone())){
					userInfo.setMobilePhone(user.getMobile());
				}
				userInfo.setUser(user);
			}
			List<Certificate> certificateList = certificateService.getListByUserInfoId(id);
			if(certificateList!=null && certificateList.size()>0){
				for(Certificate c : certificateList){
					Workattachment w = workattachmentService.getByAttachmentIdAndCompanyIdAndFlag(c.getId(),UserUtils.getSelectCompany().getId(),"64");
					c.setCertificate(w);
				}
			}
			userInfo.setCertificateList(certificateList);
		}



		return userInfo;
	}
	
	public List<UserInfo> findList(UserInfo userInfo) {
		ArrayList<UserInfo> userInfoList = Lists.newArrayList();
		User user = UserUtils.getUser();

		if(user.isAdmin()){

		}else {
			userInfo.setCompanyId(UserUtils.getSelectCompany().getId());
		}
		ArrayList<UserInfo> list = userInfoDao.findUserInfoList(userInfo);

		if(list != null && list.size()>0){
			for(int i=0;i<list.size();i++){
				UserInfo ui = list.get(i);
				if(ui.getUserOffice()!=null){
					Useroffice useroffice = ui.getUserOffice();
					if(StringUtils.isNotBlank(useroffice.getUserId())){
						User u = UserUtils.get(useroffice.getUserId());
						String userId = useroffice.getUserId();
						String companyId = useroffice.getCompanyId();
						String officeId = useroffice.getOfficeId();
						if(StringUtils.isNotBlank(userId) && StringUtils.isNotBlank(companyId)){
							ui = userInfoDao.get(ui.getId());
							if(ui == null){
								ui = new UserInfo();
							}
						}
						ui.setUserId(userId);
						ui.setCompanyId(companyId);
						ui.setOfficeId(officeId);
						ui.setCompany(officeService.get(companyId));
						ui.setOffice(officeService.get(officeId));
						ui.setUser(u);
						ui.setUserOffice(useroffice);
					}
				}
				userInfoList.add(ui);
			}
		}
		return userInfoList;
	}

	public Page<UserInfo> findPage(Page<UserInfo> page, UserInfo userInfo) {
		userInfo.setPage(page);
		page.setList(findList(userInfo));
		return page;
	}
	
	@Transactional(readOnly = false)
	public void save(UserInfo userInfo) {
		super.save(userInfo);
	}
	
	@Transactional(readOnly = false)
	public void delete(UserInfo userInfo) {
		super.delete(userInfo);
	}

	//保存人员简历(62)信息
	@Transactional(readOnly = false)
    public void saveJobResume(UserInfo ui, MultipartFile file) {
		if(file!=null && !file.isEmpty() && file.getSize()>0){
			String userId = ui.getUserId();
			String fileName = file.getOriginalFilename();
			String fileType = fileName.substring(fileName.lastIndexOf(".")+ 1);
			Workattachment w = workattachmentService.getByAttachmentIdAndCompanyIdAndFlag(userId,UserUtils.getSelectCompany().getId(),"62");
			if(w != null){
				workattachmentService.deleteFileFromAliyun(w,w==null?"":w.getUrl());
				workattachmentService.delete(w);
			}
			OSSClientUtil ossUtil = new OSSClientUtil();
			String aliyunPath = ossUtil.uploadFile2OSS(file, "jobResume");
			Workattachment workattachment = new Workattachment();
			workattachment.setUrl(aliyunPath);
			workattachment.setType(fileType);
			workattachment.setAttachmentId(userId);
			workattachment.setAttachmentUser(UserUtils.getUser().getId());
			workattachment.setAttachmentName(fileName);
			workattachment.setCompanyId(UserUtils.getSelectCompany().getId());
			workattachment.setAttachmentFlag("62");
			workattachmentService.save(workattachment);

		}
    }

	//保存评价表(0)和客户满意表(63)
	@Transactional(readOnly = false)
	public void saveEvaluationAndSatisfaction(UserInfo ui, MultipartFile this_upload_files, MultipartFile next_upload_files) {
		if(this_upload_files!=null && !this_upload_files.isEmpty() && this_upload_files.getSize()>0){
			String userId = ui.getUserId();
			String fileName = this_upload_files.getOriginalFilename();
			String fileType = fileName.substring(fileName.lastIndexOf(".")+ 1);
			Workattachment w = workattachmentService.getByAttachmentIdAndCompanyIdAndFlag(userId,UserUtils.getSelectCompany().getId(),"0");
			if(w != null){
				workattachmentService.deleteFileFromAliyun(w,w==null?"":w.getUrl());
				workattachmentService.delete(w);
			}
			OSSClientUtil ossUtil = new OSSClientUtil();
			String aliyunPath = ossUtil.uploadFile2OSS(this_upload_files, "userEvaluation");
			Workattachment workattachment = new Workattachment();
			workattachment.setUrl(aliyunPath);
			workattachment.setType(fileType);
			workattachment.setAttachmentId(userId);
			workattachment.setAttachmentUser(UserUtils.getUser().getId());
			workattachment.setAttachmentName(fileName);
			workattachment.setCompanyId(UserUtils.getSelectCompany().getId());
			workattachment.setAttachmentFlag("0");
			workattachmentService.save(workattachment);
		}
		if(next_upload_files!=null && !next_upload_files.isEmpty() && next_upload_files.getSize()>0){
			String userId = ui.getUserId();
			String fileName = next_upload_files.getOriginalFilename();
			String fileType = fileName.substring(fileName.lastIndexOf(".")+ 1);
			Workattachment w = workattachmentService.getByAttachmentIdAndCompanyIdAndFlag(userId,UserUtils.getSelectCompany().getId(),"63");
			if(w != null){
				workattachmentService.deleteFileFromAliyun(w,w==null?"":w.getUrl());
				workattachmentService.delete(w);
			}
			OSSClientUtil ossUtil = new OSSClientUtil();
			String aliyunPath = ossUtil.uploadFile2OSS(next_upload_files, "satisfaction");
			Workattachment workattachment = new Workattachment();
			workattachment.setUrl(aliyunPath);
			workattachment.setType(fileType);
			workattachment.setAttachmentId(userId);
			workattachment.setAttachmentUser(UserUtils.getUser().getId());
			workattachment.setAttachmentName(fileName);
			workattachment.setCompanyId(UserUtils.getSelectCompany().getId());
			workattachment.setAttachmentFlag("63");
			workattachmentService.save(workattachment);
		}

	}
	//保存人员证书信息
	@Transactional(readOnly = false)
	public void saveCertificate(Certificate certificate, MultipartFile this_upload_files) {
		certificateService.save(certificate);
		if(this_upload_files!=null && !this_upload_files.isEmpty() && this_upload_files.getSize()>0){
			String userId = certificate.getUserId();
			String fileName = this_upload_files.getOriginalFilename();
			String fileType = fileName.substring(fileName.lastIndexOf(".")+ 1);
			Workattachment w = workattachmentService.getByAttachmentIdAndCompanyIdAndFlag(certificate.getId(),UserUtils.getSelectCompany().getId(),"64");
			if(w != null){
				workattachmentService.deleteFileFromAliyun(w,w==null?"":w.getUrl());
				workattachmentService.delete(w);
			}
			OSSClientUtil ossUtil = new OSSClientUtil();
			String aliyunPath = ossUtil.uploadFile2OSS(this_upload_files, "certificate");
			Workattachment workattachment = new Workattachment();
			workattachment.setUrl(aliyunPath);
			workattachment.setType(fileType);
			workattachment.setAttachmentId(certificate.getId());
			workattachment.setAttachmentUser(UserUtils.getUser().getId());
			workattachment.setAttachmentName(fileName);
			workattachment.setCompanyId(UserUtils.getSelectCompany().getId());
			workattachment.setAttachmentFlag("64");
			workattachmentService.save(workattachment);
		}
	}
}