package com.microservices.zone.detail.controller;

import com.microservices.common.core.domain.R;
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.InnerAuth;
import com.microservices.common.security.annotation.RequiresPermissions;
import com.microservices.system.api.domain.SysDept;
import com.microservices.zone.detail.domain.vo.ZoneDetailDataVo;
import com.microservices.zone.detail.domain.vo.ZoneDetailSearchVo;
import com.microservices.zone.detail.domain.vo.ZoneDetailUpdateVo;
import com.microservices.zone.detail.service.IZoneDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 特色专区详情Controller
 *
 * @author otto
 * @date 2023-05-09
 */
@RestController
@RequestMapping("/zoneDetail")
@Api(tags = "特色专区详情接口")
public class ZoneDetailController extends BaseController {
    @Autowired
    private IZoneDetailService zoneDetailService;


    /**
     * 内部接口，新增组织时自动创建特色专区
     */
    @InnerAuth
    @PostMapping("/autoAddZoneByDept/{deptId}")
    @ApiOperation(value = "新增组织时自动创建GitLink项目", hidden = true)
    public R<Long> autoAddZoneByDept(@PathVariable(value = "deptId") Long deptId) {
        Long zoneId = zoneDetailService.insertZoneDetailByDept(deptId);
        return R.ok(zoneId);
    }

    /**
     * 内部接口，修改组织时自动更新特色专区
     */
    @InnerAuth
    @PutMapping("/autoUpdateZoneByDept")
    @ApiOperation(value = "修改组织时自动更新GitLink项目", hidden = true)
    public R<Boolean> autoUpdateZoneByDept(@RequestBody SysDept dept) {
        Boolean addResult = zoneDetailService.updateZoneDetailByDept(dept);
        return R.ok(addResult);
    }

    /**
     * 内部接口，通过专区Id获取专区详情JSON对象
     */
    @InnerAuth
    @GetMapping("/getZoneJSONByZoneId/{zoneId}")
    @ApiOperation(value = "通过专区Id获取专区详情JSON对象", hidden = true)
    public R<String> getZoneJSONByZoneId(@PathVariable(value = "zoneId") Long zoneId) {
        String zoneJson = zoneDetailService.getZoneJSONByZoneId(zoneId);
        return R.ok(zoneJson);
    }

    /**
     * 内部接口，通过组织Id获取专区详情JSON对象
     */
    @InnerAuth
    @GetMapping("/getZoneJSONByDeptId/{deptId}")
    @ApiOperation(value = "通过组织Id获取专区详情JSON对象", hidden = true)
    public R<String> getZoneJSONByDeptId(@PathVariable(value = "deptId") Long deptId) {
        String zoneJson = zoneDetailService.getZoneJSONByDeptId(deptId);
        return R.ok(zoneJson);
    }

    /**
     * 获取切换身份后的专区访问地址
     */
    @GetMapping("/getChangeUserIdentityZoneUrl")
    @ApiOperation(value = "获取切换身份后的专区访问地址")
    public AjaxResult getChangeUserIdentityZoneUrl() {
        return success((Object) zoneDetailService.getChangeUserIdentityZoneUrl());
    }

    /**
     * 内部接口，通过组织Id获取专区首页文章最大数量
     */
    @InnerAuth
    @GetMapping("/getZoneHomePageCmsSizeByDeptId/{deptId}")
    @ApiOperation(value = "通过组织Id获取专区首页文章最大数量", hidden = true)
    public R<Long> getZoneHomePageCmsSizeByDeptId(@PathVariable(value = "deptId") Long deptId) {
        Long homePageCmsSize = zoneDetailService.getZoneHomePageCmsSizeByDeptId(deptId);
        return R.ok(homePageCmsSize);
    }

    /**
     * 查询特色专区详情列表
     */
    @RequiresPermissions("zone:zoneDetail:list")
    @GetMapping("/list")
    @ApiOperation(value = "查询特色专区详情列表")
    public GenericsTableDataInfo<ZoneDetailDataVo> list(ZoneDetailSearchVo zoneDetailSearchVo) {
        startPage();
        return zoneDetailService.selectZoneDetailList(zoneDetailSearchVo);
    }

    /**
     * 获取特色专区详情详细信息
     */
    @RequiresPermissions("zone:zoneDetail:query")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取特色专区详情详细信息")
    public GenericsAjaxResult<ZoneDetailDataVo> getInfo(@PathVariable("id") Long id) {
        return genericsSuccess(zoneDetailService.selectZoneDetailDataById(id));
    }

    /**
     * 修改特色专区详情
     */
    @RequiresPermissions("zone:zoneDetail:edit")
    @Log(title = "特色专区详情", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改特色专区详情")
    public AjaxResult edit(@RequestBody ZoneDetailUpdateVo zoneDetailUpdateVo) {
        return toAjax(zoneDetailService.updateZoneDetail(zoneDetailUpdateVo));
    }

    /**
     * 发布/下架特色专区
     */
    @RequiresPermissions("zone:zoneDetail:publish")
    @Log(title = "发布特色专区", businessType = BusinessType.UPDATE)
    @PutMapping("/publish/{id}")
    @ApiOperation(value = "发布特色专区")
    public AjaxResult publish(@PathVariable("id") Long id) {
        return toAjax(zoneDetailService.publishZoneDetail(id));
    }

    /**
     * 通过组织名称删除特色专区
     */
    @InnerAuth
    @Log(title = "特色专区详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deptName}")
    @ApiOperation(value = "删除特色专区")
    public R<Boolean> deleteByDeptName(@PathVariable(value = "deptName") String deptName) {
        return R.ok(zoneDetailService.deleteZoneDetailByDeptName(deptName) > 0);
    }

    /**
     * 通过组织id锁定/解锁专区资讯
     */
    @InnerAuth
    @Log(title = "锁定专区资讯", businessType = BusinessType.UPDATE)
    @PutMapping("/{deptId}/lockCms/{lockStatus}")
    @ApiOperation(value = "通过组织id锁定/解锁专区资讯（0：未锁定，1：锁定）", hidden = true)
    public R<Boolean> lockCmsByDeptId(@PathVariable(value = "deptId") Long deptId
            , @PathVariable(value = "lockStatus") Integer lockStatus) {
        return R.ok(zoneDetailService.lockCmsByDeptId(deptId, lockStatus) > 0);
    }
}
