package cn.iocoder.yudao.module.member.controller.admin.refund;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.module.member.dal.dataobject.order.DishOrderDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.dal.mysql.order.DishOrderMapper;
import cn.iocoder.yudao.module.member.dal.mysql.user.MemberUserMapper;
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 cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.member.controller.admin.refund.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.refund.RefundDO;
import cn.iocoder.yudao.module.member.service.refund.RefundService;

@Tag(name = "管理后台 - 退款审核")
@RestController
@RequestMapping("/member/refund")
@Validated
public class RefundController {

    @Resource
    private RefundService refundService;

    @Resource
    private DishOrderMapper orderMapper;

    @PostMapping("/create")
    @Operation(summary = "创建退款审核")
    @PreAuthorize("@ss.hasPermission('member:refund:create')")
    public CommonResult<Long> createRefund(@Valid @RequestBody RefundSaveReqVO createReqVO) {
        return success(refundService.createRefund(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新退款审核")
    @PreAuthorize("@ss.hasPermission('member:refund:update')")
    public CommonResult<Boolean> updateRefund(@Valid @RequestBody RefundSaveReqVO updateReqVO) {
        refundService.updateRefund(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除退款审核")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:refund:delete')")
    public CommonResult<Boolean> deleteRefund(@RequestParam("id") Long id) {
        refundService.deleteRefund(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得退款审核")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:refund:query')")
    public CommonResult<RefundRespVO> getRefund(@RequestParam("id") Long id) {
        RefundDO refund = refundService.getRefund(id);
        return success(BeanUtils.toBean(refund, RefundRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得退款审核分页")
    @PreAuthorize("@ss.hasPermission('member:refund:query')")
    public CommonResult<PageResult<RefundRespVO>> getRefundPage(@Valid RefundPageReqVO pageReqVO) {
        PageResult<RefundDO> pageResult = refundService.getRefundPage(pageReqVO);
        PageResult<RefundRespVO> bean = BeanUtils.toBean(pageResult, RefundRespVO.class);
        if(CollectionUtil.isNotEmpty(bean.getList())){
            for (RefundRespVO vo : bean.getList()){
                DishOrderDO dishOrderDO = orderMapper.selectById(vo.getOrderId());
                if(ObjectUtil.isNotEmpty(dishOrderDO)){
                    vo.setRefundAmount(dishOrderDO.getRefundAmount());
                }else {
                    vo.setRefundAmount(BigDecimal.ZERO);
                }

            }
        }
        return success(bean);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出退款审核 Excel")
    @PreAuthorize("@ss.hasPermission('member:refund:export')")
    @OperateLog(type = EXPORT)
    public void exportRefundExcel(@Valid RefundPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RefundDO> list = refundService.getRefundPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "退款审核.xls", "数据", RefundRespVO.class,
                        BeanUtils.toBean(list, RefundRespVO.class));
    }

}