package cn.iocoder.yudao.module.erp.controller.admin.mold;

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.common.controller.admin.mold.vo.*;
import cn.iocoder.yudao.module.common.controller.admin.moldrepair.vo.MoldRepairPageReqVO;
import cn.iocoder.yudao.module.common.controller.admin.moldrepair.vo.MoldRepairRespVO;
import cn.iocoder.yudao.module.common.dal.dataobject.mold.MoldBrandDO;
import cn.iocoder.yudao.module.common.dal.dataobject.mold.MoldBrandProductDO;
import cn.iocoder.yudao.module.common.dal.dataobject.mold.MoldDO;
import cn.iocoder.yudao.module.common.dal.dataobject.moldrepair.MoldRepairDO;
import cn.iocoder.yudao.module.erp.controller.admin.mold.vo.MoldBrandTreeRespVO;
import cn.iocoder.yudao.module.erp.service.mold.MoldBrandService;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
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("/erp/mold-brand")
@Validated
public class MoldBrandController {

    @Resource
    private MoldBrandService moldBrandService;

    @PostMapping("/create")
    @Operation(summary = "创建模具型号")
    @PreAuthorize("@ss.hasPermission('erp:mold-brand:create')")
    public CommonResult<Long> createMoldBrand(@Valid @RequestBody MoldBrandSaveReqVO createReqVO) {
        return success(moldBrandService.createMoldBrand(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新模具型号")
    @PreAuthorize("@ss.hasPermission('erp:mold-brand:update')")
    public CommonResult<Boolean> updateMoldBrand(@Valid @RequestBody MoldBrandSaveReqVO updateReqVO) {
        moldBrandService.updateMoldBrand(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除模具型号")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:mold-brand:delete')")
    public CommonResult<Boolean> deleteMoldBrand(@RequestParam("id") Long id) {
        moldBrandService.deleteMoldBrand(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得模具型号")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:mold-brand:query')")
    public CommonResult<MoldBrandRespVO> getMoldBrand(@RequestParam("id") Long id) {
        MoldBrandDO moldBrand = moldBrandService.getMoldBrand(id);
        return success(BeanUtils.toBean(moldBrand, MoldBrandRespVO.class));
    }
    @GetMapping("/getBrandList")
    @Operation(summary = "获得模具型号列表")
    public CommonResult<List<MoldBrandDO>> getBrandList(MoldBrandPageReqVO pageReqVO) {
        List<MoldBrandDO> brandDOList = moldBrandService.selectBy(pageReqVO);
        return success(brandDOList);
    }
    @GetMapping("/page")
    @Operation(summary = "获得模具型号分页")
    @PreAuthorize("@ss.hasPermission('erp:mold-brand:query')")
    public CommonResult<PageResult<MoldBrandRespVO>> getMoldBrandPage(@Valid MoldBrandPageReqVO pageReqVO) {
        PageResult<MoldBrandRespVO> pageResult = moldBrandService.getMoldBrandPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出模具型号 Excel")
    @PreAuthorize("@ss.hasPermission('erp:mold-brand:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMoldBrandExcel(@Valid MoldBrandPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MoldBrandRespVO> list = moldBrandService.getMoldBrandPage(pageReqVO).getList();
        List<MoldBrandRespVO> moldBrandRespVOS = BeanUtils.toBean(list, MoldBrandRespVO.class);
        // 导出 Excel
        ExcelUtils.write(response, "模具型号.xls", "数据", MoldBrandRespVO.class, moldBrandRespVOS);
    }

    @GetMapping("/tree")
    @Operation(summary = "获得设备类型树")
    @PreAuthorize("@ss.hasPermission('mes:device-type:query')")
    public CommonResult<List<MoldBrandTreeRespVO>> getDeviceTypeTree() {
        List<MoldBrandTreeRespVO> tree = moldBrandService.getMoldBrandTree();
        return success(tree);
    }

    // ==================== 子表（模具） ====================
    @GetMapping("/getMoldList")
    @Operation(summary = "获得模具型号")
    public CommonResult<List<MoldDO>> getMoldList(MoldDO moldDO) {
        List<MoldDO> doList = moldBrandService.selectBy(moldDO);
        return success(doList);
    }

    @GetMapping("/getMoldAllList")
    @Operation(summary = "获得模具型号")
    public CommonResult<List<MoldDO>> getMoldAllList() {
        List<MoldDO> doList = moldBrandService.getAllList();
        return success(doList);
    }

    @GetMapping("/mold/page")
    @Operation(summary = "获得模具分页")
    @PreAuthorize("@ss.hasPermission('erp:mold-brand:query')")
    public CommonResult<PageResult<MoldRespVO>> getMoldPage(@Valid MoldPageReqVO pageReqVO) {
        return success(moldBrandService.getMoldPage(pageReqVO));
    }


    @GetMapping("/mold/list")
    @Operation(summary = "获得模具列表")
    @PreAuthorize("@ss.hasPermission('erp:mold-brand:query')")
    public CommonResult<List<MoldDO>> getMoldList() {
        List<MoldDO> moldDOList = moldBrandService.getMoldList();
        return success(moldDOList);
    }

    @PostMapping("/mold/create")
    @Operation(summary = "创建模具")
    @PreAuthorize("@ss.hasPermission('erp:mold-brand:create')")
    public CommonResult<Long> createMold(@Valid @RequestBody MoldSaveReqVO createReqVO) {
        return success(moldBrandService.createMold(createReqVO));
    }

    @PutMapping("/mold/update")
    @Operation(summary = "更新模具")
    @PreAuthorize("@ss.hasPermission('erp:mold-brand:update')")
    public CommonResult<Boolean> updateMold(@Valid @RequestBody MoldDO mold) {
        moldBrandService.updateMold(mold);
        return success(true);
    }

    @DeleteMapping("/mold/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除模具")
    @PreAuthorize("@ss.hasPermission('erp:mold-brand:delete')")
    public CommonResult<Boolean> deleteMold(@RequestParam("id") Long id) {
        moldBrandService.deleteMold(id);
        return success(true);
    }

	@GetMapping("/mold/get")
	@Operation(summary = "获得模具")
	@Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:mold-brand:query')")
	public CommonResult<MoldDO> getMold(@RequestParam("id") Long id) {
	    return success(moldBrandService.getMold(id));
	}

    @GetMapping("/mold/export-excel")
    @Operation(summary = "导出模具 Excel")
    @Parameter(name = "brandId", description = "型号ID", required = true)
    @PreAuthorize("@ss.hasPermission('erp:mold-brand:query')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMoldExcel(@Valid MoldPageReqVO pageReqVO,
                                HttpServletResponse response, @RequestParam("brandId") Long brandId) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        pageReqVO.setBrandId(brandId); // 将brandId设置到pageReqVO中
        List<MoldRespVO> list = moldBrandService.getMoldPage(pageReqVO).getList(); // 调用正确的方法
        // 导出 Excel
        ExcelUtils.write(response, "模具.xls", "数据", MoldRespVO.class, list);
    }

    // ==================== 子表（模具产品） ====================

    @GetMapping("/mold-brand-product/page")
    @Operation(summary = "获得模具产品分页")
    @Parameter(name = "brandId", description = "型号ID")
    @PreAuthorize("@ss.hasPermission('erp:mold-brand:query')")
    public CommonResult<PageResult<MoldProductRespVO>> getMoldBrandProductPage(PageParam pageReqVO,
                                                                                @RequestParam("brandId") Long brandId) {
        PageResult<MoldBrandProductDO> pageResult = moldBrandService.getMoldBrandProductPage(pageReqVO, brandId);
        PageResult<MoldProductRespVO> result = new PageResult<>(moldBrandService.buildProduct(pageResult.getList()),pageResult.getTotal());
        return success(result);
    }

    @PostMapping("/mold-brand-product/create")
    @Operation(summary = "创建模具产品")
    @PreAuthorize("@ss.hasPermission('erp:mold-brand:create')")
    public CommonResult<Long> createMoldBrandProduct(@Valid @RequestBody MoldBrandProductDO moldBrandProduct) {
        return success(moldBrandService.createMoldBrandProduct(moldBrandProduct));
    }

    @PutMapping("/mold-brand-product/update")
    @Operation(summary = "更新模具产品")
    @PreAuthorize("@ss.hasPermission('erp:mold-brand:update')")
    public CommonResult<Boolean> updateMoldBrandProduct(@Valid @RequestBody MoldBrandProductDO moldBrandProduct) {
        moldBrandService.updateMoldBrandProduct(moldBrandProduct);
        return success(true);
    }

    @DeleteMapping("/mold-brand-product/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除模具产品")
    @PreAuthorize("@ss.hasPermission('erp:mold-brand:delete')")
    public CommonResult<Boolean> deleteMoldBrandProduct(@RequestParam("id") Long id) {
        moldBrandService.deleteMoldBrandProduct(id);
        return success(true);
    }

    @GetMapping("/mold-brand-product/get")
    @Operation(summary = "获得模具产品")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:mold-brand:query')")
    public CommonResult<MoldBrandProductDO> getMoldBrandProduct(@RequestParam("id") Long id) {
        return success(moldBrandService.getMoldBrandProduct(id));
    }

}