package com.younike.admin;

import com.younike.common.annotation.Log;
import com.younike.common.core.controller.BaseController;
import com.younike.common.core.domain.AjaxResult;
import com.younike.common.core.page.TableDataInfo;
import com.younike.common.enums.BusinessType;
import com.younike.common.utils.StringUtils;
import com.younike.common.utils.poi.ExcelUtil;
import com.younike.framework.config.AnonymousAccess;
import com.younike.system.domain.Member;
import com.younike.system.domain.vo.MemberVo;
import com.younike.system.param.MemberQueryParam;
import com.younike.system.param.UserLoginParam;
import com.younike.system.service.IMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 会员Controller
 *
 * @author zulu.chen
 * @date 2022-01-04
 */
@Api("member用户接口")
@RestController
@RequestMapping("/admin/member")
public class AdminMemberController extends BaseController {
    @Autowired
    private IMemberService memberService;

    /**
     * 查询会员列表
     */
    @ApiOperation("用户分页列表")
    @PreAuthorize("@ss.hasPermi('system:member:list')")
//    @AnonymousAccess
    @PutMapping("/memberList")
    public TableDataInfo pageList(MemberQueryParam member) {
        startPage();
        List<MemberVo> list = memberService.pageList(member);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:member:edit')")
    @ApiOperation("停用用户")
    @PutMapping("/takeOff")
    public AjaxResult takeOff(@RequestBody Member member) {
        if (member == null || member.getMemberId() == null || StringUtils.isEmpty(member.getCloseReason())) {
            return AjaxResult.error("参数错误");
        }
        member.setCloseTime(LocalDateTime.now());
        member.setStatus("1");
        return toAjax(memberService.updateMember(member));
    }

    @PreAuthorize("@ss.hasPermi('system:member:edit')")
    @ApiOperation("启用用户")
    @PutMapping("/takeOn")
    public AjaxResult takeOn(@RequestBody Member member) {
        if (member == null || member.getMemberId() == null) {
            return AjaxResult.error("参数错误");
        }
        member.setStatus("0");
        return toAjax(memberService.updateMember(member));
    }

    /**
     * 查询会员列表
     */
    @PreAuthorize("@ss.hasPermi('system:member:list')")
    @GetMapping("/list")
    public TableDataInfo list(Member member) {
        startPage();
        List<Member> list = memberService.selectMemberList(member);
        return getDataTable(list);
    }

    /**
     * 导出会员列表
     */
    @PreAuthorize("@ss.hasPermi('member:member:export')")
    @Log(title = "会员", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Member member) {
        List<Member> list = memberService.selectMemberList(member);
        ExcelUtil<Member> util = new ExcelUtil<Member>(Member.class);
        return util.exportExcel(list, "会员数据");
    }

    /**
     * 新增会员
     */
    @PreAuthorize("@ss.hasPermi('member:member:add')")
    @Log(title = "会员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Member member) {
        return toAjax(memberService.insertMember(member));
    }

    /**
     * 修改会员
     */
//    @PreAuthorize("@ss.hasPermi('member:member:edit')")
//    @Log(title = "会员", businessType = BusinessType.UPDATE)
    @PutMapping
    @AnonymousAccess
    @ApiOperation("编辑会员信息")
    public AjaxResult edit(@RequestBody Member member) {


        //参数验证
        if(ObjectUtils.isEmpty(member)){
            return AjaxResult.error("参数错误");
        }
        //验证会员d是否为空
        if(!StringUtils.isNotNull(member.getMemberId())){
            return AjaxResult.error("参数错误(会员id不能为空)");
        }
        return toAjax(memberService.updateMember(member));
    }

    /**
     * 删除会员
     */
    @PreAuthorize("@ss.hasPermi('member:member:remove')")
    @Log(title = "会员", businessType = BusinessType.DELETE)
	@DeleteMapping("/{memberIds}")
    public AjaxResult remove(@PathVariable Long[] memberIds) {
        return toAjax(memberService.deleteMemberByMemberIds(memberIds));
    }
}
