package com.mcwl.web.controller.memberCenter;

import cn.hutool.core.bean.BeanUtil;
import com.mcwl.common.core.domain.R;
import com.mcwl.memberCenter.domain.Benefit;
import com.mcwl.memberCenter.domain.dto.AddBenefitDto;
import com.mcwl.memberCenter.domain.dto.EditBenefitDto;
import com.mcwl.memberCenter.domain.vo.BenefitVo;
import com.mcwl.memberCenter.enums.MemberBenefitTypeEnum;
import com.mcwl.memberCenter.service.BenefitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

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

/***
 * 权益
 */
@RestController
@RequestMapping("benefit")
@RequiredArgsConstructor
@Api(tags = "会员中心")
public class BenefitController {

    private final BenefitService benefitService;


    /**
     * 权益列表
     */
//    @PreAuthorize("@ss.hasPermi('system:benefit:list')")
    @ApiOperation(value = "权益列表")
    @GetMapping("list")
    public R<List<BenefitVo>> list() {

        return R.ok(benefitService.benefitList());
    }

    /**
     * 添加权益
     */
//    @PreAuthorize("@ss.hasPermi('system:benefit:add')")
    @ApiOperation(value = "添加权益")
    @PostMapping("add")
    public R<Object> add(@RequestBody @Valid AddBenefitDto addBenefitDto) {

        benefitService.addBenefit(addBenefitDto);

        return R.ok();
    }

    /**
     * 根据权益名称id查询权益
     */
    @ApiOperation(value = "根据权益名称id查询权益")
    @GetMapping("getBenefitByBenefitId")
    public R<List<Benefit>> getBenefitByBenefitId(@NotNull(message = "权益名称id不能为空") Long benefitNameId) {
        List<Benefit> benefitList = benefitService.lambdaQuery()
                .eq(Benefit::getBenefitNameId, benefitNameId)
                .list();
        return R.ok(benefitList);
    }


    /**
     * 修改权益
     */
//    @PreAuthorize("@ss.hasPermi('system:benefit:edit')")
    @ApiOperation(value = "修改权益")
    @PostMapping("update")
    public R<Object> update(@RequestBody @Valid EditBenefitDto editBenefitDto) {

        Benefit benefit = BeanUtil.copyProperties(editBenefitDto, Benefit.class);
        benefitService.updateById(benefit);


        return R.ok();

    }

    //    @PreAuthorize("@ss.hasPermi('system:benefit:remove')")
    @ApiOperation(value = "删除权益")
    @GetMapping("delete")
    public R<Object> delete(@NotNull(message = "id不能为空") Long id) {
        benefitService.removeById(id);
        return R.ok();
    }


}
