package com.fzm.mall.server.admin.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fzm.mall.server.admin.annotation.AdminLog;
import com.fzm.mall.server.admin.enums.log.OperateModuleEnum;
import com.fzm.mall.server.admin.enums.log.OperateTypeEnum;
import com.fzm.mall.server.admin.repo.ResponseFactory;
import com.fzm.mall.server.admin.repo.ResponseVO;
import com.fzm.mall.server.admin.system.entity.dto.SysConfigDTO;
import com.fzm.mall.server.admin.system.entity.dto.SysConfigSaveDTO;
import com.fzm.mall.server.admin.system.entity.dto.SysConfigUpdateDTO;
import com.fzm.mall.server.admin.system.service.ISysConfigService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 系统配置表 前端控制器
 * </p>
 *
 * @author wulixian
 * @since 2021-03-29
 */
@Api(tags = "系统中心")
@RestController
@RequestMapping("/sys-config")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysConfigController {

    private final ISysConfigService sysConfigService;
    private final ResponseFactory resFac;

    @ApiOperation(value = "系统中心-全局配置-新增")
    @ApiOperationSupport(order = 1)
    @AdminLog(description = "系统中心-全局配置-新增", module = OperateModuleEnum.SYSTEM_CENTER, opType = OperateTypeEnum.ADD)
    @PostMapping("/save")
    public ResponseVO save(@RequestBody @Valid SysConfigSaveDTO sysConfigSaveDTO) {

        sysConfigService.save(sysConfigSaveDTO);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "系统中心-全局配置-编辑")
    @ApiOperationSupport(order = 2)
    @AdminLog(description = "系统中心-全局配置-编辑", module = OperateModuleEnum.SYSTEM_CENTER, opType = OperateTypeEnum.UPDATE)
    @PutMapping("/update")
    public ResponseVO update(@RequestBody @Valid SysConfigUpdateDTO sysConfigUpdateDTO) {

        sysConfigService.update(sysConfigUpdateDTO);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "系统中心-全局配置-启用状态-编辑")
    @ApiOperationSupport(order = 6)
    @AdminLog(description = "系统中心-全局配置-启用状态-编辑", module = OperateModuleEnum.SYSTEM_CENTER, opType = OperateTypeEnum.UPDATE)
    @PutMapping("/validState/update/{configId}")
    public ResponseVO validStateUpdate(@ApiParam(value = "configId", required = true) @PathVariable(value = "configId") String configId,
                                       @ApiParam(value = "是否可用(false--停用 true--可用 )") @RequestParam Boolean validState,
                                       @ApiParam(value = "信息修改时间") @RequestParam Long updateTime) {

        sysConfigService.validStateUpdate(configId, validState, updateTime);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "系统中心-全局配置-删除")
    @ApiOperationSupport(order = 3)
    @AdminLog(description = "系统中心-全局配置-删除", module = OperateModuleEnum.SYSTEM_CENTER, opType = OperateTypeEnum.DELETE)
    @DeleteMapping("/delete/{configId}")
    public ResponseVO delete(
            @ApiParam(value = "configId", required = true) @PathVariable(value = "configId") String configId) {
        sysConfigService.delete(configId);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "系统中心-全局配置-主键查询")
    @ApiOperationSupport(order = 4)
    @GetMapping("/{configId}")
    public ResponseVO<SysConfigDTO> read(
            @ApiParam(value = "configId", required = true) @PathVariable(value = "configId") String configId) {
        SysConfigDTO result = sysConfigService.read(configId);
        return resFac.getSuccessResponseWithData(result);
    }

    @ApiOperation(value = "系统中心-全局配置-列表查询")
    @ApiOperationSupport(order = 5)
    @GetMapping("/list")
    public ResponseVO list(@ApiParam(value = "设置项键或值") @RequestParam(required = false) String keyword,
                           @ApiParam(value = "每页条数") @RequestParam(required = false) Long pageSize,
                           @ApiParam(value = "页码") @RequestParam(required = false) Long pageNum) {
        Page<SysConfigDTO> page = new Page<>();
        if (ObjectUtils.isNotEmpty(pageSize) && ObjectUtils.isNotEmpty(pageNum)) {
            page.setSize(pageSize);
            page.setCurrent(pageNum);
        }

        IPage<SysConfigDTO> result = sysConfigService.page(page, keyword);
        return resFac.getSuccessResponseWithData(result);
    }
}
