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.StockWorkshopPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.stockworkshop.vo.StockWorkshopRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.stockworkshop.vo.StockWorkshopSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.stockworkshop.StockWorkshopDO;
import cn.iocoder.yudao.module.mes.service.stockworkshop.StockWorkshopService;
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")
@Validated
public class StockWorkshopController {

    @Resource
    private StockWorkshopService stockWorkshopService;

    @PostMapping("/create")
    @Operation(summary = "创建车间仓库存")
    @PreAuthorize("@ss.hasPermission('mes:stock-workshop:create')")
    public CommonResult<Long> createStockWorkshop(@Valid @RequestBody StockWorkshopSaveReqVO createReqVO) {
        return success(stockWorkshopService.createStockWorkshop(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新车间仓库存")
    @PreAuthorize("@ss.hasPermission('mes:stock-workshop:update')")
    public CommonResult<Boolean> updateStockWorkshop(@Valid @RequestBody StockWorkshopSaveReqVO updateReqVO) {
        stockWorkshopService.updateStockWorkshop(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得车间仓库存")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:stock-workshop:query')")
    public CommonResult<StockWorkshopRespVO> getStockWorkshop(@RequestParam("id") Long id) {
        StockWorkshopDO stockWorkshop = stockWorkshopService.getStockWorkshop(id);
        return success(BeanUtils.toBean(stockWorkshop, StockWorkshopRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得车间仓库存分页")
    @PreAuthorize("@ss.hasPermission('mes:stock-workshop:query')")
    public CommonResult<PageResult<StockWorkshopRespVO>> getStockWorkshopPage(@Valid StockWorkshopPageReqVO pageReqVO) {
        PageResult<StockWorkshopDO> pageResult = stockWorkshopService.getStockWorkshopPage(pageReqVO);
        List<StockWorkshopRespVO> list = stockWorkshopService.buildVOList(pageResult.getList());
        return success(new PageResult<>(list, pageResult.getTotal()));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出车间仓库存 Excel")
    @PreAuthorize("@ss.hasPermission('mes:stock-workshop:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportStockWorkshopExcel(@Valid StockWorkshopPageReqVO pageReqVO,
                                         HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StockWorkshopDO> list = stockWorkshopService.getStockWorkshopPage(pageReqVO).getList();
        List<StockWorkshopRespVO> list1 = stockWorkshopService.buildVOList(list);

        // 导出 Excel
        ExcelUtils.write(response, "车间仓库存.xls", "数据", StockWorkshopRespVO.class,
                list1);
    }

}