package com.limap.controller.manager;

import java.util.List;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.limap.common.core.domain.entity.SysRole;
import com.limap.core.basic.vo.BasicUserWithdrawVo;
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.limap.common.log.annotation.Log;
import io.swagger.annotations.Api;
import com.limap.common.core.controller.BaseController;
import com.limap.common.core.domain.AjaxResult;
import com.limap.common.enums.BusinessType;
import io.swagger.annotations.ApiOperation;
import com.limap.core.basic.domain.BasicUserWithdraw;
import com.limap.core.basic.service.IBasicUserWithdrawService;
import com.limap.common.utils.poi.ExcelUtil;
import com.limap.common.core.page.TableDataInfo;

/**
 * 提现记录Controller
 *
 * @author jjb
 * @date 2025-04-11
 */
@Api(tags = "提现记录")
@RestController
@RequestMapping("/basic/withdraw")
public class BasicUserWithdrawController extends BaseController {
    @Autowired
    private IBasicUserWithdrawService basicUserWithdrawService;

    /**
     * 查询提现记录列表
     */
    @ApiOperation("查询提现记录列表")
    @PreAuthorize("@ss.hasPermi('basic:withdraw:list')")
    @GetMapping("/list")
    public TableDataInfo list(BasicUserWithdraw basicUserWithdraw) {
        startPage();
        List<SysRole> roles = getLoginUser().getUser().getRoles();
        if(roles.stream().noneMatch(role -> role.getRoleId().compareTo(100L) <0)){
            basicUserWithdraw.setUserId(getUserId());
        }
        List<BasicUserWithdraw> list = basicUserWithdrawService.selectBasicUserWithdrawList(basicUserWithdraw);
        return getDataTable(list);
    }

    /**
     * 导出提现记录列表
     */
    @ApiOperation("导出提现记录列表")
    @PreAuthorize("@ss.hasPermi('basic:withdraw:export')")
    @Log(title = "提现记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BasicUserWithdraw basicUserWithdraw) {
        List<BasicUserWithdraw> list = basicUserWithdrawService.selectBasicUserWithdrawList(basicUserWithdraw);
        ExcelUtil<BasicUserWithdraw> util = new ExcelUtil<BasicUserWithdraw>(BasicUserWithdraw.class);
        util.exportExcel(response, list, "提现记录数据");
    }

    @ApiOperation("用户提现")
    @PreAuthorize("@ss.hasPermi('basic:withdraw:add')")
    @Log(title = "用户提现", businessType = BusinessType.INSERT)
    @PostMapping("user/withdraw")
    public AjaxResult addUserWithdraw(@RequestBody BasicUserWithdrawVo basicUserWithdraw) {
        basicUserWithdraw.setUserId(getUserId());
        return basicUserWithdrawService.addUserWithdraw(basicUserWithdraw);
    }


    /**
     * 获取提现记录详细信息
     */
    @ApiOperation("获取提现记录详细信息")
    @PreAuthorize("@ss.hasPermi('basic:withdraw:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(basicUserWithdrawService.getById(id));
    }

    /**
     * 新增提现记录
     */
    @ApiOperation("新增提现记录")
    @PreAuthorize("@ss.hasPermi('basic:withdraw:add')")
    @Log(title = "提现记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BasicUserWithdraw basicUserWithdraw) {
        return toAjax(basicUserWithdrawService.save(basicUserWithdraw));
    }

    /**
     * 修改提现记录
     */
    @ApiOperation("修改提现记录")
    @PreAuthorize("@ss.hasPermi('basic:withdraw:edit')")
    @Log(title = "提现记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BasicUserWithdraw basicUserWithdraw) {
        boolean result = basicUserWithdrawService.updateById(basicUserWithdraw);
        if(result){
            if("9".equals(basicUserWithdraw.getPaymentStatus())){

            }
            if("1".equals(basicUserWithdraw.getPaymentStatus())){
                basicUserWithdrawService.updatePayment(basicUserWithdraw.getId(),basicUserWithdraw.getPaymentAmount());
            }

        }
        return toAjax(result);
    }

    /**
     * 删除提现记录
     */
    @ApiOperation("删除提现记录")
    @PreAuthorize("@ss.hasPermi('basic:withdraw:remove')")
    @Log(title = "提现记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(basicUserWithdrawService.removeByIds(Arrays.asList(ids)));
    }
}
