package com.hzs.member.empty.controller.manage;

import com.hzs.common.core.utils.poi.ExcelUtil;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.member.empty.service.ICuMemberEmptyCodeService;
import com.hzs.member.empty.vo.CuMemberEmptyCodeTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 空单生成会员编号控制器
 * @Author: jiang chao
 * @Time: 2023/8/29 11:14
 * @Classname: CuMemberEmptyCodeController
 * @PackageName: com.hzs.member.empty.controller.manage
 */
@RequestMapping("/manage/member-empty-code")
@RestController
public class CuMemberEmptyCodeController {

    @Autowired
    private ICuMemberEmptyCodeService iCuMemberEmptyCodeService;

    /**
     * 生成会员编号
     *
     * @param response
     * @param quantity 生成数量
     */
    @PostMapping("/create-code")
    public void exportTemplate(HttpServletResponse response, @RequestParam Integer quantity) {
        // 转成导出数据列表
        List<CuMemberEmptyCodeTemplate> exportList = new ArrayList<>();

        // 生成会员编号
        List<String> resultList = iCuMemberEmptyCodeService.createCodeBatch(quantity, SecurityUtils.getPkCountry(), SecurityUtils.getUserId());

        for (String code : resultList) {
            exportList.add(CuMemberEmptyCodeTemplate.builder()
                    .memberCode(code)
                    .build());
        }

        ExcelUtil<CuMemberEmptyCodeTemplate> util = new ExcelUtil<>(CuMemberEmptyCodeTemplate.class);
        util.exportExcel(response, exportList, "生成会员编号");
    }

}
