package com.ruoyi.system.controller;

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

import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
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.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.IServiceGoodsService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.ServiceGoods;

/**
 * 服务订单Controller
 * 
 * @author ruoyi
 * @date 2025-05-13
 */
@RestController
@RequestMapping("/system/Order")
public class OrderController extends BaseController
{
    @Autowired
    private IOrderService orderService;
    @Autowired
    private IServiceGoodsService serviceGoodsService;
    @Autowired
    IUsersService usersService;
    @Autowired
    IOrderCallService  orderCallService;
    @Autowired
    IOrderCommentService orderCommentService;
    @Autowired
    IOrderSoundLogService orderSoundLogService;
    @Autowired
    IOrderSoundService orderSoundService;

    @Autowired
    IOrderLogService orderLogService;
    @Autowired
    INotifyOrderService notifyOrderService;
    @Autowired
    private ISysUserService sysUserService;
    /**
     * 查询服务订单列表
     */
    @PreAuthorize("@ss.hasPermi('system:Order:list')")
    @GetMapping("/list")
    public TableDataInfo list(Order order)
    {
        startPage();
        List<Order> list = orderService.selectOrderList(order);
        for(Order orderdata:list){

            ServiceGoods serviceGoods=serviceGoodsService.selectServiceGoodsById(orderdata.getProductId());
            if(serviceGoods!=null){
                orderdata.setProductName(serviceGoods.getTitle());
            }
            Users users=usersService.selectUsersById(orderdata.getUid());
            if (users!=null){
                orderdata.setUname(users.getName());
            }
            orderdata.setThjl(orderCallService.selectCountOrderCallByOid(orderdata.getId()));
            orderdata.setFwpj(orderCommentService.selectCountOrderCommentByOid(orderdata.getId()));
            orderdata.setLywj(orderSoundService.selectCountOrderSoundByOid(orderdata.getId()));
            orderdata.setJdjl(orderLogService.selectCountOrderLogByOrderId(orderdata.getOrderId()));
            orderdata.setTzjl(notifyOrderService.selectNotifyOrderCountByOid(orderdata.getId()));

        }
        return getDataTable(list);
    }

    /**
     * 导出服务订单列表
     */
    @PreAuthorize("@ss.hasPermi('system:Order:export')")
    @Log(title = "服务订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Order order)
    {
        List<Order> list = orderService.selectOrderList(order);
        ExcelUtil<Order> util = new ExcelUtil<Order>(Order.class);
        util.exportExcel(response, list, "服务订单数据");
    }


    /**
     * 获取服务订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:Order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(orderService.selectOrderById(id));
    }

    /**
     * 新增服务订单
     */
    @PreAuthorize("@ss.hasPermi('system:Order:add')")
    @Log(title = "服务订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Order order)
    {
        return toAjax(orderService.insertOrder(order));
    }

    /**
     * 修改服务订单
     */
    @PreAuthorize("@ss.hasPermi('system:Order:edit')")
    @Log(title = "服务订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Order order)
    {
        return toAjax(orderService.updateOrder(order));
    }

    /**
     * 删除服务订单
     */
    @PreAuthorize("@ss.hasPermi('system:Order:remove')")
    @Log(title = "服务订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(orderService.deleteOrderByIds(ids));
    }



    /**
     * 获取订单接单记录
     */
    @PreAuthorize("@ss.hasPermi('system:Order:query')")
    @GetMapping("/receive-records/{orderId}")
    public AjaxResult getReceiveRecords(@PathVariable("orderId") String orderId)
    {
        List<OrderLog> list =orderLogService.selectOrderLogByOrderId(orderId);
        for(OrderLog orderLogdata:list){
            Users users=usersService.selectUsersById(orderLogdata.getWorkerId());
            if(users!=null){
                orderLogdata.setWorkerName(users.getName());
            }
        }
        return success(list);
    }

    // /**
    //  * 获取订单通话记录
    //  */
     @PreAuthorize("@ss.hasPermi('system:Order:query')")
     @GetMapping("/call-records/{orderId}")
     public AjaxResult getCallRecords(@PathVariable("orderId") String orderId)
     {
         Order   data=orderService.selectOrderByOrderId(orderId);;
         if(data!=null){
             return success(orderCallService.selectOrderCallByOid(data.getId()));
         }else{
             return AjaxResult.error("订单不存在");
         }

     }

    /**
     * 获取订单录音文件
     */
     @PreAuthorize("@ss.hasPermi('system:Order:query')")
     @GetMapping("/audio-records/{orderId}")
     public AjaxResult getAudioRecords(@PathVariable("orderId") String orderId)
     {
         Order   data=orderService.selectOrderByOrderId(orderId);;
         if(data!=null){
             List<OrderSound> list =orderSoundService.selectOrderSoundByOid(data.getId());
             for(OrderSound orderSounddata:list){
                 orderSounddata.setFile("https://img.huafurenjia.cn/"+orderSounddata.getFile());
                 // https://img.huafurenjia.cn/
                 Users users=usersService.selectUsersById(orderSounddata.getWorkerUid()) ;
                 Order order=orderService.selectOrderById(orderSounddata.getOid());
                 if(users!=null){
                     orderSounddata.setWorkerName(users.getName());
                 }
                 if(order!=null){
                     orderSounddata.setOcode(order.getOrderId());
                 }
             }
             return success(list);
         }else{
             return AjaxResult.error("订单不存在");
         }
     }

    /**
     * 获取订单通知记录
     */
     @PreAuthorize("@ss.hasPermi('system:Order:query')")
     @GetMapping("/notify-records/{orderId}")
     public AjaxResult getNotifyRecords(@PathVariable("orderId") String orderId)
     {
         Order   data=orderService.selectOrderByOrderId(orderId);;
         if(data!=null){
             return success(notifyOrderService.selectNotifyOrderByOid(data.getId()));
         }else{
             return AjaxResult.error("订单不存在");
         }
     }

    /**
     * 获取订单通知记录
     */
    @PreAuthorize("@ss.hasPermi('system:Order:query')")
    @GetMapping("/comment-records/{orderId}")
    public AjaxResult getCommentRecords(@PathVariable("orderId") String orderId)
    {
        Order   data=orderService.selectOrderByOrderId(orderId);;
        if(data!=null){
            List<OrderComment> list =orderCommentService.selectOrderCommentByOid(data.getId());
            for(OrderComment orderCommentdata:list){
                Users users=usersService.selectUsersById(orderCommentdata.getUid());
                if(users!=null){
                    orderCommentdata.setUname(users.getName());
                }
            }
            return success(list);
        }else{
            return AjaxResult.error("订单不存在");
        }
    }

    /**
     * 获取用户下拉列表
     */
    @GetMapping("/userOptions")
    public AjaxResult getUserOptions() {
        List<SysUser> users = sysUserService.selectUserList(new SysUser());
        return AjaxResult.success(users);
    }

    /**
     * 获取商品下拉列表
     */
    @GetMapping("/serviceGoodsOptions")
    public AjaxResult getServiceGoodsOptions() {
        List<ServiceGoods> goods = serviceGoodsService.selectServiceGoodsList(new ServiceGoods());
        return AjaxResult.success(goods);
    }

}
