package com.wgb.system.service.controller;

import com.alibaba.fastjson.JSON;
import com.data.model.restful.response.InternalResponse;
import com.wgb.system.service.domain.model.Business;
import com.wgb.system.service.domain.request.BusinessRequest;
import com.wgb.system.service.domain.request.BusinessTask;
import com.wgb.system.service.domain.response.LayuiTableResponse;
import com.wgb.system.service.service.BusinessService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/business")
@RestController
@Slf4j
public class BusinessController {

    private final BusinessService businessService;

    public BusinessController(BusinessService businessService) {
        this.businessService = businessService;
    }


    @ApiOperation(value = "/获取维修单列表", response = LayuiTableResponse.class)
    @ResponseBody
    @RequestMapping(value = "/getList",method = {RequestMethod.POST})
    public LayuiTableResponse getList(@RequestBody BusinessRequest businessRequest){
        log.info("Start to execute /business/getList with, Param={}", businessRequest);
        LayuiTableResponse response= businessService.getBusinessList(businessRequest);
        log.info("Return object for /business/getList is result={}", JSON.toJSON(response));
        return response;
    }

    @ApiOperation(value = "/新增or修改维修单", response = InternalResponse.class)
    @ResponseBody
    @RequestMapping(value = "/saveOrUpdate",method = {RequestMethod.POST})
    public InternalResponse saveOrUpdate(@RequestBody Business business){
        log.info("Start to execute /business/saveOrUpdate with, param={}", JSON.toJSON(business));
        businessService.saveOrUpdate(business);
        log.info("Return object for /business/saveOrUpdate is result={}", true);
        return  InternalResponse.success();
    }

    @ApiOperation(value = "/根据ID查询维修号", response = Business.class)
    @ResponseBody
    @RequestMapping(value = "/getBusinessById",method = {RequestMethod.GET})
    public InternalResponse getBusinessById(@RequestParam Long id){
        log.info("Start to execute /business/saveOrUpdate with, param={}", id);
        Business business= businessService.getBusinessById(id);
        log.info("Return object for /business/saveOrUpdate is result={}", business);
        return InternalResponse.success().withBody(business);
    }

    @ApiOperation(value = "/根据ID删除维修列表", response = InternalResponse.class)
    @ResponseBody
    @RequestMapping(value = "/deleteBusinessById",method = {RequestMethod.GET})
    public InternalResponse deleteBusinessById(@RequestParam String ids){
        log.info("Start to execute /business/deleteBusinessById with, param={}", ids);
        businessService.deleteBusinessById(ids);
        log.info("Return object for /business/deleteBusinessById is result={}","success");
        return InternalResponse.success();
    }


    @ApiOperation(value = "/分配任务给师傅", response = InternalResponse.class)
    @ResponseBody
    @RequestMapping(value = "/pushTask",method = {RequestMethod.POST})
    public InternalResponse pushTask(@RequestBody BusinessTask businessTask)throws Exception{
        log.info("Start to execute /business/pushTask with, param={}", businessTask);
        businessService.pushTask(businessTask);
        log.info("Return object for /business/pushTask is result={}","success");
        return InternalResponse.success();
    }

    @ApiOperation(value = "/根据手机号查询订单信息", response = InternalResponse.class)
    @ResponseBody
    @RequestMapping(value = "/getUseBusinessInfo",method = {RequestMethod.POST})
    public InternalResponse getUseBusinessInfo(@RequestParam(value = "userCellphone",required = false) String userCellphone,@RequestParam(value = "repairId",required = false) Long repairId){
        log.info("Start to execute /business/getUseBusinessInfo with, userCellphone={}, repairId={}", userCellphone,repairId);
        List<Business> list= businessService.getUseBusinessInfo(userCellphone,repairId);
        log.info("Return object for /business/getUseBusinessInfo is result={}",list);
        return InternalResponse.success().withBody(list);
    }

}
