package com.ruoyi.mall.controller;

import java.util.HashMap;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ApiResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mall.domain.MallSchool;
import com.ruoyi.mall.service.IMallSchoolService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
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.enums.BusinessType;
import com.ruoyi.mall.domain.MallMemberUser;
import com.ruoyi.mall.service.IMallMemberUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户会员Controller
 *
 * @author ruoyi
 * @date 2023-10-12
 */
@RestController
@RequestMapping("/mall/mallMemberUser")
public class MallMemberUserController extends BaseController
{
    @Resource
    private IMallMemberUserService mallMemberUserService;
    @Resource
    private IMallSchoolService schoolService;

    /**
     * 查询用户会员列表
     */
    @PreAuthorize("@ss.hasPermi('mall:mallMemberUser:list')")
    @GetMapping("/list")
    public TableDataInfo list(MallMemberUser mallMemberUser)
    {
        // 获取登陆用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        // MallSchool school = schoolService.selectMallSchoolBySysUserId(loginUser.getUserId());
        // if (school != null && loginUser.getUserId() != 1) {
        //     mallMemberUser.setSchoolId(school.getSchoolId());
        // }

        MallMemberUser sysUserId = mallMemberUserService.selectMallMemberUserBySysUserId(loginUser.getUserId());
        if (sysUserId != null && loginUser.getUserId() != 1) {
            mallMemberUser.setIndexUserId(sysUserId.getId());
        }

        startPage();
        List<MallMemberUser> list = mallMemberUserService.selectMallMemberUserList(mallMemberUser);
        return getDataTable(list);
    }

    /**
     * 导出用户会员列表
     */
    @PreAuthorize("@ss.hasPermi('mall:mallMemberUser:export')")
    @Log(title = "用户会员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MallMemberUser mallMemberUser)
    {
        List<MallMemberUser> list = mallMemberUserService.selectMallMemberUserList(mallMemberUser);
        ExcelUtil<MallMemberUser> util = new ExcelUtil<MallMemberUser>(MallMemberUser.class);
        util.exportExcel(response, 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)
    {
        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));
    }

    /**
     * 查询我的下级会员列表
     */
    @GetMapping("/getMyChildren")
    public TableDataInfo getMyChildren(MallMemberUser mallMemberUser)
    {
        startPage();
        List<MallMemberUser> list = mallMemberUserService.selectMallMemberUserList(mallMemberUser);
        return getDataTable(list);
    }

    /**
     * 新增登录用户
     */
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('mall:mallMemberUser:addSchoolUser')")
    @Log(title = "新增登录用户", businessType = BusinessType.UPDATE)
    @PutMapping("/submitUser")
    public AjaxResult submitUser(@RequestBody MallMemberUser mallMemberUser)
    {
        return mallMemberUserService.submitUser(mallMemberUser);
    }

    /**
     * 统计-开通未开通指南人数
     */
    @GetMapping("/setIsOpenStatisticsNum")
    public AjaxResult setIsOpenStatisticsNum()
    {
        // 获取登陆用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        MallMemberUser sysUserId = mallMemberUserService.selectMallMemberUserBySysUserId(loginUser.getUserId());
        HashMap<String, Object> map = new HashMap<>();
        if (sysUserId != null && loginUser.getUserId() != 1) {
            map.put("indexUserId", sysUserId.getId());
        }
        return AjaxResult.success(mallMemberUserService.setIsOpenStatisticsNum(map));
    }
}
