package com.ruoyi.model.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.cyl.h5.pojo.vo.H5OrderVO;
import com.cyl.h5.service.H5OrderService;
import com.cyl.manager.oms.service.OrderService;
import com.ruoyi.applet.service.IApiMallOrderService;
import com.ruoyi.applet.service.IMallOrderService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.model.service.IAppletUsersService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.model.domain.AppletOrder;
import com.ruoyi.model.service.IAppletOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单信息Controller
 * 
 * @author ruoyi
 * @date 2025-03-28
 */
@RestController
@RequestMapping("/model/AppletOrder")
public class AppletOrderController extends BaseController
{
    @Autowired
    private IAppletOrderService appletOrderService;
    @Autowired
    private IMallOrderService mallOrderService;
    //下单小程序：那边来的订单信息
    @Autowired
    private H5OrderService h5OrderService;
    @Autowired
    private OrderService service;
    @Autowired
    private IAppletUsersService appletUsersService;

    @Autowired
    private IApiMallOrderService apiMallOrderService;


    /**
     * 查询订单信息列表
     */
    @PreAuthorize("@ss.hasPermi('model:AppletOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(AppletOrder appletOrder)
    {
        startPage();
        List<AppletOrder> list = appletOrderService.selectAppletOrderList(appletOrder);
        return getDataTable(list);
    }

    /**
     * 导出订单信息列表
     */
    @PreAuthorize("@ss.hasPermi('model:AppletOrder:export')")
    @Log(title = "订单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AppletOrder appletOrder) throws IOException {
        List<AppletOrder> list = appletOrderService.selectAppletOrderList(appletOrder);
        ExcelUtil<AppletOrder> util = new ExcelUtil<AppletOrder>(AppletOrder.class);
        util.exportExcel(response, list, "订单信息数据");
    }

    /**
     * 获取订单信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('model:AppletOrder:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(appletOrderService.selectAppletOrderById(id));
    }

    /**
     * 获取订单信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('model:AppletOrder:query')")
    @GetMapping(value = "/detail/{orderId}")
    public AjaxResult getInfoDetail(@PathVariable("orderId") Long orderId)
    {
        return success(apiMallOrderService.orderDetail(orderId));
    }



    /**
     * 新增订单信息
     */
    @PreAuthorize("@ss.hasPermi('model:AppletOrder:add')")
    @Log(title = "订单信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AppletOrder appletOrder)
    {
        H5OrderVO h5OrderVO = h5OrderService.orderDetail(appletOrder.getOrderId());
        if(h5OrderVO == null){
            return AjaxResult.error("订单不存在");
        }
        if(appletOrder.getType().equals("0")){
            //系统派单
            AjaxResult ajaxResult = mallOrderService.acceptDispatchAll(h5OrderVO);
            return ajaxResult;
        }else if(appletOrder.getType().equals("1")){
            if(StringUtils.isEmpty(appletOrder.getUserIdJson())){
                return AjaxResult.error("userIdJson不能为空");
            }
            //指定用户派单
//            Long appletUserId =Long.getLong(appletOrder.getUserIdJson());
            Long appletUserId = Long.valueOf(appletOrder.getUserIdJson());
            AjaxResult ajaxResult = mallOrderService.acceptDispatch(appletUserId, h5OrderVO);
            return ajaxResult;
        }else{
            return AjaxResult.error("type状态不能为空");
        }
    }

    /**
     * 修改订单信息
     */
    @PreAuthorize("@ss.hasPermi('model:AppletOrder:edit')")
    @Log(title = "订单信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppletOrder appletOrder)
    {
        return toAjax(appletOrderService.updateAppletOrder(appletOrder));
    }

    /**
     * 删除订单信息
     */
    @PreAuthorize("@ss.hasPermi('model:AppletOrder:remove')")
    @Log(title = "订单信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(appletOrderService.deleteAppletOrderByIds(ids));
    }
}
