package com.zfire.mall.manager.controller.manage.product.price;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zfire.mall.manager.bean.ExcelData;
import com.zfire.mall.manager.bean.manage.product.price.ProductPriceBean;
import com.zfire.mall.manager.exception.RemoteServiceException;
import com.zfire.mall.manager.helper.ResponseHelper;
import com.zfire.mall.manager.logic.manage.product.price.ProductBusinessPriceLogic;
import com.zfire.mall.manager.logic.manage.product.price.ProductPriceLogic;
import com.zfire.mall.manager.plus.entity.ProductBusinessPrice;
import com.zfire.mall.manager.utils.CommonUtils;
import com.zfire.mall.manager.utils.excel.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

@Slf4j
@RestController
@Api(value = "商用工程产品价格列表", tags ={"商用工程产品价格"} )
@RequestMapping(value = "/bus/price", produces = "application/json; charset=utf-8")
public class ProductBusinessPriceController {

    @Resource
    ProductBusinessPriceLogic  productBusinessPriceLogic;

    @ApiOperation(value = "商用工程产品价格列表")
    @GetMapping("/list")
    public ResponseHelper<IPage<ProductBusinessPrice>> list(
            @ApiParam(value = "物料编码",required = false) @RequestParam(required = false) String materialNumber,
            @ApiParam(value = "物料名称",required = false) @RequestParam(required = false) String materialName,
            @ApiParam(value = "销售类型",required = false) @RequestParam(required = false) String saleTypeId,
            @ApiParam(value = "产品品类",required = false) @RequestParam(required = false) String categoryId,
            @ApiParam(value = "规格",required = false) @RequestParam(required = false) String specification,
            @ApiParam(value = "页号",required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true)@RequestParam Integer pageSize
    ) throws Exception {
        IPage<ProductBusinessPrice> listPage = productBusinessPriceLogic.listPage(materialNumber, materialName,
                saleTypeId, specification, pageNum, pageSize);
        return ResponseHelper.success(listPage);
    }


    @PostMapping("/import")
    @ApiOperation("商用工程产品价格-导入")
    public ResponseHelper importRebateOrder(MultipartFile file) throws Exception {
        List<Object> objects = ExcelUtils.importExcel(file);
        productBusinessPriceLogic.importData(objects);
        return ResponseHelper.success();
    }

    @GetMapping("/export")
    @ApiOperation("商用工程产品价格-导出")
    public void export(
            @ApiParam(value = "物料编码",required = false) @RequestParam(required = false) String materialNumber,
            @ApiParam(value = "物料名称",required = false) @RequestParam(required = false) String materialName,
            @ApiParam(value = "销售类型",required = false) @RequestParam(required = false) String saleTypeId,
            @ApiParam(value = "产品品类",required = false) @RequestParam(required = false) String categoryId,
            @ApiParam(value = "规格",required = false) @RequestParam(required = false) String specification,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        IPage<ProductBusinessPrice> list = productBusinessPriceLogic.listPage(materialNumber, materialName,
                saleTypeId, specification, 1, -1);
        ExcelData export = productBusinessPriceLogic.export(list.getRecords());
        ExcelUtils.exportExcel(request,response,"商用工程产品价格数据.xlsx",export);
    }


    @GetMapping("/download")
    @ApiOperation("下载商用工程产品价格模板")
    public void download(
            HttpServletResponse response
    ) throws IOException {
        CommonUtils.downloadFile("商用工程产品价格.xlsx",response);
    }

    @ApiOperation(value = "删除")
    @PostMapping("/delete")
    public ResponseHelper delete(
            @ApiParam(required = true, value = "工程产品价单ids") @RequestParam List<String> ids
    ) throws Exception {
        productBusinessPriceLogic.delete(ids);
        return ResponseHelper.success();
    }


    @PostMapping("/update")
    @ApiOperation("修改")
    public ResponseHelper editRecordCompany(@RequestBody ProductBusinessPrice productBusinessPrice){
        productBusinessPriceLogic.update(productBusinessPrice);
        return ResponseHelper.success();
    }

    @GetMapping("/detail")
    @ApiOperation(value = "详情")
    public ResponseHelper<ProductBusinessPrice> detail(
            @ApiParam(value = "id",required = true) @RequestParam(required = true) String id
    ) throws RemoteServiceException {
        ProductBusinessPrice detail = productBusinessPriceLogic.detail(id);
        return ResponseHelper.success(detail);
    }

}
