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

import com.fzm.mall.server.admin.annotation.AdminLog;
import com.fzm.mall.server.admin.constant.MallResponseError;
import com.fzm.mall.server.admin.enums.log.OperateModuleEnum;
import com.fzm.mall.server.admin.home_page.enums.type.AdminTypeEnum;
import com.fzm.mall.server.admin.home_page.service.IAdminService;
import com.fzm.mall.server.admin.repo.ResponseFactory;
import com.fzm.mall.server.admin.repo.ResponseVO;
import com.fzm.mall.server.admin.user_center.entity.dto.MemberConfigDTO;
import com.fzm.mall.server.admin.user_center.entity.vo.MemberConfigVO;
import com.fzm.mall.server.admin.user_center.service.ISysMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author wangp
 * @date 2021/3/22 14:33
 * @description 会员
 * @since JDK 1.8
 */
@Api(tags = "用户中心-会员")
@Validated
@RestController
@RequestMapping("/member")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysMemberController {

    private final ISysMemberService memberService;
    private final IAdminService adminService;
    private final ResponseFactory resFac;

    @ApiOperation("编辑")
    @PostMapping("/memberSettings/edit")
    @AdminLog(description = "用户中心-编辑会员设置", module = OperateModuleEnum.USER_CENTER)
    public ResponseVO edit(@RequestBody @Valid List<MemberConfigDTO> memberConfigDTOList, @ApiParam(name = "adminId", value = "管理员id")
    @RequestHeader String adminId) {
        if (memberConfigDTOList.size() != 5) {
            return resFac.getResponse(MallResponseError.PARAM_ERROR);
        }
        adminService.adminAuthChecks(adminId, AdminTypeEnum.getPlatFormTypeArr());
        memberService.edit(memberConfigDTOList);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation("查询-设置列表")
    @GetMapping("/memberSettings/list")
    public ResponseVO list(@ApiParam(name = "adminId", value = "管理员id") @RequestHeader String adminId) {
        adminService.adminAuthChecks(adminId, AdminTypeEnum.getPlatFormTypeArr());
        List<MemberConfigVO> configVOS = memberService.listSelfDefine();
        return resFac.getSuccessResponseWithData(configVOS);
    }

}
