package com.codvision.base.controller.manager;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.codvision.authcore.annotation.RequiresPermissions;
import com.codvision.base.entity.SysTag;
import com.codvision.base.entity.vo.SysTagVO;
import com.codvision.base.service.ISysTagService;
import com.codvision.commoncore.common.ResponseEntity;
import com.codvision.webcore.bean.QueryPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 系统标签 管理模块
 *
 * @author lingee
 * @date 2023-11-17
 */
@RestController
@RequestMapping("/v1/manager/sysTag")
@Api(tags = "平台管理端-系统标签模块")
public class ManagerSysTagController {

    @Autowired
    private ISysTagService sysTagService;

    @ApiOperation(value = "获取系统标签详情", notes = "获取系统标签详情接口")
    @PostMapping("/details")
    @RequiresPermissions("sys_tag_list")
    public ResponseEntity<SysTagVO> getById(@ApiParam(value = "系统标签ID", required = true) @RequestParam Integer tagId) {
        SysTagVO vo = sysTagService.getDetail(tagId);
        return new ResponseEntity<SysTagVO>().ok().data(vo);
    }

    @ApiOperation(value = "获取系统标签分页列表", notes = "获取系统标签分页列表接口")
    @PostMapping("/findAll")
    @RequiresPermissions("sys_tag_list")
    public ResponseEntity<IPage<SysTagVO>> getPageList(@ApiParam("标签名称") @RequestParam(required = false) String tagName,
                                                       @ApiParam("标签所属模块：字典项表中的item_value") @RequestParam(required = false) String dictItemValue,
                                                       @ApiParam("标签父id") @RequestParam(required = false) Integer parentId,
                                                       @RequestParam(value = "page", required = false, defaultValue = "1") int page,
                                                       @RequestParam(value = "size", required = false, defaultValue = "10") int size) {
        SysTag queryBean = new SysTag();
        queryBean.setTagName(tagName);
        queryBean.setDictItemValue(dictItemValue);
        queryBean.setParentId(parentId);
        QueryPage<SysTag> queryPage = new QueryPage<>(queryBean, page, size);

        IPage<SysTagVO> pageResult = sysTagService.getPageResult(queryPage);
        return new ResponseEntity<IPage<SysTagVO>>().ok().data(pageResult);
    }

    @ApiOperation(value = "添加系统标签", notes = "添加系统标签接口")
    @PostMapping("/addTag")
    @RequiresPermissions("sys_tag_add")
    public ResponseEntity<String> saveSysTag(@Valid @RequestBody SysTag sysTag) {
        sysTagService.save(sysTag);
        return new ResponseEntity<String>().ok().message("添加成功");
    }

    @ApiOperation(value = "删除系统标签", notes = "删除系统标签接口")
    @PostMapping("/batchRemoveTag")
    @RequiresPermissions("sys_tag_del")
    public ResponseEntity<String> batchRemoveTag(@ApiParam(value = "系统标签ID", required = true) @RequestBody List<Integer> tagIds) {
        for (Integer tagId : tagIds) {
            sysTagService.remove(Wrappers.<SysTag>lambdaQuery().like(SysTag::getIdPath, tagId));
        }
        return new ResponseEntity<String>().ok().message("删除成功");
    }

    @ApiOperation(value = "更新系统标签", notes = "更新系统标签接口")
    @PostMapping("/updateTag")
    @RequiresPermissions("sys_tag_edit")
    public ResponseEntity<String> updateSysTag(@RequestBody SysTag sysTag) {
        sysTagService.updateById(sysTag);
        return new ResponseEntity<String>().ok().message("更新成功");
    }
}
