package com.microservices.zone.common.controller;

import com.microservices.common.core.web.controller.BaseController;
import com.microservices.common.core.web.domain.AjaxResult;
import com.microservices.common.core.web.domain.GenericsAjaxResult;
import com.microservices.common.core.web.page.GenericsTableDataInfo;
import com.microservices.common.log.annotation.Log;
import com.microservices.common.log.enums.BusinessType;
import com.microservices.common.security.annotation.RequiresPermissions;
import com.microservices.common.security.utils.SecurityUtils;
import com.microservices.zone.detail.domain.vo.ZoneFrontRoleVo;
import com.microservices.zone.detail.service.IZoneDetailService;
import com.microservices.zone.resource.domain.ZoneResource;
import com.microservices.zone.resource.domain.vo.ZoneResourceDataVo;
import com.microservices.zone.resource.domain.vo.ZoneResourceInputVo;
import com.microservices.zone.resource.domain.vo.ZoneResourceSearchVo;
import com.microservices.zone.resource.domain.vo.ZoneResourceUpdateVo;
import com.microservices.zone.resource.service.IZoneResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author otto
 */
@RestController
@RequestMapping("/zoneFront")
@Api(tags = "特色专区前台接口")
public class FrontController extends BaseController {

    @Autowired
    private IZoneResourceService zoneResourceService;

    @Autowired
    private IZoneDetailService zoneDetailService;

    /**
     * 查询我的资源列表
     */
    @RequiresPermissions("zone:front:resource:myList")
    @GetMapping("/myResourceList")
    @ApiOperation(value = "查询我的资源列表")
    public GenericsTableDataInfo<ZoneResourceDataVo> myResourceList(ZoneResourceSearchVo zoneResourceSearchVo) {
        ZoneResource zoneResourceSearch = zoneResourceSearchVo.toZoneResource();
        zoneResourceSearch.setCreateBy(SecurityUtils.getUsername());
        startPage();
        return zoneResourceService.selectMyZoneResourceList(zoneResourceSearch);
    }

    /**
     * 查询我的资源详情
     */
    @RequiresPermissions("zone:front:resource:query")
    @GetMapping("/myResource/{id}")
    @ApiOperation(value = "查询我的资源详情")
    public GenericsAjaxResult<ZoneResourceDataVo> myResource(@PathVariable("id") Long id) {
        return genericsSuccess(zoneResourceService.selectZoneResourceDataById(id));
    }

    /**
     * 新增资源
     */
    @RequiresPermissions("zone:front:resource:add")
    @Log(title = "专区前台资源操作", businessType = BusinessType.INSERT)
    @PostMapping("/addResource")
    @ApiOperation(value = "新增资源")
    public AjaxResult addResource(@RequestBody ZoneResourceInputVo zoneResourceInputVo) {
        return success(zoneResourceService.insertZoneResource(zoneResourceInputVo));
    }

    /**
     * 编辑资源
     */
    @RequiresPermissions("zone:front:resource:edit")
    @Log(title = "专区前台资源操作", businessType = BusinessType.UPDATE)
    @PutMapping("/editResource")
    @ApiOperation(value = "编辑资源")
    public AjaxResult editResource(@RequestBody ZoneResourceUpdateVo zoneResourceUpdateVo) {
        return success(zoneResourceService.updateZoneResource(zoneResourceUpdateVo));
    }

    /**
     * 删除资源
     */
    @RequiresPermissions("zone:front:resource:remove")
    @Log(title = "专区前台资源操作", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeResource/{ids}")
    @ApiOperation(value = "删除资源")
    public AjaxResult removeResource(@PathVariable Long[] ids) {
        return toAjax(zoneResourceService.deleteMyZoneResourceByIds(ids));
    }

    /**
     * 检查当前登录用户在当前专区下角色
     */
    @GetMapping("/{zoneId}/checkCurrentRole")
    @ApiOperation(value = "检查当前登录用户在当前专区下权限",
            notes = "1、当权限为Manager时，后台返回资源管理访问地址和资讯管理访问地址，前台可提供（资源管理/资讯管理）按钮跳转到对应的后台地址\n" +
                    "2、当权限为Member时，用户可在前台操作资源和资讯\n" +
                    "3、当权限为None时，不展示前台操作按钮\n" +
                    "4、检测到用户登录后才能调用该接口，否则不展示前台操作按钮")
    public GenericsAjaxResult<ZoneFrontRoleVo> checkCurrentRole(@PathVariable Long zoneId) {
        return genericsSuccess(zoneDetailService.checkCurrentRoleInZone(zoneId));
    }
}
