package com.yonge.cooleshow.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;

import com.yonge.cooleshow.biz.dal.dto.excel.UserOrderExport;
import com.yonge.toolset.mybatis.support.PageUtil;
import com.yonge.cooleshow.biz.dal.vo.UserOrderVo;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.base.page.PageInfo;
import com.yonge.toolset.utils.easyexcel.ExcelUtils;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.yonge.cooleshow.biz.dal.dto.search.OrderSearch;
import com.yonge.cooleshow.biz.dal.service.UserOrderService;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/userOrder")
@Api(value = "订单管理", tags = "订单管理")
public class UserOrderController extends BaseController {

    @Autowired
    private UserOrderService userOrderService;

    /**
     * 查询单条
     */
    @GetMapping("/detail/{id}")
    @ApiOperation(value = "详情", notes = "传入id")
    @PreAuthorize("@pcs.hasPermissions('userOrder/detail')")
    public HttpResponseResult<UserOrderVo> detail(@PathVariable("id") Long id) {
        return succeed(userOrderService.detail(id));
    }

    /**
     * 查询分页
     */
    @PostMapping("/page")
    @ApiOperation(value = "查询分页", notes = "传入orderSearch")
    @PreAuthorize("@pcs.hasPermissions('userOrder/page')")
    public HttpResponseResult<PageInfo<UserOrderVo>> page(@RequestBody OrderSearch query) {
        IPage<UserOrderVo> pages = userOrderService.selectPage(PageUtil.getPage(query), query);
        return succeed(PageUtil.pageInfo(pages));
    }

    /**
     * 查询导出
     */
    @PostMapping("/export")
    @ApiOperation(value = "查询导出", notes = "传入orderSearch")
    @PreAuthorize("@pcs.hasPermissions('userOrder/export')")
    public void export(@RequestBody OrderSearch query) {
        List<UserOrderVo> userOrderVos = userOrderService.selectAllList(query);
        List<UserOrderExport> list = new ArrayList<>();
        userOrderVos.forEach(o -> {
            UserOrderExport export = new UserOrderExport();
            BeanUtils.copyProperties(o, export);
            if (null == export.getFeeAmt()) {
                export.setFeeAmt(BigDecimal.ZERO);
            }
            list.add(export);
        });
        ExcelUtils.exportExcel(list, "订单列表数据" + System.currentTimeMillis(),
                "订单列表数据");
    }
}