package cn.superdesk.uniorder.service.setting.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.StrUtil;
import cn.superdesk.uniorder.common.numerical.AnDateHelper;
import cn.superdesk.uniorder.common.numerical.AnStringUtils;
import cn.superdesk.uniorder.common.security.crypt.SuperdeskUniorderRsaUtils;
import cn.superdesk.uniorder.dao.entity.setting.UnifiedSnConfig;
import cn.superdesk.uniorder.dao.mapper.setting.UnifiedSnConfigMapper;
import cn.superdesk.uniorder.service.setting.UnifiedSnConfigService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;


/**
 * @Description: 生成编号
 * @Author: linjinyu
 * @CreateDate: 2021/5/14 下午5:06
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/5/14 下午5:06
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Service
public class UnifiedSnConfigServiceImpl implements UnifiedSnConfigService {


    @Autowired
    private UnifiedSnConfigMapper unifiedSnConfigMapper;

    @Resource
    private Snowflake snowflake;


    @Override
    public int insertSelective(UnifiedSnConfig t) {
        return unifiedSnConfigMapper.insertSelective(t);

    }

    @Override
    public int updateByPrimaryKeySelective(UnifiedSnConfig t) {

        return unifiedSnConfigMapper.updateByPrimaryKeySelective(t);
    }

    @Override
    public void deleteObj(Long id) {


    }

    @Override
    public UnifiedSnConfig findById(Long id) {

        return unifiedSnConfigMapper.selectByPrimaryKey(id);
    }


    @Override
    public List<UnifiedSnConfig> findAll(Map<String, Object> queryMap) {

        return null;
    }

    @Override
    public PageInfo<UnifiedSnConfig> findByPaging(Map<String, Object> params, Integer pageNo, Integer pageSize) {

        return null;
    }


    /**
     * SELECT * FROM `unified_sn_config`
     * 原本有这个配置表 但要做界面
     *
     * @return
     */
    public String getCustomerStatementOfAccountSnBySnowflake(String serviceNo) {

        String createSn = getSnBySnowflake(serviceNo, "CA");

        return createSn;
    }

    public String getSupplierStatementOfAccountSnBySnowflake(String serviceNo) {

        String createSn = getSnBySnowflake(serviceNo, "SA");

        return createSn;
    }

    public String getCustomerInvoiceApplySnBySnowflake() {

//		String serviceNo
        String createSn = getSnBySnowflake("", "CI");

        return createSn;
    }

    public String getSupplierInvoiceApplySnBySnowflake() {

//		String serviceNo
        String createSn = getSnBySnowflake("", "SI");

        return createSn;
    }

    /****************************************** 银行流水 start ******************************************/

    public String getBankRecordSnBySnowflake() {

        String createSn = getSnBySnowflake("", "SI");

        return createSn;
    }

    /****************************************** 银行流水 start ******************************************/


    public String getSnBySnowflake(String serviceNo, String postfix) {

        Long   snowflakeId    = snowflake.nextId();
        String snowflakeIdStr = AnStringUtils.getString(snowflakeId);

        // 截取
        String snowflakeStrSub = snowflakeIdStr.substring(snowflakeIdStr.length() - 12, snowflakeIdStr.length());

        String part01 = "";
        String part02 = "";

        if (StrUtil.isNotBlank(serviceNo)) {
            part01 = serviceNo + "";
        }

        if (StrUtil.isNotBlank(postfix)) {
            part02 = postfix + "A";
        }

        String createSn = StrUtil.format("{}{}{}{}", part01, part02, AnDateHelper.getCurrentDateStr("yyyyMMdd"), snowflakeStrSub);

        log.info("新的生成SN getStatementOfAccountSn {}", createSn);

        return createSn;
    }

    public String getSnBySnowflakeBefore(String serviceNo, String postfix) {

        Long   snowflakeId    = snowflake.nextId();
        String snowflakeIdStr = AnStringUtils.getString(snowflakeId);

        // 截取
        String snowflakeStrSub = snowflakeIdStr.substring(snowflakeIdStr.length() - 5, snowflakeIdStr.length());

        String part01 = "";
        String part02 = "";

        if (StrUtil.isNotBlank(serviceNo)) {
            part01 = serviceNo + "-";
        }

        if (StrUtil.isNotBlank(postfix)) {
            part02 = postfix + "-";
        }

        String createSn = StrUtil.format("{}{}{}{}", part01, part02, AnDateHelper.getCurrentDateStr("yyyyMMdd"), snowflakeStrSub);

        log.info("新的生成SN getStatementOfAccountSn {}", createSn);

        return createSn;
    }

    /**
     * 获取单号
     *
     * @param serviceNo
     * @return
     */
    @Deprecated
    @Override
    public String getSnQuoteDatabase(String serviceNo) {


        UnifiedSnConfig unifiedSnConfig = unifiedSnConfigMapper.selectByServiceNo(serviceNo);

        if (unifiedSnConfig == null) {
            return getSupplierStatementOfAccountSnBySnowflake(serviceNo);
        }

        if (unifiedSnConfig.getEndRodom() == 1) {
            return SuperdeskUniorderRsaUtils.genId(unifiedSnConfig.getStartStr());
        } else {
            UnifiedSnConfig update = new UnifiedSnConfig();
            update.setId(unifiedSnConfig.getId());
            update.setNextSn(getNextSn(unifiedSnConfig));
            unifiedSnConfigMapper.updateByPrimaryKeySelective(update);
            return update.getNextSn();
        }

    }

    @Deprecated
    private String getNextSn(UnifiedSnConfig snconfig) {

        // yyyyMMdd
        SimpleDateFormat sdf = new SimpleDateFormat(snconfig.getMidDateformat());

        String sn      = snconfig.getNextSn();
        String datestr = sn.replace(snconfig.getStartStr(), "");

        datestr = datestr.substring(0, datestr.length() - snconfig.getEndLength());

        // 1
        int maxNumber = snconfig.getEndInivalue();

        if (sdf.format(DateUtil.date()).equals(datestr)) {
            maxNumber = Integer.parseInt(sn.substring(sn.length() - snconfig.getEndLength())) + 1;
        } else {
        }

        String endstr = "";
        for (int i = (maxNumber + "").length(); i < snconfig.getEndLength(); i++) {
            endstr += "0";
        }
        endstr += maxNumber;

        return snconfig.getStartStr() + sdf.format(DateUtil.date()) + endstr;

    }

}
