package com.zfire.mall.manager.controller.manage.material;

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zfire.mall.manager.annotation.ZfireList;
import com.zfire.mall.manager.bean.listV2.MaterialReferPriceVO;
import com.zfire.mall.manager.exception.RemoteServiceException;
import com.zfire.mall.manager.helper.ResponseHelper;
import com.zfire.mall.manager.logic.manage.material.MaterialReferPriceLogic;
import com.zfire.mall.manager.plus.entity.MaterialReferPrice;
import com.zfire.mall.manager.utils.CommonUtils;
import com.zfire.mall.manager.utils.excel.ExcelUtils;
import com.zfire.mall.manager.zfire.bean.ZfireParamBean;
import com.zfire.mall.manager.zfire.util.FieldUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RequiredArgsConstructor
@Slf4j
@RestController
@Api(value = "物料参考价格API", tags ={"物料参考价格API"} )
@RequestMapping(value = "/material/refer/price", produces = "application/json; charset=utf-8")
public class MaterialReferPriceController {

    private final MaterialReferPriceLogic materialReferPriceLogic;


    @ZfireList
    @PostMapping("/list")
    @ApiOperation("列表")
    public ResponseHelper<IPage<MaterialReferPriceVO>> list(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        IPage<MaterialReferPriceVO> list = materialReferPriceLogic.list(zfireParamBean);
        return ResponseHelper.success(list, new TypeReference<MaterialReferPriceVO>() {});
    }

    @PostMapping("/list/export")
    @ApiOperation("导出")
    public void listV2Export(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<MaterialReferPriceVO> list = materialReferPriceLogic.list(zfireParamBean);
        //3.导出
        FieldUtils.exportData(list.getRecords(),zfireParam.getExportFields(),request,response);
    }


    @PostMapping("/save")
    @ApiOperation("保存")
    public ResponseHelper save(
            @RequestBody MaterialReferPrice materialReferPrice
    ){
        materialReferPriceLogic.add(materialReferPrice);
        return ResponseHelper.success();
    }

    @PostMapping("/detail")
    @ApiOperation("详情")
    public ResponseHelper<MaterialReferPrice> detail(
            @RequestParam String id
    ){
        MaterialReferPrice detail = materialReferPriceLogic.detail(id);
        return ResponseHelper.success(detail);
    }

    @PostMapping("/delete")
    @ApiOperation("删除")
    public ResponseHelper delete(
            @RequestParam String id
    ){
        materialReferPriceLogic.delete(id);
        return ResponseHelper.success();
    }

    @GetMapping("/download")
    @ApiOperation("下载模板")
    public void download(
            HttpServletResponse response
    ) throws IOException {
        CommonUtils.downloadFile("参考成本价.xlsx",response);
    }

    @PostMapping("/import")
    @ApiOperation("导入成本价")
    public ResponseHelper importData(
            @RequestPart("file") MultipartFile file
    ) throws IOException {
        List<Object> objects = ExcelUtils.importExcel(file);
        materialReferPriceLogic.importData(objects);
        return ResponseHelper.success();
    }

}
