package cn.iocoder.yudao.module.mes.controller.admin.stockworkshop;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.mes.controller.admin.stockworkshop.vo.StockWorkshopDetailPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.stockworkshop.vo.StockWorkshopDetailRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.stockworkshop.vo.StockWorkshopDetailSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.stockworkshop.StockWorkshopDetailDO;
import cn.iocoder.yudao.module.mes.service.stockworkshop.StockWorkshopDetailService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 车间仓明细")
@RestController
@RequestMapping("/mes/stock-workshop-detail")
@Validated
public class StockWorkshopDetailController {

    @Resource
    private StockWorkshopDetailService stockWorkshopDetailService;

    @PostMapping("/create")
    @Operation(summary = "创建车间仓明细")
    @PreAuthorize("@ss.hasPermission('mes:stock-workshop-detail:create')")
    public CommonResult<Long> createStockWorkshopDetail(@Valid @RequestBody StockWorkshopDetailSaveReqVO createReqVO) {
        return success(stockWorkshopDetailService.createStockWorkshopDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新车间仓明细")
    @PreAuthorize("@ss.hasPermission('mes:stock-workshop-detail:update')")
    public CommonResult<Boolean> updateStockWorkshopDetail(@Valid @RequestBody StockWorkshopDetailSaveReqVO updateReqVO) {
        stockWorkshopDetailService.updateStockWorkshopDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除车间仓明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:stock-workshop-detail:delete')")
    public CommonResult<Boolean> deleteStockWorkshopDetail(@RequestParam("id") Long id) {
        stockWorkshopDetailService.deleteStockWorkshopDetail(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得车间仓明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:stock-workshop-detail:query')")
    public CommonResult<StockWorkshopDetailRespVO> getStockWorkshopDetail(@RequestParam("id") Long id) {
        StockWorkshopDetailDO stockWorkshopDetail = stockWorkshopDetailService.getStockWorkshopDetail(id);
        return success(BeanUtils.toBean(stockWorkshopDetail, StockWorkshopDetailRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得车间仓明细分页")
    @PreAuthorize("@ss.hasPermission('mes:stock-workshop-detail:query')")
    public CommonResult<PageResult<StockWorkshopDetailRespVO>> getStockWorkshopDetailPage(@Valid StockWorkshopDetailPageReqVO pageReqVO) {
        PageResult<StockWorkshopDetailDO> pageResult = stockWorkshopDetailService.getStockWorkshopDetailPage(pageReqVO);
        List<StockWorkshopDetailRespVO> list = stockWorkshopDetailService.buildVOList(pageResult.getList());
        return success(new PageResult<>(list, pageResult.getTotal()));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出车间仓明细 Excel")
    @PreAuthorize("@ss.hasPermission('mes:stock-workshop-detail:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportStockWorkshopDetailExcel(@Valid StockWorkshopDetailPageReqVO pageReqVO,
                                               HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StockWorkshopDetailDO> list = stockWorkshopDetailService.getStockWorkshopDetailPage(pageReqVO).getList();
        List<StockWorkshopDetailRespVO> list1 = stockWorkshopDetailService.buildVOList(list);

        // 导出 Excel
        ExcelUtils.write(response, "车间仓明细.xls", "数据", StockWorkshopDetailRespVO.class,
                list1);
    }

}