package com.lz.modules.app.controller;

import com.lz.common.utils.NumberUtil;
import com.lz.common.utils.PageUtils;
import com.lz.common.utils.R;
import com.lz.modules.flow.entity.FlowDepartment;
import com.lz.modules.flow.entity.RecordAuth;
import com.lz.modules.flow.entity.RecordRole;
import com.lz.modules.flow.entity.RecordRoleAuth;
import com.lz.modules.flow.service.RecordAuthService;
import com.lz.modules.flow.service.RecordRoleAuthService;
import com.lz.modules.flow.service.RecordRoleService;
import com.lz.modules.sys.entity.SysMenuEntity;
import com.lz.modules.sys.entity.SysRoleEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;


/**
 * 记录表
 *
 * @author zgh
 * @email zgh@ldxinyong.com
 * @date 2020-09-18 16:33:18
 */
@RestController
@RequestMapping("user/lzrecordrole")
public class RecordRoleController {
    @Autowired
    private RecordRoleService lzRecordRoleService;

    @Autowired
    private RecordAuthService recordAuthService;

    @Autowired
    private RecordRoleAuthService recordRoleAuthService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("user:lzrecordrole:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = lzRecordRoleService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        SysRoleEntity  role = lzRecordRoleService.getRole(id);
        return R.ok().put("role", role);
    }

    @RequestMapping("/menu/list")
    public List<SysMenuEntity> menuList() {
        List<SysMenuEntity> list = lzRecordRoleService.selectMenuList();
        return list;
    }

    /**
     * 修改角色
     */
    @PostMapping("/updateOrAdd")
    public R update(@RequestBody SysRoleEntity role) {
        RecordRole recordRole = null;
        if (role.getRoleId() != null) {
            recordRole = lzRecordRoleService.getById(role.getRoleId());
            recordRole.setName(role.getRoleName());
            lzRecordRoleService.updateRecordRoleById(recordRole);
        }else{
            recordRole = new RecordRole();
            recordRole.setName(role.getRoleName());
            lzRecordRoleService.insertRecordRole(recordRole);
        }

        List<RecordRoleAuth> recordRoleAuths = recordRoleAuthService.selectByRoleId(role.getRoleId());
        List<Long> menuIds = role.getMenuIdList();
        for (RecordRoleAuth roleAuth : recordRoleAuths) {
            if (!menuIds.contains(roleAuth.getAuthId())) {
                recordRoleAuthService.deleteRecordRoleAuth(roleAuth.getId());
            }
        }
        recordRoleAuths = recordRoleAuthService.selectByRoleId(role.getRoleId());
        Map<Long, RecordRoleAuth> staffEntityMap = new HashMap<>();
        for(RecordRoleAuth auth : recordRoleAuths){
            staffEntityMap.put(auth.getAuthId(),auth);
        }
        for(Long menuId:menuIds){
            if(menuId <= 0 || (menuId.toString().endsWith("000000"))){
                continue;
            }
            RecordRoleAuth recordRoleAuth = staffEntityMap.get(menuId);
            if(recordRoleAuth == null){
                recordRoleAuth = new RecordRoleAuth();
                recordRoleAuth.setRoleId(recordRole.getId());
                recordRoleAuth.setAuthId(menuId);
                recordRoleAuthService.insertRecordRoleAuth(recordRoleAuth);
            }
        }
        return R.ok();
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("user:lzrecordrole:save")
    public R save(@RequestBody RecordRole lzRecordRole) {
        lzRecordRoleService.insertRecordRole(lzRecordRole);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("user:lzrecordrole:update")
    public R update(@RequestBody RecordRole lzRecordRole) {
        lzRecordRoleService.updateRecordRoleById(lzRecordRole);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("user:lzrecordrole:delete")
    public R delete(@RequestBody Long[] ids) {
        lzRecordRoleService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }

}
