package com.younike.merchant;

import com.younike.common.annotation.Log;
import com.younike.common.core.controller.BaseController;
import com.younike.common.core.domain.AjaxResult;
import com.younike.common.core.page.TableDataInfo;
import com.younike.common.enums.BusinessType;
import com.younike.common.utils.StringUtils;
import com.younike.common.utils.poi.ExcelUtil;
import com.younike.system.domain.vo.OrdersVO;
import com.younike.system.param.OrderQueryParam;
import com.younike.system.service.IMerchantOrderService;
import com.younike.system.service.IOrderAddressService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单主Controller
 *
 * @author jubs
 * @date 2022-01-05
 */
@RestController
@RequestMapping("/system/order")
public class MerchantOrderController extends BaseController {
    @Autowired
    private IMerchantOrderService orderService;
    @Autowired
    private IOrderAddressService orderAddressService;

    /**
     * 查询订单主列表
     */
    @GetMapping("/list")
    @PreAuthorize("@ss.hasRole('merchant')")
    public TableDataInfo list(OrdersVO order)
    {
        List<OrdersVO> list = orderService.selectOrderList(order);
        return getDataTable(list);
    }

    /**
     * 导出订单主列表
     */
    @PreAuthorize("@ss.hasRole('merchant')")
    @Log(title = "订单主", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(OrdersVO order)
    {
        List<OrdersVO> list = orderService.selectOrderList(order);
        ExcelUtil<OrdersVO> util = new ExcelUtil<OrdersVO>(OrdersVO.class);
        return util.exportExcel(list, "订单主数据");
    }

    /**
     * 获取订单主详细信息
     */
    @GetMapping(value = "/{orderId}")
    @PreAuthorize("@ss.hasAnyRoles({'merchant','operation'})")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId)
    {
        return AjaxResult.success(orderService.selectOrderByOrderId(orderId));
    }

    /**
     * 新增订单主
     */
    @PreAuthorize("@ss.hasRole('merchant')")
    @Log(title = "订单主", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrdersVO order)
    {
        return toAjax(orderService.insertOrder(order));
    }

    /**
     * 修改订单主
     * 修改上门时间
     */
    @PreAuthorize("@ss.hasRole('merchant')")
    @Log(title = "订单主", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrdersVO order){
//        return toAjax(orderService.updateOrder(order));
        if(ObjectUtils.isEmpty(order) || order.getOrderId() == null || ObjectUtils.isEmpty(order.getAppointmentStartTime())){
            return AjaxResult.error("参数错误");
        }
        return orderService.updateServiceStartTime(order);
    }

    @PreAuthorize("@ss.hasRole('merchant')")
    @Log(title = "修改单价", businessType = BusinessType.UPDATE)
    @PutMapping("modifyPrice")
    public AjaxResult modifyPrice(@RequestBody OrdersVO order)
    {
        return toAjax(orderService.modifyPrice(order));
    }

    /**
     * 删除订单主
     */
    @PreAuthorize("@ss.hasRole('merchant')")
    @Log(title = "订单主", businessType = BusinessType.DELETE)
    @DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds)
    {
        return toAjax(orderService.deleteOrderByOrderIds(orderIds));
    }

    @ApiOperation("获取订单服务地址详细信息")
    @PreAuthorize("@ss.hasAnyRoles({'merchant','operation'})")
    @GetMapping(value = "getAddressInfo/{orderId}")
    public AjaxResult getAddressInfo(@PathVariable("orderId") Long orderId) {
        return AjaxResult.success(orderAddressService.selectOrderAddressByOrderId(orderId));
    }


    /**
     * 修改订单主
     * 修改上门时间(校验修改时间)
     */
    @GetMapping("/editVerify")
    public AjaxResult editVerify(OrdersVO order){
        if(ObjectUtils.isEmpty(order) || order.getOrderId() == null || ObjectUtils.isEmpty(order.getAppointmentStartTime()) || StringUtils.isEmpty(order.getServerId())){
            return AjaxResult.error("参数错误");
        }
        return orderService.editVerify(order);
    }

    /**
     * 查询团购订单列表
     */
    @ApiOperation("查询团购订单列表")
    @PreAuthorize("@ss.hasRole('merchant')")
    @GetMapping("/listGroupOrder")
    public TableDataInfo listGroupOrder(OrderQueryParam queryParam) {
        return getDataTable(orderService.listGroupOrder(queryParam));
    }

    /**
     * 根据核销码查询订单详情
     */
    @GetMapping(value = "/getOrderByVerificationCode/{orderVerificationCode}")
    @PreAuthorize("@ss.hasAnyRoles({'merchant','operation'})")
    public AjaxResult getOrderByVerificationCode(@PathVariable("orderVerificationCode") String orderVerificationCode)
    {
        return AjaxResult.success(orderService.getOrderByVerificationCode(orderVerificationCode));
    }
}
