package com.yiqi.admin.controller.order;

import com.yiqi.common.annotation.Log;
import com.yiqi.common.constant.UrlConstants;
import com.yiqi.common.core.controller.BaseController;
import com.yiqi.common.core.domain.AjaxResult;
import com.yiqi.common.core.domain.R;
import com.yiqi.common.core.page.TableDataInfo;
import com.yiqi.common.enums.BusinessType;
import com.yiqi.common.enums.SourceType;
import com.yiqi.common.utils.poi.ExcelUtil;
import com.yiqi.order.domain.OrderActivity;
import com.yiqi.order.domain.dto.ActivityOrderDTO;
import com.yiqi.order.domain.dto.OrderActivityQueryDTO;
import com.yiqi.order.domain.dto.OrderActivityRefundDTO;
import com.yiqi.order.domain.vo.OrderActivityVO;
import com.yiqi.order.service.IOrderActivityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 活动订单Controller
 *
 * @author ruoyi
 * @date 2023-09-30
 */
@Api(tags = "管理后台 - 活动订单")
@RestController
@RequestMapping(UrlConstants.managerApi + "/order/activity")
public class OrderActivityController extends BaseController {
    @Autowired
    private IOrderActivityService orderActivityService;

    /**
     * 查询活动订单列表
     */
    @PreAuthorize("@ss.hasPermi('order:activity:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询活动订单列表")
    public TableDataInfo list(OrderActivityQueryDTO orderActivityQueryDTO) {
        startPage();
        List<OrderActivityVO> list = orderActivityService.selectOrderActivityList(orderActivityQueryDTO);
        return getDataTable(list);
    }

    /**
     * 导出活动订单列表
     */
    @PreAuthorize("@ss.hasPermi('order:activity:export')")
    @Log(title = "活动订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出活动订单列表")
    public void export(HttpServletResponse response, OrderActivityQueryDTO orderActivityQueryDTO) {
        orderActivityQueryDTO.setPageSizeExport(50000);
        List<OrderActivityVO> list = orderActivityService.selectOrderActivityList(orderActivityQueryDTO);
        ExcelUtil<OrderActivityVO> util = new ExcelUtil<OrderActivityVO>(OrderActivityVO.class);
        util.exportExcel(response, list, "活动订单数据");
    }

    /**
     * 获取活动订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('order:activity:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取活动订单详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(orderActivityService.selectOrderActivityById(id));
    }

    /**
     * 新增活动订单
     */
    @Transactional(rollbackFor = Exception.class)
    @PreAuthorize("@ss.hasPermi('order:activity:add')")
    @Log(title = "活动订单", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增活动订单")
    public R add(@RequestBody ActivityOrderDTO activityOrderDTO) {
        return R.ok(orderActivityService.insertOrderActivity(activityOrderDTO));
    }

    /**
     * 修改活动订单
     */
    @PreAuthorize("@ss.hasPermi('order:activity:edit')")
    @Log(title = "活动订单", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改活动订单")
    public AjaxResult edit(@RequestBody OrderActivity orderActivity) {
        orderActivity.setUpdateById(getUserId());
        orderActivity.setUpdateBy(getUsername());
        return toAjax(orderActivityService.updateOrderActivity(orderActivity));
    }

    /**
     * 删除活动订单
     */
    @PreAuthorize("@ss.hasPermi('order:activity:remove')")
    @Log(title = "活动订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除活动订单")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(orderActivityService.deleteOrderActivityByIds(ids));
    }


    /**
     * 活动订单-总部撤单
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/refundOrderActivity")
    @ApiOperation(value = "活动订单-总部撤单")
    public R refundOrderActivity(@RequestBody @Valid OrderActivityRefundDTO orderActivityRefundDTO) {
        return R.ok(orderActivityService.refundOrderActivityApply(orderActivityRefundDTO, SourceType.MANAGER.getCode()));
    }
}
