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

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.field.vo.*;
import cn.ey.biz.module.system.convert.field.FieldItemConvert;
import cn.ey.biz.module.system.dal.dataobject.field.FieldItemDO;
import cn.ey.biz.module.system.service.field.FieldItemService;
import cn.hutool.core.collection.CollectionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

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/field-item")
@Validated
public class FieldItemController {

    @Resource
    private FieldItemService fieldItemService;

/*    @PostMapping("/create")
    @ApiOperation("创建字段条目")
    @PreAuthorize("@ss.hasPermission('system:field-item:create')")
    public R<Long> createFieldItem(@Validated @RequestBody FieldItemCreateReqVO createReqVO) {
        return success(*//*fieldItemService.createFieldItem(createReqVO)*//*null);
    }*/

    @PutMapping("/update")
    @ApiOperation("更新字段条目")
    @PreAuthorize("@ss.hasPermission('system:field-item:update')")
    public R<Boolean> updateFieldItem(@Valid @RequestBody FieldItemUpdateReqVO updateReqVO) {
        fieldItemService.updateFieldItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除字段条目")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:field-item:delete')")
    public R<Boolean> deleteFieldItem(@RequestParam("id") Long id) {
        fieldItemService.deleteFieldItem(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得字段条目")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:field-item:query')")
    public R<FieldItemRespVO> getFieldItem(@RequestParam("id") Long id) {
        FieldItemDO fieldItem = fieldItemService.getFieldItem(id);
        return success(FieldItemConvert.INSTANCE.convert(fieldItem));
    }

    @GetMapping("/list")
    @ApiOperation("获得字段条目列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('system:field-item:query')")
    public R<List<FieldItemRespVO>> getFieldItemList(@RequestParam("ids") Collection<Long> ids) {
        List<FieldItemDO> list = fieldItemService.getFieldItemList(ids);
        return success(FieldItemConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得字段条目分页")
    @PreAuthorize("@ss.hasPermission('system:field-item:query')")
    public R<PageResult<FieldItemRespVO>> getFieldItemPage(@Valid FieldItemPageReqVO pageVO) {
        PageResult<FieldItemDO> pageResult = fieldItemService.getFieldItemPage(pageVO);
        return success(FieldItemConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出字段条目 Excel")
    @PreAuthorize("@ss.hasPermission('system:field-item:export')")
    @OperateLog(type = EXPORT)
    public void exportFieldItemExcel(@Valid FieldItemExportReqVO exportReqVO,
                                     HttpServletResponse response) throws IOException {
        List<FieldItemDO> list = fieldItemService.getFieldItemList(exportReqVO);
        if (CollectionUtil.isNotEmpty(list)) {
            // 导出 Excel
            List<FieldItemExcelVO> datas = FieldItemConvert.INSTANCE.convertList02(list);
            ExcelUtils.write(response, "字段条目.xls", "数据", FieldItemExcelVO.class, datas);
        }
    }

/*    @PutMapping("/update-fielditem-belongto")
    @ApiOperation("更新展示的内容")
    @PreAuthorize("@ss.hasPermission('system:field-item:export')")
    public R<Boolean> updateFieldItemBelongto(@Validated @RequestBody FieldItemUpdateReqVO updateReqVO) {
        fieldItemService.updateFieldItemBelongto(updateReqVO);
        return success(true);
    }*/

}
