package com.ghy.web.controller.order;

import com.ghy.common.annotation.Log;
import com.ghy.common.constant.UserConstants;
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.enums.BusinessType;
import com.ghy.common.utils.poi.ExcelUtil;
import com.ghy.order.domain.OrderDetail;
import com.ghy.order.service.OrderDetailService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * 详细订单API
 *
 * @author HH 2022/4/25
 */
@Controller
@RequestMapping("/order/detail")
public class OrderDetailController extends BaseController {

    private final String prefix = "order/detail";

    @Resource
    private OrderDetailService orderDetailService;

    @RequiresPermissions("order:detail:view")
    @GetMapping()
    public String orderDetail() {
        return prefix;
    }

    @RequiresPermissions("order:detail:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OrderDetail orderDetail) {
        startPage();
        List<OrderDetail> list = orderDetailService.selectOrderDetailList(orderDetail);
        return getDataTable(list);
    }

    @Log(title = "详细订单管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("order:detail:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(OrderDetail orderDetail) {
        List<OrderDetail> list = orderDetailService.selectOrderDetailList(orderDetail);
        ExcelUtil<OrderDetail> util = new ExcelUtil<>(OrderDetail.class);
        return util.exportExcel(list, "详细订单数据");
    }

    @RequiresPermissions("order:detail:remove")
    @Log(title = "详细订单管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            return toAjax(orderDetailService.deleteOrderDetailByIds(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 修改详细订单
     */
    @RequiresPermissions("order:detail:edit")
    @GetMapping("/edit/{orderDetailId}")
    public String edit(@PathVariable("orderDetailId") Long orderDetailId, ModelMap mmap) {
        mmap.put("orderDetail", orderDetailService.selectById(orderDetailId));
        return prefix + "/edit";
    }

    /**
     * 修改保存详细订单
     */
    @RequiresPermissions("order:detail:edit")
    @Log(title = "详细订单管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated OrderDetail orderDetail) {
        if (UserConstants.ORDER_CODE_NOT_UNIQUE.equals(orderDetailService.checkOrderDetailCodeUnique(orderDetail))) {
            return error("修改详细订单'" + orderDetail.getCode() + "'失败，详细订单编码已存在");
        }
        orderDetail.setUpdateBy(getLoginName());
        return toAjax(orderDetailService.updateOrderDetail(orderDetail));
    }

    /**
     * 校验详细订单编码
     */
    @PostMapping("/checkOrderDetailCodeUnique")
    @ResponseBody
    public String checkOrderDetailCodeUnique(OrderDetail orderDetail) {
        return orderDetailService.checkOrderDetailCodeUnique(orderDetail);
    }
}
