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.price.domain.PriceInfo;
import com.miaxis.price.dto.PriceInfoDto;
import com.miaxis.price.service.IPriceInfoService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
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-10
 */
@RestController
@RequestMapping("/price/info")
@Api(tags={"【pc-报价】"})
public class PriceInfoController extends BaseController{
    @Autowired
    private IPriceInfoService priceInfoService;

    /**
     * 查询报价列表
     */
//    @PreAuthorize("@ss.hasPermi('price: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<PriceInfo> list(@ModelAttribute PriceInfoDto priceInfoTDto){
        startPage();
        List<PriceInfo> list = priceInfoService.selectPriceInfoList(priceInfoTDto);
        return toResponsePageInfo(list);
    }

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

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

    /**
     * 查询字段所有枚举值
     */
//    @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(priceInfoService.getListByFieldName(name));
    }

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

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

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

    /**
     * 删除报价
     */
//    @PreAuthorize("@ss.hasPermi('price: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(priceInfoService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
