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

import cn.iocoder.yudao.module.erp.controller.admin.autocode.util.AutoCodeUtil;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.beans.factory.annotation.Autowired;
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 cn.iocoder.yudao.module.erp.controller.admin.autocode.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.autocode.AutocodeRuleDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.autocode.AutocodePartDO;
import cn.iocoder.yudao.module.erp.service.autocode.AutocodeRuleService;

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

@Tag(name = "管理后台 - 编码规则")
@RestController
@RequestMapping("/erp/autocode-rule")
@Validated
public class AutocodeRuleController {

    @Resource
    private AutocodeRuleService autocodeRuleService;

    @PostMapping("/create")
    @Operation(summary = "创建编码规则")
    @PreAuthorize("@ss.hasPermission('erp:autocode-rule:create')")
    public CommonResult<Long> createAutocodeRule(@Valid @RequestBody AutocodeRuleSaveReqVO createReqVO) {
        return success(autocodeRuleService.createAutocodeRule(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新编码规则")
    @PreAuthorize("@ss.hasPermission('erp:autocode-rule:update')")
    public CommonResult<Boolean> updateAutocodeRule(@Valid @RequestBody AutocodeRuleSaveReqVO updateReqVO) {
        autocodeRuleService.updateAutocodeRule(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除编码规则")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:autocode-rule:delete')")
    public CommonResult<Boolean> deleteAutocodeRule(@RequestParam("id") Long id) {
        autocodeRuleService.deleteAutocodeRule(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得编码规则")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:autocode-rule:query')")
    public CommonResult<AutocodeRuleRespVO> getAutocodeRule(@RequestParam("id") Long id) {
        AutocodeRuleDO autocodeRule = autocodeRuleService.getAutocodeRule(id);
        return success(BeanUtils.toBean(autocodeRule, AutocodeRuleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得编码规则分页")
    @PreAuthorize("@ss.hasPermission('erp:autocode-rule:query')")
    public CommonResult<PageResult<AutocodeRuleRespVO>> getAutocodeRulePage(@Valid AutocodeRulePageReqVO pageReqVO) {
        PageResult<AutocodeRuleDO> pageResult = autocodeRuleService.getAutocodeRulePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AutocodeRuleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出编码规则 Excel")
    @PreAuthorize("@ss.hasPermission('erp:autocode-rule:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAutocodeRuleExcel(@Valid AutocodeRulePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AutocodeRuleDO> list = autocodeRuleService.getAutocodeRulePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "编码规则.xls", "数据", AutocodeRuleRespVO.class,
                        BeanUtils.toBean(list, AutocodeRuleRespVO.class));
    }

    // ==================== 子表（编码规则组成） ====================

    @GetMapping("/autocode-part/list-by-rule-id")
    @Operation(summary = "获得编码规则组成列表")
    @Parameter(name = "ruleId", description = "规则ID")
    @PreAuthorize("@ss.hasPermission('erp:autocode-rule:query')")
    public CommonResult<List<AutocodePartDO>> getAutocodePartListByRuleId(@RequestParam("ruleId") Long ruleId) {
        return success(autocodeRuleService.getAutocodePartListByRuleId(ruleId));
    }
    @Autowired
    private AutoCodeUtil autoCodeUtil;

    @Operation(summary = "获取一个自动生成的编码")
    @Parameters({
            @Parameter(name = "ruleCode", description = "规则编码", required = true),
            @Parameter(name = "inputCharacter", description = "输入字符", required = false)
    })
    @GetMapping(value = "/getAutoCode")
    public CommonResult<String> getAutoCode(@RequestParam("ruleCode") String ruleCode, String inputCharacter){
        String code = autoCodeUtil.genSerialCode(ruleCode,inputCharacter);
        return CommonResult.success(code);
    }
    @Operation(summary = "测试自动生成的编码")
    @Parameters({
            @Parameter(name = "ruleCode", description = "规则编码", required = true),
            @Parameter(name = "inputCharacter", description = "输入字符", required = false)
    })
    @GetMapping(value = "/getTestCode")
    public CommonResult<String> getAutoCodeTest(@RequestParam("ruleCode") String ruleCode,String inputCharacter){
        String code = autoCodeUtil.genSerialCodeTest(ruleCode,inputCharacter);
        return CommonResult.success(code);
    }
}