package com.ruoyi.mall.controller;

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.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.mall.domain.MallMemberUser;
import com.ruoyi.mall.domain.MallSalesman;
import com.ruoyi.mall.domain.MallShopShop;
import com.ruoyi.mall.service.IMallMemberUserService;
import com.ruoyi.mall.service.IMallSalesmanService;
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.util.List;

/**
 * 会员Controller
 *
 * @author ruoyi
 * @date 2021-11-25
 */
@RestController
@RequestMapping("/mall/mallMemberUser")
public class MallMemberUserController extends BaseController {
    @Autowired
    private IMallMemberUserService mallMemberUserService;
    @Autowired
    private IMallSalesmanService mallSalesmanService;

    /**
     * 查询会员列表
     */
    @PreAuthorize("@ss.hasPermi('mall:mallMemberUser:list')")
    @GetMapping("/list")
    public TableDataInfo list(MallMemberUser mallMemberUser) {
        startPage();
        List<MallMemberUser> list = mallMemberUserService.selectMallMemberUserList(mallMemberUser);
        return getDataTable(list);
    }

    /**
     * 查询会员列表
     */
    @PreAuthorize("@ss.hasPermi('mall:customerinformation:list')")
    @GetMapping("/customerinformationlist")
    public TableDataInfo customerinformationlist(MallMemberUser mallMemberUser) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        MallSalesman mallSalesman = mallSalesmanService.selectMallSalesmanBysysUserId(loginUser.getUserId());
        if (ObjectUtils.isNotEmpty(mallSalesman)) {
            mallMemberUser.setSalesmanId(mallSalesman.getId());
        }
        startPage();
        List<MallMemberUser> list = mallMemberUserService.selectMallMemberUserList(mallMemberUser);
        return getDataTable(list);
    }

    /**
     * 查询会员列表
     */
    @PreAuthorize("@ss.hasPermi('mall:mallMemberUser:list')")
    @GetMapping("/selectList")
    public AjaxResult selectList() {
        MallMemberUser memberUser = new MallMemberUser();
        List<MallMemberUser> list = mallMemberUserService.selectMallMemberUserList(memberUser);
        return AjaxResult.success(list);
    }

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

    /**
     * 获取会员详细信息
     */
    @PreAuthorize("@ss.hasPermi('mall:mallMemberUser:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(mallMemberUserService.selectMallMemberUserById(id));
    }

    /**
     * 新增会员
     */
    @PreAuthorize("@ss.hasPermi('mall:mallMemberUser:add')")
    @Log(title = "会员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MallMemberUser mallMemberUser) {
        return toAjax(mallMemberUserService.insertMallMemberUser(mallMemberUser));
    }

    /**
     * 修改会员
     */
    @PreAuthorize("@ss.hasPermi('mall:mallMemberUser:edit')")
    @Log(title = "会员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MallMemberUser mallMemberUser) {
        mallMemberUser.setUpdateAt(DateUtils.getTime());
        return toAjax(mallMemberUserService.updateMallMemberUser(mallMemberUser));
    }

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