package com.org.hktshop.controller.Admin;

import com.org.hktshop.Interface.admin.IHktAdminResourceService;
import com.org.hktshop.base.BaseResult;
import com.org.hktshop.controller.BaseController;
import com.org.hktshop.entity.admin.HktAdminResource;
import com.org.hktshop.utils.HttpCode;
import com.org.hktshop.utils.Ret;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@Api(value = "管理员资源设置api")
public class HktAdminResourceController extends BaseController {

    @Autowired
    IHktAdminResourceService resourceService;

    /***
     * 资源设置
     * @param request
     * @return
     */
    @GetMapping("/async/resource/queryResource")
    public Object queryResource(HttpServletRequest request) {
        try {
            List<HktAdminResource> resources = resourceService.queryList(false);
            return BaseResult.successWithData(resources);
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }

    /****
     * 新增/修改资源文件
     * @param resource
     * @return
     */
    @PostMapping("/async/resource/updateResource")
    @ApiOperation(value = "新增/修改资源", notes = "新增/修改资源")
    public Object updateResource(@RequestBody HktAdminResource resource) {
        try {
            Ret ret = resourceService.updateResource(resource);
            if (ret.isFail()) return BaseResult.fail(HttpCode.BAD_REQUEST.getCode(),ret.getStr("msg"));
             return BaseResult.successWithData(HttpCode.BAD_REQUEST.getCode());
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }

    /****
     * 删除 管理员资源
     * @param resource
     * @return
     */
    @PostMapping("/async/resource/deleteResource")
    @ApiOperation(value = "删除资源", notes = "删除资源")
    public Object deleteLink(@RequestBody HktAdminResource resource) {
        try {
            Ret ret = resourceService.deleteResource(resource);
            if (ret.isFail())  return BaseResult.fail(HttpCode.BAD_REQUEST.getCode());
            return BaseResult.success();
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }
}
