package com.sckw.mine.common;

import com.sckw.core.utils.IdWorker;
import com.sckw.core.utils.TenantUtil;
import com.sckw.mine.entity.KwBusinessActionTrack;
import com.sckw.mine.enums.MineStatusEnum;
import com.sckw.mine.mapper.KwBusinessActionTrackMapper;
import com.sckw.redis.utils.GenOrderCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * @desc:
 * @author: Lt
 * @date: 2024-05-29
 */
@Component
public class OrderServerCommon {

    @Autowired
    KwBusinessActionTrackMapper kwBusinessActionTrackMapper;

    @Autowired
    private GenOrderCode genOrderCode;


    public void saveActionTrack(Long mineOrderId, Integer status, String remark, String prefix) {
        KwBusinessActionTrack kwBusinessActionTrack = new KwBusinessActionTrack();
        kwBusinessActionTrack.setId(new IdWorker(1L).nextId());
        kwBusinessActionTrack.setTenantId(TenantUtil.getTenant());
        kwBusinessActionTrack.setMineOrderId(mineOrderId);
        kwBusinessActionTrack.setContent(MineStatusEnum.getNameByCode(status));
        kwBusinessActionTrack.setStatus(status);
        kwBusinessActionTrack.setTaskOrderNum(generatorNum("T", 14));
        kwBusinessActionTrack.setRemark(remark);
        kwBusinessActionTrackMapper.insert(kwBusinessActionTrack);
    }

    public String generatorNum(String prefix, Integer length)
    {
        LocalDate currentDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String orderPrefix = currentDate.format(formatter);
        String key = prefix + orderPrefix;
        return genOrderCode.genOrderCode(length, key, key);
    }
}
