package cn.newfeifan.mall.module.distri.controller.admin.applicationforwithdrawal;

import cn.newfeifan.mall.module.distri.enums.WithdrawalChannelTypeEnum;
import cn.newfeifan.mall.module.member.dal.dataobject.user.MemberUserDO;
import cn.newfeifan.mall.module.member.service.user.MemberUserService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.newfeifan.mall.framework.common.pojo.PageParam;
import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.common.pojo.CommonResult;
import cn.newfeifan.mall.framework.common.util.object.BeanUtils;
import static cn.newfeifan.mall.framework.common.pojo.CommonResult.success;

import cn.newfeifan.mall.framework.excel.core.util.ExcelUtils;

import cn.newfeifan.mall.framework.operatelog.core.annotations.OperateLog;
import static cn.newfeifan.mall.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.newfeifan.mall.module.distri.controller.admin.applicationforwithdrawal.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.applicationforwithdrawal.ApplicationForWithdrawalDO;
import cn.newfeifan.mall.module.distri.service.applicationforwithdrawal.ApplicationForWithdrawalService;

@Tag(name = "管理后台 - 提现申请记录")
@RestController
@RequestMapping("/distri/application-for-withdrawal")
@Validated
public class ApplicationForWithdrawalController {

    @Resource
    private ApplicationForWithdrawalService applicationForWithdrawalService;

    @Resource
    private MemberUserService memberUserService;

    @PostMapping("/create")
    @Operation(summary = "创建提现申请记录")
    @PreAuthorize("@ss.hasPermission('distri:application-for-withdrawal:create')")
    public CommonResult<Long> createApplicationForWithdrawal(@Valid @RequestBody ApplicationForWithdrawalSaveReqVO createReqVO) {
        return success(applicationForWithdrawalService.createApplicationForWithdrawal(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新提现申请记录")
    @PreAuthorize("@ss.hasPermission('distri:application-for-withdrawal:update')")
    public CommonResult<Boolean> updateApplicationForWithdrawal(@Valid @RequestBody ApplicationForWithdrawalSaveReqVO updateReqVO) {
        applicationForWithdrawalService.updateApplicationForWithdrawal(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除提现申请记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('distri:application-for-withdrawal:delete')")
    public CommonResult<Boolean> deleteApplicationForWithdrawal(@RequestParam("id") Long id) {
        applicationForWithdrawalService.deleteApplicationForWithdrawal(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得提现申请记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('distri:application-for-withdrawal:query')")
    public CommonResult<ApplicationForWithdrawalRespVO> getApplicationForWithdrawal(@RequestParam("id") Long id) {
        ApplicationForWithdrawalDO applicationForWithdrawal = applicationForWithdrawalService.getApplicationForWithdrawal(id);
        return success(BeanUtils.toBean(applicationForWithdrawal, ApplicationForWithdrawalRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得提现申请记录分页")
    @PreAuthorize("@ss.hasPermission('distri:application-for-withdrawal:query')")
    public CommonResult<PageResult<ApplicationForWithdrawalRespVO>> getApplicationForWithdrawalPage(@Valid ApplicationForWithdrawalPageReqVO pageReqVO) {
        PageResult<ApplicationForWithdrawalDO> pageResult = applicationForWithdrawalService.getApplicationForWithdrawalPage(pageReqVO);
        PageResult<ApplicationForWithdrawalRespVO> bean = BeanUtils.toBean(pageResult, ApplicationForWithdrawalRespVO.class);

        for (ApplicationForWithdrawalRespVO applicationForWithdrawalRespVO : bean.getList()) {
            MemberUserDO user = memberUserService.getUser(applicationForWithdrawalRespVO.getUserId());

            if(applicationForWithdrawalRespVO.getWithdrawalType().equals(WithdrawalChannelTypeEnum.ALIPAY.getType())){
                applicationForWithdrawalRespVO.setAccountName(user.getAlipayName());
            }else if(applicationForWithdrawalRespVO.getWithdrawalType().equals(WithdrawalChannelTypeEnum.BANK.getType())){
                applicationForWithdrawalRespVO.setAccountName(user.getAccountName());
            }
        }

        return success(bean);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出提现申请记录 Excel")
    @PreAuthorize("@ss.hasPermission('distri:application-for-withdrawal:export')")
    @OperateLog(type = EXPORT)
    public void exportApplicationForWithdrawalExcel(@Valid ApplicationForWithdrawalPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ApplicationForWithdrawalDO> list = applicationForWithdrawalService.getApplicationForWithdrawalPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "提现申请记录.xls", "数据", ApplicationForWithdrawalRespVO.class,
                        BeanUtils.toBean(list, ApplicationForWithdrawalRespVO.class));
    }

}