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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zfire.mall.manager.bean.ExcelData;
import com.zfire.mall.manager.bean.manage.material.ProductComposeBean;
import com.zfire.mall.manager.bean.manage.material.ProductComposeListBean;
import com.zfire.mall.manager.bean.valid.EditGroup;
import com.zfire.mall.manager.helper.ResponseHelper;
import com.zfire.mall.manager.logic.manage.material.ProductComposeLogic;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.groups.Default;
import java.math.BigDecimal;
import java.util.List;

@Slf4j
@RestController
@Api(value = "产品套机对应关系", tags ={"产品套机对应关系"} )
@RequestMapping(value = "/product-compose", produces = "application/json; charset=utf-8")
public class ProductComposeController {

    @Resource
    ProductComposeLogic productComposeLogic;

    @ApiOperation(value = "产品套机对应关系列表")
    @GetMapping("/list")
    public ResponseHelper<IPage<ProductComposeListBean>> list(
            @ApiParam(value = "产品名称") @RequestParam(required = false) String name,
            @ApiParam(value = "产品型号") @RequestParam(required = false) String type,
            @ApiParam(value = "套机编码") @RequestParam(required = false) String number,
            @ApiParam(value = "产品体积") @RequestParam(required = false) BigDecimal volume,
            @ApiParam(value = "类型") @RequestParam(required = false) String machineType,
            @ApiParam(value = "名称") @RequestParam(required = false) String machineName,
            @ApiParam(value = "型号") @RequestParam(required = false) String machineModel,
            @ApiParam(value = "编码") @RequestParam(required = false) String machineNumber,
            @ApiParam(value = "页号",required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true)@RequestParam Integer pageSize
    ) throws Exception {
        IPage<ProductComposeListBean> listPage = productComposeLogic.listPage(name, type, number, volume,
                machineType, machineName, machineModel, machineNumber, pageNum, pageSize);
        return ResponseHelper.success(listPage);
    }

    @ApiOperation(value = "产品套机对应关系列表导出")
    @GetMapping("/export")
    public void export(
            @ApiParam(value = "产品名称") @RequestParam(required = false) String name,
            @ApiParam(value = "产品型号") @RequestParam(required = false) String type,
            @ApiParam(value = "套机编码") @RequestParam(required = false) String number,
            @ApiParam(value = "产品体积") @RequestParam(required = false) BigDecimal volume,
            @ApiParam(value = "类型") @RequestParam(required = false) String machineType,
            @ApiParam(value = "名称") @RequestParam(required = false) String machineName,
            @ApiParam(value = "型号") @RequestParam(required = false) String machineModel,
            @ApiParam(value = "编码") @RequestParam(required = false) String machineNumber,
            HttpServletRequest request, HttpServletResponse response
    ) throws Exception {
        IPage<ProductComposeListBean> listPage = productComposeLogic.listPage(name, type, number, volume,
                machineType, machineName, machineModel, machineNumber, 1, -1);
        ExcelData excelData = productComposeLogic.export(listPage.getRecords());
        ExcelUtils.exportExcel(request,response, "产品套机对应关系.xlsx",excelData);
    }

    @ApiOperation(value = "产品套机对应关系详情")
    @GetMapping("/detail")
    public ResponseHelper<ProductComposeBean> detail(
            @ApiParam(value = "产品名称", required = true) @RequestParam String id
    ) throws Exception {
        ProductComposeBean productComposeBean = productComposeLogic.detail(id);
        return ResponseHelper.success(productComposeBean);
    }

    @ApiOperation(value = "新增产品套机对应关系")
    @PostMapping("/add")
    public ResponseHelper add(
            @ApiParam(required = true, value = "产品套机对应关系") @RequestBody @Valid ProductComposeBean productComposeBean
    ) throws Exception {
        productComposeLogic.add(productComposeBean);
        return ResponseHelper.success();
    }

    @ApiOperation(value = "编辑产品套机对应关系")
    @PostMapping("/edit")
    public ResponseHelper edit(
            @ApiParam(required = true, value = "产品套机对应关系") @Validated(value = { EditGroup.class, Default.class }) @RequestBody ProductComposeBean productComposeBean
    ) throws Exception {
        productComposeLogic.edit(productComposeBean);
        return ResponseHelper.success();
    }

    @ApiOperation(value = "删除产品套机对应关系")
    @PostMapping("/del")
    public ResponseHelper del(
            @ApiParam(required = true, value = "产品套机对应关系") @RequestParam List<String> ids
    ) throws Exception {
        productComposeLogic.del(ids);
        return ResponseHelper.success();
    }
}
