package com.ruoyi.cai.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.cai.domain.AccountCash;
import com.ruoyi.cai.dto.admin.vo.AccountCashAdminVo;
import com.ruoyi.cai.dto.admin.vo.account.ExportBatchAuditVo;
import com.ruoyi.cai.enums.AccountCashStatusEnum;
import com.ruoyi.cai.service.AccountCashService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户提现记录
 *
 * @author 77
 * @date 2023-12-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/cai/accountCash")
@Slf4j
public class AccountCashController extends BaseController {

    private final AccountCashService accountCashService;

    /**
     * 查询用户提现记录列表
     */
    @SaCheckPermission("cai:accountCash:list")
    @GetMapping("/list")
    public TableDataInfo<AccountCashAdminVo> list(AccountCashAdminVo bo, PageQuery pageQuery) {
        Page<AccountCashAdminVo> page = accountCashService.pageAdmin(pageQuery, bo);
        return TableDataInfo.build(page);
    }

    /**
     * 导出用户提现记录列表
     */
    @SaCheckPermission("cai:accountCash:export")
    @Log(title = "用户提现记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AccountCashAdminVo bo, HttpServletResponse response) {
        PageQuery query = new PageQuery();
        query.setPageSize(Integer.MAX_VALUE);
        query.setPageNum(1);
        List<AccountCashAdminVo> list = accountCashService.pageAdmin(query,bo).getRecords();
        ExcelUtil.exportExcel(list, "用户提现记录", AccountCashAdminVo.class, response);
    }

    @Log(title = "用户导入提现通过", businessType = BusinessType.IMPORT)
    @SaCheckPermission("cai:accountCash:edit")
    @PostMapping(value = "/importAudit", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<ExportBatchAuditVo> importData(@RequestPart("file") MultipartFile file) throws Exception {
        List<AccountCashAdminVo> result = ExcelUtil.importExcel(file.getInputStream(), AccountCashAdminVo.class);
        List<Long> ids = result.stream().map(AccountCash::getId).collect(Collectors.toList());
        ExportBatchAuditVo audit = accountCashService.batchAudit(ids);
        return R.ok(audit);
    }

    /**
     * 获取用户提现记录详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("cai:accountCash:query")
    @GetMapping("/{id}")
    public R<AccountCash> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Integer id) {
        return R.ok(accountCashService.getById(id));
    }

    @SaCheckPermission("cai:accountCash:edit")
    @Log(title = "用户提现通过", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @GetMapping("/success")
    public R<Void> success(Long id) {
        accountCashService.success(id);
        return R.ok();
    }

    @SaCheckPermission("cai:accountCash:edit")
    @Log(title = "用户提现不通过", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @GetMapping("/fail")
    public R<Void> fail(Long id) {
        accountCashService.fail(id);
        return R.ok();
    }


    @SaCheckPermission("cai:accountCash:edit")
    @Log(title = "用户提现取消", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @GetMapping("/close")
    public R<Void> close(Long id) {
        accountCashService.update(Wrappers.lambdaUpdate(AccountCash.class)
                .set(AccountCash::getVerifyTime, LocalDateTime.now())
                .set(AccountCash::getOperateIp, ServletUtils.getClientIP())
                .set(AccountCash::getStatus, AccountCashStatusEnum.CLOSE.getCode())
                .eq(AccountCash::getStatus, AccountCashStatusEnum.READY.getCode())
                .eq(AccountCash::getId,id));
        return R.ok();
    }

    @SaCheckPermission("cai:accountCash:edit")
    @Log(title = "用户提现批量通过", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @GetMapping("/batchSuccess")
    public R<ExportBatchAuditVo> batchSuccess(String ids) {
        List<Long> idList = Arrays.stream(ids.split(",")).map(Long::valueOf).collect(Collectors.toList());
        ExportBatchAuditVo audit = accountCashService.batchAudit(idList);
        return R.ok(audit);
    }

}
