package com.codvision.base.controller.manager;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codvision.authcore.annotation.RequiresPermissions;
import com.codvision.base.entity.LocalIconLibrary;
import com.codvision.base.entity.vo.LocalIconLibraryVO;
import com.codvision.base.service.ILocalIconLibraryService;
import com.codvision.commoncore.common.ResponseEntity;
import com.codvision.commoncore.exception.BusinessException;
import com.codvision.webcore.bean.QueryPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
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-20
 */
@RestController
@RequestMapping("/v1/manager/localIconLibrary")
@Api(tags = "平台管理端-本地图标管理模块")
public class ManagerLocalIconLibraryController {

    @Autowired
    private ILocalIconLibraryService localIconLibraryService;

    @ApiOperation(value = "获取本地图标分页列表", notes = "获取本地图标分页列表接口")
    @PostMapping("/findAll")
    @RequiresPermissions("local_icon_library_list")
    public ResponseEntity<IPage<LocalIconLibraryVO>> findAll(@RequestParam(required = false) String keyword,
                                                             @RequestParam(required = false) Integer type,
                                                             @RequestParam(value = "page", required = false, defaultValue = "1") int page,
                                                             @RequestParam(value = "size", required = false, defaultValue = "10") int size) {
        LocalIconLibrary queryBean = new LocalIconLibrary();
        queryBean.setIconName(keyword);
        queryBean.setType(type);
        IPage<LocalIconLibraryVO> pageResult = localIconLibraryService.getPageResult(new QueryPage<>(queryBean, page, size));
        return new ResponseEntity<IPage<LocalIconLibraryVO>>().ok().data(pageResult);
    }

    @ApiOperation(value = "新增本地图标", notes = "新增本地图标接口")
    @PostMapping("/add")
    @RequiresPermissions("local_icon_library_add")
    public ResponseEntity<String> addLocalIconLibrary(@Valid @RequestBody LocalIconLibrary localIconLibrary) {
        localIconLibraryService.save(localIconLibrary);
        return new ResponseEntity<String>().ok().message("添加成功");
    }

    @ApiOperation(value = "编辑本地图标", notes = "编辑本地图标接口")
    @PostMapping("/update")
    @RequiresPermissions("local_icon_library_edit")
    public ResponseEntity<String> updateLocalIconLibrary(@RequestBody LocalIconLibrary localIconLibrary) {
        localIconLibraryService.updateById(localIconLibrary);
        return new ResponseEntity<String>().ok().message("编辑成功");
    }

    @PostMapping("/batchDel")
    @ApiOperation(value = "批量删除", notes = "批量删除接口")
    @RequiresPermissions("local_icon_library_del")
    public ResponseEntity<String> batchDel(@RequestBody List<Integer> idList) {
        ResponseEntity<String> responseEntity = new ResponseEntity<>();
        if (CollectionUtils.isEmpty(idList)) {
            throw new BusinessException(30002);
        }
        localIconLibraryService.removeBatchByIds(idList);
        return responseEntity.ok();
    }
}
