package com.ycwl.basic.service.pc.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ycwl.basic.mapper.MenuMapper;
import com.ycwl.basic.mapper.RoleMapper;
import com.ycwl.basic.model.pc.menu.MenuNode;
import com.ycwl.basic.model.pc.role.req.AddOrUpdateRoleReqVO;
import com.ycwl.basic.model.pc.role.req.RoleListReqVO;
import com.ycwl.basic.model.pc.role.resp.RoleListRespVO;
import com.ycwl.basic.service.pc.RoleService;
import com.ycwl.basic.utils.ApiResponse;
import com.ycwl.basic.utils.SnowFlakeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.ycwl.basic.constant.PermissionConstant.ROLE_STATUS;


@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    MenuMapper menuMapper;

    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public ApiResponse<PageInfo<RoleListRespVO>> pageQuery(RoleListReqVO roleListReqVO) {
        PageHelper.startPage(roleListReqVO.getPageNum(),roleListReqVO.getPageSize());
//        if(roleListReqVO.getType()==null){
//            roleListReqVO.setType(0);
//        }
        List<RoleListRespVO> list = roleMapper.list(roleListReqVO);
        PageInfo<RoleListRespVO> pageInfo = new PageInfo(list);
        return ApiResponse.success(pageInfo);
    }

    @Override
    public ApiResponse list(RoleListReqVO roleListReqVO) {
//        if(roleListReqVO.getType()==null){
//            roleListReqVO.setType(0);
//        }
        List<RoleListRespVO> list = roleMapper.list(roleListReqVO);
        return ApiResponse.success(list);
    }

    @Override
    public ApiResponse addOrUpdate(AddOrUpdateRoleReqVO addOrUpdateRoleReqVO) {
        String id = addOrUpdateRoleReqVO.getId();
        if(StringUtils.isBlank(id)){
            String roleId = SnowFlakeUtil.getId();
            addOrUpdateRoleReqVO.setId(roleId);
            if(roleMapper.add(addOrUpdateRoleReqVO)>0){
                if (addOrUpdateRoleReqVO.getMenuIdList() != null && !addOrUpdateRoleReqVO.getMenuIdList().isEmpty()) {
                    menuMapper.addRoleMenu(roleId, addOrUpdateRoleReqVO.getMenuIdList());
                }
                return ApiResponse.success(null);
            }
        }else {
            if(roleMapper.update(addOrUpdateRoleReqVO)>0){
                menuMapper.deleteRoleMenuByRoleId(addOrUpdateRoleReqVO.getId());
                if (addOrUpdateRoleReqVO.getMenuIdList() != null && !addOrUpdateRoleReqVO.getMenuIdList().isEmpty()) {
                    menuMapper.addRoleMenu(addOrUpdateRoleReqVO.getId(), addOrUpdateRoleReqVO.getMenuIdList());
                }
                return ApiResponse.success(null);
            }
        }
        return ApiResponse.fail("失败");
    }

    @Override
    public ApiResponse delete(String id) {
        if(roleMapper.delete(id)>0){
            menuMapper.deleteRoleMenuByRoleId(id);
            return ApiResponse.success(null);
        }
        return ApiResponse.fail("失败");
    }

    @Override
    public ApiResponse updateReturnMenu(String id) {
        List<MenuNode> menuById = roleMapper.getMenuById(id);
        List<MenuNode> MenuList = new ArrayList<>();
        for (MenuNode item :menuById) {
            if ("-1".equals(item.getParentId())) {
                MenuList.add(item);
            }
            for (MenuNode item2 : menuById) {
                if (item2.getParentId().equals(item.getId())) {
                    if (item.getChildrenList() == null) {
                        item.setChildrenList(new ArrayList<>());
                    }
                    item.getChildrenList().add(item2);
                }
            }
        }
        return ApiResponse.success(MenuList);
    }

    @Override
    public ApiResponse updateStatus(String id) {
        if(roleMapper.updateStatus(id)>0){
            redisTemplate.opsForValue().set(ROLE_STATUS+id,roleMapper.getRoleStatus(id));
            return ApiResponse.success(null);
        }
        return ApiResponse.fail("失败");
    }

}
