/*
 *      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.security.trans.outsourcing.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.security.ht.vo.common.UploadVO;
import org.security.trans.common.service.IFileService;
import org.security.trans.dataStatistics.vo.PieChartDataVO;
import org.security.trans.outsourcing.dto.OutEmployeeDTO;
import org.security.trans.outsourcing.entity.OutEmployee;
import org.security.trans.outsourcing.entity.OutEmployeeFamily;
import org.security.trans.outsourcing.entity.OutEmployeeJobRecord;
import org.security.trans.outsourcing.mapper.OutEmployeeMapper;
import org.security.trans.outsourcing.service.IOutCompanyService;
import org.security.trans.outsourcing.service.IOutEmployeeFamilyService;
import org.security.trans.outsourcing.service.IOutEmployeeJobRecordService;
import org.security.trans.outsourcing.service.IOutEmployeeService;
import org.security.trans.outsourcing.vo.OutEmployeeFamilyVO;
import org.security.trans.outsourcing.vo.OutEmployeeJobRecordVO;
import org.security.trans.outsourcing.vo.OutEmployeeVO;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static org.security.common.Constant.*;

/**
 * 外包员工信息表 服务实现类
 *
 * @author BladeX
 * @since 2022-02-22
 */
@AllArgsConstructor
@Service
public class OutEmployeeServiceImpl extends ServiceImpl<OutEmployeeMapper, OutEmployee> implements IOutEmployeeService {

	private final IOutEmployeeFamilyService outEmployeeFamilyService;
	private final IOutEmployeeJobRecordService outEmployeeJobRecordService;
	private final IOutCompanyService outCompanyService;
	private final IFileService fileService;

	@Override
	public IPage<OutEmployeeVO> getPage(Query query, OutEmployeeDTO outEmployeeDTO) {
		Page<OutEmployeeVO> page = new Page<>(query.getCurrent(), query.getSize());
		List<OutEmployeeVO> list = baseMapper.selectPageData(page, outEmployeeDTO);
		if (Func.isNotEmpty(list)){
			list.forEach(item->{
				if(Func.isNotEmpty(item.getCompanyId())){
					item.setCompanyName(outCompanyService.getById(item.getCompanyId()).getOutCompanyName());
				}
				if (SEX_BOY.equals(item.getSex())){
					item.setSex("男");
				}
				if (SEX_GIRL.equals(item.getSex())){
					item.setSex("女");
				}
			});
		}
		return page.setRecords(list);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean removeEmployeeAndFamilyesAndJobRecords(List<Long> ids) {
		this.removeByIds(ids);
		outEmployeeFamilyService.removeByEmployeeId(ids);
		outEmployeeJobRecordService.removeByEmployeeId(ids);
		return true;
	}

	@Override
	public List<PieChartDataVO> getNationTypeData(OutEmployeeDTO outEmployeeDTO) {
		return baseMapper.getNationTypeData(outEmployeeDTO);
	}

	@Override
	public String getDictName(String key) {
		return baseMapper.getDictName(key);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean updateEmployeeAndFamilyesAndJobRecords(OutEmployeeDTO employeeVO) {
		OutEmployee employee = Func.copy(employeeVO, OutEmployee.class);
		this.updateById(employee);
		List<OutEmployeeFamily> families = Func.copy(employeeVO.getFamilyes(), OutEmployeeFamily.class);
		outEmployeeFamilyService.updateBatchById(families);
		List<OutEmployeeJobRecord> jobRecords = Func.copy(employeeVO.getJobRecords(), OutEmployeeJobRecord.class);
		outEmployeeJobRecordService.updateBatchById(jobRecords);
		return true;
	}

	@Override
	public OutEmployeeVO getEmployeeAndFamilyesAndJobRecords(String employeeId) {
		OutEmployee detail = this.getById(employeeId);
		List<OutEmployeeFamilyVO> familyVOS = outEmployeeFamilyService.getByEmployeeId(employeeId);
		List<OutEmployeeJobRecordVO> jobRecordVOS = outEmployeeJobRecordService.getByEmployeeId(employeeId);
		//获取相关附件
		List<UploadVO> attachments = fileService.getAttachments(employeeId,detail.getGroupName());
		//获取相关附件-证件照
		List<UploadVO> attachmentsHead = fileService.getAttachments(employeeId,detail.getGroupNameHead());
		//获取相关附件-身份证正面
		List<UploadVO> attachmentsFront = fileService.getAttachments(employeeId,detail.getGroupNameFront());
		//获取相关附件-身份证反面
		List<UploadVO> attachmentsBack = fileService.getAttachments(employeeId,detail.getGroupNameBack());

		OutEmployeeVO employeeVO = Func.copy(detail, OutEmployeeVO.class);
		employeeVO.setFamilyes(familyVOS);
		employeeVO.setJobRecords(jobRecordVOS);
		employeeVO.setRelevantAttachments(attachments);
		employeeVO.setPicList(attachmentsHead);
		employeeVO.setIdCardFrontList(attachmentsFront);
		employeeVO.setIdCardBackList(attachmentsBack);
		return employeeVO;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean saveEmployeeAndFamilyesAndJobRecords(OutEmployeeDTO employeeVO) {
		OutEmployee employee = Func.copy(employeeVO, OutEmployee.class);
		employee.setDeptId(Long.parseLong(AuthUtil.getDeptId()));
		this.saveOrUpdate(employee);
//		保存家庭成员明细
		List<OutEmployeeFamily> families = Func.copy(employeeVO.getFamilyes(), OutEmployeeFamily.class);
		families.forEach(item -> item.setEmployeeId(employee.getId()));
		outEmployeeFamilyService.saveOrUpdateBatch(families);
//		保存工作履历明细
		List<OutEmployeeJobRecord> jobRecords = Func.copy(employeeVO.getJobRecords(), OutEmployeeJobRecord.class);
		jobRecords.forEach(item -> item.setEmployeeId(employee.getId()));
		outEmployeeJobRecordService.saveOrUpdateBatch(jobRecords);
		//保存相关附件信息
		fileService.saveAttachments(employeeVO.getRelevantAttachments(),employee.getId());
		//保存附件信息
		fileService.saveAttachments(employeeVO.getIdCardFrontList(),employee.getId());
		//保存附件信息
		fileService.saveAttachments(employeeVO.getIdCardBackList(),employee.getId());
		//保存附件信息
		fileService.saveAttachments(employeeVO.getPicList(),employee.getId());
		return true;
	}
}
