package com.lz.modules.flow.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lz.common.utils.StringUtil;
import com.lz.modules.app.dao.DepartmentsDao;
import com.lz.modules.app.entity.DepartmentsEntity;
import com.lz.modules.app.entity.DepartmentsStaffRelateEntity;
import com.lz.modules.app.entity.StaffEntity;
import com.lz.modules.app.service.DepartmentsService;
import com.lz.modules.app.service.DepartmentsStaffRelateService;
import com.lz.modules.app.service.StaffService;
import com.lz.modules.flow.dao.RecordAuthMapper;
import com.lz.modules.flow.entity.AuthDto;
import com.lz.modules.flow.entity.RecordAuth;
import com.lz.modules.flow.model.Auth;
import com.lz.modules.flow.model.StaffRoleDto;
import com.lz.modules.flow.service.RecordAuthService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* <p>
* 审批权限表 服务类
* </p>
*
* @author quyixiao
* @since 2020-08-18
*/

@Service
public class RecordAuthServiceImpl extends ServiceImpl<RecordAuthMapper, RecordAuth> implements RecordAuthService {


    @Autowired
	private RecordAuthMapper recordAuthMapper;

    @Autowired
	private StaffService staffService;

    @Autowired
	private DepartmentsStaffRelateService departmentsStaffRelateService;

    @Autowired
	private DepartmentsService departmentsService;

    @Autowired
	private DepartmentsDao departmentsDao;

	@Override
	public RecordAuth selectRecordAuthById(Long id){
		return recordAuthMapper.selectRecordAuthById(id);
	}



	@Override
	public Long insertRecordAuth(RecordAuth recordAuth){
		return recordAuthMapper.insertRecordAuth(recordAuth);
	}



	@Override
	public int updateRecordAuthById(RecordAuth recordAuth){
		return recordAuthMapper.updateRecordAuthById(recordAuth);
	}



	@Override
	public int updateCoverRecordAuthById(RecordAuth recordAuth){
		return recordAuthMapper.updateCoverRecordAuthById(recordAuth);
	}

	@Override
	public int deleteRecordAuthById(Long id){
		return recordAuthMapper.deleteRecordAuthById(id);
	}

    @Override
    public List<RecordAuth> selectAuthInfo(Long roleId) {
        return recordAuthMapper.selectAuthInfo(roleId);
    }

	@Override
	public Map<Long, Long> selectRoleIdByStaffRoleInfo(String flowStaffIdRole) {
		Map<Long, Long> staffEntityMap = new HashMap<>();
		if (StringUtil.isNotBlank(flowStaffIdRole)) {
			List<StaffRoleDto> list = JSONObject.parseArray(flowStaffIdRole, StaffRoleDto.class);
			if (CollectionUtils.isNotEmpty(list)) {
				staffEntityMap = list.stream().collect(Collectors.toMap(StaffRoleDto::getStaffId, StaffRoleDto::getRoleId));
			}
		}
		return staffEntityMap;
	}

	@Override
	public Long getRoleIdByStaffRoleInfo(String flowStaffIdRole,Long approvalStaffId) {
		if (StringUtil.isNotBlank(flowStaffIdRole)) {
			List<StaffRoleDto> list = JSONObject.parseArray(flowStaffIdRole, StaffRoleDto.class);
			if (CollectionUtils.isNotEmpty(list)) {
				for(StaffRoleDto staffRoleDto:list){
					if(staffRoleDto.getStaffId().equals(approvalStaffId)){
						return staffRoleDto.getRoleId();
					}
				}
			}
		}
		return 0l;
	}

    @Override
    public Map<String, Integer> selectAuthByRoleIds(List<Long> roleIds) {
		Map<String,Integer> map = new HashMap<>();
		List<AuthDto> list =  recordAuthMapper.selectAuthByRoleIds(roleIds);
		if(CollectionUtils.isNotEmpty(list)){
			for(AuthDto authDto:list){
				map.put(authDto.getIdentity(),authDto.getStatus());
			}
		}
		return map;
    }

	@Override
	public Map<String, Integer> selectAuthByIds(List<Long> authIds) {
		Map<String,Integer> map = new HashMap<>();
		List<AuthDto> list =  recordAuthMapper.selectAuthByIds(authIds);
		if(CollectionUtils.isNotEmpty(list)){
			for(AuthDto authDto:list){
				map.put(authDto.getIdentity(),authDto.getStatus());
			}
		}
		return map;
	}


	public Auth getAuth(List<RecordAuth> auths) {
		Map<String, Integer> map = new HashMap<>();
		if (CollectionUtils.isNotEmpty(auths)) {
			for(RecordAuth recordAuth : auths){
				map.put(recordAuth.getIdentity(),recordAuth.getStatus());
			}
		}
		return JSONObject.parseObject(JSON.toJSONString(map),Auth.class);
	}


	@Override
	public String selectByStaffId(Long staffId) {
		DepartmentsStaffRelateEntity departmentsStaffRelateEntity = departmentsStaffRelateService.selectLastDepartmentByStaffId(staffId);
		if (departmentsStaffRelateEntity.getIsLeader().equals(1)) {//如果是部门领导
			DepartmentsEntity department = departmentsService.selectByDepartmentId(departmentsStaffRelateEntity.getDepartmentId());
			if (department != null) {
				DepartmentsStaffRelateEntity parentRelation = departmentsStaffRelateService.selectDepartmentByDepartmentId(department.getDepartmentParentId());
				if (parentRelation != null) {
					StaffEntity staffEntity = staffService.selectStaffById(parentRelation.getStaffId());
					return staffEntity.getName();
				}
			}
			return "";
		} else {
			DepartmentsStaffRelateEntity leader = departmentsStaffRelateService.selectLeaderByDepartmentId(departmentsStaffRelateEntity.getDepartmentId());
			if (leader != null) {
				StaffEntity staffEntity = staffService.selectStaffById(leader.getStaffId());
				return staffEntity.getName();
			} else {
				Map<String, String> map = departmentsService.selectUserAllDepartmentInFo(departmentsStaffRelateEntity.getDepartmentId());
				if (StringUtil.isNotBlank(map.get("dd2"))) {
					StaffEntity staffEntity = getLeader(map.get("dd2"));
					if (staffEntity != null) {
						return staffEntity.getName();
					}
				}
				if (StringUtil.isNotBlank(map.get("dd3"))) {
					StaffEntity staffEntity = getLeader(map.get("dd3"));
					if (staffEntity != null) {
						return staffEntity.getName();
					}
				}
			}
			return "";
		}
	}

	@Override
	public List<RecordAuth> selectAll() {

		return recordAuthMapper.selectAll();
	}

	public StaffEntity getLeader(String departmentId){
		DepartmentsStaffRelateEntity leader = departmentsStaffRelateService.selectLeaderByDepartmentId(departmentId);
		if(leader !=null && leader.getIsLeader().equals(1)){
			StaffEntity staffEntity = staffService.selectStaffById(leader.getStaffId());
			return staffEntity;
		}
		return null;
	}


}
