package com.mcwl.web.controller.memberCenter;

import cn.hutool.core.bean.BeanUtil;
import com.mcwl.common.core.domain.R;
import com.mcwl.memberCenter.domain.MemberLevel;
import com.mcwl.memberCenter.domain.dto.AddMemberLevelDto;
import com.mcwl.memberCenter.domain.dto.EditMemberLevelDto;
import com.mcwl.memberCenter.domain.vo.MemberLevelVo;
import com.mcwl.memberCenter.service.MemberLevelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/***
 * 会员等级
 */
@RestController
@RequestMapping("memberLevel")
@RequiredArgsConstructor
@Api(tags = "会员中心")
public class MemberLevelController {

    private final MemberLevelService memberLevelService;


    /**
     * 添加会员等级
     */
//    @PreAuthorize("@ss.hasPermi('system:memberLevel:add')")
    @PostMapping("addMemberLevel")
    @ApiOperation(value = "添加会员等级")
    public R<Object> add(@RequestBody @Valid AddMemberLevelDto addMemberLevelDto) {
        MemberLevel memberLevel = BeanUtil.copyProperties(addMemberLevelDto, MemberLevel.class);
        memberLevelService.save(memberLevel);
        return R.ok();
    }

    /**
     * 获取会员等级列表
     *
     * @return 会员等级列表
     */
//    @PreAuthorize("@ss.hasPermi('system:memberLevel:list')")
    @GetMapping("list")
    @ApiOperation(value = "获取会员等级列表")
    public R<List<MemberLevelVo>> list() {

        List<MemberLevel> memberLevelList = memberLevelService.list();
        List<MemberLevelVo> memberLevelListVo = new ArrayList<>();
        for (MemberLevel memberLevel : memberLevelList) {
            MemberLevelVo memberLevelVo = BeanUtil.copyProperties(memberLevel, MemberLevelVo.class);
            memberLevelVo.setSubscriptionPeriod(memberLevel.getSubscriptionPeriod().getValue());
            memberLevelListVo.add(memberLevelVo);
        }

        return R.ok(memberLevelListVo);
    }

    /**
     * 获取会员等级
     */
//    @PreAuthorize("@ss.hasPermi('system:memberLevel:query')")
    @GetMapping("getMemberLevel")
    @ApiOperation(value = "获取会员等级")
    public R<MemberLevelVo> getMemberLevel(@Valid
                                           @NotNull(message = "会员等级id不能为空")
                                           @ApiParam(value = "会员等级id", required = true)
                                           Long id) {
        MemberLevel memberLevel = memberLevelService.getById(id);
        MemberLevelVo memberLevelVo = BeanUtil.copyProperties(memberLevel, MemberLevelVo.class);
        memberLevelVo.setSubscriptionPeriod(memberLevel.getSubscriptionPeriod().getValue());
        return R.ok(memberLevelVo);
    }


    /**
     * 修改会员等级
     */
//    @PreAuthorize("@ss.hasPermi('system:memberLevel:edit')")
    @PostMapping("updateMemberLevel")
    @ApiOperation(value = "修改会员等级")
    public R<Object> update(@RequestBody @Valid EditMemberLevelDto editMemberLevelDto) {
        memberLevelService.updateById(BeanUtil.copyProperties(editMemberLevelDto, MemberLevel.class));
        return R.ok();
    }

    /**
     * 删除会员等级
     */
//    @PreAuthorize("@ss.hasPermi('system:memberLevel:remove')")
    @GetMapping("deleteMemberLevel")
    @ApiOperation(value = "删除会员等级")
    public R<Object> delete(@Valid
                            @NotNull(message = "会员等级id不能为空")
                            @ApiParam(value = "会员等级id", required = true)
                            Long id) {
        memberLevelService.removeById(id);
        return R.ok();
    }

}
