package com.sw.mes.store.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sw.mes.core.response.ResponsePage;
import com.sw.mes.core.response.Result;
import com.sw.mes.core.response.ResultGenerator;
import com.sw.mes.core.utils.DynmaicQueryWrapper;
import com.sw.mes.core.utils.ExcelUtil;
import com.sw.mes.core.utils.RequestPage;
import com.sw.mes.store.entity.WarehouseStocktaking;
import com.sw.mes.store.service.WarehouseStocktakingService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * @Author：wanjia
 * @Date：2024/3/17 22:33
 */
@RestController
@RequestMapping("/warehouse/stocktaking")
@Slf4j
@Api(tags = "出库审核记录单明细接口")
public class WarehouseStockingController {


    @Autowired
    private WarehouseStocktakingService warehouseStocktakingService;


    @ApiOperation(value = "分页查询出库明细列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rows", value = "行数", dataType = "integer", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码", dataType = "integer", paramType = "query"),
    })
    @GetMapping("/listByPage")
    public Result<ResponsePage<WarehouseStocktaking>> listByPage(ServletRequest request, @ApiIgnore RequestPage requestPage) {
        QueryWrapper<WarehouseStocktaking> queryWrapper = DynmaicQueryWrapper.bySearchFilter(request, WarehouseStocktaking.class);
        Page<WarehouseStocktaking> page = new Page<>(requestPage.getPage(), requestPage.getRows());
        Page<WarehouseStocktaking> listPage = warehouseStocktakingService.page(page, queryWrapper);
        ResponsePage<WarehouseStocktaking> responsePage = new ResponsePage<>(listPage.getRecords(), listPage.getTotal());
        return ResultGenerator.success(responsePage);
    }


    @ApiOperation(value = "创建")
    @PostMapping("/create")
    public Result<Integer> create(@RequestBody @Valid @ApiParam(value = "记录") List<WarehouseStocktaking> warehouseStocktakings, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultGenerator.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        return ResultGenerator.success(warehouseStocktakingService.batchInsert(warehouseStocktakings));
    }


    @ApiOperation(value = "单个查询")
    @GetMapping("/query")
    public Result<List<WarehouseStocktaking>> queryByNo(String batchNo, String serialNo, String traceId) {
        return warehouseStocktakingService.queryStocktaking(batchNo, serialNo, traceId, null);
    }


    @ApiOperation(value = "导出")
    @GetMapping("/exportExcel")
    public void export(ServletRequest request, HttpServletResponse response) {
        try {
            QueryWrapper<WarehouseStocktaking> queryWrapper = DynmaicQueryWrapper.bySearchFilter(request, WarehouseStocktaking.class);
            List<WarehouseStocktaking> entities = warehouseStocktakingService.list(queryWrapper);
            long i = 1;
            for (WarehouseStocktaking stocktaking : entities) {
                stocktaking.setNo(i++);
            }
            ExcelUtil.exportExcel(entities, "盘点记录", WarehouseStocktaking.class, response);
        } catch (Exception e) {

            throw new RuntimeException("导出盘点记录出错，错误信息：" + e.getMessage());
        }
    }


}
