package com.willalp.web.controller.canteen;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.willalp.canteen.domain.HsGiftOrderDetail;
import com.willalp.canteen.domain.resp.OrderResp;
import com.willalp.canteen.domain.resp.ShopOrderStatExcel;
import com.willalp.canteen.service.IHsGiftOrderDetailService;
import com.willalp.common.annotation.Log;
import com.willalp.common.core.controller.BaseController;
import com.willalp.common.core.domain.AjaxResult;
import com.willalp.common.core.page.TableDataInfo;
import com.willalp.common.enums.BusinessType;
import com.willalp.common.utils.poi.ExcelUtil;
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 java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 订单明细Controller
 *
 * @author songyu
 * @date 2021-12-03
 */
@RestController
@RequestMapping("/gift/detail")
public class HsGiftOrderDetailController extends BaseController {
    @Autowired
    private IHsGiftOrderDetailService hsCanteenOrderDetailService;

    /**
     * 查询订购统计列表
     */
    @GetMapping("/shopOrder/stat")
    public AjaxResult shopOrderStat(@RequestParam("eventId") String eventId) {
        return AjaxResult.success(hsCanteenOrderDetailService.shopOrderStatList(eventId));
    }

    /**
     * 查询订单明细列表
     */
    @GetMapping("/list")
    public TableDataInfo list(HsGiftOrderDetail hsGiftOrderDetail) {
        startPage();
        List<OrderResp> list = hsCanteenOrderDetailService.selectHsCanteenOrderDetailList(hsGiftOrderDetail);
        return getDataTable(list);
    }

    /**
     * 导出订购统计列表
     */
    @Log(title = "订购统计", businessType = BusinessType.EXPORT)
    @GetMapping("/shopOrderStatExport")
    public AjaxResult shopOrderStatExport(@RequestParam("eventId") String eventId) {
        List<HsGiftOrderDetail> hsGiftOrderDetails = hsCanteenOrderDetailService.shopOrderStatList(eventId);
        List<ShopOrderStatExcel> list = new ArrayList<>();
        if (hsGiftOrderDetails.size() > 0) {
            hsGiftOrderDetails.forEach(item -> {
                ShopOrderStatExcel shopOrderStatExcel = new ShopOrderStatExcel();
                BeanUtils.copyProperties(item, shopOrderStatExcel);
                list.add(shopOrderStatExcel);
            });
        }
        ExcelUtil<ShopOrderStatExcel> util = new ExcelUtil<>(ShopOrderStatExcel.class);
        return util.exportExcel(list, "订单明细数据");
    }

    /**
     * 导出订单明细列表
     */
    @Log(title = "订单明细", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HsGiftOrderDetail hsGiftOrderDetail) {
        List<OrderResp> list = hsCanteenOrderDetailService.selectHsCanteenOrderDetailList(hsGiftOrderDetail);
        ExcelUtil<OrderResp> util = new ExcelUtil<OrderResp>(OrderResp.class);
        return util.exportExcel(list, "订单明细数据");
    }

    /**
     * 获取订单明细详细信息
     */
    @GetMapping(value = "/{detailId}")
    public AjaxResult getInfo(@PathVariable("detailId") String detailId) {
        return AjaxResult.success(hsCanteenOrderDetailService.getById(detailId));
    }

    /**
     * 新增订单明细
     */
    @PreAuthorize("@ss.hasPermi('canteen:detail:add')")
    @Log(title = "订单明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HsGiftOrderDetail hsGiftOrderDetail) {
        while (true) {
            String idStr = IdWorker.getIdStr();
            HsGiftOrderDetail byId = hsCanteenOrderDetailService.getById(idStr);
            if (null == byId) {
                hsGiftOrderDetail.setDetailId(idStr);
                break;
            }
        }
        return toAjax(hsCanteenOrderDetailService.save(hsGiftOrderDetail));
    }

    /**
     * 修改订单明细
     */
    @Log(title = "订单明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HsGiftOrderDetail hsGiftOrderDetail) {
        return toAjax(hsCanteenOrderDetailService.updateById(hsGiftOrderDetail));
    }

    /**
     * 删除订单明细
     */
    @Log(title = "订单明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{detailIds}")
    public AjaxResult remove(@PathVariable String[] detailIds) {
        return toAjax(hsCanteenOrderDetailService.removeByIds(Arrays.asList(detailIds)));
    }
}
