package com.sysu.admin.support.system.role;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Objects;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import com.sysu.admin.support.base.BaseVo;
import com.sysu.admin.support.base.ServiceContext;
import com.sysu.admin.support.system.menu.SMenu;
import com.sysu.admin.support.system.menu.SMenuRepository;
import com.sysu.admin.support.system.menu.SMenuService;
import com.xiesx.fastboot.base.pagination.PaginationHelper;
import com.xiesx.fastboot.base.pagination.PaginationResult;
import com.xiesx.fastboot.base.pagination.PaginationVo;
import com.xiesx.fastboot.base.result.BaseResult;
import com.xiesx.fastboot.base.result.R;
import com.sysu.admin.support.system.role_permission.PKRP;
import com.sysu.admin.support.system.role_permission.RolePermission;
import com.sysu.admin.support.system.role_permission.RolePermissionRepository;
import com.sysu.admin.support.system.role_permission.RolePermissionService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @title RoleController
 * @description
 * @author XIE
 * @date 2020年4月24日下午10:07:33
 */
@Controller
@RequestMapping("/role")
public class SRoleController extends ServiceContext {

    @Autowired
    SRoleRepository mRoleRepository;

    @Autowired
    RolePermissionRepository mRolePermissionRepository;

    @Autowired
    SMenuRepository mMenuRepository;

    @Autowired
    SMenuService mMenuService;

    @Autowired
    RolePermissionService mRolePermissionService;

    /**
     * 页面
     *
     * @return
     */
    @RequiresPermissions("rule:view")
    @RequestMapping(value = "index")
    public String index(Model model) {
        return "system/role/index";
    }

    /**
     * 分页
     *
     * @param base
     * @param page
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "page")
    @RequiresPermissions("rule:view")
    public PaginationResult page(BaseVo base, PaginationVo page) {
        // 对象
        QSRole qRole = QSRole.sRole;
        // 条件
        Predicate predicate = qRole.id.isNotNull();
        if (ObjectUtils.isNotEmpty(base.getKeyword())) {
            Predicate p1 = qRole.id.like("%" + base.getKeyword() + "%");
            Predicate p2 = qRole.name.likeIgnoreCase("%" + base.getKeyword() + "%");
            Predicate p3 = qRole.description.likeIgnoreCase("%" + base.getKeyword() + "%");
            predicate = ExpressionUtils.and(predicate, ExpressionUtils.anyOf(p1, p2, p3));
        }
        // 排序
        Sort sort = Sort.by(Direction.ASC, SRole.FIELDS.sort);
        // 分页
        Pageable pageable = PageRequest.of(page.getPage(), page.getLimit(), sort);
        // 查询数据
        Page<SRole> data = mRoleRepository.findAll(predicate, pageable);
        // 构造
        return PaginationHelper.create(data);
    }

    /**
     * 详情
     *
     * @param base
     * @param model
     * @return @
     */
    @RequestMapping(value = "form")
    @RequiresPermissions("rule:view")
    public String form(BaseVo base, Model model) {
        SRole role = new SRole().setId(base.getId());
        if (!Objects.equal(role.getId(), -1L)) {
            role = mRoleRepository.findOne(role.getId());
        }
        model.addAttribute("role", role);
        model.addAttribute("json", role.toJSONString());
        model.addAttribute("rolePermissions", JSON.toJSONString(mRolePermissionService.getIds(base.getId())));
        model.addAttribute("menus", JSON.toJSONString(mMenuService.getMenuTree()));
        return "system/role/form";
    }

    /**
     * 添加
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "submit")
    @RequiresPermissions("rule:update")
    public BaseResult submit(BaseVo base, SRole req) {
        // 删除所有
        List<RolePermission> rolePermissions = mRolePermissionRepository.getListByRole(base.getId());
        for (RolePermission rolePermission : rolePermissions) {
            mRolePermissionRepository.delete(rolePermission);
        }
        // 更新权限
        for (Long permissionId : base.getIds()) {
            SMenu menu = mMenuRepository.findOne(permissionId);
            RolePermission rp = new RolePermission().setPk(new PKRP().roles(base.getId()).menu(menu.getId()));
            rp.setPermission(menu.getPermission());
            mRolePermissionRepository.save(rp);
        }
        return R.succ();
    }
}
