package com.chinaitop.depot.intelligent.utils;

import com.chinaitop.depot.intelligent.basicdata.mapper.TSendNumberMapper;
import com.chinaitop.depot.intelligent.basicdata.model.TSendNumber;
import com.chinaitop.depot.intelligent.basicdata.model.TSendNumberExample;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

@Component
public class UuidUtils {

    @Value("${localOrCloud}")
    private int platform;

    @Resource
    private TSendNumberMapper tSendNumberMapper;

    /**
     * 生成32位随机数
     *
     * @return 返回32位随机数
     */
    public static String getCode() {
        UUID uuid = UUID.randomUUID();
        return uuid.toString().replace("-", "");
    }

    /**
     * 生成规则code
     *
     * @param orgId     库点编码
     * @param tableName 表名
     * @return codeid
     */
    public String getCodeId(String orgId, String tableName) {
        String codeId = null;
        if (StringUtils.isNotBlank(orgId) && StringUtils.isNotBlank(tableName)) {
            //库点补全
            String org = getKuDian(orgId);
            String date = DateUtils.getCurrentDate();
            TSendNumber tSendNumber1 = getTSendNumber(tableName);
            if (tSendNumber1 != null) {
                String nyr = DateUtils.getNYR();
                //获取平台号库点号序号时间
                String code = tSendNumber1.getCode();
                String nians = code.substring(0, 13);
                //获取序号
                String number = code.substring(code.length() - 6, code.length());
                TSendNumber tSendNumber = new TSendNumber();
                if (nians.equals(platform + org + nyr)) {
                    number = (Integer.valueOf(number) + 1) + "";
                    if (number.length() < 6) {
                        number = "000000" + number;
                        number = number.substring(number.length() - 6, number.length());
                        codeId = platform + org + date + number;
                        tSendNumber.setCode(codeId);
                    } else {
                        codeId = platform + org + date + number;
                        tSendNumber.setCode(codeId);
                    }
                } else {
                    codeId = platform + org + date + "000001";
                    tSendNumber.setCode(codeId);
                }
                tSendNumber.setId(tSendNumber1.getId());
                tSendNumber.setTime(DateUtils.getDate());
                tSendNumberMapper.updateByPrimaryKeySelective(tSendNumber);
            } else {
                String sCode = platform + org + date + "000001";
                TSendNumber tSendNumber = new TSendNumber();
                tSendNumber.setId(UuidUtils.getCode());
                tSendNumber.setOrgId(orgId);
                tSendNumber.setTableName(tableName);
                tSendNumber.setCode(sCode);
                tSendNumber.setTime(DateUtils.getDate());
                tSendNumberMapper.insert(tSendNumber);
                codeId = sCode;
            }
        }
        return codeId;
    }

    private String getKuDian(String orgId) {
        String org = "";
        if (orgId.length() < 4) {
            org = "0000" + orgId;
            org = org.substring(org.length() - 4, org.length());
        }
        return org;
    }

    public TSendNumber getTSendNumber(String tableName) {
        TSendNumberExample example = new TSendNumberExample();
        example.createCriteria().andTableNameEqualTo(tableName);
        List<TSendNumber> tSendNumbers = tSendNumberMapper.selectByExample(example);
        if (tSendNumbers.size() > 0) {
            return tSendNumbers.get(0);
        }
        return null;
    }
}