package cn.ey.biz.module.system.controller.admin.category;

import cn.ey.biz.common.pojo.PageResult;
import cn.ey.biz.common.pojo.R;
import cn.ey.biz.framework.excel.core.util.ExcelUtils;
import cn.ey.biz.framework.operatelog.core.annotations.OperateLog;
import cn.ey.biz.module.system.controller.admin.category.vo.*;
import cn.ey.biz.module.system.convert.category.CategoryAppConvert;
import cn.ey.biz.module.system.dal.dataobject.category.CategoryAppDO;
import cn.ey.biz.module.system.service.category.CategoryAppService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import static cn.ey.biz.common.pojo.R.success;
import static cn.ey.biz.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 字典分类应用")
@RestController
@RequestMapping("/system/category-app")
@Validated
public class CategoryAppController {

    @Resource
    private CategoryAppService categoryAppService;

    @PostMapping("/create")
    @ApiOperation("创建字典分类应用")
    @PreAuthorize("@ss.hasPermission('system:category-app:create')")
    public R<Long> createCategoryApp(@Valid @RequestBody CategoryAppCreateReqVO createReqVO) {
        return success(categoryAppService.createCategoryApp(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新字典分类应用")
    @PreAuthorize("@ss.hasPermission('system:category-app:update')")
    public R<Boolean> updateCategoryApp(@Valid @RequestBody CategoryAppUpdateReqVO updateReqVO) {
        categoryAppService.updateCategoryApp(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除字典分类应用")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:category-app:delete')")
    public R<Boolean> deleteCategoryApp(@RequestParam("id") Long id) {
        categoryAppService.deleteCategoryApp(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得字典分类应用")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:category-app:query')")
    public R<CategoryAppRespVO> getCategoryApp(@RequestParam("id") Long id) {
        CategoryAppDO categoryApp = categoryAppService.getCategoryApp(id);
        return success(CategoryAppConvert.INSTANCE.convert(categoryApp));
    }

    @GetMapping("/list")
    @ApiOperation("获得字典分类应用列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('system:category-app:query')")
    public R<List<CategoryAppRespVO>> getCategoryAppList(@RequestParam("ids") Collection<Long> ids) {
        List<CategoryAppDO> list = categoryAppService.getCategoryAppList(ids);
        return success(CategoryAppConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得字典分类应用分页")
    @PreAuthorize("@ss.hasPermission('system:category-app:query')")
    public R<PageResult<CategoryAppRespVO>> getCategoryAppPage(@Valid CategoryAppPageReqVO pageVO) {
        PageResult<CategoryAppDO> pageResult = categoryAppService.getCategoryAppPage(pageVO);
        return success(CategoryAppConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出字典分类应用 Excel")
    @PreAuthorize("@ss.hasPermission('system:category-app:export')")
    @OperateLog(type = EXPORT)
    public void exportCategoryAppExcel(@Valid CategoryAppExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<CategoryAppDO> list = categoryAppService.getCategoryAppList(exportReqVO);
        // 导出 Excel
        List<CategoryAppExcelVO> datas = CategoryAppConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "字典分类应用.xls", "数据", CategoryAppExcelVO.class, datas);
    }

}
