package com.miaxis.pc.controller;

import com.miaxis.common.annotation.Log;
import com.miaxis.common.core.controller.BaseController;
import com.miaxis.common.core.domain.Response;
import com.miaxis.common.core.page.ResponsePageInfo;
import com.miaxis.common.enums.BusinessTypeEnum;
import com.miaxis.common.utils.poi.ExcelUtil;
import com.miaxis.engine.EngineInfoDTO;
import com.miaxis.engine.domain.EngineInfo;
import com.miaxis.engine.service.IEngineInfoService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

/**
 * 【报价】Controller
 *
 * @author miaxis
 * @date 2023-01-29
 */
@RestController
@RequestMapping("/engine/info")
@Api(tags={"【pc-动力报价】"})
public class EngineInfoController extends BaseController{
    @Autowired
    private IEngineInfoService engineInfoService;

    /**
     * 查询报价列表
     */
//    @PreAuthorize("@ss.hasPermi('engine:info:list')")
    @GetMapping("/list")
    @ApiOperation("查询报价列表")
        @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前页码" ,dataType = "int", paramType = "query", required = false),
            @ApiImplicitParam(name = "pageSize",value = "每页数据量" , dataType = "int", paramType = "query", required = false),
    })
    public ResponsePageInfo<EngineInfo> list(@ModelAttribute EngineInfoDTO engineInfoDTO){
        startPage();
        List<EngineInfo> list = engineInfoService.selectEngineInfoList2(engineInfoDTO);
        return toResponsePageInfo(list);
    }

    /**
     * 导出报价列表
     */
//    @PreAuthorize("@ss.hasPermi('engine:info:export')")
    @Log(title = "报价", businessType = BusinessTypeEnum.EXPORT)
    @GetMapping("/export")
    @ApiOperation("导出报价列表Excel")
    public Response<String> export(@ModelAttribute EngineInfo engineInfo){
        List<EngineInfo> list = engineInfoService.selectEngineInfoList(engineInfo);
        ExcelUtil<EngineInfo> util = new ExcelUtil<EngineInfo>(EngineInfo.class);
        return util.exportExcel(list, "info");
    }

    /**
     * 导入报价列表
     */
//    @PreAuthorize("@ss.hasPermi('price:info:import')")
    @Log(title = "报价", businessType = BusinessTypeEnum.IMPORT)
    @PostMapping("/import")
    @ApiOperation("导入报价列表Excel")
    public Response<String> importEngineInfo(@RequestPart("file") MultipartFile file) throws Exception {
        ExcelUtil<EngineInfo> util = new ExcelUtil<EngineInfo>(EngineInfo.class);
        List<EngineInfo> list = util.importExcel(file.getInputStream(),3,false);
        engineInfoService.saveBatch(list);
        return Response.success();
    }

    /**
     * 获取报价详细信息
     */
//    @PreAuthorize("@ss.hasPermi('engine:info:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取报价详细信息")
    public Response<EngineInfo> getInfo(
            @ApiParam(name = "id", value = "报价参数", required = true)
            @PathVariable("id") Long id
    ){
        return Response.success(engineInfoService.getById(id));
    }

    /**
     * 新增报价
     */
//    @PreAuthorize("@ss.hasPermi('engine:info:add')")
    @Log(title = "报价", businessType = BusinessTypeEnum.INSERT)
    @PostMapping
    @ApiOperation("新增报价")
    public Response<Integer> add(@RequestBody EngineInfo engineInfo){
        return toResponse(engineInfoService.save(engineInfo) ? 1 : 0);
    }

    /**
     * 修改报价
     */
//    @PreAuthorize("@ss.hasPermi('engine:info:edit')")
    @Log(title = "报价", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping
    @ApiOperation("修改报价")
    public Response<Integer> edit(@RequestBody EngineInfo engineInfo){
        return toResponse(engineInfoService.updateById(engineInfo) ? 1 : 0);
    }

    /**
     * 删除报价
     */
//    @PreAuthorize("@ss.hasPermi('engine:info:remove')")
    @Log(title = "报价", businessType = BusinessTypeEnum.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除报价")
    public  Response<Integer> remove(
            @ApiParam(name = "ids", value = "报价ids参数", required = true)
            @PathVariable Long[] ids
    ){
        return toResponse(engineInfoService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }


    /**
     * 查询字段所有枚举值
     */
//    @PreAuthorize("@ss.hasPermi('price:info:query')")
    @GetMapping(value = "/field/{name}")
    @ApiOperation("查询字段所有枚举值")
    public Response<List<String>> getFieldName(
            @ApiParam(name = "name", value = "字段名", required = true)
            @PathVariable("name") String name
    ){
        return Response.success(engineInfoService.getListByFieldName(name));
    }
}
