package com.ruoyi.app.user;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.app.utils.Md5Pay;
import com.ruoyi.app.utils.UUIDUtil;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.IInfoUserService;
import com.ruoyi.system.service.IUserMarginService;
import com.ruoyi.system.utils.Auth;
import com.ruoyi.system.utils.JwtUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
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.system.service.IPosMarginService;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * marginController
 *
 * @author ruoyi
 * @date 2023-08-02
 */
@RestController
@RequestMapping("/system/margin")
public class PosMarginController extends BaseController
{
    @Autowired
    private IPosMarginService posMarginService;
    @Autowired
    private IInfoUserService infoUserService;
    @Autowired
    private IUserMarginService userMarginService;


    /**
     * 获取用户保证金记录
     * @param token
     * @return
     */
    @Anonymous
    @GetMapping("/getmymargin")
    public AjaxResult getmymargin(@RequestHeader String token) {
        JwtUtil jwtUtil = new JwtUtil();
        String id = jwtUtil.getusid(token);
        QueryWrapper<UserMargin> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
        List<UserMargin> margin = userMarginService.list(queryWrapper);
        if(margin==null){
            return error(MessageUtils.message("no.user.deposit.not.exist"));
        }else {
            return success(MessageUtils.message("no.obtained.permission"), margin);
        }
    }

    /**
     * 获取用户保证金
     * @param type
     * @return
     */
    @Anonymous
    @GetMapping("/getmargin")
    public AjaxResult getmargin(@RequestParam String type) {
        QueryWrapper<PosMargin> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("type",type);
        PosMargin margin = posMarginService.getOne(queryWrapper);
        if(margin==null){
            return error(MessageUtils.message("no.user.deposit.type.not.exist"));
        }else {
            return success(MessageUtils.message("no.obtained.permission"), margin);
        }
    }
    /**
     * 查询margin列表
     */
    @PreAuthorize("@ss.hasPermi('system:margin:list')")
    @GetMapping("/list")
    public TableDataInfo list(PosMargin posMargin)
    {
        startPage();
        List<PosMargin> list = posMarginService.selectPosMarginList(posMargin);
        return getDataTable(list);
    }

    /**
     * 导出margin列表
     */
    @PreAuthorize("@ss.hasPermi('system:margin:export')")
    @Log(title = "margin", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PosMargin posMargin)
    {
        List<PosMargin> list = posMarginService.selectPosMarginList(posMargin);
        ExcelUtil<PosMargin> util = new ExcelUtil<PosMargin>(PosMargin.class);
        util.exportExcel(response, list, MessageUtils.message("no.export.excel.margin"));
    }

    /**
     * 获取margin详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:margin:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(posMarginService.selectPosMarginById(id));
    }

    /**
     * 新增margin
     */
    @PreAuthorize("@ss.hasPermi('system:margin:add')")
    @Log(title = "margin", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PosMargin posMargin)
    {
        return toAjax(posMarginService.insertPosMargin(posMargin));
    }

    /**
     * 修改margin
     */
    @PreAuthorize("@ss.hasPermi('system:margin:edit')")
    @Log(title = "margin", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PosMargin posMargin)
    {
        return toAjax(posMarginService.updatePosMargin(posMargin));
    }

    /**
     * 删除margin
     */
    @PreAuthorize("@ss.hasPermi('system:margin:remove')")
    @Log(title = "margin", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(posMarginService.deletePosMarginByIds(ids));
    }
}
