package com.ruoyi.web.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import io.swagger.annotations.ApiOperation;
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.web.domain.DealInfo;
import com.ruoyi.web.service.IDealInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 交易信息Controller
 * 
 * @author shenzx
 * @date 2025-09-23
 */
@RestController
@RequestMapping("/deal/info")
public class DealInfoController extends BaseController
{
    private IDealInfoService dealInfoService;
    @Autowired
    public void setDealInfoService(IDealInfoService dealInfoService) {
        this.dealInfoService = dealInfoService;
    }

    @GetMapping("grye")
    @ApiOperation("个人余额")
    public AjaxResult grye(){
        return success(dealInfoService.grye());
    }

    @GetMapping("czzs")
    @ApiOperation("充值自审")
    public AjaxResult czzs(){
        QueryWrapper<DealInfo> qw = new QueryWrapper<>();
        qw.lambda().eq(DealInfo::getUserId,SecurityUtils.getUserId()).eq(DealInfo::getJylx,"1").eq(DealInfo::getJyzt,"2");
        return success(dealInfoService.list(qw));
    }

    /**
     * 查询交易信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(DealInfo dealInfo)
    {
        startPage();
        QueryWrapper<DealInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(dealInfo);
        List<DealInfo> dealInfoList = dealInfoService.list(queryWrapper);
        return getDataTable(dealInfoList);
    }

    /**
     * 导出交易信息列表
     */
    @Log(title = "交易信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DealInfo dealInfo)
    {
        QueryWrapper<DealInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(dealInfo);
        List<DealInfo> list = dealInfoService.list(queryWrapper);
        ExcelUtil<DealInfo> util = new ExcelUtil<>(DealInfo.class);
        util.exportExcel(response, list, "交易信息数据");
    }

    /**
     * 获取交易信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(dealInfoService.getById(id));
    }

    /**
     * 新增交易信息
     */
    @Log(title = "交易信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DealInfo dealInfo)
    {
        dealInfo.setJysj(DateUtils.getNowDate());
        if ("1".equals(dealInfo.getJyfs())) {
            BigDecimal grye = dealInfoService.grye();
            if (grye.compareTo(dealInfo.getAmount()) < 0) throw new RuntimeException("余额不足");
            dealInfo.setJyzt("1");
        }
        else dealInfo.setJyzt("2");
        dealInfo.setSzlx(getSzlx(dealInfo.getJylx()));
        dealInfo.setUserId(SecurityUtils.getUserId());
        dealInfoService.save(dealInfo);
        return success(dealInfo);
    }

    private String getSzlx(String jylx){
        switch (jylx) {
            case "1": return "1";
            case "2": return "2";
        }
        throw new RuntimeException("未定义的交易类型");
    }

    /**
     * 修改交易信息
     */
    @Log(title = "交易信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DealInfo dealInfo)
    {
        dealInfoService.updateById(dealInfo);
        return success(dealInfo);
    }

    /**
     * 删除交易信息
     */
    @Log(title = "交易信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(dealInfoService.removeByIds(Arrays.asList(ids)));
    }
}
