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.MachineConfigureRecordBean;
import com.zfire.mall.manager.bean.valid.EditGroup;
import com.zfire.mall.manager.helper.ResponseHelper;
import com.zfire.mall.manager.logic.manage.material.MachineRecordLogic;
import com.zfire.mall.manager.plus.entity.MachineConfigureRecord;
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 = "/machine-record", produces = "application/json; charset=utf-8")
public class MachineConfigureRecordController {

    @Resource
    MachineRecordLogic machineRecordLogic;

    @ApiOperation(value = "内外机档案列表")
    @GetMapping("/list")
    public ResponseHelper<IPage<MachineConfigureRecord>> list(
            @ApiParam(value = "类型") @RequestParam(required = false) String type,
            @ApiParam(value = "名称") @RequestParam(required = false) String name,
            @ApiParam(value = "型号") @RequestParam(required = false) String model,
            @ApiParam(value = "编码") @RequestParam(required = false) String number,
            @ApiParam(value = "体积") @RequestParam(required = false) BigDecimal volume,
            @ApiParam(value = "页号",required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true)@RequestParam Integer pageSize
    ) throws Exception {
        IPage<MachineConfigureRecord> listPage = machineRecordLogic.list(type, name, model, number, volume, pageNum, pageSize);
        return ResponseHelper.success(listPage);
    }

    @ApiOperation(value = "内外机档案列表导出")
    @GetMapping("/export")
    public void export(
            @ApiParam(value = "类型") @RequestParam(required = false) String type,
            @ApiParam(value = "名称") @RequestParam(required = false) String name,
            @ApiParam(value = "型号") @RequestParam(required = false) String model,
            @ApiParam(value = "编码") @RequestParam(required = false) String number,
            @ApiParam(value = "体积") @RequestParam(required = false) BigDecimal volume,
            HttpServletRequest request, HttpServletResponse response
    ) throws Exception {
        IPage<MachineConfigureRecord> listPage = machineRecordLogic.list(type, name, model, number, volume, 1, -1);
        ExcelData excelData = machineRecordLogic.export(listPage.getRecords());
        ExcelUtils.exportExcel(request,response, "内外机档案.xlsx",excelData);
    }

    @ApiOperation(value = "内外机档案详情")
    @GetMapping("/detail")
    public ResponseHelper<MachineConfigureRecord> detail(
            @ApiParam(value = "内外机档案id") @RequestParam(required = false) String id
    ) throws Exception {
        return ResponseHelper.success(machineRecordLogic.detail(id));
    }

    @ApiOperation(value = "新增内外机档案")
    @PostMapping("/add")
    public ResponseHelper add(
            @ApiParam(required = true, value = "内外机档案") @RequestBody @Valid MachineConfigureRecordBean machineConfigureRecordBean
    ) throws Exception {
        machineRecordLogic.add(machineConfigureRecordBean);
        return ResponseHelper.success();
    }

    @ApiOperation(value = "编辑内外机档案")
    @PostMapping("/edit")
    public ResponseHelper edit(
            @ApiParam(required = true, value = "内外机档案") @Validated(value = { EditGroup.class, Default.class }) @RequestBody MachineConfigureRecordBean machineConfigureRecordBean
    ) throws Exception {
        machineRecordLogic.edit(machineConfigureRecordBean);
        return ResponseHelper.success();
    }

    @ApiOperation(value = "删除内外机档案")
    @PostMapping("/del")
    public ResponseHelper del(
            @ApiParam(required = true, value = "内外机档案") @RequestBody List<String> ids
    ) throws Exception {
        machineRecordLogic.del(ids);
        return ResponseHelper.success();
    }
}
