package org.jeecg.modules.basedata.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.plugins.pagination.Page;
import com.google.common.collect.Maps;
import org.apache.commons.lang.StringUtils;
import org.jeecg.modules.basedata.entity.FdPersonnel;
import org.jeecg.modules.basedata.mapper.FdPersonnelMapper;
import org.jeecg.modules.basedata.service.IFdPersonnelService;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysRole;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @Description: 人员表
 * @author： jeecg-boot
 * @date：   2019-05-09
 * @version： V1.0
 */
@Service
@Transactional
public class FdPersonnelServiceImpl extends ServiceImpl<FdPersonnelMapper, FdPersonnel> implements IFdPersonnelService {

    @Resource
    public FdPersonnelMapper fdPersonnelMapper;

    @Resource
    IFdPersonnelService fdPersonnelService;
    
    @Resource
    ISysDepartService sysDepartService;

    @Resource
    ISysUserService iSysUserService;

    @Override
    public Map<String, Object> getPageFdPersonnel(Map<String, Object> mp) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("records",fdPersonnelMapper.getAllPage(mp));
        map.put("total",fdPersonnelMapper.getCount());
        map.put("current",mp.get("current"));
        return map;
    }

    /**
     * @Author gurui
     * @Description //TODO 根据ID进行查询
     * @Date 9:59 2019/5/10
     * @Param [id]
     * @return org.jeecg.modules.basedata.entity.FdPersonnel
     **/
    @Override
    public FdPersonnel getByIdPersonnel(String id) {
        return fdPersonnelMapper.getByIdPersonnel(id);
    }

    @Override
    public List<FdPersonnel> queryApprover() {
        return fdPersonnelMapper.queryApprover();
    }

    /***
     * @Title:根据角色id查询人员
            * @Description:TODO
            * @param roleId
            * @return java.util.List<org.jeecg.modules.basedata.entity.FdPersonnel>
            * @exception
            * @author 陈创
            * @date 2019/7/4 10:05
            * @updatehistory:
     *   2019/7/4 10:05 查询
            */
    @Override
    public List<FdPersonnel> getPersonnelByRole(String roleId) {
        return fdPersonnelMapper.getPersonnelByRole(roleId);
    }

    /***
     * @Title:查询部门下的全部人员
            * @Description:TODO
            * @return java.util.List<java.util.Map<java.lang.Object,java.lang.Object>>
            * @exception 
            * @author 陈创
            * @date 2019/7/16 12:55
            * @updatehistory:
     *   2019/7/16 12:55 新增
            */
    @Override
    public List<Map<Object, Object>> getPersonnelOrDepart() {
        //人员id
        List<FdPersonnel> fdPersonnelList =fdPersonnelService.list() ;
        //获取人员所对应的角色
        getRoleByPersonnelList(fdPersonnelList);
        //部门数据
        List<SysDepart> sysDepartList=new ArrayList<>();
        if(!fdPersonnelList.isEmpty()){
            //部门数据
            sysDepartList = sysDepartService.list();
        }

        List<Map<Object, Object>> mapList = new ArrayList<>();
        if (!sysDepartList.isEmpty()) {
            //循环部门
            for (SysDepart sysDepart : sysDepartList) {
                Map<Object, Object> map = new HashMap<>();
                //部门名称
                map.put("title", sysDepart.getDepartName());
                //部门id
                map.put("key", sysDepart.getId());
                //子级
                List<Map<Object, Object>> listChildMap = new ArrayList<>();
                //根据部门获取对应人员
                for (FdPersonnel fdPersonnel : fdPersonnelList) {
                    Map<Object, Object> childMap=new HashMap<>();
                    if (fdPersonnel.getPkDepart().equals(sysDepart.getId())) {
                        childMap.put("title", fdPersonnel.getName());
                        childMap.put("key", fdPersonnel.getId());
                        childMap.put("code",fdPersonnel.getCode());
                        childMap.put("name",fdPersonnel.getName());
                        childMap.put("roleName",fdPersonnel.getRoleName());
                        childMap.put("isLeaf",true);
                        listChildMap.add(childMap);
                    }
                }
                //部门下面的人员
                map.put("children", listChildMap);
                mapList.add(map);
            }
        }
        return mapList;
    }

    public void getRoleByPersonnelList(List<FdPersonnel> fdPersonnelList){
        List<SysRole> roleList=fdPersonnelMapper.getRoleBypersonnelList();

        for(FdPersonnel fdPersonnel:fdPersonnelList){
            //角色名称
            String roleName="";
            int i=0;
            //找到人员所对应的角色
            for(SysRole sysRole:roleList){
                if(fdPersonnel.getId().equals(sysRole.getPersonnelId())){
                    if(i==0){
                        roleName=sysRole.getRoleName();
                    }else{
                        roleName=roleName+","+sysRole.getRoleName();
                    }
                    i++;
                }
            }
            fdPersonnel.setRoleName(roleName);
        }

    }

    @Override
    public String queryNameByCode(String code) {
        return fdPersonnelMapper.queryNameByCode(code);
    }

    @Override
    public IPage<FdPersonnel> findByPage(Page<FdPersonnel> page, QueryWrapper<FdPersonnel> queryWrapper) {
        return fdPersonnelMapper.findByPage(page,queryWrapper);
    }



    @Override
    public boolean personnelUpdate(FdPersonnel fdPersonnel) {
        boolean update=false;
        int updateR=fdPersonnelMapper.updateById(fdPersonnel);
        if(updateR>0){
            update=true;
        }
        //同步用户状态
        updateUserStatus(fdPersonnel);
        return update;
    }

    //同步用户的状态
    public void updateUserStatus(FdPersonnel fdPersonnel){
        if(StringUtils.isNotBlank(fdPersonnel.getStatus())){
            //查询对应的用户
            LambdaQueryWrapper<SysUser> lambdaQueryWrapper=new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(SysUser::getDelFlag,"0");
            lambdaQueryWrapper.eq(SysUser::getPersonnelId,fdPersonnel.getId());
            SysUser sysUser=new SysUser();
            //如果在职修改为用户正常
            if(fdPersonnel.getStatus().equals("1")){
                sysUser.setStatus(1);
            }
            //如果离职修改为用户冻结
            if(fdPersonnel.getStatus().equals("2")){
                sysUser.setStatus(2);
            }
            iSysUserService.update(sysUser,lambdaQueryWrapper);
        }
    }

}
