package com.czg.service.account.service.impl;

import cn.hutool.core.util.IdUtil;
import com.czg.account.dto.PageDTO;
import com.czg.account.dto.register.MerchantRegisterDTO;
import com.czg.account.entity.MerchantRegister;
import com.czg.account.service.MerchantRegisterService;
import com.czg.service.account.mapper.MerchantRegisterMapper;
import com.czg.utils.PageUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * 激活码 服务层实现。
 *
 * @author Administrator
 * @since 2025-02-11
 */
@Service
public class MerchantRegisterServiceImpl extends ServiceImpl<MerchantRegisterMapper, MerchantRegister> implements MerchantRegisterService {

    @Override
    public Page<MerchantRegister> get(PageDTO pageDTO, Integer state, String startTime, String endTime) {
        Page<Object> page = PageUtil.buildPage();
        PageHelper.startPage(Math.toIntExact(page.getPageNumber()), Math.toIntExact(page.getPageSize()));
        return PageUtil.convert(new PageInfo<>(mapper.pageInfo(state, startTime, endTime)));
    }

    @Override
    public Boolean add(MerchantRegisterDTO merchantRegisterDTO) {
        ArrayList<MerchantRegister> registers = new ArrayList<>();
        for (int i = 0; i < merchantRegisterDTO.num(); i++) {
            MerchantRegister tbMerchantRegister = new MerchantRegister();
            tbMerchantRegister.setRegisterCode(IdUtil.simpleUUID());
            tbMerchantRegister.setStatus(0);
            tbMerchantRegister.setPeriodMonth(merchantRegisterDTO.periodMonth());
            registers.add(tbMerchantRegister);
        }
        int i = mapper.insertBatchSelective(registers, 50);
        return i > 0;
    }
}
