package com.huiju.estateagents.center.taUser.service.impl;

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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.center.taUser.entity.TaUser;
import com.huiju.estateagents.center.taUser.mapper.TaUserAuthButtonMapper;
import com.huiju.estateagents.center.taUser.mapper.TaUserMapper;
import com.huiju.estateagents.center.taUser.service.ITaUserAuthButtonService;
import com.huiju.estateagents.common.CommConstant;
import com.huiju.estateagents.entity.SysAdvancedConfigRelation;
import com.huiju.estateagents.entity.SysButtonInMenu;
import com.huiju.estateagents.entity.SysMenu;
import com.huiju.estateagents.entity.TaAdvancedController;
import com.huiju.estateagents.entity.TaRole;
import com.huiju.estateagents.entity.TaRoleButton;
import com.huiju.estateagents.mapper.SysAdvancedConfigRelationMapper;
import com.huiju.estateagents.mapper.TaAdvancedControllerMapper;
import com.huiju.estateagents.mapper.TaRoleButtonMapper;
import com.huiju.estateagents.mapper.TaRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author admin
 * @Title: TaUserAuthButtonServiceImpl
 * @Description: TODO
 * @date 2019/9/23
 */
@Service
public class TaUserAuthButtonServiceImpl extends ServiceImpl<TaUserAuthButtonMapper, SysButtonInMenu> implements ITaUserAuthButtonService {

    @Autowired
    TaUserAuthButtonMapper taUserAuthButtonMapper;
    @Autowired
    TaRoleButtonMapper taRoleButtonMapper;
    @Autowired
    private TaUserMapper taUserMapper;
    @Autowired
    private TaRoleMapper taRoleMapper;
    @Autowired
    private TaAdvancedControllerMapper taAdvancedControllerMapper;
    @Autowired
    private SysAdvancedConfigRelationMapper sysAdvancedConfigRelationMapper;

    @Override
    public ResponseBean getButtonByUserId(Integer pageNum, Integer pageSize, String userId, Integer orgId) {
        List<SysButtonInMenu> records = new ArrayList<>();
        List<SysButtonInMenu> menuList = taUserAuthButtonMapper.sysButtonListByUserId(userId);
        records.addAll(menuList);
        //添加高级配置的过滤条件
        List<TaAdvancedController> taAdvancedControllerList = taAdvancedControllerMapper.selectList(new QueryWrapper<TaAdvancedController>().eq("org_id", orgId));

        //需要过滤的菜单
        QueryWrapper<SysAdvancedConfigRelation> sysAdvancedConfigRelationQueryWrapper = new QueryWrapper<>();
        sysAdvancedConfigRelationQueryWrapper.eq("target_type", CommConstant.ADVANCED_TYPE_BUTTON);
        sysAdvancedConfigRelationQueryWrapper.notIn(taAdvancedControllerList.size() > 0,"advanced_config_id",taAdvancedControllerList.stream().map(TaAdvancedController::getAdvancedConfigId).collect(Collectors.toList()));
        List<SysAdvancedConfigRelation> sysAdvancedConfigRelations = sysAdvancedConfigRelationMapper.selectList(sysAdvancedConfigRelationQueryWrapper);
        menuList.forEach(e -> {
            sysAdvancedConfigRelations.forEach(s -> {
                if ((e.getBtnId()).equals(Integer.valueOf(s.getTargetId()))){
                    records.remove(e);
                }
            });
        });
        return ResponseBean.success(records);

    }

    @Override
    public ResponseBean getAllButton(Integer pageNum, Integer pageSize) {
        List<SysButtonInMenu> records = taUserAuthButtonMapper.sysButtonListByRoleId();
        records.forEach(e -> {
            List<TaRole> taRoles = taRoleMapper.getRolesByBtnId(e.getBtnId());
            e.setRolesList(taRoles);
        });

        return ResponseBean.success(records);
    }

    @Override
    public ResponseBean updateUserAuthButton(Integer userId, List<SysButtonInMenu> sysButtonInMenus) {
        ResponseBean responseBean = new ResponseBean();

        // 人员
        TaUser taUser = taUserMapper.selectById(userId);
        if (null == taUser) {
            responseBean.addError("授权人不存在");
            return responseBean;
        }


        try{
            //授权菜单按钮，先删除在新增
            taUserAuthButtonMapper.deleteUserAuthButton(userId);
            taUserAuthButtonMapper.addUserAuthButton(userId, sysButtonInMenus);
            responseBean.addSuccess("操作成功");
            return  responseBean;
        }catch (Exception e){
            responseBean.addSuccess("操作失败");
            e.printStackTrace();
            return responseBean;
        }

    }
}
