package co.yixiang.yshop.module.distributor.controller.admin.distributorwageslog;

import co.yixiang.yshop.module.distributor.controller.admin.distributor.vo.DistributorApplyReqVO;
import co.yixiang.yshop.module.distributor.controller.admin.distributorwithdrawalmethod.vo.DistributorWithdrawalMethodExportReqVO;
import co.yixiang.yshop.module.distributor.dal.dataobject.distributorwithdrawalmethod.DistributorWithdrawalMethodDO;
import co.yixiang.yshop.module.distributor.enums.DistributorWagesLogTypeEnum;
import co.yixiang.yshop.module.distributor.enums.DistributorWithdrawalMethodTypeEnum;
import co.yixiang.yshop.module.distributor.service.distributorwageslog.DistributorWagesLogFacade;
import co.yixiang.yshop.module.distributor.service.distributorwithdrawalmethod.DistributorWithdrawalMethodService;
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.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.math.BigDecimal;
import java.util.*;
import java.io.IOException;

import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.common.pojo.CommonResult;

import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;

import co.yixiang.yshop.framework.excel.core.util.ExcelUtils;

import co.yixiang.yshop.framework.operatelog.core.annotations.OperateLog;

import static co.yixiang.yshop.framework.operatelog.core.enums.OperateTypeEnum.*;

import co.yixiang.yshop.module.distributor.controller.admin.distributorwageslog.vo.*;
import co.yixiang.yshop.module.distributor.dal.dataobject.distributorwageslog.DistributorWagesLogDO;
import co.yixiang.yshop.module.distributor.convert.distributorwageslog.DistributorWagesLogConvert;
import co.yixiang.yshop.module.distributor.service.distributorwageslog.DistributorWagesLogService;

@Tag(name = "管理后台 - 分销商佣金记录")
@RestController
@RequestMapping("/distributor/wages-log")
@Validated
public class DistributorWagesLogController {

    @Resource
    private DistributorWagesLogService wagesLogService;

    @Resource
    private DistributorWagesLogFacade distributorWagesLogFacade;

    @Resource
    private DistributorWithdrawalMethodService distributorWithdrawalMethodService;

    @PostMapping("/create")
    @Operation(summary = "创建分销商佣金记录")
    @PreAuthorize("@ss.hasPermission('distributor:wages-log:create')")
    public CommonResult<Long> createWagesLog(@Valid @RequestBody DistributorWagesLogCreateReqVO createReqVO) {
        return success(wagesLogService.createWagesLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新分销商佣金记录")
    @PreAuthorize("@ss.hasPermission('distributor:wages-log:update')")
    public CommonResult<Boolean> updateWagesLog(@Valid @RequestBody DistributorWagesLogUpdateReqVO updateReqVO) {
        wagesLogService.updateWagesLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除分销商佣金记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('distributor:wages-log:delete')")
    public CommonResult<Boolean> deleteWagesLog(@RequestParam("id") Long id) {
        wagesLogService.deleteWagesLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得分销商佣金记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('distributor:wages-log:query')")
    public CommonResult<DistributorWagesLogRespVO> getWagesLog(@RequestParam("id") Long id) {
        DistributorWagesLogDO wagesLog = wagesLogService.getWagesLog(id);
        return success(DistributorWagesLogConvert.INSTANCE.convert(wagesLog));
    }

    @GetMapping("/list")
    @Operation(summary = "获得分销商佣金记录列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('distributor:wages-log:query')")
    public CommonResult<List<DistributorWagesLogRespVO>> getWagesLogList(@RequestParam("ids") Collection<Long> ids) {
        List<DistributorWagesLogDO> list = wagesLogService.getWagesLogList(ids);
        return success(DistributorWagesLogConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得分销商佣金记录分页")
    @PreAuthorize("@ss.hasPermission('distributor:wages-log:query')")
    public CommonResult<PageResult<DistributorWagesLogRespVO>> getWagesLogPage(@Valid DistributorWagesLogPageReqVO pageVO) {
        PageResult<DistributorWagesLogDO> pageResult = wagesLogService.getWagesLogPage(pageVO);
        return success(DistributorWagesLogConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/verify-page")
    @Operation(summary = "获得分销商佣金提现记录分页")
    @PreAuthorize("@ss.hasPermission('distributor:wages-log:query')")
    public CommonResult<PageResult<DistributorWagesLogRespVO>> getWagesLogVerifyPage(@Valid DistributorWagesLogPageReqVO pageVO) {
        List<Integer> list = new ArrayList<>();
        list.add(DistributorWagesLogTypeEnum.WITHDRAW_WAIT.getType());
        list.add(DistributorWagesLogTypeEnum.WITHDRAW_COMPLETE.getType());
        list.add(DistributorWagesLogTypeEnum.WITHDRAW_FAIL.getType());
        pageVO.setTypeList(list);
        PageResult<DistributorWagesLogDO> pageResult = wagesLogService.getWagesLogPage(pageVO);
        return success(DistributorWagesLogConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出分销商佣金记录 Excel")
    @PreAuthorize("@ss.hasPermission('distributor:wages-log:export')")
    @OperateLog(type = EXPORT)
    public void exportWagesLogExcel(@Valid DistributorWagesLogExportReqVO exportReqVO,
                                    HttpServletResponse response) throws IOException {
        List<DistributorWithdrawExcelVO> datas = new ArrayList<>();
        // 获取所有记录
        List<Integer> typeList = new ArrayList<>();
        typeList.add(DistributorWagesLogTypeEnum.WITHDRAW_WAIT.getType());
        typeList.add(DistributorWagesLogTypeEnum.WITHDRAW_COMPLETE.getType());
        typeList.add(DistributorWagesLogTypeEnum.WITHDRAW_FAIL.getType());

        exportReqVO.setTypeList(typeList);
        exportReqVO.setMethodType(DistributorWithdrawalMethodTypeEnum.AIL_PAY.getType());
        List<DistributorWagesLogDO> list = wagesLogService.getWagesLogList(exportReqVO);
        for (DistributorWagesLogDO wagesLogDO : list) {
            DistributorWithdrawExcelVO excelVO = new DistributorWithdrawExcelVO();
            excelVO.setId(wagesLogDO.getId());
            excelVO.setPaymentMethod(DistributorWithdrawalMethodTypeEnum.AIL_PAY.getPaymentMethod());
            excelVO.setUserName(wagesLogDO.getDistributorName());
            List<DistributorWithdrawalMethodDO> withdrawalMethodList = distributorWithdrawalMethodService.getWithdrawalMethodList(
                    new DistributorWithdrawalMethodExportReqVO().setType(wagesLogDO.getMethodType()).setDistributorId(wagesLogDO.getDistributorId())
            );
            DistributorWithdrawalMethodDO distributorWithdrawalMethodDO = withdrawalMethodList.get(0);
            excelVO.setAccountNum(distributorWithdrawalMethodDO.getAccountNum());
            if (wagesLogDO.getType().equals(DistributorWagesLogTypeEnum.WITHDRAW_WAIT.getType())) {
                excelVO.setStatusStr(DistributorWagesLogTypeEnum.WITHDRAW_WAIT.getName());
            } else if (wagesLogDO.getType().equals(DistributorWagesLogTypeEnum.WITHDRAW_COMPLETE.getType())) {
                excelVO.setStatusStr(DistributorWagesLogTypeEnum.WITHDRAW_COMPLETE.getName());
            }
            if (wagesLogDO.getType().equals(DistributorWagesLogTypeEnum.WITHDRAW_FAIL.getType())) {
                excelVO.setStatusStr(DistributorWagesLogTypeEnum.WITHDRAW_FAIL.getName());
            }
            excelVO.setAmountStr(wagesLogDO.getAmount().setScale(2, BigDecimal.ROUND_HALF_UP).toString());
            datas.add(excelVO);
        }

        ExcelUtils.write(response, "分销商提现记录.xls", "数据", DistributorWithdrawExcelVO.class, datas);
    }

    @GetMapping("/get-verify")
    @Operation(summary = "提现详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('distributor:wages-log:query')")
    public CommonResult<DistributorWagesLogRespVO> getVerify(@RequestParam("id") Long id) {
        DistributorWagesLogDO wagesLog = wagesLogService.getWagesLog(id);
        DistributorWagesLogRespVO resp = DistributorWagesLogConvert.INSTANCE.convert(wagesLog);
        if (wagesLog.getMethodType().equals(DistributorWithdrawalMethodTypeEnum.AIL_PAY.getType())) {
            List<DistributorWithdrawalMethodDO> withdrawalMethodList = distributorWithdrawalMethodService.getWithdrawalMethodList(
                    new DistributorWithdrawalMethodExportReqVO().setType(wagesLog.getMethodType()).setDistributorId(wagesLog.getDistributorId())
            );
            DistributorWithdrawalMethodDO distributorWithdrawalMethodDO = withdrawalMethodList.get(0);
            resp.setPaymentMethod(distributorWithdrawalMethodDO.getPaymentMethod());
            resp.setRealName(distributorWithdrawalMethodDO.getRealName());
            resp.setAccountNum(distributorWithdrawalMethodDO.getAccountNum());
        }
        return success(resp);
    }

    // 提现审核通过
    @PostMapping("/verify")
    @Operation(summary = "审核提现记录 4-通过 5-拒绝")
    public CommonResult<Boolean> verify(@Valid @RequestBody DistributorWagesLogVerifyReqVO reqVO) {
        distributorWagesLogFacade.verify(reqVO);
        return success(Boolean.TRUE);
    }

}
