package com.judong.chuanyiserver.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.judong.chuanyiserver.dao.PermissionDao;
import com.judong.chuanyiserver.entity.Permission;
import com.judong.chuanyiserver.enums.ResultEnum;
import com.judong.chuanyiserver.service.PermissionService;
import com.judong.chuanyiserver.util.Blank;
import com.judong.chuanyiserver.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;

    @Override
    public synchronized Result addPermission(Permission permission) {
        if (Blank.isEmpty(permission.getPermissionUrl()) || Blank.isEmpty(permission.getPermissionName())) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "权限路径和名称不能为空");
        }
        if (Blank.isNotEmpty(permissionDao.getPermissionByUrl(permission.getPermissionUrl()))) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "该路径已分配权限，请更换其他路径");
        }
        if (permissionDao.addPermission(permission) <= 0) {
            return Result.no(ResultEnum.SERVER_ERROR.getRespCode(), "新增权限失败");
        }
        return Result.ok("新增资源权限成功");
    }

    @Override
    public synchronized Result updatePermission(Permission permission) {
        if (Blank.isEmpty(permission.getId()) || Blank.isEmpty(permission.getPermissionUrl()) || Blank.isEmpty(permission.getPermissionName())) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "权限id，路径,名称不能为空");
        }
        if (Blank.isNotEmpty(permissionDao.getPermissionByNoIdUrl(permission.getId(), permission.getPermissionUrl()))) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "已存在该权限路径，请更换权限路径");
        }
        if (permissionDao.updatePermission(permission) <= 0) {
            return Result.no(ResultEnum.SERVER_ERROR.getRespCode(), "修改权限失败");
        }
        return Result.ok("修改权限成功");
    }

    @Override
    public Result getPermissionById(Integer id) {
        return Result.ok(permissionDao.getPermissionById(id));
    }

    @Override
    public Result getPermissionListByPage(Integer page, Integer limit, String permissionUrl, String permissionName) {
        JSONObject jsonObject = new JSONObject();
        Long count = permissionDao.getPermissionCount(permissionUrl, permissionName);
        Long startNum = Long.valueOf((page - 1) * limit);
        List<Permission> permissionList = permissionDao.getPermissionListByPage(startNum, Long.valueOf(limit), permissionUrl, permissionName);
        jsonObject.put("count", count);
        jsonObject.put("permissionList", permissionList);
        return Result.ok(jsonObject);
    }

    @Override
    public synchronized Result deletePermissionById(Integer id) {
        if (permissionDao.deletePermissionById(id) <= 0) {
            return Result.no(ResultEnum.SERVER_ERROR.getRespCode(), "删除权限失败");
        }
        permissionDao.deleteMenuPermissionById(id);
        return Result.ok("删除权限成功");
    }
}
