package com.wgb.system.service.controller;

import com.data.model.restful.response.InternalResponse;
import com.wgb.system.service.domain.model.BusinessCategory;
import com.wgb.system.service.domain.request.BusinessCategoryRequest;
import com.wgb.system.service.domain.response.LayuiTableResponse;
import com.wgb.system.service.service.BusinessCategoryService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RequestMapping("/businessCategory")
@RestController
@Slf4j
public class BusinessCategoryController {

    private final BusinessCategoryService businessCategoryService;

    public BusinessCategoryController(BusinessCategoryService businessCategoryService) {
        this.businessCategoryService = businessCategoryService;
    }

    @ApiOperation(value = "/查询维修类目", response = InternalResponse.class)
    @ResponseBody
    @RequestMapping(value = "/getList", method = {RequestMethod.POST})
    public LayuiTableResponse getList(@RequestBody BusinessCategoryRequest request) {
        log.info("Start to execute /businessCategory/list with, param={}",request);
        LayuiTableResponse layuiTableResponse = businessCategoryService.getBusinessCategoryList(request);
        log.info("Return object for /businessCategory/list is result={}", layuiTableResponse);
        return layuiTableResponse;
    }

    @ApiOperation(value = "/查询维修类目", response = InternalResponse.class)
    @ResponseBody
    @RequestMapping(value = "/getById", method = {RequestMethod.GET})
    public InternalResponse getById(@RequestParam Long id,@RequestParam(name = "state",required = false)Integer state) {
        log.info("Start to execute /businessCategory/getById with, param={}", id);
        BusinessCategory businessCategory = businessCategoryService.getBusinessCategoryById(id,state);
        log.info("Return object for /businessCategory/getById is result={}", businessCategory);
        return InternalResponse.success().withBody(businessCategory);
    }

    @ApiOperation(value = "/根据父节点查询维修类目", response = InternalResponse.class)
    @ResponseBody
    @RequestMapping(value = "/getByParentId", method = {RequestMethod.GET})
    public InternalResponse getByParentId(@RequestParam(value = "parentId", required = false) Long parentId,@RequestParam(value = "state", required = false) Integer state) {
        log.info("Start to execute /businessCategory/getByParentId with, param={}", parentId);
        List<BusinessCategory> list = businessCategoryService.getBusinessCategoryByParentId(parentId,state);
        log.info("Return object for /businessCategory/getByParentId is result={}", list);
        return InternalResponse.success().withBody(list);
    }


    @ApiOperation(value = "/删除父节点", response = InternalResponse.class)
    @ResponseBody
    @RequestMapping(value = "/deleteById", method = {RequestMethod.GET})
    public InternalResponse deleteById(@RequestParam(value = "id", required = false) Long id) {
        log.info("Start to execute /businessCategory/deleteById with, param={}", id);
        businessCategoryService.deleteById(id);
        log.info("Return object for /businessCategory/deleteById is result={}", "true");
        return InternalResponse.success();
    }

    @ApiOperation(value = "/新增品牌类目以及修改类目", response = InternalResponse.class)
    @ResponseBody
    @RequestMapping(value = "/saveOrUpdate", method = {RequestMethod.POST})
    public InternalResponse saveOrUpdate(@RequestBody BusinessCategory businessCategory) {
        log.info("Start to execute /businessCategory/saveOrUpdate with, param={}", businessCategory);
        businessCategoryService.saveOrUpdate(businessCategory);
        log.info("Return object for /businessCategory/saveOrUpdate is result={}", "true");
        return InternalResponse.success();
    }

    @ApiOperation(value = "/修改类目状态", response = InternalResponse.class)
    @ResponseBody
    @RequestMapping(value = "/updateByState", method = {RequestMethod.GET})
    public InternalResponse updateByState(@RequestParam(value = "id", required = true) Long id, @RequestParam(value = "state", required = true) Integer state) {
        log.info("Start to execute /businessCategory/updateByState with, id={},state={}", id, state);
        businessCategoryService.updateByState(id,state);
        log.info("Return object for /businessCategory/updateByState is result={}", "true");
        return InternalResponse.success();
    }

    @ApiOperation(value = "/导入类目数据", response = InternalResponse.class)
    @ResponseBody
    @RequestMapping(value = "/uploadExcelFile", method = {RequestMethod.POST})
    public InternalResponse uploadExcelFile(@RequestParam("file") MultipartFile file)throws Exception{
        if (file.isEmpty()) {
            return InternalResponse.fail();
        }
        businessCategoryService.uploadExcelFile(file);
        return InternalResponse.success();
    }

}
