package com.ghy.web.controller.order;

import com.ghy.common.annotation.Log;
import com.ghy.common.core.controller.BaseController;
import com.ghy.common.core.domain.AjaxResult;
import com.ghy.common.core.page.TableDataInfo;
import com.ghy.common.core.text.Convert;
import com.ghy.common.enums.BusinessType;
import com.ghy.common.utils.poi.ExcelUtil;
import com.ghy.order.domain.OrderGoods;
import com.ghy.order.domain.OrderMaster;
import com.ghy.order.service.OrderGoodsService;
import com.ghy.order.service.OrderMasterService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 订单商品API
 *
 * @author HH 2022/4/25
 */
@Controller
@RequestMapping("/order/goods")
public class OrderGoodsController extends BaseController {

    private final String prefix = "order/goods";

    @Resource
    private OrderGoodsService orderGoodsService;
    @Resource
    private OrderMasterService orderMasterService;

//    @RequiresPermissions("order:goods:view")
    @GetMapping()
    public String orderGoods(Long orderId, ModelMap mmap) {
        OrderMaster orderMaster = orderMasterService.selectById(orderId);
        mmap.put("orderId", orderId);
        mmap.put("orderMaster", orderMaster);
        return prefix;
    }

    @GetMapping(value = "/master")
    public String orderMasterGoods(Long orderId, ModelMap mmap) {
        OrderMaster orderMaster = orderMasterService.selectById(orderId);
        mmap.put("orderId", orderId);
        mmap.put("orderMaster", orderMaster);
        return "order/masterGoods";
    }

//    @RequiresPermissions("order:goods:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OrderGoods orderGoods) {
        startPage();
        List<OrderGoods> list = orderGoodsService.selectOrderGoodsList(orderGoods);
        return getDataTable(list);
    }

    @Log(title = "主订单管理", businessType = BusinessType.EXPORT)
//    @RequiresPermissions("order:goods:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(OrderGoods orderGoods) {
        List<OrderGoods> list = orderGoodsService.selectOrderGoodsList(orderGoods);
        ExcelUtil<OrderGoods> util = new ExcelUtil<>(OrderGoods.class);
        return util.exportExcel(list, "主订单数据");
    }

//    @RequiresPermissions("order:goods:remove")
    @Log(title = "主订单管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            Long[] idArray = Convert.toLongArray(ids);
            return toAjax(orderGoodsService.deleteOrderGoodsByIds(idArray));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 修改主订单
     */
//    @RequiresPermissions("order:goods:edit")
    @GetMapping("/edit/{orderGoodsId}")
    public String edit(@PathVariable("orderGoodsId") Long orderGoodsId, ModelMap mmap) {
        mmap.put("orderGoods", orderGoodsService.selectById(orderGoodsId));
        return prefix + "/edit";
    }

}
