package io.hmit.modules.fhjw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.hmit.common.page.PageData;
import io.hmit.common.service.impl.CrudServiceImpl;
import io.hmit.common.utils.ConvertUtils;
import io.hmit.modules.fhjw.dao.FhjwTempDao;
import io.hmit.modules.fhjw.dao.RoleDao;
import io.hmit.modules.fhjw.dto.FhiwFunctionDTO;
import io.hmit.modules.fhjw.dto.FhjwRoleFunctionDTO;
import io.hmit.modules.fhjw.dto.FhjwTempDTO;
import io.hmit.modules.fhjw.dto.RoleDTO;
import io.hmit.modules.fhjw.entity.FhjwTempEntity;
import io.hmit.modules.fhjw.entity.RoleEntity;
import io.hmit.modules.fhjw.service.FhiwFunctionService;
import io.hmit.modules.fhjw.service.FhjwRoleFunctionService;
import io.hmit.modules.fhjw.service.FhjwTempService;
import io.hmit.modules.fhjw.service.RoleService;
import io.hmit.modules.sys.dto.SysRoleDTO;
import io.hmit.modules.sys.entity.SysRoleEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.management.relation.Role;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 用户权限
 *
 * @author zsh 408538940@qq.com
 * @since 1.0.0 2021-06-03
 */
@Service
public class RoleServiceImpl extends CrudServiceImpl<RoleDao, RoleEntity, RoleDTO> implements RoleService {
    @Autowired
    private FhjwTempService fhjwTempService;

    @Autowired
    private FhjwRoleFunctionService fhjwRoleFunctionService;

    @Autowired
    private FhiwFunctionService fhiwFunctionService;

    @Autowired
    private RoleDao roleDao;

    @Override
    public QueryWrapper<RoleEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<RoleEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);


        return wrapper;
    }


    @Override
    public PageData<RoleDTO> page(Map<String, Object> params){
        IPage<RoleEntity> page = super.page2(params);

        page.getRecords().stream().forEach(item ->{
            Optional<FhjwTempEntity> temp = Optional.ofNullable(fhjwTempService.selectById(item.getTempId()));
            temp.ifPresent( q -> item.setTempName(q.getTempName()));

            Map<String, Object> query = new HashMap<>();
            query.put("roleId",item.getId());
            List<Long> funcIds = fhjwRoleFunctionService.list(query).stream()
                    .map(FhjwRoleFunctionDTO::getFunctionId).collect(Collectors.toList());

            String funcName ="";
            for(Long funcId:funcIds){
                FhiwFunctionDTO functionDTO = fhiwFunctionService.get(funcId);
                if("".equals(funcName)){
                    funcName = functionDTO.getName();
                }else{
                    funcName = funcName+ ","+functionDTO.getName();
                }

            }
            System.out.println("funcName:"+funcName);
            item.setFuncName(funcName);
        });
        return getPageData(page, currentDtoClass());
    }

    @Override
    public List<RoleDTO> getAllRoleList() {
        List<RoleEntity> entity = roleDao.getAllRoleList();
        return ConvertUtils.sourceToTarget(entity,RoleDTO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(RoleDTO dto) {

        RoleEntity entity = ConvertUtils.sourceToTarget(dto, RoleEntity.class);

        //保存角色
        insert(entity);

        //保存角色评价模块关系
        fhjwRoleFunctionService.saveOrUpdate(entity.getId(), dto.getFuncIdList());

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(RoleDTO dto) {

        RoleEntity entity = ConvertUtils.sourceToTarget(dto, RoleEntity.class);

        //更新角色
        updateById(entity);

        //保存角色评价模块关系
        fhjwRoleFunctionService.saveOrUpdate(entity.getId(), dto.getFuncIdList());

    }

    @Override
    public RoleDTO get(Long id){
        RoleEntity roleEntity = baseDao.selectById(id);
        RoleDTO roleDTO = ConvertUtils.sourceToTarget(roleEntity, RoleDTO.class);

        Map<String, Object> params = new HashMap<>();
        params.put("roleId",roleEntity.getId());
        List<Long> funcIds = fhjwRoleFunctionService.list(params).stream()
                .map(FhjwRoleFunctionDTO::getFunctionId).collect(Collectors.toList());
        roleDTO.setFuncIdList(funcIds);
        return roleDTO;
    }

    @Override
    public Long getIdByRoleName(String roleName) {
        Long roleId = baseDao.selectOne(new QueryWrapper<RoleEntity>().eq("name",roleName)).getId();
        return roleId;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long[] ids) {
        baseDao.deleteBatchIds(Arrays.asList(ids));

        Arrays.stream(ids).forEach(id ->{
            fhjwRoleFunctionService.deleteByRoleIds(id);
        });

    }


}
