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

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

import cn.iocoder.yudao.module.common.controller.admin.mold.vo.*;
import cn.iocoder.yudao.module.common.dal.dataobject.mold.MoldDO;
import cn.iocoder.yudao.module.erp.service.mold.MoldService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 模具")
@RestController
@RequestMapping("/erp/mold")
@Validated
public class MoldController {

    @Resource
    private MoldService moldService;


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

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

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

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

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

    @GetMapping("/export-excel")
    @Operation(summary = "导出模具 Excel")
    @PreAuthorize("@ss.hasPermission('erp:mold:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMoldExcel(@Valid MoldPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MoldDO> list = moldService.getMoldPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "模具.xls", "数据", MoldRespVO.class,
                        BeanUtils.toBean(list, MoldRespVO.class));
    }


    @GetMapping("/getInTransitMoldAllList")
    @Operation(summary = "获得上下模模具列表（在途-3）")
    public CommonResult<List<MoldDO>> getInTransitMoldAllList() {
        List<MoldDO> doList = moldService.getInTransitMoldAllList();
        return success(doList);
    }

    @GetMapping("/getMoldListByStatus")
    @Operation(summary = "根据状态获得模具列表")
    @Parameter(name = "id", description = "设备id", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:mold-operate:query')")
    public CommonResult<List<MoldDO>> getMoldListByStatus(@RequestParam("status") Long status) {
        List<MoldDO> moldDO = moldService.getMoldListByStatus(status);
        return success(moldDO);
    }
}