package com.rwk.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Map;
import com.rwk.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.rwk.system.domain.RwkFamilyMember;
import com.rwk.system.mapper.RwkStaffInformationMapper;
import com.rwk.system.domain.RwkStaffInformation;
import com.rwk.system.service.IRwkStaffInformationService;
import com.rwk.common.core.text.Convert;

/**
 * 人员信息Service业务层处理
 *
 * @author rick
 * @date 2024-11-25
 */
@Service
public class RwkStaffInformationServiceImpl implements IRwkStaffInformationService
{
    @Autowired
    private RwkStaffInformationMapper rwkStaffInformationMapper;

    /**
     * 查询人员信息
     *
     * @param id 人员信息主键
     * @return 人员信息
     */
    @Override
    public RwkStaffInformation selectRwkStaffInformationById(Long id)
    {
        return rwkStaffInformationMapper.selectRwkStaffInformationById(id);
    }

    /**
     * 查询人员信息列表
     *
     * @param rwkStaffInformation 人员信息
     * @return 人员信息
     */
    @Override
    public List<RwkStaffInformation> selectRwkStaffInformationList(RwkStaffInformation rwkStaffInformation)
    {
        return rwkStaffInformationMapper.selectRwkStaffInformationList(rwkStaffInformation);
    }

    /**
     * 新增人员信息
     *
     * @param rwkStaffInformation 人员信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertRwkStaffInformation(RwkStaffInformation rwkStaffInformation)
    {
        int rows = rwkStaffInformationMapper.insertRwkStaffInformation(rwkStaffInformation);
        insertRwkFamilyMember(rwkStaffInformation);
        return rows;
    }

    /**
     * 修改人员信息
     *
     * @param rwkStaffInformation 人员信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateRwkStaffInformation(RwkStaffInformation rwkStaffInformation)
    {
        rwkStaffInformationMapper.deleteRwkFamilyMemberByFamilyMemberCode(rwkStaffInformation.getId());
        insertRwkFamilyMember(rwkStaffInformation);
        return rwkStaffInformationMapper.updateRwkStaffInformation(rwkStaffInformation);
    }

    /**
     * 批量删除人员信息
     *
     * @param ids 需要删除的人员信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRwkStaffInformationByIds(String ids)
    {
        rwkStaffInformationMapper.deleteRwkFamilyMemberByFamilyMemberCodes(Convert.toStrArray(ids));
        return rwkStaffInformationMapper.deleteRwkStaffInformationByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除人员信息信息
     *
     * @param id 人员信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRwkStaffInformationById(Long id) {
        rwkStaffInformationMapper.deleteRwkFamilyMemberByFamilyMemberCode(id);
        return rwkStaffInformationMapper.deleteRwkStaffInformationById(id);
    }

    /**
     * 新增家庭成员信息
     *
     * @param rwkStaffInformation 人员信息对象
     */
    public void insertRwkFamilyMember(RwkStaffInformation rwkStaffInformation)
    {
        List<RwkFamilyMember> rwkFamilyMemberList = rwkStaffInformation.getRwkFamilyMemberList();
        String familyMemberCode = rwkStaffInformation.getFamilyMemberCode();
        if (StringUtils.isNotNull(rwkFamilyMemberList)) {
            List<RwkFamilyMember> list = new ArrayList<>();
            for (RwkFamilyMember rwkFamilyMember : rwkFamilyMemberList) {
                if (emptyJudge(rwkFamilyMember)) {
                    rwkFamilyMember.setFamilyMemberCode(familyMemberCode);
                    list.add(rwkFamilyMember);
                }
            }
            if (!list.isEmpty()) {
                rwkStaffInformationMapper.batchRwkFamilyMember(list);
            }
        }
    }

    @Override
    public List<Map<String, Object>> selectTotalByJob() {
        return rwkStaffInformationMapper.selectTotalByJob();
    }

    @Override
    public List<Map<String, Object>> selectTotalByParty() {
        return rwkStaffInformationMapper.selectTotalByParty();
    }

    @Override
    public List<Map<String, Object>> selectTotalByAge() {
        return rwkStaffInformationMapper.selectTotalByAge();
    }

    @Override
    public List<Map<String, Object>> selectTotalByUnitAttribute() {
        return rwkStaffInformationMapper.selectTotalByUnitAttribute();
    }

    @Override
    public List<Map<String, Object>> selectTotalByQrzQualification() {
        return rwkStaffInformationMapper.selectTotalByQrzQualification();
    }

    @Override
    public List<Map<String, Object>> selectTotalByQrzDegree() {
        return rwkStaffInformationMapper.selectTotalByQrzDegree();
    }

    @Override
    public List<Map<String, Object>> selectTotalByZzQualification() {
        return rwkStaffInformationMapper.selectTotalByZzQualification();
    }

    @Override
    public List<Map<String, Object>> selectTotalByZzDegree() {
        return rwkStaffInformationMapper.selectTotalByZzDegree();
    }

    private boolean emptyJudge(RwkFamilyMember rwkFamilyMember){
        boolean flag = true;
        int emptyCount = 0;
        // 检查每个字段是否为空
        if (rwkFamilyMember.getName() == null || rwkFamilyMember.getName().isEmpty()) emptyCount++;
        if (rwkFamilyMember.getAppellation() == null || rwkFamilyMember.getAppellation().isEmpty()) emptyCount++;
        if (rwkFamilyMember.getBirthday() == null || rwkFamilyMember.getBirthday().isEmpty()) emptyCount++;
        if (rwkFamilyMember.getPoliticalStatus() == null || rwkFamilyMember.getPoliticalStatus().isEmpty()) emptyCount++;
        if (rwkFamilyMember.getUnitJob() == null || rwkFamilyMember.getUnitJob().isEmpty()) emptyCount++;
        if (emptyCount >= 3) {
            flag = false;
        }
        return flag;
    }
}
