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.FieldConvert;
import cn.ey.biz.module.system.dal.dataobject.field.FieldDO;
import cn.ey.biz.module.system.service.field.FieldConfigService;
import cn.ey.biz.module.system.service.field.FieldService;
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/field")
@Validated
public class FieldController {

    @Resource
    private FieldService fieldService;
    @Resource
    private FieldConfigService fieldConfigService;

    @PostMapping("/create")
    @ApiOperation("创建字段权限-字段")
    @PreAuthorize("@ss.hasPermission('system:field:create')")
    public R<Long> createField(@Valid @RequestBody FieldCreateReqVO createReqVO) {
        return success(fieldService.createField(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新字段权限-字段")
    @PreAuthorize("@ss.hasPermission('system:field:update')")
    public R<Boolean> updateField(@Valid @RequestBody FieldUpdateReqVO updateReqVO) {
        fieldService.updateField(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @ApiOperation("获得字段权限-字段")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:field:query')")
    public R<FieldRespVO> getField(@RequestParam("id") Long id) {
        FieldDO field = fieldService.getField(id);
        return success(FieldConvert.INSTANCE.convert(field));
    }

    @GetMapping("/list")
    @ApiOperation("获得字段权限-字段列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('system:field:query')")
    public R<List<FieldRespVO>> getFieldList(@RequestParam("ids") Collection<Long> ids) {
        List<FieldDO> list = fieldService.getFieldList(ids);
        return success(FieldConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/list-by-config-id")
    @ApiOperation("获得字段权限-字段列表（通过配置）")
    @ApiImplicitParam(name = "configId", value = "配置编号", required = true, example = "1", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:field:query')")
    public R<List<FieldRespVO>> getFieldList(@RequestParam("configId") Long configId) {
        FieldExportReqVO query = new FieldExportReqVO().setConfigId(configId);
        List<FieldRespVO> list = fieldService.getFieldListWithBelongTo(query);
        return success(list);
    }

    @GetMapping("/list-by-config-code")
    @ApiOperation("获得字段权限-字段列表（通过配置）")
    @ApiImplicitParam(name = "configCode", value = "配置编号", required = true, example = "timesheet", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('system:field:query')")
    public R<List<FieldRespVO>> getFieldList(@RequestParam("configCode") String configCode) {
        List<FieldRespVO> list = fieldService.getFieldListBelongTo(configCode);
        list.sort(Comparator.comparing(FieldRespVO::getId));
        return success(list);
    }

    @GetMapping("/page")
    @ApiOperation("获得字段权限-字段分页")
    @PreAuthorize("@ss.hasPermission('system:field:query')")
    public R<PageResult<FieldRespVO>> getFieldPage(@Valid FieldPageReqVO pageVO) {
        PageResult<FieldDO> pageResult = fieldService.getFieldPage(pageVO);
        return success(FieldConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出字段权限-字段 Excel")
    @PreAuthorize("@ss.hasPermission('system:field:export')")
    @OperateLog(type = EXPORT)
    public void exportFieldExcel(@Valid FieldExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<FieldDO> list = fieldService.getFieldList(exportReqVO);
        // 导出 Excel
        List<FieldExcelVO> datas = FieldConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "字段权限-字段.xls", "数据", FieldExcelVO.class, datas);
    }

}
