package com.xhpc.activity.controller;


import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.web.controller.BaseController;
import com.xhpc.common.core.web.page.TableDataInfo;
import com.xhpc.common.log.annotation.Log;
import com.xhpc.common.log.enums.BusinessType;
import com.xhpc.common.util.LogUserUtils;
import com.xhpc.system.api.model.LoginUser;
import com.xhpc.activity.domain.XhpcWorkOrderDomain;
import com.xhpc.activity.service.WorkOrderService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/order")
public class WorkOrderController extends BaseController {

    @Resource
    WorkOrderService workOrderService;
    @Resource
    LogUserUtils logUserUtils;

    @GetMapping("/getPage")
    public TableDataInfo getOrderPage(HttpServletRequest request, Integer orderType,
                                      String userName, Integer status){
        startPage();
        LoginUser logUser = logUserUtils.getLogUser(request);
        Map<String, Object> params = new HashMap<>();
        params.put("orderType", orderType);
        params.put("userName", userName);
        params.put("tenantId", logUser.getTenantId());
        params.put("status", status);

        return getDataTable(workOrderService.getOrderPage(params));
    }

    @Log(title = "工单-新增工单", businessType = BusinessType.INSERT)
    @PostMapping("/")
    public R insertNewOrder(HttpServletRequest request, @RequestBody XhpcWorkOrderDomain domain){
        LoginUser logUser = logUserUtils.getLogUser(request);
        domain.setTenantId(logUser.getTenantId());
        return R.ok(workOrderService.insertOrder(domain));
    }


    @GetMapping("/detail")
    public R getOrderInfo(@RequestParam("orderId")Long orderId){
        return R.ok(workOrderService.selectOrderById(orderId));
    }


    @Log(title = "工单-维护工单", businessType = BusinessType.UPDATE)
    @PutMapping("/detail")
    public R updateOrderInfo(@RequestBody XhpcWorkOrderDomain domain){
        return R.ok(workOrderService.updateOrder(domain));
    }


    @Log(title = "工单-处理工单", businessType = BusinessType.UPDATE)
    @PutMapping("/reply")
    public R replyOrder(@RequestBody XhpcWorkOrderDomain domain){
        return R.ok(workOrderService.replyOrder(domain));
    }


    @Log(title = "工单-删除工单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{orderId}")
    public R deleteOrder(@PathVariable("orderId")Long orderId) throws Exception {
        return R.ok(workOrderService.deleteOrder(orderId));
    }


    @GetMapping("/addNewOrder")
    public R addNewOrder(@RequestParam(value = "type") String type, @RequestParam(value = "title") String title, @RequestParam(value = "content")String content, @RequestParam(value = "reason")String reason, @RequestParam(value = "stationId")String stationId, @RequestParam(value = "deviceType")String deviceType,@RequestParam(value = "serialNumber")String serialNumber){
        return R.ok(workOrderService.addNewOrder(type, title, content, reason,stationId,deviceType,serialNumber));
    }

    @GetMapping("/workOrderMessage")
    public R workOrderMessage(@RequestParam(value = "type") Integer type, @RequestParam(value = "time") String time){
        return workOrderService.workOrderMessage(type, time);
    }



}
