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.FieldConfigConvert;
import cn.ey.biz.module.system.dal.dataobject.field.FieldConfigDO;
import cn.ey.biz.module.system.enums.field.StatusEnum;
import cn.ey.biz.module.system.service.field.FieldConfigService;
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.exception.util.ServiceExceptionUtil.exception;
import static cn.ey.biz.common.pojo.R.success;
import static cn.ey.biz.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;
import static cn.ey.biz.module.system.enums.ErrorCodeConstants.FIELD_CONFIG_QUERY_ERROR;

@Api(tags = "管理后台 - 字段权限配置")
@RestController
@RequestMapping("/system/field-config")
@Validated
public class FieldConfigController {

    @Resource
    private FieldConfigService fieldConfigService;

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

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

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

    @GetMapping("/get")
    @ApiOperation("获得字段权限配置")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('system:field-config:query')")
    public R<FieldConfigRespVO> getFieldConfig(@RequestParam("id") Long id) {
        FieldConfigDO fieldConfig = fieldConfigService.getFieldConfig(id);
        return success(FieldConfigConvert.INSTANCE.convert(fieldConfig));
    }

    @GetMapping("/get-default")
    @ApiOperation("获得字段权限配置（默认）")
    @ApiImplicitParam(name = "configCode", value = "配置编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('system:field-config:query')")
    public R<FieldConfigRespVO> getFieldConfig(@RequestParam("configCode") String configCode) {
        FieldConfigExportReqVO query = new FieldConfigExportReqVO()
                .setConfigCode(configCode)
                .setEnable(StatusEnum.ENABLE.getStatus());
        List<FieldConfigDO> configs = fieldConfigService.getFieldConfigList(query);
        if(configs == null || configs.size() != 1){
            throw exception(FIELD_CONFIG_QUERY_ERROR);
        }
        return success(FieldConfigConvert.INSTANCE.convert(configs.get(0)));
    }

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

    @GetMapping("/page")
    @ApiOperation("获得字段权限配置分页")
    @PreAuthorize("@ss.hasPermission('system:field-config:query')")
    public R<PageResult<FieldConfigRespVO>> getFieldConfigPage(@Valid FieldConfigPageReqVO pageVO) {
        PageResult<FieldConfigDO> pageResult = fieldConfigService.getFieldConfigPage(pageVO);
        return success(FieldConfigConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出字段权限配置 Excel")
    @PreAuthorize("@ss.hasPermission('system:field-config:export')")
    @OperateLog(type = EXPORT)
    public void exportFieldConfigExcel(@Valid FieldConfigExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<FieldConfigDO> list = fieldConfigService.getFieldConfigList(exportReqVO);
        // 导出 Excel
        List<FieldConfigExcelVO> datas = FieldConfigConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "字段权限配置.xls", "数据", FieldConfigExcelVO.class, datas);
    }

    @PutMapping("/update-fieldconfig-enable")
    @ApiOperation("修改字段权限配置字段（启用 or 禁用）")
    @PreAuthorize("@ss.hasPermission('system:field-config:update')")
    public R<Boolean> updateFieldconfigEnable(@Valid @RequestBody FieldConfigUpdateReqVO updateReqVO){
        fieldConfigService.updateFieldconfigEnable(updateReqVO);
        return success(true);
    }

    @GetMapping("/get-fieldconfig-version")
    @ApiOperation("获取当前启动状态下版本")
    @PreAuthorize("@ss.hasPermission('system:field-config:query')")
    public R<FieldConfigDO> getFieldConfigVersion(@RequestParam("configCode") String configCode) {
        FieldConfigDO fieldConfigDO = fieldConfigService.getFieldConfigVersion(configCode);
        return success(fieldConfigDO);
    }

}
