import _ from 'lodash'

/**
 * 发票号码校验 校验发票号码是否存在重号 连号及有序隔号
 * @author fengjiansheng
 * @date 2021-11-30
 */
export default {
    execute: function () {
        console.log("s1 CheckInvoiceNumber")
        if (arguments && arguments[0] && arguments[0].length > 1 && arguments[1]) {
            let context = arguments[1]
            let subField = arguments[0][1]
            let tempSubTableObject = context.getTableType(subField.FieldTableCode)
            let allSubData = context.getAllSubData()
            let fphmArray = []
            if (allSubData) {
                let tempAllSubData = allSubData[tempSubTableObject.title]
                if (tempAllSubData && tempAllSubData instanceof Array) {
                    tempAllSubData.forEach(e => {
                        fphmArray.push(_.toNumber(e.getValue(subField.FieldCode)))
                    })
                }
            }
            return InnerChecker.frontCheckNumber(fphmArray)
        }
    },
    getResultType: function () {
        return FMR.ConstDataTypes.Bool
    }
}

class InnerChecker {
    /**
     * 返回是否含有重复、连续、间隔的发票号
     * @param arr
     * @returns {boolean}
     */
    static frontCheckNumber(arr) {
        let tmp = arr;
        let invalidFlag = true
        tmp.sort((a, b) => a - b);
        for (let i = 0; i < tmp.length - 1; i++) {
            if (arr[i] === arr[i + 1] || arr[i] === arr[i + 1] - 1 || arr[i] === arr[i + 1] - 2) {
                invalidFlag = false
                break
            }
        }
        return invalidFlag
    }
}
