package com.supervision.peopleManage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.supervision.common.annotation.DataScope;
import com.supervision.common.core.domain.entity.SysDept;
import com.supervision.grassrootsregistration.resultVo.SysDeptDto;
import com.supervision.peopleManage.domain.BdglPeople;
import com.supervision.peopleManage.domain.FamilyInformation;
import com.supervision.peopleManage.domain.dto.StatOutPeopleDto;
import com.supervision.peopleManage.mapper.BdglPeopleMapper;
import com.supervision.peopleManage.mapper.FamilyInformationMapper;
import com.supervision.peopleManage.service.IBdglPeopleService;
import com.supervision.print.domain.PrintsLog;
import com.supervision.system.mapper.SysDeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 人员管理Service业务层处理
 *
 * @author supervision
 * @date 2022-01-22
 */
@Service
public class BdglPeopleServiceImpl implements IBdglPeopleService
{
    @Autowired
    private BdglPeopleMapper bdglPeopleMapper;
    @Autowired
    private FamilyInformationMapper familyInformationMapper;

    @Resource
    private SysDeptMapper deptMapper;

    /**
     * 查询人员管理
     *
     * @param id 人员管理主键
     * @return 人员管理
     */
    @Override
    public BdglPeople selectBdglPeopleById(Long id)
    {
        /*----------------------------返回家庭信息------------------------------*/
        FamilyInformation familyInformation = new FamilyInformation();
        familyInformation.setPeopleId(id);
        List<FamilyInformation> familyInformations = familyInformationMapper.selectFamilyInformationList(familyInformation);
        
        /*----------------------------返回家庭信息------------------------------*/
        BdglPeople bdglPeople = bdglPeopleMapper.selectBdglPeopleById(id);
        if(familyInformations!=null&&familyInformations.size()>0&&bdglPeople!=null) {
        	bdglPeople.setFamilyInformation(familyInformations);
        }
        
        return bdglPeople;
    }

    /**
     * 权限查询数据列表
     * @param bdglPeople
     * @return
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<BdglPeople> selectPowerPeoleList(BdglPeople bdglPeople) {
        return bdglPeopleMapper.selectPowerPeoleList(bdglPeople);
    }

    /**
     * 查询人员管理列表
     *
     * @param bdglPeople 人员管理
     * @return 人员管理
     */
    @Override
    public List<BdglPeople> selectBdglPeopleList(BdglPeople bdglPeople)
    {
        return bdglPeopleMapper.selectBdglPeopleList(bdglPeople);
    }

    /**
     * 新增人员管理
     *
     * @param bdglPeople 人员管理
     * @return 结果
     */
    @Override
    public int insertBdglPeople(BdglPeople bdglPeople)
    {
        int i = bdglPeopleMapper.insertBdglPeople(bdglPeople);
        /*--------------------------添加家庭信息-----------------------------*/
        List<FamilyInformation> familyInformation = bdglPeople.getFamilyInformation();
        if(familyInformation!=null&&familyInformation.size() > 0){
            for (FamilyInformation information : familyInformation) {
                if(information.getName()!=null){
                    information.setPeopleId(bdglPeople.getId());
                    familyInformationMapper.insertFamilyInformation(information);
                }
            }
        }
        /*--------------------------添加家庭信息-----------------------------*/
        return i;
    }

    /**
     * 修改人员管理
     *
     * @param bdglPeople 人员管理
     * @return 结果
     */
    @Override
    public int updateBdglPeople(BdglPeople bdglPeople)
    {
        List<FamilyInformation> familyInformation = bdglPeople.getFamilyInformation();
        if(familyInformation!=null&&familyInformation.size() > 0){
            for (FamilyInformation information : familyInformation) {
                information.setPeopleId(bdglPeople.getId());
                familyInformationMapper.updateFamilyInformation(information);
                if(information.getId()==null){
                    information.setPeopleId(bdglPeople.getId());
                    familyInformationMapper.insertFamilyInformation(information);
                }
            }
        }
        return bdglPeopleMapper.updateBdglPeople(bdglPeople);
    }

    /**
     * 批量删除人员管理
     *
     * @param ids 需要删除的人员管理主键
     * @return 结果
     */
    @Override
    public int deleteBdglPeopleByIds(Long[] ids)
    {
        return bdglPeopleMapper.deleteBdglPeopleByIds(ids);
    }

    /**
     * 删除人员管理信息
     *
     * @param id 人员管理主键
     * @return 结果
     */
    @Override
    public int deleteBdglPeopleById(Long id)
    {
        return bdglPeopleMapper.deleteBdglPeopleById(id);
    }
    /**获取当前部门  在位 在编 未在编  人员情况*/
    @Override
    public Map<String, Object> getDeptXiangQi(BdglPeople bdglPeople) {
        return bdglPeopleMapper.getDeptXiangQi(bdglPeople);
    }

    /**
     * 根据人员状态和是否在编 获取人员数量
     * @param type 是否在编
     * @return
     */
    public List<Map<String, String>> selectPeopleByRegin(String type) {
        return  bdglPeopleMapper.selectPeopleByRegin(type);
    }
    //根据人员id获取人员姓名
    @Override
    public List<BdglPeople> selectname(String[] split) {
        return bdglPeopleMapper.selectname(split);
    }

    @Override
    public Map<String, Object> examinationCountss(BdglPeople bdglPeople) {
        return bdglPeopleMapper.examinationCountss(bdglPeople);
    }

    @Override
    public List<BdglPeople> selectBdglPeopleInfo(BdglPeople bdglPeople) {
        return bdglPeopleMapper.selectBdglPeopleInfo(bdglPeople);
    }
    /**查询当前部门及下属部门人员*/
    @Override
    public List<BdglPeople> selectPowerPeoleLists(BdglPeople bdglPeople) {
        return bdglPeopleMapper.selectPowerPeoleLists(bdglPeople);
    }
    @Override
    public List<BdglPeople> selectExaminationList(BdglPeople bdglPeople){
        return bdglPeopleMapper.selectExaminationList(bdglPeople);
    }

    @Override
    public List<HashMap<String, Object>> selectGroupBYPeope(BdglPeople bdglPeople) {
        return bdglPeopleMapper.selectGroupBYPeope(bdglPeople);
    }

    @Override
    public int selectTotalCountByDeptId(Long deptId) {
        return bdglPeopleMapper.selectTotalCountByDeptId(deptId);
    }

    @Override
    public int selectReginCountByDeptId(Long deptId) {
        return bdglPeopleMapper.selectReginCountByDeptId(deptId);
    }

    @Override
    public List<SysDeptDto> getPeopleList(String year) {
        //所有营级单位
        List<Map<String, SysDept>> mapList = deptMapper.selectAllCamp();
        //根据营级单位ID查询当前单位下是否有数据
        List<SysDeptDto> dtoList = new ArrayList<>();
        mapList.forEach(item -> {
            SysDeptDto deptDto = BeanUtil.mapToBean(item, SysDeptDto.class, false);
            int deptId = deptDto.getDeptId().intValue();
            int countByunitId = bdglPeopleMapper.selectBdglPeopleByunitId(deptId,year);
            if (countByunitId > 0) {
                dtoList.add(deptDto);
            }
        });
        return dtoList;
    }

    @Override
    public List<BdglPeople> getPeopleOnUnitList(Integer unitId) {
        BdglPeople bdglPeople = new BdglPeople();
        bdglPeople.setUnitId(unitId+"");
        return bdglPeopleMapper.selectBdglPeopleOnUnitList(bdglPeople);
    }

    @Override
    public int selectReginCount2ByDeptId(Long deptId) {
        return bdglPeopleMapper.selectReginCount2ByDeptId(deptId);
    }

    @Override
    public List<StatOutPeopleDto> selectReginByDeptId(Long deptId) {
        return bdglPeopleMapper.selectReginByDeptId(deptId);
    }

    @Override
    public List<BdglPeople> countTypeNumber( ) {
        return bdglPeopleMapper.countTypeNumber();
    }

    @Override
    public Map<String, Object> getDeptXiangQi2(BdglPeople bdglPeople) {
        return bdglPeopleMapper.getDeptXiangQi2(bdglPeople);
    }

    @Override
    public Map<String, Object> getDeptXiangQi3(BdglPeople bdglPeople) {
        return bdglPeopleMapper.getDeptXiangQi3(bdglPeople);
    }
    
    @Override
    public BdglPeople selectBdglPeople(String ename) {
        return bdglPeopleMapper.selectBdglPeople(ename);
    }
    
    @Override
    public List<String> selectPowerPeoleListes(String peopleId) {
        return bdglPeopleMapper.selectPowerPeoleListes(peopleId);
    }
    
    @Override
    public List<BdglPeople> selectPowerPeoleListsss(BdglPeople bdglPeople) {
        return bdglPeopleMapper.selectPowerPeoleListsss(bdglPeople);
    }
    
    
}
