package com.ruoyi.web.modules.account.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.modules.account.domain.BizMember;
import com.ruoyi.web.modules.account.dto.MemberCreateDTO;
import com.ruoyi.web.modules.account.dto.MemberOrderQueryDTO;
import com.ruoyi.web.modules.account.dto.MemberStatusDTO;
import com.ruoyi.web.modules.account.service.IMemberService;

/**
 * 平台端会员管理
 */
@RestController
@RequestMapping("/agri/member")
public class MemberController extends BaseController
{
    @Autowired
    private IMemberService memberService;

    @PreAuthorize("@ss.hasPermi('agri:member:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizMember query)
    {
        startPage();
        return getDataTable(memberService.selectMemberList(query));
    }

    @PreAuthorize("@ss.hasPermi('agri:member:add')")
    @Log(title = "会员管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MemberCreateDTO dto)
    {
        Long memberId = memberService.createMember(dto, getUsername());
        return success(memberId);
    }

    @PreAuthorize("@ss.hasPermi('agri:member:list')")
    @GetMapping("/levels")
    public AjaxResult levels()
    {
        return success(memberService.selectMemberLevelList());
    }

    @PreAuthorize("@ss.hasPermi('agri:member:query')")
    @GetMapping("/{memberId}")
    public AjaxResult getInfo(@PathVariable("memberId") Long memberId)
    {
        return success(memberService.selectMemberDetail(memberId));
    }

    @PreAuthorize("@ss.hasPermi('agri:member:edit')")
    @Log(title = "会员管理", businessType = BusinessType.UPDATE)
    @PutMapping("/{memberId}/status")
    public AjaxResult updateStatus(@PathVariable("memberId") Long memberId, @RequestBody MemberStatusDTO dto)
    {
        memberService.updateMemberStatus(memberId, dto, getUsername());
        return success();
    }

    @PreAuthorize("@ss.hasPermi('agri:member:query')")
    @GetMapping("/{memberId}/orders")
    public TableDataInfo orders(@PathVariable("memberId") Long memberId, MemberOrderQueryDTO query)
    {
        startPage();
        return memberService.selectMemberOrders(memberId, query);
    }

    @PreAuthorize("@ss.hasPermi('agri:member:query')")
    @GetMapping("/{memberId}/orders/{orderId}")
    public AjaxResult orderDetail(@PathVariable("memberId") Long memberId, @PathVariable("orderId") Long orderId)
    {
        return success(memberService.selectMemberOrderDetail(memberId, orderId));
    }
}
