//#import Util.js
//#import $owl_org_user_address:services/modelService.jsx
//#import $owl_org_user_address_default:services/modelService.jsx

var OrgUserAddressService = (function () {
    var f = {
        getAddressList: function (userId) {
            var searchArgs = {};
            searchArgs.user_id = userId;

            var searchResult = owl_org_user_addressService.search(null, searchArgs, null, 0, 1000, [{"owl_createTime": {order: "desc"}}]);
            if (searchResult.state !== 'ok') {
                return [];
            }
            return searchResult.list;
        },
        removeAllDefaultAddress: function (userId, addressId) {
            var searchArgs = {};
            searchArgs.user_id = userId;
            // searchArgs.default = "1";

            var searchResult = owl_org_user_addressService.search(null, searchArgs, null, 0, 1000, null);
            if (searchResult.state !== 'ok') {
                return;
            }
            var listData = searchResult.list;
            if (listData) {
                for (var i = 0; i < listData.length; i++) {
                    var jRecord = listData[i];
                    if (jRecord.id == addressId) {
                        continue;
                    }
                    jRecord = owl_org_user_addressService.get(jRecord.id);
                    if (!jRecord || jRecord.del == "T") {
                        continue;
                    }
                    if (jRecord.default) {
                        jRecord.default = "0";
                        owl_org_user_addressService.update(jRecord);
                    }
                }
            }
        },
        getDefaultAddress: function (userId) {
            var searchArgs = {};
            searchArgs.user_id = userId;
            searchArgs.default = "1";

            var searchResult = owl_org_user_addressService.search(null, searchArgs, null, 0, 1, null);
            if (searchResult.state !== 'ok') {
                return null;
            }
            return searchResult.list[0];
        },
        getDefaultAddressByUserId: function (userId) {
            if (!userId) {
                return null;
            }
            var defaultAddressId = "owl_org_user_address_default_" + userId;
            var jDefaultAddress = owl_org_user_address_defaultService.get(defaultAddressId);
            if (jDefaultAddress && jDefaultAddress.address_id) {
                jDefaultAddress = owl_org_user_addressService.get(jDefaultAddress.address_id);
                if (jDefaultAddress.del != "T") {
                    return jDefaultAddress;
                }
            }
            return f.getDefaultAddress(userId);
        },
        getAddressById: function (addressId) {
            return owl_org_user_addressService.get(addressId);
        },
        saveDefaultAddressId: function (userId, addressId) {
            var id = "owl_org_user_address_default_" + userId;
            var jDefaultId = owl_org_user_address_defaultService.get(id);
            if (jDefaultId) {
                jDefaultId.address_id = addressId;
                jDefaultId.owl_lastModifiedUserId = userId;
                owl_org_user_address_defaultService.update(jDefaultId);
            } else {
                jDefaultId = {};
                jDefaultId.user_id = userId;
                jDefaultId.address_id = addressId;
                jDefaultId.owl_createUserId = userId;
                jDefaultId.owl_lastModifiedUserId = userId;
                owl_org_user_address_defaultService.add(jDefaultId);
            }
        },
        checkAndRemoveDefaultAddressId: function (userId, addressId) {
            var id = "owl_org_user_address_default_" + userId;
            var jDefaultId = owl_org_user_address_defaultService.get(id);
            if (jDefaultId && jDefaultId.address_id == addressId) {
                jDefaultId.address_id = "";
                jDefaultId.owl_lastModifiedUserId = userId;
                owl_org_user_address_defaultService.update(jDefaultId);
            }
        }
    };
    return f;
})();