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

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.erp.controller.admin.product.vo.product.ErpProductPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.bom.vo.BomDetailRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.bom.vo.BomPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.bom.vo.BomRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.bom.vo.BomSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.bom.BomDO;
import cn.iocoder.yudao.module.mes.dal.mysql.bom.BomMapper;
import cn.iocoder.yudao.module.mes.service.bom.BomService;
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.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 产品BOM")
@RestController
@RequestMapping("/mes/bom")
@Validated
public class BomController {

    @Resource
    private BomService bomService;
    @Resource
    private BomMapper bomMapper;

    @PostMapping("/create")
    @Operation(summary = "创建产品BOM")
    @PreAuthorize("@ss.hasPermission('mes:bom:create')")
    public CommonResult<Long> createBom(@Valid @RequestBody BomSaveReqVO createReqVO) {
        BomPageReqVO bomPageReqVO =  new BomPageReqVO();
        bomPageReqVO.setCode(createReqVO.getCode());
        if (!bomMapper.selectBomExist(bomPageReqVO).getList().isEmpty()) {
            return error(400,"编码不能重复");
        }
        return success(bomService.createBom(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新产品BOM")
    @PreAuthorize("@ss.hasPermission('mes:bom:update')")
    public CommonResult<Boolean> updateBom(@Valid @RequestBody BomSaveReqVO updateReqVO) {
        bomService.updateBom(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除产品BOM")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:bom:delete')")
    public CommonResult<Boolean> deleteBom(@RequestParam("id") Long id) {
        bomService.deleteBom(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得产品BOM")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:bom:query')")
    public CommonResult<BomRespVO> getBom(@RequestParam("id") Long id) {
        BomDO bom = bomService.getBom(id);
        return success(BeanUtils.toBean(bom, BomRespVO.class));
    }
    @GetMapping("/getByProductId")
    @Operation(summary = "获得产品BOM")
    @Parameter(name = "productId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:bom:query')")
    public CommonResult<BomRespVO> getByProductId(@RequestParam("id") Long id) {
        BomDO bom = bomService.getBom(id);
        return success(BeanUtils.toBean(bom, BomRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得产品BOM分页")
    @PreAuthorize("@ss.hasPermission('mes:bom:query')")
    public CommonResult<PageResult<BomRespVO>> getBomPage(@Valid BomPageReqVO pageReqVO) {
        PageResult<BomRespVO> pageResult = bomService.getBomPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出产品BOM Excel")
    @PreAuthorize("@ss.hasPermission('mes:bom:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBomExcel(@Valid BomPageReqVO pageReqVO,
                               HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BomRespVO> list = bomService.getBomPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "产品BOM.xls", "数据", BomRespVO.class,
                list);
    }

    // ==================== 子表（产品BOM明细） ====================

    @GetMapping("/bom-detail/list-by-bom-id")
    @Operation(summary = "获得产品BOM明细列表")
    @Parameter(name = "bomId", description = "BOM ID")
    @PreAuthorize("@ss.hasPermission('mes:bom:query')")
    public CommonResult<List<BomDetailRespVO>> getBomDetailListByBomId(@RequestParam("bomId") Long bomId) {
        return success(bomService.getBomDetailListByBomId(bomId));
    }

}