/*
 *      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.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.security.common.Constant;
import org.security.trans.assessment.entity.Assessment;
import org.security.trans.basic.service.IDepartmentService;
import org.security.ht.vo.basic.DeptDataVO;
import org.security.trans.common.service.IFileService;
import org.security.ht.vo.common.UploadVO;
import org.security.trans.outsourcing.dto.OutCompanyDTO;
import org.security.trans.outsourcing.entity.OutCompany;
import org.security.trans.outsourcing.entity.OutEmployee;
import org.security.trans.outsourcing.mapper.OutCompanyMapper;
import org.security.trans.outsourcing.service.IOutCompanyService;
import org.security.trans.outsourcing.vo.OutCompanyVO;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

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

/**
 * 外包公司表 服务实现类
 *
 * @author BladeX
 * @since 2022-02-21
 */
@Service
@Slf4j
@AllArgsConstructor
public class OutCompanyServiceImpl extends ServiceImpl<OutCompanyMapper, OutCompany> implements IOutCompanyService {
	private final IDepartmentService departmentService;
	private final IFileService fileService;

	@Override
	public IPage<OutCompanyVO> selectOutCompanyPage(IPage<OutCompanyVO> page, OutCompanyDTO outCompany) {
//		List<String> collect = Arrays.stream(outCompany.getSupperOrganizationName().split(",")).collect(Collectors.toList());
//		outCompany.setSupperOrganizationCode(collect.get(collect.size() - 1));
		return page.setRecords(baseMapper.selectOutCompanyPage(page, outCompany));
	}

	@Override
	public boolean saveOutCompany(OutCompanyVO outCompanyVO){
		OutCompany outCompany = setOutCompany(outCompanyVO);
		DeptDataVO department = departmentService.getDepartmentById(Long.valueOf(AuthUtil.getDeptId()));
		outCompany.setSupperOrganizationCode(String.valueOf(AuthUtil.getDeptId()));
		outCompany.setSupperOrganizationName(department.getFullName());
		outCompany.setDelFlag(Constant.DELETE_FLAG_0);
		this.save(outCompany);
		List<UploadVO> fileList = outCompanyVO.getFileList();
		if (Objects.nonNull(fileList) && !fileList.isEmpty()) {
			fileService.saveAttachments(fileList, outCompany.getId());
		}
		List<UploadVO> licenseList = outCompanyVO.getLicenseList();
		if (Objects.nonNull(licenseList) && !licenseList.isEmpty()) {
			fileService.saveAttachments(licenseList, outCompany.getId());
		}
		List<UploadVO> qualificationsList = outCompanyVO.getQualificationsList();
		if (Objects.nonNull(qualificationsList) && !qualificationsList.isEmpty()) {
			fileService.saveAttachments(qualificationsList, outCompany.getId());
		}
		return true;
	}

	private OutCompany setOutCompany(OutCompanyVO outCompanyVO) {
		OutCompany outCompany = Func.copy(outCompanyVO, OutCompany.class);
		outCompany.setDeptId(Long.parseLong(AuthUtil.getDeptId()));
		return outCompany;
	}

	@Override
	public boolean updateOutCompany(OutCompanyVO outCompanyVO){
		List<UploadVO> fileList = outCompanyVO.getFileList();
		if (Objects.nonNull(fileList) && !fileList.isEmpty()) {
			fileService.saveAttachments(fileList, outCompanyVO.getId());
		}
		List<UploadVO> licenseList = outCompanyVO.getLicenseList();
		if (Objects.nonNull(licenseList) && !licenseList.isEmpty()) {
			fileService.saveAttachments(licenseList, outCompanyVO.getId());
		}
		List<UploadVO> qualificationsList = outCompanyVO.getQualificationsList();
		if (Objects.nonNull(qualificationsList) && !qualificationsList.isEmpty()) {
			fileService.saveAttachments(qualificationsList, outCompanyVO.getId());
		}
		return this.updateById(outCompanyVO);
	}

	@Override
	public List<OutCompanyVO> getOutCompany() {
		LambdaQueryWrapper<OutCompany> wrapper = new QueryWrapper<OutCompany>().lambda().eq(OutCompany::getDelFlag, DELETE_FLAG_0);
		return baseMapper.getOutCompany(wrapper);
	}

	@Override
	public OutCompanyVO getDetail(String id) {
		OutCompany detail = this.getById(id);
		//获取营业执照附件
		List<UploadVO> licenseList = fileService.getAttachments(id,detail.getGroupNameLicense());
		//获取资质和其他附件
		List<UploadVO> quaList = fileService.getAttachments(id,detail.getGroupNameQualifications());
		OutCompanyVO outCompanyVO = Func.copy(detail, OutCompanyVO.class);
		outCompanyVO.setLicenseList(licenseList);
		outCompanyVO.setQualificationsList(quaList);
		return outCompanyVO;
	}

	@Override
	public R removeCompanys(List<Long> toLongList) {
		if (Func.isNotEmpty(toLongList)) {
			toLongList.forEach(item -> {
				String companyId = String.valueOf(item);
				//查询该公司相关的未删除的人员
				List<OutEmployee> outEmployeeList = baseMapper.selectOutEmployees(companyId);
				if(outEmployeeList.size()>0){
					log.info("=====相关方【{}】存在人员信息，无法删除",item);
					throw new ServiceException("当前相关方存在人员信息，无法删除");
				}
				//查询该公司相关的未删除的考评记录
				List<Assessment> assessmentList = baseMapper.selectOutCompanyAssessment(companyId);
				if(assessmentList.size()>0){
					log.info("=====相关方【{}】存在考核评价记录，无法删除",item);
					throw new ServiceException("当前相关方存在考核评价记录，无法删除");
				}
			});
		}
		return R.status(this.removeByIds(toLongList));
	}
}
